11import { Notice , Plugin } from 'obsidian' ;
2- import { DEFAULT_SETTINGS , migrateSettings , PropertiesToGraphSettings } from './settings' ;
2+ import { DEFAULT_SETTINGS , LegacyPropertiesToGraphSettings , migrateSettings , PropertiesToGraphSettings } from './settings' ;
33import { Hierarchy } from './hierarchy' ;
44import { PropertiesToGraphSettingTab } from './settingsTab' ;
55import { GraphData , GraphRenderer , GraphView } from './graphTypes' ;
@@ -16,7 +16,7 @@ export default class PropertiesToGraphPlugin extends Plugin {
1616 allNodeIds = new Set < string > ( ) ;
1717 hierarchy = new Hierarchy ( ) ;
1818
19- private refreshTimer : ReturnType < typeof setTimeout > | null = null ;
19+ private refreshTimer : number | null = null ;
2020 private originalOpenLinkText : typeof this . app . workspace . openLinkText | null = null ;
2121 private shiftHeld = false ;
2222
@@ -80,7 +80,7 @@ export default class PropertiesToGraphPlugin extends Plugin {
8080 window . removeEventListener ( 'keydown' , this . onKeyDown , true ) ;
8181 window . removeEventListener ( 'keyup' , this . onKeyUp , true ) ;
8282 window . removeEventListener ( 'blur' , this . onBlur , true ) ;
83- if ( this . refreshTimer ) clearTimeout ( this . refreshTimer ) ;
83+ if ( this . refreshTimer ) window . clearTimeout ( this . refreshTimer ) ;
8484
8585 for ( const leaf of this . app . workspace . getLeavesOfType ( 'graph' ) ) {
8686 const view = leaf . view as unknown as GraphView ;
@@ -95,7 +95,8 @@ export default class PropertiesToGraphPlugin extends Plugin {
9595 }
9696
9797 async loadSettings ( ) : Promise < void > {
98- const stored = await this . loadData ( ) ;
98+ const raw : unknown = await this . loadData ( ) ;
99+ const stored = raw as ( Partial < PropertiesToGraphSettings > & LegacyPropertiesToGraphSettings ) | null | undefined ;
99100 const { settings, migrated } = migrateSettings ( stored ) ;
100101 this . settings = settings ;
101102 if ( migrated ) await this . saveSettings ( ) ;
@@ -106,8 +107,8 @@ export default class PropertiesToGraphPlugin extends Plugin {
106107 }
107108
108109 scheduleRefresh ( ) : void {
109- if ( this . refreshTimer ) clearTimeout ( this . refreshTimer ) ;
110- this . refreshTimer = setTimeout ( ( ) => {
110+ if ( this . refreshTimer ) window . clearTimeout ( this . refreshTimer ) ;
111+ this . refreshTimer = window . setTimeout ( ( ) => {
111112 this . refreshTimer = null ;
112113 this . refreshGraphLeaves ( ) ;
113114 } , 350 ) ;
@@ -190,7 +191,7 @@ export default class PropertiesToGraphPlugin extends Plugin {
190191 const entry = this . settings . properties . find ( p => p . property === property ) ;
191192 if ( ! entry ) return ;
192193 entry . visible = visible ;
193- this . saveSettings ( ) . then ( ( ) => this . refreshGraphLeaves ( ) ) ;
194+ void this . saveSettings ( ) . then ( ( ) => this . refreshGraphLeaves ( ) ) ;
194195 }
195196 } ) ;
196197 } catch ( e ) {
@@ -200,31 +201,35 @@ export default class PropertiesToGraphPlugin extends Plugin {
200201 }
201202
202203 private installInjector ( renderer : GraphRenderer ) : void {
203- if ( ! renderer . __p2gOriginalSetData ) renderer . __p2gOriginalSetData = renderer . setData ;
204- const plugin = this ;
204+ if ( ! renderer . __p2gOriginalSetData ) renderer . __p2gOriginalSetData = renderer . setData . bind ( renderer ) ;
205+ const injectAndPatch = this . handleSetData . bind ( this ) ;
205206 renderer . setData = function ( this : GraphRenderer , data : GraphData ) {
206- if ( ! this . __p2gOriginalSetData ) return ;
207- try {
208- const injected = injectPropertyNodes ( plugin . app , plugin . settings , plugin . hierarchy , data ) ;
209- plugin . propertyNodeIds = injected . propertyNodeIds ;
210- plugin . allNodeIds = injected . allNodeIds ;
211- // Mutate the existing Maps in place so the patched prototype's
212- // captured `deps.nodeLabels`/`deps.nodeProperty` references
213- // (bound once, on first patch) stay live instead of pointing
214- // at a stale snapshot from the first render.
215- plugin . nodeLabels . clear ( ) ;
216- injected . nodeLabels . forEach ( ( label , id ) => plugin . nodeLabels . set ( id , label ) ) ;
217- plugin . nodeProperty . clear ( ) ;
218- injected . nodeProperty . forEach ( ( property , id ) => plugin . nodeProperty . set ( id , property ) ) ;
219- } catch ( e ) {
220- console . error ( 'Properties to Graph: injection failed' , e ) ;
221- }
222- const setDataResult = this . __p2gOriginalSetData . call ( this , data ) ;
223- plugin . patchNodePrototypeFor ( this ) ;
224- return setDataResult ;
207+ return injectAndPatch ( this , data ) ;
225208 } ;
226209 }
227210
211+ private handleSetData ( renderer : GraphRenderer , data : GraphData ) : unknown {
212+ if ( ! renderer . __p2gOriginalSetData ) return undefined ;
213+ try {
214+ const injected = injectPropertyNodes ( this . app , this . settings , this . hierarchy , data ) ;
215+ this . propertyNodeIds = injected . propertyNodeIds ;
216+ this . allNodeIds = injected . allNodeIds ;
217+ // Mutate the existing Maps in place so the patched prototype's
218+ // captured `deps.nodeLabels`/`deps.nodeProperty` references
219+ // (bound once, on first patch) stay live instead of pointing
220+ // at a stale snapshot from the first render.
221+ this . nodeLabels . clear ( ) ;
222+ injected . nodeLabels . forEach ( ( label , id ) => this . nodeLabels . set ( id , label ) ) ;
223+ this . nodeProperty . clear ( ) ;
224+ injected . nodeProperty . forEach ( ( property , id ) => this . nodeProperty . set ( id , property ) ) ;
225+ } catch ( e ) {
226+ console . error ( 'Properties to Graph: injection failed' , e ) ;
227+ }
228+ const setDataResult = renderer . __p2gOriginalSetData ( data ) ;
229+ this . patchNodePrototypeFor ( renderer ) ;
230+ return setDataResult ;
231+ }
232+
228233 private patchNodePrototypeFor ( renderer : GraphRenderer ) : void {
229234 patchNodePrototype ( renderer , {
230235 getColorForProperty : ( property : string ) => this . getColorForProperty ( property ) ,
@@ -240,30 +245,24 @@ export default class PropertiesToGraphPlugin extends Plugin {
240245
241246 const workspace = this . app . workspace ;
242247 this . originalOpenLinkText = workspace . openLinkText . bind ( workspace ) ;
243- const plugin = this ;
244248
245- workspace . openLinkText = function (
246- linktext : string ,
247- sourcePath : string ,
248- newLeaf ?: boolean ,
249- openViewState ?: unknown
250- ) {
251- const activeIsGraph = plugin . app . workspace . getMostRecentLeaf ( ) ?. view ?. getViewType ?.( ) === 'graph' ;
249+ workspace . openLinkText = ( linktext : string , sourcePath : string , newLeaf ?: boolean , openViewState ?: unknown ) => {
250+ const activeIsGraph = this . app . workspace . getMostRecentLeaf ( ) ?. view ?. getViewType ?.( ) === 'graph' ;
252251
253252 // Shift+click on a graph node folds/unfolds its structural subtree.
254- if ( plugin . shiftHeld && activeIsGraph && plugin . allNodeIds . has ( linktext ) ) {
255- plugin . handleFoldToggle ( linktext ) ;
253+ if ( this . shiftHeld && activeIsGraph && this . allNodeIds . has ( linktext ) ) {
254+ this . handleFoldToggle ( linktext ) ;
256255 return Promise . resolve ( ) ;
257256 }
258257
259258 // Property nodes are virtual graph nodes. Clicking them behaves
260259 // like clicking a native tag: filter the current graph.
261- if ( plugin . propertyNodeIds . has ( linktext ) ) {
262- plugin . applyPropertyFilter ( linktext ) ;
260+ if ( this . propertyNodeIds . has ( linktext ) ) {
261+ this . applyPropertyFilter ( linktext ) ;
263262 return Promise . resolve ( ) ;
264263 }
265264
266- return plugin . originalOpenLinkText ! ( linktext , sourcePath , newLeaf , openViewState as never ) ;
265+ return this . originalOpenLinkText ! ( linktext , sourcePath , newLeaf , openViewState as never ) ;
267266 } ;
268267 }
269268
@@ -309,7 +308,7 @@ export default class PropertiesToGraphPlugin extends Plugin {
309308 }
310309
311310 try {
312- view && view . showSearch && view . showSearch ( ) ;
311+ view ? .showSearch ?. ( ) ;
313312 } catch {
314313 // Non-fatal: some Obsidian versions may not expose showSearch().
315314 }
@@ -326,11 +325,11 @@ export default class PropertiesToGraphPlugin extends Plugin {
326325 } else {
327326 for ( const id of descendants ) this . settings . hiddenNodes [ id ] = true ;
328327 }
329- this . saveSettings ( ) . then ( ( ) => this . refreshGraphLeaves ( ) ) ;
328+ void this . saveSettings ( ) . then ( ( ) => this . refreshGraphLeaves ( ) ) ;
330329 }
331330
332331 unfoldAll ( ) : void {
333332 this . settings . hiddenNodes = { } ;
334- this . saveSettings ( ) . then ( ( ) => this . refreshGraphLeaves ( ) ) ;
333+ void this . saveSettings ( ) . then ( ( ) => this . refreshGraphLeaves ( ) ) ;
335334 }
336335}
0 commit comments