Skip to content

Build your personal knowledge base with Trilium Notes

fix(images): version images of unloaded notes, refresh only tracked images

Elian Doran7b850b117b44
diff --git a/apps/client/src/services/froca_updater.spec.ts b/apps/client/src/services/froca_updater.spec.ts
index d0f4f4217c..caed058416 100644
--- a/apps/client/src/services/froca_updater.spec.ts
+++ b/apps/client/src/services/froca_updater.spec.ts
@@ -830,4 +830,18 @@ describe("froca_updater - note images", () => {
}) ]);
expect(getNoteImageSourceToLoad(src)).not.toBe(afterAttachment);
});
+
+ it("gives a new URL to the image of a note that froca has not loaded", async () => {
+ const src = "api/images/notInFroca/Picture";
+ const before = getNoteImageSourceToLoad(src);
+
+ await process([ ec({
+ entityName: "notes",
+ entityId: "notInFroca",
+ entity: { noteId: "notInFroca", title: "Picture", type: "image" } as any
+ }) ]);
+
+ expect(triggerSpy).not.toHaveBeenCalled();
+ expect(getNoteImageSourceToLoad(src)).not.toBe(before);
+ });
});
diff --git a/apps/client/src/services/froca_updater.ts b/apps/client/src/services/froca_updater.ts
index bf160dd743..5fe279a64d 100644
--- a/apps/client/src/services/froca_updater.ts
+++ b/apps/client/src/services/froca_updater.ts
@@ -77,19 +77,15 @@ async function processEntityChanges(entityChanges: EntityChange[]) {
await froca.reloadNotes(missingNoteIds);
}
+ // Read from the entity changes rather than `loadResults`, which skips the notes froca has not
+ // loaded: a text note can show the image of such a note without loading it.
+ invalidateNoteImages(getImageNoteIds(entityChanges));
+
if (!loadResults.isEmpty()) {
if (loadResults.hasAttributeRelatedChanges()) {
noteAttributeCache.invalidate();
}
- // A note's image is its content or one of its attachments, such as `canvas-export.svg`.
- invalidateNoteImages([
- ...loadResults.getNoteIds(),
- // An erased attachment arrives without its row.
- ...loadResults.getAttachmentRows()
- .flatMap((row) => (row?.ownerId ? [ row.ownerId ] : []))
- ]);
-
await appContext.triggerEvent("entitiesReloaded", { loadResults });
}
}
@@ -331,6 +327,25 @@ function processAttachment(loadResults: LoadResults, ec: EntityChange) {
loadResults.addAttachmentRow(attachmentEntity, ec.componentId);
}
+/**
+ * Returns the notes whose image might have changed: a note's image is its content or one of its
+ * attachments, such as `canvas-export.svg`. An erased attachment arrives without its row.
+ */
+function getImageNoteIds(entityChanges: EntityChange[]) {
+ const noteIds: string[] = [];
+ for (const ec of entityChanges) {
+ if (ec.entityName === "notes") {
+ noteIds.push(ec.entityId);
+ } else if (ec.entityName === "attachments") {
+ const ownerId = (ec.entity as FAttachmentRow | undefined)?.ownerId;
+ if (ownerId) {
+ noteIds.push(ownerId);
+ }
+ }
+ }
+ return noteIds;
+}
+
export default {
processEntityChanges
};
diff --git a/packages/ckeditor5/src/plugins/image_editing_source.spec.ts b/packages/ckeditor5/src/plugins/image_editing_source.spec.ts
index 075c5ddaa0..6d544de1cf 100644
--- a/packages/ckeditor5/src/plugins/image_editing_source.spec.ts
+++ b/packages/ckeditor5/src/plugins/image_editing_source.spec.ts
@@ -54,4 +54,24 @@ describe("ImageEditingSource", () => {
expect(onDataChange).not.toHaveBeenCalled();
expect(editor.getData()).not.toContain("?v=");
});
+
+ it("refreshes images added later and drops the ones removed from the document", async () => {
+ let version = 1;
+ const resolve = vi.fn((src: string) => `${src}?v=${version}`);
+ const editor = await createEditor(resolve);
+ const plugin = editor.plugins.get(ImageEditingSource);
+
+ editor.setData(`<figure class="image"><img src="${INLINE_SRC}"></figure>`);
+ version = 2;
+ resolve.mockClear();
+ plugin.refresh();
+
+ expect(renderedSources(editor)).toEqual([`${INLINE_SRC}?v=2`]);
+ // The images of the replaced content are not resolved again.
+ expect(resolve.mock.calls).toEqual([ [ INLINE_SRC ] ]);
+
+ resolve.mockClear();
+ plugin.refresh();
+ expect(resolve.mock.calls).toEqual([ [ INLINE_SRC ] ]);
+ });
});
diff --git a/packages/ckeditor5/src/plugins/image_editing_source.ts b/packages/ckeditor5/src/plugins/image_editing_source.ts
index eee824551f..1b3a58ff32 100644
--- a/packages/ckeditor5/src/plugins/image_editing_source.ts
+++ b/packages/ckeditor5/src/plugins/image_editing_source.ts
@@ -31,6 +31,9 @@ export default class ImageEditingSource extends Plugin {
return "ImageEditingSource" as const;
}
+ /** The images the editing downcast rendered. `refresh()` drops those that left the document. */
+ private readonly _images = new Set<ModelElement>();
+
init() {
const editor = this.editor;
@@ -46,6 +49,7 @@ export default class ImageEditingSource extends Plugin {
return;
}
+ this._images.add(data.item);
conversionApi.writer.setAttribute("src", this._resolve(src), img);
},
{ priority: "low" }
@@ -55,30 +59,30 @@ export default class ImageEditingSource extends Plugin {
}
/**
- * Points every image of the editing view at the URL `resolve` returns for it now. It changes
- * the view only, so the document does not fire `change:data` and the note is not saved again.
+ * Points every image of the editing view at the URL `resolve` returns for it now. It visits the
+ * images the converter rendered rather than the whole document, and changes the view only, so
+ * the document does not fire `change:data` and the note is not saved again.
*/
refresh() {
const editor = this.editor;
- const imageUtils = editor.plugins.get(ImageUtils);
+ const graveyard = editor.model.document.graveyard;
const updates: { img: ViewElement; url: string }[] = [];
- for (const root of editor.model.document.getRoots()) {
- for (const item of editor.model.createRangeIn(root).getItems()) {
- if (!item.is("element") || !imageUtils.isImage(item)) {
- continue;
- }
-
- const src = item.getAttribute("src");
- const img = this._findViewImg(item);
- if (typeof src !== "string" || !img) {
- continue;
- }
-
- const url = this._resolve(src);
- if (img.getAttribute("src") !== url) {
- updates.push({ img, url });
- }
+ for (const image of this._images) {
+ if (!image.isAttached() || image.root === graveyard) {
+ this._images.delete(image);
+ continue;
+ }
+
+ const src = image.getAttribute("src");
+ const img = this._findViewImg(image);
+ if (typeof src !== "string" || !img) {
+ continue;
+ }
+
+ const url = this._resolve(src);
+ if (img.getAttribute("src") !== url) {
+ updates.push({ img, url });
}
}