1- import { Plugin , WorkspaceLeaf , Notice } from 'obsidian' ;
2- import { getAPI } from 'obsidian-dataview' ;
1+ import { Plugin , WorkspaceLeaf , Notice } from 'obsidian' ;
2+ import { getAPI , Page } from 'obsidian-dataview' ;
33import * as PIXI from 'pixi.js' ;
44
55export default class GraphLinkTypesPlugin extends Plugin {
66 api = getAPI ( ) ;
7- uniqueKeys = new Set < string > ( ) ;
8- nodeTextMap = new Map ( ) ; // Store link-text pairs
9-
10-
7+ nodeTextMap : Map < string , PIXI . Text > = new Map ( ) ;
118
12- async onload ( ) {
9+ async onload ( ) : Promise < void > {
1310 if ( this . api ) {
14- // Listen for DataView 'index-ready' event
15- this . registerEvent ( this . app . metadataCache . on ( "dataview:index-ready" , ( ) => {
16- console . log ( "Dataview index is ready." ) ;
11+ this . registerEvent ( this . app . workspace . on ( 'layout-change' , ( ) => {
12+ this . startUpdateLoop ( ) ;
1713 } ) ) ;
1814
19- // Listen for DataView 'metadata-change' event
20- this . registerEvent ( this . app . metadataCache . on ( "dataview:metadata-change" ,
21- ( type , file , oldPath ) => {
22- console . log ( `Metadata changed in file: ${ file . path } ` ) ;
23- }
24- ) ) ;
25-
15+ this . addCommand ( {
16+ id : 'print-link-type' ,
17+ name : 'Print Link Type' ,
18+ callback : ( ) => this . startUpdateLoop ( 1 )
19+ } ) ;
2620 } else {
2721 console . error ( "Dataview plugin is not available." ) ;
2822 }
29-
30- this . registerEvent ( this . app . workspace . on ( 'layout-change' , ( ) => {
31- this . getUniqueMetadataKeys ( ) ;
32- this . startUpdateLoop ( ) ;
33- } ) ) ;
34-
35-
36-
37- this . addCommand ( {
38- id : 'print-link-type' ,
39- name : 'Print Link Type' ,
40- callback : ( ) => this . startUpdateLoop ( 1 )
41- } ) ;
42-
43- }
44-
45- getUniqueMetadataKeys ( ) {
46- const allPages = this . api . pages ( '' ) ;
47-
48- for ( const page of allPages ) {
49- for ( const [ key , value ] of Object . entries ( page ) ) {
50- if ( this . isLink ( value ) ) {
51- this . uniqueKeys . add ( key ) ;
52- }
53- }
54- }
55- this . uniqueKeys . delete ( "file" ) ;
5623 }
57-
58-
5924
6025 getMetadataKeyForLink ( sourceId : string , targetId : string ) : string | null {
61- const sourcePage = this . api . page ( sourceId ) ;
62- if ( ! sourcePage ) {
63- return null ;
64- }
26+ const sourcePage : Page | undefined = this . api . page ( sourceId ) ;
27+ if ( ! sourcePage ) return null ;
6528
6629 for ( const [ key , value ] of Object . entries ( sourcePage ) ) {
6730 if ( this . isLink ( value ) && value . path === targetId ) {
6831 return key ;
6932 }
33+ if ( Array . isArray ( value ) ) {
34+ for ( const link of value ) {
35+ if ( this . isLink ( link ) && link . path === targetId ) {
36+ return key ;
37+ }
38+ }
39+ }
7040 }
71-
7241 return null ;
7342 }
7443
@@ -77,98 +46,96 @@ export default class GraphLinkTypesPlugin extends Plugin {
7746 }
7847
7948 findGraphLeaf ( ) : WorkspaceLeaf | null {
80- let graphLeaves = this . app . workspace . getLeavesOfType ( 'graph' ) ;
81- if ( graphLeaves . length != 1 ) {
82- return null ;
83- }
84- return graphLeaves [ 0 ]
49+ const graphLeaves : WorkspaceLeaf [ ] = this . app . workspace . getLeavesOfType ( 'graph' ) ;
50+ return graphLeaves . length === 1 ? graphLeaves [ 0 ] : null ;
8551 }
8652
87- createTextForLink ( renderer , link ) {
88- const linkString = this . getMetadataKeyForLink ( link . source . id , link . target . id ) ;
53+ createTextForLink ( renderer : any , link : any ) : void {
54+ const linkString : string | null = this . getMetadataKeyForLink ( link . source . id , link . target . id ) ;
8955 if ( linkString === null ) return ;
9056
91- // Check if text already exists for the link and remove it
92- if ( this . nodeTextMap . has ( link ) ) {
93- const existingText = this . nodeTextMap . get ( link ) ;
57+ const linkKey : string = `${ link . source . id } -${ link . target . id } ` ;
58+
59+ if ( this . nodeTextMap . has ( linkKey ) ) {
60+ const existingText = this . nodeTextMap . get ( linkKey ) ! ;
9461 renderer . px . stage . removeChild ( existingText ) ;
9562 existingText . destroy ( ) ;
9663 }
9764
98- // Create new text for the link
99- const textStyle = new PIXI . TextStyle ( {
65+ const textStyle : PIXI . TextStyle = new PIXI . TextStyle ( {
10066 fontFamily : 'Arial' ,
10167 fontSize : 36 ,
102- fill : 0x00000
68+ fill : 0x000000
10369 } ) ;
104- const text = new PIXI . Text ( linkString , textStyle ) ;
70+ const text : PIXI . Text = new PIXI . Text ( linkString , textStyle ) ;
10571 text . alpha = 0.7 ;
10672 text . anchor . set ( 0.5 , 0.5 ) ;
107- this . nodeTextMap . set ( link , text ) ;
108-
73+ this . nodeTextMap . set ( linkKey , text ) ;
74+
10975 this . updateTextPosition ( renderer , link ) ;
11076 renderer . px . stage . addChild ( text ) ;
11177 }
112-
113-
114- updateTextPosition ( renderer , link ) {
115- const text = this . nodeTextMap . get ( link ) ;
78+
79+ updateTextPosition ( renderer : any , link : any ) : void {
80+ const linkKey : string = ` ${ link . source . id } - ${ link . target . id } ` ;
81+ const text : PIXI . Text | undefined = this . nodeTextMap . get ( linkKey ) ;
11682 if ( ! text || ! link . source || ! link . target ) {
11783 return ;
11884 }
119- const midX = ( link . source . x + link . target . x ) / 2 ;
120- const midY = ( link . source . y + link . target . y ) / 2 ;
85+ const midX : number = ( link . source . x + link . target . x ) / 2 ;
86+ const midY : number = ( link . source . y + link . target . y ) / 2 ;
12187 const { x, y } = this . getLinkToTextCoordinates ( midX , midY , renderer . panX , renderer . panY , renderer . scale ) ;
12288 text . x = x ;
12389 text . y = y ;
124- text . scale . set ( 1 / ( 3 * renderer . nodeScale ) ) ;
90+ text . scale . set ( 1 / ( 3 * renderer . nodeScale ) ) ;
12591 }
12692
127- destroyMap ( renderer ) {
93+ destroyMap ( renderer : any ) : void {
94+ console . log ( "Destroying Map" ) ;
12895 if ( this . nodeTextMap . size > 0 ) {
129- this . nodeTextMap . forEach ( ( text , link ) => {
96+ this . nodeTextMap . forEach ( ( text , linkKey ) => {
13097 if ( text && renderer . px . stage . children . includes ( text ) ) {
131- renderer . px . stage . removeChild ( text ) ; // Remove the text from the PIXI container only if it exists
132- text . destroy ( ) ; // Destroy the text object
98+ renderer . px . stage . removeChild ( text ) ;
99+ text . destroy ( ) ;
133100 }
134- this . nodeTextMap . delete ( link ) ;
101+ this . nodeTextMap . delete ( linkKey ) ;
135102 } ) ;
136103 }
137104 }
138-
139- startUpdateLoop ( verbosity : number = 0 ) {
140- const graphLeaf = this . findGraphLeaf ( ) ;
141- if ( graphLeaf === null ) {
105+
106+ startUpdateLoop ( verbosity : number = 0 ) : void {
107+ const graphLeaf : WorkspaceLeaf | null = this . findGraphLeaf ( ) ;
108+ if ( ! graphLeaf ) {
142109 if ( verbosity > 0 ) {
143- new Notice ( "No graph or multiple graphs present." )
110+ new Notice ( "No graph or multiple graphs present." ) ;
144111 }
145112 return ;
146113 }
147- const renderer = graphLeaf . view . renderer ;
114+ const renderer : any = graphLeaf . view . renderer ;
148115 this . destroyMap ( renderer ) ;
149- const links = renderer . links ;
150- links . forEach ( link => this . createTextForLink ( renderer , link ) ) ;
116+ renderer . links . forEach ( ( link : any ) => this . createTextForLink ( renderer , link ) ) ;
151117 requestAnimationFrame ( this . updatePositions . bind ( this ) ) ;
152118 }
153-
154- updatePositions ( ) {
155- const graphLeaf = this . findGraphLeaf ( ) ;
156- if ( graphLeaf === null ) {
119+
120+ updatePositions ( ) : void {
121+ const graphLeaf : WorkspaceLeaf | null = this . findGraphLeaf ( ) ;
122+ if ( ! graphLeaf ) {
157123 return ;
158124 }
159- const renderer = graphLeaf . view . renderer ;
160-
161- renderer . links . forEach ( link => {
162- if ( ! this . nodeTextMap . has ( link ) ) {
125+ const renderer : any = graphLeaf . view . renderer ;
126+
127+ renderer . links . forEach ( ( link : any ) => {
128+ const linkKey : string = `${ link . source . id } -${ link . target . id } ` ;
129+ if ( ! this . nodeTextMap . has ( linkKey ) ) {
163130 this . createTextForLink ( renderer , link ) ;
164131 }
165132 this . updateTextPosition ( renderer , link ) ;
166133 } ) ;
167134
168135 requestAnimationFrame ( this . updatePositions . bind ( this ) ) ;
169136 }
170-
171- getLinkToTextCoordinates ( linkX : number , linkY : number , panX : number , panY : number , scale : number ) {
137+
138+ getLinkToTextCoordinates ( linkX : number , linkY : number , panX : number , panY : number , scale : number ) : { x : number , y : number } {
172139 return { x : linkX * scale + panX , y : linkY * scale + panY } ;
173140 }
174141}
0 commit comments