11import { TFile } from "obsidian"
22import { CanvasData , CanvasNodeData } from "src/@types/Canvas"
3- import { FileCacheEntry , MetadataCacheEntry , MetadataCacheMap , ResolvedLinks } from "src/@types/Obsidian"
3+ import { CanvasPos , FileCacheEntry , MetadataCacheEntry , MetadataCacheMap , ResolvedLinks } from "src/@types/Obsidian"
44import HashHelper from "src/utils/hash-helper"
55import PatchHelper from "src/utils/patch-helper"
66import PathHelper from "src/utils/path-helper"
@@ -44,31 +44,51 @@ export default class MetadataCachePatcher extends Patcher {
4444 // Extract canvas file node embeds
4545 const fileNodesEmbeds = content . nodes
4646 . filter ( ( node : CanvasNodeData ) => node . type === 'file' && node . file )
47- . map ( ( node : CanvasNodeData ) => node . file )
48- . map ( ( path : string ) => ( {
49- link : path ,
50- original : path ,
51- displayText : path ,
52- position : { start : { line : 0 , col : 0 , offset : 0 } , end : { line : 0 , col : 0 , offset : 0 } }
47+ . map ( ( node : CanvasNodeData ) => ( {
48+ link : node . file ,
49+ original : node . file ,
50+ displayText : node . file ,
51+ position : {
52+ nodeId : node . id ,
53+ start : { line : 0 , col : 0 , offset : 0 } ,
54+ end : { line : 0 , col : 0 , offset : 0 }
55+ }
5356 } ) )
5457
5558 // Extract canvas text node links/embeds
5659 const textEncoder = new TextEncoder ( )
57- const textNodesMetadataPromises = content . nodes
60+ const textNodes = content . nodes
5861 . filter ( ( node : CanvasNodeData ) => node . type === 'text' && node . text )
59- . map ( ( node : CanvasNodeData ) => node . text )
60- . map ( ( text : string ) => textEncoder . encode ( text ) . buffer )
62+
63+ const textNodesIds = textNodes
64+ . map ( ( node : CanvasNodeData ) => node . id )
65+
66+ const textNodesMetadataPromises = textNodes
67+ . map ( ( node : CanvasNodeData ) => textEncoder . encode ( node . text ) . buffer )
6168 . map ( ( buffer : ArrayBuffer ) => this . computeMetadataAsync ( buffer ) as Promise < MetadataCacheEntry > )
62-
6369 const textNodesMetadata = await Promise . all ( textNodesMetadataPromises ) // Wait for all text nodes to be resolved
6470
6571 const textNodesEmbeds = textNodesMetadata
66- . map ( ( metadata : MetadataCacheEntry ) => metadata . embeds || [ ] )
67- . flat ( )
72+ . map ( ( metadata : MetadataCacheEntry , index : number ) => (
73+ ( metadata . embeds || [ ] ) . map ( embed => ( {
74+ ...embed ,
75+ position : {
76+ nodeId : textNodesIds [ index ] ,
77+ ...embed . position
78+ }
79+ } ) )
80+ ) ) . flat ( )
6881
6982 const textNodesLinks = textNodesMetadata
70- . map ( ( metadata : MetadataCacheEntry ) => metadata . links || [ ] )
71- . flat ( )
83+ . map ( ( metadata : MetadataCacheEntry , index : number ) => (
84+ ( metadata . links || [ ] ) . map ( link => ( {
85+ ...link ,
86+ position : {
87+ nodeId : textNodesIds [ index ] ,
88+ ...link . position
89+ }
90+ } ) )
91+ ) ) . flat ( )
7292
7393 // Update metadata cache
7494 ; ( this . metadataCache as MetadataCacheMap ) [ fileHash ] = {
@@ -169,11 +189,5 @@ export default class MetadataCachePatcher extends Patcher {
169189 if ( PathHelper . extension ( file . path ) !== 'canvas' ) return
170190 this . plugin . app . metadataCache . computeFileMetadataAsync ( file )
171191 } ) )
172-
173- // Patch complete - reload graph views and local graph views as soon as the layout is ready
174- this . plugin . app . workspace . onLayoutReady ( ( ) => {
175- const graphViews = [ ...this . plugin . app . workspace . getLeavesOfType ( 'graph' ) , ...this . plugin . app . workspace . getLeavesOfType ( 'localgraph' ) ]
176- for ( const view of graphViews ) ( view as any ) . rebuildView ( )
177- } )
178192 }
179193}
0 commit comments