materiahq / ngx-monaco-editor

Monaco Editor Library for Angular v6 and above
MIT License
159 stars 35 forks source link

are there anyway to dynamically change theme of the editor? #6

Closed datavisorfrankhe closed 5 years ago

datavisorfrankhe commented 5 years ago

I need to dynamically change the editor theme, are there any method I can call to change it dynamically? Thanks

jmarc-roy commented 5 years ago

Yes you can do it by modifying the options input and changing its reference (because our monaco components have an OnPush change detection strategy).

Here is an example :

component.html

<ngx-monaco-editor [options]="editorOptions" [(ngModel)]="code"></ngx-monaco-editor>

component.ts

editorOptions = {theme: 'vs-dark', language: 'javascript'};
....
changeTheme(theme: string) {
  this.editorOptions = { ...this.editorOptions, theme: theme }; // Or Object.assign({}, this.editorOptions, {theme: theme});
}
datavisorfrankhe commented 5 years ago

Thanks it worked. But Object.assign is not triggering the updates

jmarc-roy commented 5 years ago

Glad it works! Object.assign() should works too, I am using this method in the live demo.

anhminhtran235 commented 4 years ago

I use Object.assign() but it removes all decorations and selections. If that's not what you want, try monaco.editor.setModelLanguage() and monaco.editor.setTheme()