@@ -5,8 +5,7 @@ import * as PIXI from 'pixi.js';
55export default class GraphLinkTypesPlugin extends Plugin {
66 api = getAPI ( ) ;
77 uniqueKeys = new Set < string > ( ) ;
8- nodeTextMap = new Map ( ) ; // Store node-text pairs
9- selectedKey : string | null ;
8+ nodeTextMap = new Map ( ) ; // Store link-text pairs
109
1110
1211
@@ -88,6 +87,15 @@ export default class GraphLinkTypesPlugin extends Plugin {
8887 createTextForLink ( renderer , link ) {
8988 const linkString = this . getMetadataKeyForLink ( link . source . id , link . target . id ) ;
9089 if ( linkString === null ) return ;
90+
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 ) ;
94+ renderer . px . stage . removeChild ( existingText ) ;
95+ existingText . destroy ( ) ;
96+ }
97+
98+ // Create new text for the link
9199 const textStyle = new PIXI . TextStyle ( {
92100 fontFamily : 'Arial' ,
93101 fontSize : 36 ,
@@ -96,9 +104,7 @@ export default class GraphLinkTypesPlugin extends Plugin {
96104 const text = new PIXI . Text ( linkString , textStyle ) ;
97105 text . alpha = 0.7 ;
98106 text . anchor . set ( 0.5 , 0.5 ) ;
99- if ( ! this . nodeTextMap . has ( link ) ) {
100- this . nodeTextMap . set ( link , text ) ;
101- }
107+ this . nodeTextMap . set ( link , text ) ;
102108
103109 this . updateTextPosition ( renderer , link ) ;
104110 renderer . px . stage . addChild ( text ) ;
@@ -107,22 +113,23 @@ export default class GraphLinkTypesPlugin extends Plugin {
107113
108114 updateTextPosition ( renderer , link ) {
109115 const text = this . nodeTextMap . get ( link ) ;
110- if ( text ) {
111- const midX = ( link . source . x + link . target . x ) / 2 ;
112- const midY = ( link . source . y + link . target . y ) / 2 ;
113- const { x, y } = this . getLinkToTextCoordinates ( midX , midY , renderer . panX , renderer . panY , renderer . scale ) ;
114- text . x = x ;
115- text . y = y ;
116- text . scale . set ( 1 / ( 3 * renderer . nodeScale ) ) ;
116+ if ( ! text || ! link . source || ! link . target ) {
117+ return ;
117118 }
119+ const midX = ( link . source . x + link . target . x ) / 2 ;
120+ const midY = ( link . source . y + link . target . y ) / 2 ;
121+ const { x, y } = this . getLinkToTextCoordinates ( midX , midY , renderer . panX , renderer . panY , renderer . scale ) ;
122+ text . x = x ;
123+ text . y = y ;
124+ text . scale . set ( 1 / ( 3 * renderer . nodeScale ) ) ;
118125 }
119126
120- destroyMap ( ) {
127+ destroyMap ( renderer ) {
121128 if ( this . nodeTextMap . size > 0 ) {
122129 this . nodeTextMap . forEach ( ( text , link ) => {
123- if ( text !== null ) {
124- renderer . px . stage . removeChild ( text ) ; // Remove the text from the PIXI container
125- text . destroy ( ) ; // Destroy the text object
130+ 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
126133 }
127134 this . nodeTextMap . delete ( link ) ;
128135 } ) ;
@@ -138,7 +145,7 @@ export default class GraphLinkTypesPlugin extends Plugin {
138145 return ;
139146 }
140147 const renderer = graphLeaf . view . renderer ;
141- // this.destroyMap();
148+ this . destroyMap ( renderer ) ;
142149 const links = renderer . links ;
143150 links . forEach ( link => this . createTextForLink ( renderer , link ) ) ;
144151 requestAnimationFrame ( this . updatePositions . bind ( this ) ) ;
@@ -150,13 +157,18 @@ export default class GraphLinkTypesPlugin extends Plugin {
150157 return ;
151158 }
152159 const renderer = graphLeaf . view . renderer ;
153- this . nodeTextMap . forEach ( ( text , link ) => {
160+
161+ renderer . links . forEach ( link => {
162+ if ( ! this . nodeTextMap . has ( link ) ) {
163+ this . createTextForLink ( renderer , link ) ;
164+ }
154165 this . updateTextPosition ( renderer , link ) ;
155166 } ) ;
167+
156168 requestAnimationFrame ( this . updatePositions . bind ( this ) ) ;
157169 }
158170
159- getLinkToTextCoordinates ( linkX , linkY , panX , panY , scale ) {
171+ getLinkToTextCoordinates ( linkX : number , linkY : number , panX : number , panY : number , scale : number ) {
160172 return { x : linkX * scale + panX , y : linkY * scale + panY } ;
161173 }
162174}
0 commit comments