Skip to content

Commit

Permalink
fix: decouple document updates and metadata updates in InvalidLinkDec…
Browse files Browse the repository at this point in the history
…orations
  • Loading branch information
dtkav committed Sep 11, 2024
1 parent 0b7e3c7 commit 666e606
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/markdownView/InvalidLinkExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {};
Expand All @@ -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 = () => {
Expand Down Expand Up @@ -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: {
Expand All @@ -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();
}

Expand Down

0 comments on commit 666e606

Please sign in to comment.