1+ import { WorkspaceWindow } from "obsidian"
12import { Canvas } from "src/@types/Canvas"
23import { CanvasEvent } from "src/events/events"
34import AdvancedCanvasPlugin from "src/main"
@@ -12,11 +13,18 @@ export default class ColorPaletteCanvasExtension {
1213 constructor ( plugin : AdvancedCanvasPlugin ) {
1314 this . plugin = plugin
1415
16+ this . plugin . registerEvent ( this . plugin . app . workspace . on (
17+ 'advanced-canvas:window-open' ,
18+ ( ) => this . updateCustomColorModStyleClasses ( )
19+ ) )
20+
1521 this . plugin . registerEvent ( this . plugin . app . workspace . on (
1622 'css-change' ,
1723 ( ) => this . updateCustomColorModStyleClasses ( )
1824 ) )
1925
26+ this . updateCustomColorModStyleClasses ( )
27+
2028 this . plugin . registerEvent ( this . plugin . app . workspace . on (
2129 CanvasEvent . PopupMenuCreated ,
2230 ( canvas : Canvas ) => this . patchColorSelection ( canvas )
@@ -26,19 +34,22 @@ export default class ColorPaletteCanvasExtension {
2634 }
2735
2836 private updateCustomColorModStyleClasses ( ) {
29- document . getElementById ( CUSTOM_COLORS_MOD_STYLES_ID ) ?. remove ( )
30-
31- const customColorModStyle = document . createElement ( 'style' )
32- customColorModStyle . id = CUSTOM_COLORS_MOD_STYLES_ID
33- document . body . appendChild ( customColorModStyle )
34-
35- for ( const colorId of this . getCustomColors ( ) ) {
36- // Add mod-canvas-color-<colorId> style to the css
37- customColorModStyle . innerHTML += `
38- .mod-canvas-color-${ colorId } {
39- --canvas-color: var(--canvas-color-${ colorId } );
40- }
41- `
37+ const customCss = this . getCustomColors ( ) . map ( ( colorId ) => `
38+ .mod-canvas-color-${ colorId } {
39+ --canvas-color: var(--canvas-color-${ colorId } );
40+ }
41+ ` ) . join ( '' )
42+
43+ for ( const win of this . plugin . windowsManager . windows ) {
44+ const doc = win . document
45+
46+ doc . getElementById ( CUSTOM_COLORS_MOD_STYLES_ID ) ?. remove ( )
47+
48+ const customColorModStyle = doc . createElement ( 'style' )
49+ customColorModStyle . id = CUSTOM_COLORS_MOD_STYLES_ID
50+ doc . head . appendChild ( customColorModStyle )
51+
52+ customColorModStyle . textContent = customCss
4253 }
4354 }
4455
@@ -58,8 +69,6 @@ export default class ColorPaletteCanvasExtension {
5869 const submenu = canvas . menu . menuEl . querySelector ( '.canvas-submenu' )
5970 if ( ! submenu ) return
6071
61- this . updateCustomColorModStyleClasses ( )
62-
6372 const currentNodeColor = canvas . getSelectionData ( ) . nodes . map ( node => node . color ) . last ( )
6473 for ( const colorId of this . getCustomColors ( ) ) {
6574 const customColorMenuItem = this . createColorMenuItem ( canvas , colorId )
0 commit comments