fix(dees-editor-bare): make Monaco editor follow domtools theme and clean up theme subscription on disconnect
This commit is contained in:
@@ -46,6 +46,8 @@ export class DeesEditorBare extends DeesElement {
|
||||
})
|
||||
accessor wordWrap: monaco.editor.IStandaloneEditorConstructionOptions['wordWrap'] = 'off';
|
||||
|
||||
private monacoThemeSubscription: domtools.plugins.smartrx.rxjs.Subscription | null = null;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
domtools.DomTools.setupDomTools();
|
||||
@@ -102,15 +104,27 @@ export class DeesEditorBare extends DeesElement {
|
||||
paths: { vs: `${monacoCdnBase}/min/vs` },
|
||||
});
|
||||
(window as any).require(['vs/editor/editor.main'], async () => {
|
||||
// Get current theme from domtools
|
||||
const domtoolsInstance = await this.domtoolsPromise;
|
||||
const isBright = domtoolsInstance.themeManager.goBrightBoolean;
|
||||
const initialTheme = isBright ? 'vs' : 'vs-dark';
|
||||
|
||||
const editor = ((window as any).monaco.editor as typeof monaco.editor).create(container, {
|
||||
value: this.content,
|
||||
language: this.language,
|
||||
theme: 'vs-dark',
|
||||
theme: initialTheme,
|
||||
useShadowDOM: true,
|
||||
fontSize: 16,
|
||||
automaticLayout: true,
|
||||
wordWrap: this.wordWrap
|
||||
});
|
||||
|
||||
// Subscribe to theme changes
|
||||
this.monacoThemeSubscription = domtoolsInstance.themeManager.themeObservable.subscribe((goBright: boolean) => {
|
||||
const newTheme = goBright ? 'vs' : 'vs-dark';
|
||||
editor.updateOptions({ theme: newTheme });
|
||||
});
|
||||
|
||||
this.editorDeferred.resolve(editor);
|
||||
});
|
||||
const css = await (
|
||||
@@ -128,4 +142,12 @@ export class DeesEditorBare extends DeesElement {
|
||||
});
|
||||
this.contentSubject.next(editor.getValue());
|
||||
}
|
||||
|
||||
public async disconnectedCallback(): Promise<void> {
|
||||
await super.disconnectedCallback();
|
||||
if (this.monacoThemeSubscription) {
|
||||
this.monacoThemeSubscription.unsubscribe();
|
||||
this.monacoThemeSubscription = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user