Angular12 中如何更改 ngx-monaco-editor 的背景颜色?
How do you change the background color of ngx-monaco-editor in Angular 12?
我目前正在尝试更改我的 Angular 12 项目中 ngx-monaco-editor 元素的背景颜色,但到目前为止我尝试过的任何方法都没有奏效。有没有一种方法可以用我当前的设置轻松地做到这一点,或者我需要用另一种方法重做吗?这就是我目前正在尝试做的,但编辑器最终看起来像“vs”主题。我的代码片段:
component1.html
<div class="data" id="d2">
<ngx-monaco-editor id="xmlDisplayer" style="height: 100%;" [options]="editorOptions" [(ngModel)]="code"></ngx-monaco-editor>
</div>
component1.ts
import { Component, OnInit } from '@angular/core';
import { DataMapService } from '../data-map.service';
import { MonacoEditorLoaderService } from '@materia-ui/ngx-monaco-editor';
import { saveAs } from "file-saver";
import * as d3 from 'd3';
export class component1 implements OnInit {
constructor(private dataMapService: DataMapService, private monacoLoaderService: MonacoEditorLoaderService) {
this.monacoLoaderService.isMonacoLoaded$.pipe(
).subscribe(() => {
(window as any).monaco.editor.defineTheme('myCustomTheme', {
base: 'vs-dark', // can also be vs-dark or hc-black
colors: {
"editor.background": '#AA4A44', // code background
'editorCursor.foreground': '#002438', // corsour color
'editor.lineHighlightBackground': '#9B9B9B', // line highlight colour
'editorLineNumber.foreground': '#666666', // line number colour
'editor.selectionBackground': '#666666', // code selection background
'editor.inactiveSelectionBackground': '#7e890b'
}
});
});
}
ngOnInit(): void {
}
//options and text for the xml display
editorOptions = {theme: 'myCustomTheme', language: 'xml'};
code = "xml stuff goes here";
app.module.ts
import { MonacoEditorModule } from 'ngx-monaco-editor';
//Other component imports and misc
@NgModule({
declarations: [
AppComponent,
//Component declarations
],
imports: []
//Other imports
MonacoEditorModule.forRoot(),
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
}
我 运行 遇到的另一个问题是,当我尝试在其他地方执行“monaco.editor.defineTheme(...) 时,我遇到了 monaco 命名空间错误。任何帮助将不胜感激。
我无需过多更改代码即可解决问题。如果你在 component1.ts 中没有 .pipe(filter(),take()) 它将不起作用,所以一定要包括它。
这个 stackblitz 帮助我弄明白了:https://stackblitz.com/edit/materia-ngx-monaco-editor-example
component1.html
<div class="data" id="d2">
<ngx-monaco-editor id="xmlDisplayer" style="height: 100%;" [options]="editorOptions" [(ngModel)]="code"></ngx-monaco-editor>
</div>
component1.ts
import { Component, OnInit } from '@angular/core';
import { filter, take } from 'rxjs/operators';
import { MonacoEditorConstructionOptions, MonacoEditorLoaderService } from '@materia-ui/ngx-monaco-editor';
export class component1 implements OnInit {
constructor(private monacoLoaderService: MonacoEditorLoaderService) {
this.monacoLoaderService.isMonacoLoaded$
.pipe(
filter((isLoaded) => !!isLoaded),
take(1)
)
.subscribe(() => {
monaco.editor.defineTheme('myCustomTheme', {
base: 'vs-dark', // can also be vs or hc-black
inherit: true, // can also be false to completely replace the builtin rules
rules: [
{
token: 'comment',
foreground: 'ffa500',
fontStyle: 'italic underline'
},
{ token: 'comment.js', foreground: '008800', fontStyle: 'bold' },
{ token: 'comment.css', foreground: '0000ff' } // will inherit fontStyle from `comment` above
],
colors: {
"editor.background": '#ff0000', // code background
'editorCursor.foreground': '#002438', // corsour color
'editor.lineHighlightBackground': '#9B9B9B', // line highlight colour
'editorLineNumber.foreground': '#666666', // line number colour
'editor.selectionBackground': '#666666', // code selection background
'editor.inactiveSelectionBackground': '#7e890b'
}
});
});
}
ngOnInit(): void {
}
//options and text for the xml display
editorOptions: MonacoEditorConstructionOptions = {
theme: 'myCustomTheme',
language: 'xml',
};
code = "xml goes here";
}
app.module.ts
import { NgModule } from '@angular/core';
import { MonacoEditorModule, MONACO_PATH } from '@materia-ui/ngx-monaco-editor';
//Other import functions
@NgModule({
declarations: [
AppComponent,
//Other component declarations
],
imports: [
MonacoEditorModule,
//Other imports
],
providers: [
{
provide: MONACO_PATH,
useValue: 'https://unpkg.com/monaco-editor@0.24.0/min/vs'
}
],
bootstrap: [AppComponent]
})
export class AppModule {
}
我希望这对遇到此问题的任何人有所帮助
我目前正在尝试更改我的 Angular 12 项目中 ngx-monaco-editor 元素的背景颜色,但到目前为止我尝试过的任何方法都没有奏效。有没有一种方法可以用我当前的设置轻松地做到这一点,或者我需要用另一种方法重做吗?这就是我目前正在尝试做的,但编辑器最终看起来像“vs”主题。我的代码片段:
component1.html
<div class="data" id="d2">
<ngx-monaco-editor id="xmlDisplayer" style="height: 100%;" [options]="editorOptions" [(ngModel)]="code"></ngx-monaco-editor>
</div>
component1.ts
import { Component, OnInit } from '@angular/core';
import { DataMapService } from '../data-map.service';
import { MonacoEditorLoaderService } from '@materia-ui/ngx-monaco-editor';
import { saveAs } from "file-saver";
import * as d3 from 'd3';
export class component1 implements OnInit {
constructor(private dataMapService: DataMapService, private monacoLoaderService: MonacoEditorLoaderService) {
this.monacoLoaderService.isMonacoLoaded$.pipe(
).subscribe(() => {
(window as any).monaco.editor.defineTheme('myCustomTheme', {
base: 'vs-dark', // can also be vs-dark or hc-black
colors: {
"editor.background": '#AA4A44', // code background
'editorCursor.foreground': '#002438', // corsour color
'editor.lineHighlightBackground': '#9B9B9B', // line highlight colour
'editorLineNumber.foreground': '#666666', // line number colour
'editor.selectionBackground': '#666666', // code selection background
'editor.inactiveSelectionBackground': '#7e890b'
}
});
});
}
ngOnInit(): void {
}
//options and text for the xml display
editorOptions = {theme: 'myCustomTheme', language: 'xml'};
code = "xml stuff goes here";
app.module.ts
import { MonacoEditorModule } from 'ngx-monaco-editor';
//Other component imports and misc
@NgModule({
declarations: [
AppComponent,
//Component declarations
],
imports: []
//Other imports
MonacoEditorModule.forRoot(),
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
}
我 运行 遇到的另一个问题是,当我尝试在其他地方执行“monaco.editor.defineTheme(...) 时,我遇到了 monaco 命名空间错误。任何帮助将不胜感激。
我无需过多更改代码即可解决问题。如果你在 component1.ts 中没有 .pipe(filter(),take()) 它将不起作用,所以一定要包括它。
这个 stackblitz 帮助我弄明白了:https://stackblitz.com/edit/materia-ngx-monaco-editor-example
component1.html
<div class="data" id="d2">
<ngx-monaco-editor id="xmlDisplayer" style="height: 100%;" [options]="editorOptions" [(ngModel)]="code"></ngx-monaco-editor>
</div>
component1.ts
import { Component, OnInit } from '@angular/core';
import { filter, take } from 'rxjs/operators';
import { MonacoEditorConstructionOptions, MonacoEditorLoaderService } from '@materia-ui/ngx-monaco-editor';
export class component1 implements OnInit {
constructor(private monacoLoaderService: MonacoEditorLoaderService) {
this.monacoLoaderService.isMonacoLoaded$
.pipe(
filter((isLoaded) => !!isLoaded),
take(1)
)
.subscribe(() => {
monaco.editor.defineTheme('myCustomTheme', {
base: 'vs-dark', // can also be vs or hc-black
inherit: true, // can also be false to completely replace the builtin rules
rules: [
{
token: 'comment',
foreground: 'ffa500',
fontStyle: 'italic underline'
},
{ token: 'comment.js', foreground: '008800', fontStyle: 'bold' },
{ token: 'comment.css', foreground: '0000ff' } // will inherit fontStyle from `comment` above
],
colors: {
"editor.background": '#ff0000', // code background
'editorCursor.foreground': '#002438', // corsour color
'editor.lineHighlightBackground': '#9B9B9B', // line highlight colour
'editorLineNumber.foreground': '#666666', // line number colour
'editor.selectionBackground': '#666666', // code selection background
'editor.inactiveSelectionBackground': '#7e890b'
}
});
});
}
ngOnInit(): void {
}
//options and text for the xml display
editorOptions: MonacoEditorConstructionOptions = {
theme: 'myCustomTheme',
language: 'xml',
};
code = "xml goes here";
}
app.module.ts
import { NgModule } from '@angular/core';
import { MonacoEditorModule, MONACO_PATH } from '@materia-ui/ngx-monaco-editor';
//Other import functions
@NgModule({
declarations: [
AppComponent,
//Other component declarations
],
imports: [
MonacoEditorModule,
//Other imports
],
providers: [
{
provide: MONACO_PATH,
useValue: 'https://unpkg.com/monaco-editor@0.24.0/min/vs'
}
],
bootstrap: [AppComponent]
})
export class AppModule {
}
我希望这对遇到此问题的任何人有所帮助