Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(images): refresh image references when the referenced note changes (closes #12056)
Elian Dorane4f14e8f9e2c
diff --git a/apps/client/src/services/content_renderer_text.ts b/apps/client/src/services/content_renderer_text.tsindex 99e400a5ca..c8df92a5d5 100644--- a/apps/client/src/services/content_renderer_text.ts+++ b/apps/client/src/services/content_renderer_text.ts@@ -9,7 +9,7 @@ import { highlightBlockReference } from "./block_reference.js";import { default as content_renderer, type RenderOptions } from "./content_renderer.js";import froca from "./froca.js";import { t } from "./i18n.js";-import { resolveContentImageUrls } from "./image_urls.js";+import { applyNoteImageVersions, resolveContentImageUrls } from "./image_urls.js";import link from "./link.js";import { applyLinkEmbeds } from "./link_embed.js";import { getMermaidConfig, postprocessMermaidSvg } from "./mermaid.js";@@ -38,6 +38,7 @@ export async function renderTextContent(const hasContent = content !== undefined && !isHtmlEmpty(content);const $content = $('<div class="ck-content">').html(hasContent ? resolveContentImageUrls(sanitizeNoteContentHtml(content)) : "");+ applyNoteImageVersions($content[0]);if (options.block !== undefined && !sliceToBlockReference($content[0], options.block)) {$renderedContent.append($("<p>")diff --git a/apps/client/src/services/froca_updater.spec.ts b/apps/client/src/services/froca_updater.spec.tsindex 95ad7f4d47..d0f4f4217c 100644--- a/apps/client/src/services/froca_updater.spec.ts+++ b/apps/client/src/services/froca_updater.spec.ts@@ -7,6 +7,7 @@ import FBranch from "../entities/fbranch.js";import type { EntityChange } from "../server_types.js";import froca from "./froca.js";import frocaUpdater from "./froca_updater.js";+import { getNoteImageSourceToLoad } from "./image_urls.js";import type LoadResults from "./load_results.js";import noteAttributeCache from "./note_attribute_cache.js";import options from "./options.js";@@ -799,3 +800,34 @@ describe("froca_updater - missing-note ancestor reload", () => {expect(reloadNotesSpy).toHaveBeenCalledWith(["totally-absent-parent"]);});});++describe("froca_updater - note images", () => {+ it("gives a note's image a new URL once the note or an attachment changes", async () => {+ const canvas = buildNote({ title: "Canvas", type: "canvas" });+ const src = `api/images/${canvas.noteId}/Canvas`;+ const before = getNoteImageSourceToLoad(src);++ await process([ ec({+ entityName: "attachments",+ entityId: "canvas-export",+ entity: {+ attachmentId: "canvas-export",+ ownerId: canvas.noteId,+ role: "image",+ mime: "image/svg+xml",+ title: "canvas-export.svg"+ } as any+ }) ]);+ const afterAttachment = getNoteImageSourceToLoad(src);+ expect(afterAttachment).not.toBe(before);++ await process([ ec({+ entityName: "notes",+ entityId: canvas.noteId,+ entity: {+ noteId: canvas.noteId, title: "Canvas", type: "canvas", blobId: canvas.blobId+ } as any+ }) ]);+ expect(getNoteImageSourceToLoad(src)).not.toBe(afterAttachment);+ });+});diff --git a/apps/client/src/services/froca_updater.ts b/apps/client/src/services/froca_updater.tsindex 243795bc65..bf160dd743 100644--- a/apps/client/src/services/froca_updater.ts+++ b/apps/client/src/services/froca_updater.ts@@ -7,6 +7,7 @@ import FBranch, { type FBranchRow } from "../entities/fbranch.js";import type { default as FNote, FNoteRow } from "../entities/fnote.js";import type { EntityChange } from "../server_types.js";import froca from "./froca.js";+import { invalidateNoteImages } from "./image_urls.js";import LoadResults from "./load_results.js";import noteAttributeCache from "./note_attribute_cache.js";import options from "./options.js";@@ -81,6 +82,14 @@ async function processEntityChanges(entityChanges: EntityChange[]) {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 });}}diff --git a/apps/client/src/services/image.ts b/apps/client/src/services/image.tsindex 3cefe48593..7a2cb2e772 100644--- a/apps/client/src/services/image.ts+++ b/apps/client/src/services/image.ts@@ -1,6 +1,7 @@import { imageExtensionForMime } from "@triliumnext/commons";import { t } from "./i18n.js";+import { stripNoteImageVersion } from "./image_urls.js";import open, { getUrlForDownload } from "./open.js";import toastService, { showError } from "./toast.js";import utils from "./utils.js";@@ -254,7 +255,10 @@ export function embedReferenceImageAsDataUrl(src: string): string | null {/** Find a fully-loaded `<img>` currently in the document whose raw `src` attribute matches. */function findLoadedImage(src: string): HTMLImageElement | null {for (const image of document.querySelectorAll("img")) {- if (image.getAttribute("src") === src && image.complete && image.naturalWidth > 0) {+ const imageSrc = image.getAttribute("src");+ const isSameImage = !!imageSrc+ && stripNoteImageVersion(imageSrc) === stripNoteImageVersion(src);+ if (isSameImage && image.complete && image.naturalWidth > 0) {return image;}}diff --git a/apps/client/src/services/image_urls.spec.ts b/apps/client/src/services/image_urls.spec.tsindex 115856bae9..e993a2f635 100644--- a/apps/client/src/services/image_urls.spec.ts+++ b/apps/client/src/services/image_urls.spec.ts@@ -1,7 +1,8 @@import { afterEach, describe, expect, it } from "vitest";import {- getAttachmentImageUrl, getNoteImageUrl, resolveContentImageUrls, setImageUrlResolver+ applyNoteImageVersions, getAttachmentImageUrl, getNoteImageSourceToLoad, getNoteImageUrl,+ invalidateNoteImages, resolveContentImageUrls, setImageUrlResolver, stripNoteImageVersion} from "./image_urls.js";describe("image URLs", () => {@@ -35,4 +36,36 @@ describe("image URLs", () => {+ `<section data-image="files/att1" data-favicon="https://a/b.ico">`+ `<img src="files/att2"><a href="api/images/note1/A">`);});++ it("adds a new version to the URL of a note image each time the note changes", () => {+ expect(getNoteImageSourceToLoad("api/images/fresh1/A")).toBe("api/images/fresh1/A");++ invalidateNoteImages([ "canvas1" ]);+ const first = getNoteImageSourceToLoad("api/images/canvas1/A%20b");+ expect(first).toMatch(/^api\/images\/canvas1\/A%20b\?v=\d+$/);+ expect(getNoteImageSourceToLoad("api/images/other1/A")).toBe("api/images/other1/A");+ expect(getNoteImageSourceToLoad("api/attachments/canvas1/image/x.png"))+ .toBe("api/attachments/canvas1/image/x.png");++ invalidateNoteImages([ "canvas1" ]);+ const second = getNoteImageSourceToLoad(first);+ expect(second).not.toBe(first);+ expect(second).toMatch(/^api\/images\/canvas1\/A%20b\?v=\d+$/);+ expect(stripNoteImageVersion(second)).toBe("api/images/canvas1/A%20b");+ expect(getNoteImageSourceToLoad("../api/images/canvas1/A?x=1")).toMatch(/\?x=1&v=\d+$/);+ });++ it("points the note images of rendered content at their current version", () => {+ invalidateNoteImages([ "rendered1" ]);+ const container = document.createElement("div");+ container.innerHTML = `<img src="api/images/rendered1/A"><img src="api/images/still1/B">`+ + `<img src="https://example.com/c.png">`;++ applyNoteImageVersions(container);++ const sources = [ ...container.querySelectorAll("img") ]+ .map((img) => img.getAttribute("src"));+ expect(sources[0]).toMatch(/^api\/images\/rendered1\/A\?v=\d+$/);+ expect(sources.slice(1)).toEqual([ "api/images/still1/B", "https://example.com/c.png" ]);+ });});diff --git a/apps/client/src/services/image_urls.ts b/apps/client/src/services/image_urls.tsindex 8d051368e0..5e3cb41439 100644--- a/apps/client/src/services/image_urls.ts+++ b/apps/client/src/services/image_urls.ts@@ -50,6 +50,60 @@ export function resolveContentImageUrls(html: string) {});}+/**+ * Marks the images of the notes `noteIds` as changed, so that {@link getNoteImageSourceToLoad}+ * returns a URL the browser has not loaded yet. Within one page, the browser reuses an image it+ * loaded before for the same URL and ignores `Cache-Control: no-store`.+ */+export function invalidateNoteImages(noteIds: Iterable<string>) {+ for (const noteId of noteIds) {+ lastNoteImageVersion++;+ noteImageVersions.set(noteId, lastNoteImageVersion);+ }+}++/**+ * Returns the URL to load the image `src` from: for the image of a note that changed since the page+ * loaded, `src` with the current version in a `v` parameter, otherwise `src` itself.+ */+export function getNoteImageSourceToLoad(src: string) {+ const storedSrc = stripNoteImageVersion(src);+ const noteId = storedSrc.match(NOTE_IMAGE_URL)?.[1];+ const version = noteId ? noteImageVersions.get(noteId) : undefined;+ if (version === undefined) {+ return storedSrc;+ }++ return `${storedSrc}${storedSrc.includes("?") ? "&" : "?"}v=${version}`;+}++/** Returns `src` without the version that {@link getNoteImageSourceToLoad} adds. */+export function stripNoteImageVersion(src: string) {+ return NOTE_IMAGE_URL.test(src) ? src.replace(NOTE_IMAGE_VERSION, "") : src;+}++/** Points each note image under `container` at the URL {@link getNoteImageSourceToLoad} returns. */+export function applyNoteImageVersions(container: ParentNode) {+ for (const img of container.querySelectorAll("img")) {+ const src = img.getAttribute("src");+ if (!src) {+ continue;+ }++ const url = getNoteImageSourceToLoad(src);+ if (url !== src) {+ img.setAttribute("src", url);+ }+ }+}++const noteImageVersions = new Map<string, number>();+let lastNoteImageVersion = 0;++/** The image of a note on the API, capturing the `noteId`. */+const NOTE_IMAGE_URL = /(?:^|\/)api\/images\/(\w+)\//;+const NOTE_IMAGE_VERSION = /[?&]v=\d+$/;+/** An image of the API in an attribute of a note's HTML, naming a note or an attachment. */const CONTENT_IMAGE_URL =/(src|data-image|data-favicon)="[^"]*?api\/(?:images\/(\w+)\/|attachments\/(\w+)\/image\/)[^"]*"/g;diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsxindex 34c72fd658..0c138f628d 100644--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx@@ -400,6 +400,9 @@ export default function EditableText({pendingAttachmentChangesRef);}+ if (editor?.plugins.has("ImageEditingSource")) {+ editor.plugins.get("ImageEditingSource").refresh();+ }});useTriliumEvent("executeWithTextEditor", async ({ callback, resolve, ntxId: eventNtxId }) => {diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsxindex 98e5c0a8c6..8663606af2 100644--- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx@@ -15,6 +15,7 @@ import { consumeBlockReference } from "../../../services/block_reference";import { consumeBookmark } from "../../../services/bookmark_jump";import { applyInlineMermaid, rewriteMermaidDiagramsInContainer } from "../../../services/content_renderer_text";import { t } from "../../../services/i18n";+import { applyNoteImageVersions } from "../../../services/image_urls";import { applyLinkEmbeds } from "../../../services/link_embed";import { renderMathInElement } from "../../../services/math";import { trackPendingRender } from "../../../services/pending_renders";@@ -145,6 +146,7 @@ export function ReadOnlyTextContent({formatCodeBlocks($(container))]));+ applyNoteImageVersions(container);applyMath(container);applyTabs(container, { placeholder: t("text-editor.ck.tab-title") });setupImageOpening(container, true);@@ -155,6 +157,10 @@ export function ReadOnlyTextContent({return watchContentEmbeds(contentRef.current);}, [ contentRef ]);+ useTriliumEvent("entitiesReloaded", () => {+ if (contentRef.current) applyNoteImageVersions(contentRef.current);+ });+// React to embedded note changes.useTriliumEvent("refreshEmbeddedNote", ({ noteId }) => {if (!contentRef.current) return;diff --git a/apps/client/src/widgets/type_widgets/text/config.ts b/apps/client/src/widgets/type_widgets/text/config.tsindex 8071c575da..d31b3918f7 100644--- a/apps/client/src/widgets/type_widgets/text/config.ts+++ b/apps/client/src/widgets/type_widgets/text/config.ts@@ -6,6 +6,7 @@ import i18next from "i18next";import { copyHtmlWithToast, copyTextWithToast } from "../../../services/clipboard_ext.js";import { t } from "../../../services/i18n.js";import imageService from "../../../services/image.js";+import { getNoteImageSourceToLoad } from "../../../services/image_urls.js";import { getMermaidConfig } from "../../../services/mermaid.js";import { default as mimeTypesService, getHighlightJsNameForMime } from "../../../services/mime_types.js";import options from "../../../services/options.js";@@ -269,6 +270,10 @@ export async function buildConfig(opts: BuildEditorOptions): Promise<EditorConficonst imageToolbar = (config.image as { toolbar: (string | object)[] }).toolbar;imageToolbar.push("|", ...(imageService.isImageCopySupported() ? ["copyImageToClipboard"] : []), "downloadImage");+ // Loads a changed note image from a new URL, so the browser does not show the copy it loaded+ // before.+ config.imageEditingSource = { resolve: getNoteImageSourceToLoad };+// Embed internal images as data: URIs when content is copied out to external apps, while// keeping internal Trilium paste reference-based (see the ClipboardImageEmbed plugin). The// resolver does the synchronous canvas encoding; the hidden option is a kill-switch.diff --git a/packages/ckeditor5/src/index.ts b/packages/ckeditor5/src/index.tsindex 4beeccbf03..66aabfefeb 100644--- a/packages/ckeditor5/src/index.ts+++ b/packages/ckeditor5/src/index.ts@@ -7,6 +7,7 @@ import "./theme/content_colors.css";import "./theme/code_block_toolbar.css";import "./theme/link_embed_form.css";import type { ClipboardImageEmbedConfig } from "./plugins/clipboard_image_embed.js";+import type { ImageEditingSourceConfig } from "./plugins/image_editing_source.js";import type { PasteTarget } from "./plugins/cuttonote.js";import { CLASSIC_EDITOR_PLUGINS, CORE_PLUGINS, POPUP_EDITOR_PLUGINS } from "./plugins.js";import { BalloonEditor, DecoupledEditor, FindAndReplaceEditing, FindCommand } from "ckeditor5";@@ -14,6 +15,7 @@ export { onTextEditorError } from "./editor_errors.js";export { CHAT_INPUT_PLUGINS, MEMO_PLUGINS } from "./plugins.js";export type { CKEditorError, EditorConfig, MentionFeed, MentionFeedObjectItem, ModelNode, ModelPosition, ModelElement, ModelText, TextTransformationConfig, TextTypingTransformationDescription } from "ckeditor5";export type { ClipboardImageEmbedConfig } from "./plugins/clipboard_image_embed.js";+export type { ImageEditingSourceConfig } from "./plugins/image_editing_source.js";export type { FileUploadData, FileUploadEvent } from "./plugins/file_upload/fileuploadediting.js";export type { PasteTarget } from "./plugins/cuttonote.js";export type { ContentEmbedState } from "./plugins/content_embed/content_embed.js";@@ -120,6 +122,7 @@ declare module "ckeditor5" {copyHtml?(html: string, plainText: string): void;},clipboardImageEmbed?: ClipboardImageEmbedConfig,+ imageEditingSource?: ImageEditingSourceConfig,autoSort?: {/** More date formats for table sorting to recognize, in Day.js syntax. */dateFormats?: string[];diff --git a/packages/ckeditor5/src/plugins.ts b/packages/ckeditor5/src/plugins.tsindex dfcf17a1b7..f82017f079 100644--- a/packages/ckeditor5/src/plugins.ts+++ b/packages/ckeditor5/src/plugins.ts@@ -42,6 +42,7 @@ import ImageActions from "./plugins/image_actions.js";import ImageToDrawing from "./plugins/image_to_drawing.js";import ClipboardBareImage from "./plugins/clipboard_bare_image.js";import ClipboardImageEmbed from "./plugins/clipboard_image_embed.js";+import ImageEditingSource from "./plugins/image_editing_source.js";import ClipboardPageStyles from "./plugins/clipboard_page_styles.js";import ClipboardPlainTextIndent from "./plugins/clipboard_plain_text_indent.js";import PasteFromVsCode from "./plugins/paste_from_vscode.js";@@ -122,6 +123,7 @@ const TRILIUM_PLUGINS: typeof Plugin[] = [ImageActions,ImageToDrawing,ClipboardImageEmbed,+ ImageEditingSource,ClipboardBareImage,ClipboardPageStyles,ClipboardPlainTextIndent,diff --git a/packages/ckeditor5/src/plugins/image_editing_source.spec.ts b/packages/ckeditor5/src/plugins/image_editing_source.spec.tsnew file mode 100644index 0000000000..075c5ddaa0--- /dev/null+++ b/packages/ckeditor5/src/plugins/image_editing_source.spec.ts@@ -0,0 +1,57 @@+import { Essentials, Image, ImageBlock, ImageInline, Paragraph } from "ckeditor5";+import { describe, expect, it, vi } from "vitest";++import { createTestEditor } from "../../test/editor-kit.js";+import ImageEditingSource from "./image_editing_source.js";++const BLOCK_SRC = "api/images/blockNote/Block";+const INLINE_SRC = "api/images/inlineNote/Inline";+const CONTENT = `<figure class="image"><img src="${BLOCK_SRC}"></figure>`+ + `<p>x<img src="${INLINE_SRC}">y</p>`;++describe("ImageEditingSource", () => {+ async function createEditor(resolve?: (src: string) => string) {+ const plugins = [+ Essentials, Paragraph, Image, ImageBlock, ImageInline, ImageEditingSource+ ];+ const editor = await createTestEditor(plugins, { imageEditingSource: { resolve } });+ editor.setData(CONTENT);+ return editor;+ }++ function renderedSources(editor: Awaited<ReturnType<typeof createEditor>>) {+ const imgs = editor.editing.view.getDomRoot()?.querySelectorAll("img") ?? [];+ return [...imgs].map((img) => img.getAttribute("src"));+ }++ it("loads block and inline images from the resolved URL but saves the stored src", async () => {+ const editor = await createEditor((src) => `${src}?v=1`);++ expect(renderedSources(editor)).toEqual([`${BLOCK_SRC}?v=1`, `${INLINE_SRC}?v=1`]);+ expect(editor.getData()).toContain(`src="${BLOCK_SRC}"`);+ expect(editor.getData()).toContain(`src="${INLINE_SRC}"`);+ expect(editor.getData()).not.toContain("?v=1");+ });++ it("keeps the stored src without a resolver", async () => {+ const editor = await createEditor();++ expect(renderedSources(editor)).toEqual([BLOCK_SRC, INLINE_SRC]);+ });++ it("re-resolves the images on refresh without changing the data", async () => {+ let version = 1;+ const editor = await createEditor((src) => (+ src === BLOCK_SRC ? `${src}?v=${version}` : src+ ));+ const onDataChange = vi.fn();+ editor.model.document.on("change:data", onDataChange);++ version = 2;+ editor.plugins.get(ImageEditingSource).refresh();++ expect(renderedSources(editor)).toEqual([`${BLOCK_SRC}?v=2`, INLINE_SRC]);+ expect(onDataChange).not.toHaveBeenCalled();+ expect(editor.getData()).not.toContain("?v=");+ });+});diff --git a/packages/ckeditor5/src/plugins/image_editing_source.ts b/packages/ckeditor5/src/plugins/image_editing_source.tsnew file mode 100644index 0000000000..eee824551f--- /dev/null+++ b/packages/ckeditor5/src/plugins/image_editing_source.ts@@ -0,0 +1,118 @@+import {+ type DowncastAttributeEvent,+ ImageUtils,+ type ModelElement,+ Plugin,+ type ViewElement+} from "ckeditor5";++/**+ * Configuration injected by the host application (see the editor's `imageEditingSource` config).+ *+ * `resolve` is given the `src` an image stores and returns the URL the editing view loads it from.+ */+export interface ImageEditingSourceConfig {+ resolve?: (src: string) => string;+}++/**+ * Loads each image of the editing view from the URL that the `imageEditingSource.resolve` config+ * returns, while the model and `editor.getData()` keep the stored `src`. Trilium adds the version+ * of a note's image to its URL, so the browser fetches the image again once the note changes+ * instead of reusing the copy it already holds for that URL.+ */+export default class ImageEditingSource extends Plugin {++ static get requires() {+ return [ImageUtils] as const;+ }++ static get pluginName() {+ return "ImageEditingSource" as const;+ }++ init() {+ const editor = this.editor;++ editor.conversion.for("editingDowncast").add((dispatcher) => {+ for (const imageType of ["imageBlock", "imageInline"]) {+ // Runs after the converter of `ImageEditing`, which writes the stored `src`.+ dispatcher.on<DowncastAttributeEvent<ModelElement>>(+ `attribute:src:${imageType}`,+ (_evt, data, conversionApi) => {+ const src = data.attributeNewValue;+ const img = this._findViewImg(data.item);+ if (typeof src !== "string" || !img) {+ return;+ }++ conversionApi.writer.setAttribute("src", this._resolve(src), img);+ },+ { priority: "low" }+ );+ }+ });+ }++ /**+ * 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.+ */+ refresh() {+ const editor = this.editor;+ const imageUtils = editor.plugins.get(ImageUtils);+ 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 });+ }+ }+ }++ if (!updates.length) {+ return;+ }++ editor.editing.view.change((writer) => {+ for (const { img, url } of updates) {+ writer.setAttribute("src", url, img);+ }+ });+ }++ private _resolve(src: string) {+ const config = this.editor.config.get("imageEditingSource") as+ ImageEditingSourceConfig | undefined;+ return config?.resolve?.(src) ?? src;+ }++ /** The `<img>` of an image block's `<figure>` or of an inline image's wrapper. */+ private _findViewImg(image: ModelElement) {+ const viewElement = this.editor.editing.mapper.toViewElement(image);+ if (!viewElement) {+ return undefined;+ }++ return this.editor.plugins.get(ImageUtils).findViewImgElement(viewElement);+ }++}++declare module "ckeditor5" {+ interface PluginsMap {+ [ImageEditingSource.pluginName]: ImageEditingSource;+ }+}