diff --git a/src/markdownView/InvalidLinkExtension.ts b/src/markdownView/InvalidLinkExtension.ts index 20a4954..6539abb 100644 --- a/src/markdownView/InvalidLinkExtension.ts +++ b/src/markdownView/InvalidLinkExtension.ts @@ -18,6 +18,7 @@ export class InvalidLinkPluginValue { editor: EditorView; view?: S3View; connectionManager: LiveViewManager | null; + linkRanges: { from: number; to: number }[]; decorations: DecorationSet; log: (message: string) => void = (message: string) => {}; offMetadataUpdates = () => {}; @@ -26,12 +27,12 @@ export class InvalidLinkPluginValue { this.editor = editor; this.connectionManager = this.editor.state.facet(connectionManagerFacet); this.decorations = Decoration.none; + this.linkRanges = []; const cb = (tfile: TFile, data: string, cache: CachedMetadata) => { if (tfile !== this.view?.document?.tfile) { return; } - console.warn("metadata update!"); - this.updateDecorations(); + this.updateFromMetadata(); }; const offRef = app.metadataCache.on("changed", cb); this.offMetadataUpdates = () => { @@ -61,23 +62,25 @@ export class InvalidLinkPluginValue { if (this.view.document) { this.view.document.whenSynced().then(() => { + this.updateFromMetadata(); this.updateDecorations(); }); } } } + updateFromMetadata() { + if (!this.view || !this.view.document) return; + this.linkRanges = this.view.document.getInvalidLinks(); + } + updateDecorations() { if (!FeatureFlagManager.getInstance().flags.enableInvalidLinkDecoration) { this.decorations = Decoration.none; return; } - if (!this.view || !this.view.document) return; - - const invalidLinks = this.view.document.getInvalidLinks(); - - const decorations = invalidLinks.map(({ from, to }) => + const decorations = this.linkRanges.map(({ from, to }) => Decoration.mark({ class: "invalid-link", attributes: { @@ -86,16 +89,23 @@ export class InvalidLinkPluginValue { }, }).range(from, to), ); - - this.decorations = Decoration.set(decorations); + if (decorations) { + this.decorations = Decoration.set(decorations); + } else { + this.decorations = Decoration.none; + } } - update(update: ViewUpdate) { if (this.connectionManager) { this.view = this.connectionManager.findView(update.view); } if (update.docChanged) { + this.linkRanges = this.linkRanges.map((range) => { + const newFrom = update.changes.mapPos(range.from); + const newTo = update.changes.mapPos(range.to); + return { from: newFrom, to: newTo }; + }); this.updateDecorations(); }