11import AdvancedCanvasPlugin from "src/main"
2- import { BBox , CanvasNode } from "src/@types/Canvas"
2+ import { BBox , CanvasNode , SelectionData } from "src/@types/Canvas"
33import { patchWorkspaceFunction } from "src/utils/patch-helper"
44import { CanvasEvent } from "./events"
55
@@ -13,19 +13,14 @@ export default class CanvasEventEmitter {
1313 // Patch canvas
1414 patchWorkspaceFunction ( this . plugin , ( ) => this . plugin . getCurrentCanvas ( ) , {
1515 // Add custom function
16- setNodeUnknownData : ( _next : any ) => function ( node : CanvasNode , key : string , value : any ) {
17- node . unknownData [ key ] = value
18- this . requestSave ( )
19-
16+ setNodeData : ( _next : any ) => function ( node : CanvasNode , key : string , value : any ) {
17+ node . setData ( {
18+ ...node . getData ( ) ,
19+ [ key ] : value
20+ } )
21+
2022 that . triggerWorkspaceEvent ( CanvasEvent . NodesChanged , this , [ node ] )
2123 } ,
22- // Listen to canvas change
23- importData : ( next : any ) => function ( ...args : any ) {
24- const result = next . call ( this , ...args )
25- that . triggerWorkspaceEvent ( CanvasEvent . CanvasChanged , this )
26- that . triggerWorkspaceEvent ( CanvasEvent . NodesChanged , this , [ ...this . nodes . values ( ) ] )
27- return result
28- } ,
2924 markViewportChanged : ( next : any ) => function ( ...args : any ) {
3025 that . triggerWorkspaceEvent ( CanvasEvent . ViewportChanged . Before , this )
3126 const result = next . call ( this , ...args )
@@ -48,9 +43,9 @@ export default class CanvasEventEmitter {
4843 that . triggerWorkspaceEvent ( CanvasEvent . NodesChanged , this , [ ...this . nodes . values ( ) ] )
4944 return result
5045 } ,
51- addNode : ( next : any ) => function ( node : CanvasNode ) {
52- const result = next . call ( this , node )
53- that . triggerWorkspaceEvent ( CanvasEvent . NodesChanged , this , [ node ] )
46+ handlePaste : ( next : any ) => function ( ... args : any ) {
47+ const result = next . call ( this , ... args )
48+ that . triggerWorkspaceEvent ( CanvasEvent . NodesChanged , this , [ ... this . nodes . values ( ) ] )
5449 return result
5550 } ,
5651 setReadonly : ( next : any ) => function ( readonly : boolean ) {
@@ -62,13 +57,15 @@ export default class CanvasEventEmitter {
6257
6358 // Patch canvas popup menu
6459 patchWorkspaceFunction ( this . plugin , ( ) => this . plugin . getCurrentCanvas ( ) ?. menu , {
65- render : ( next : any ) => function ( ... args : any ) {
66- const result = next . call ( this , ... args )
60+ render : ( next : any ) => function ( visible : boolean ) {
61+ const result = next . call ( this , visible )
6762
68- that . triggerWorkspaceEvent ( CanvasEvent . PopupMenuCreated , this . canvas )
63+ if ( visible ) {
64+ that . triggerWorkspaceEvent ( CanvasEvent . PopupMenuCreated , this . canvas )
6965
70- // Re-Center the popup menu
71- next . call ( this )
66+ // Re-Center the popup menu
67+ next . call ( this , false )
68+ }
7269
7370 return result
7471 }
@@ -85,25 +82,24 @@ export default class CanvasEventEmitter {
8582 }
8683 } )
8784
88- // Update current canvas on startup
89- const startupListener = this . plugin . app . workspace . on ( 'active-leaf-change' , ( ) => {
85+ // Listen to canvas changes
86+ const onCanvasChangeListener = this . plugin . app . workspace . on ( 'active-leaf-change' , ( ) => {
9087 const canvas = this . plugin . getCurrentCanvas ( )
9188 if ( ! canvas ) return
9289
9390 this . triggerWorkspaceEvent ( CanvasEvent . CanvasChanged , canvas )
9491 this . triggerWorkspaceEvent ( CanvasEvent . ViewportChanged . After , canvas )
9592 this . triggerWorkspaceEvent ( CanvasEvent . ReadonlyChanged , canvas , canvas . readonly )
9693 this . triggerWorkspaceEvent ( CanvasEvent . NodesChanged , canvas , [ ...canvas . nodes . values ( ) ] )
97-
98- this . plugin . app . workspace . offref ( startupListener )
9994 } )
100- this . plugin . registerEvent ( startupListener )
95+ this . plugin . registerEvent ( onCanvasChangeListener )
10196
10297 // Trigger instantly (Plugin reload)
103- startupListener . fn . call ( this . plugin . app . workspace )
98+ onCanvasChangeListener . fn . call ( this . plugin . app . workspace )
10499 }
105100
106101 private triggerWorkspaceEvent ( event : string , ...args : any ) {
102+ if ( event === CanvasEvent . CanvasChanged ) console . log ( 'Canvas changed' )
107103 this . plugin . app . workspace . trigger ( event , ...args )
108104 }
109105}
0 commit comments