Skip to content

Build your personal knowledge base with Trilium Notes

refactor(editor): let the embed switch be read afresh on every copy

Elian Doranacf3db4c0be5
diff --git a/apps/client/src/widgets/type_widgets/text/config.ts b/apps/client/src/widgets/type_widgets/text/config.ts
index 74088dcebe..b9e620385e 100644
--- a/apps/client/src/widgets/type_widgets/text/config.ts
+++ b/apps/client/src/widgets/type_widgets/text/config.ts
@@ -229,8 +229,11 @@ export async function buildConfig(opts: BuildEditorOptions): Promise<EditorConfi
// 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.
+ // `enabled` is a getter for the same reason as `autoLinkPreviewsEnabled` above, and because the
+ // application-level handler covering read-only surfaces reads the option per copy — a baked-in
+ // boolean would leave an open editor still embedding after the switch was flipped.
config.clipboardImageEmbed = {
- enabled: options.get("clipboardImageEmbedEnabled") === "true",
+ enabled: () => options.get("clipboardImageEmbedEnabled") === "true",
embedImage: (src: string) => imageService.embedReferenceImageAsDataUrl(src)
};
diff --git a/packages/ckeditor5/src/plugins/clipboard_image_embed.spec.ts b/packages/ckeditor5/src/plugins/clipboard_image_embed.spec.ts
index 2c2ac67941..f8c93fd8e2 100644
--- a/packages/ckeditor5/src/plugins/clipboard_image_embed.spec.ts
+++ b/packages/ckeditor5/src/plugins/clipboard_image_embed.spec.ts
@@ -155,7 +155,7 @@ describe("ClipboardImageEmbed", () => {
it("embeds internal images in the serialized clipboard HTML", async () => {
const html = await copy(`<figure class="image"><img src="${REFERENCE}"></figure>`, {
- enabled: true,
+ enabled: () => true,
embedImage: () => DATA_URI
});
@@ -165,7 +165,7 @@ describe("ClipboardImageEmbed", () => {
it("does not embed when the kill-switch option is disabled", async () => {
const html = await copy(`<figure class="image"><img src="${REFERENCE}"></figure>`, {
- enabled: false,
+ enabled: () => false,
embedImage: () => DATA_URI
});
@@ -173,8 +173,29 @@ describe("ClipboardImageEmbed", () => {
expect(html).not.toContain(TRILIUM_SRC_ATTRIBUTE);
});
+ it("consults the switch on every copy, so flipping it reaches an already-open editor", async () => {
+ let enabled = true;
+ const editor = await createTestEditor(
+ [Essentials, Paragraph, Image, ImageBlock, ImageInline, ClipboardImageEmbed],
+ { clipboardImageEmbed: { enabled: () => enabled, embedImage: () => DATA_URI } }
+ );
+
+ function copyOnce() {
+ const dataTransfer = recordingDataTransfer();
+ const content = editor.data.processor.toView(`<figure class="image"><img src="${REFERENCE}"></figure>`) as ViewDocumentFragment;
+ editor.editing.view.document.fire("clipboardOutput", { dataTransfer, content, method: "copy" });
+ return dataTransfer.getData("text/html");
+ }
+
+ expect(copyOnce()).toContain(`src="${DATA_URI}"`);
+
+ // A boolean read once at construction would keep embedding here.
+ enabled = false;
+ expect(copyOnce()).toContain(`src="${REFERENCE}"`);
+ });
+
it("does not embed when no resolver is configured", async () => {
- const html = await copy(`<figure class="image"><img src="${REFERENCE}"></figure>`, { enabled: true });
+ const html = await copy(`<figure class="image"><img src="${REFERENCE}"></figure>`, { enabled: () => true });
expect(html).toContain(`src="${REFERENCE}"`);
expect(html).not.toContain(TRILIUM_SRC_ATTRIBUTE);
diff --git a/packages/ckeditor5/src/plugins/clipboard_image_embed.ts b/packages/ckeditor5/src/plugins/clipboard_image_embed.ts
index f0cd8442d7..85b69835bb 100644
--- a/packages/ckeditor5/src/plugins/clipboard_image_embed.ts
+++ b/packages/ckeditor5/src/plugins/clipboard_image_embed.ts
@@ -9,9 +9,12 @@ import type { ClipboardInputTransformationData, ClipboardInputTransformationEven
* `null` to leave the image untouched (non-internal images, images not yet loaded, or ones over a
* size cap). The DOM/canvas work lives in the client so this plugin stays platform-agnostic and
* unit-testable with a stub resolver.
+ *
+ * `enabled` is consulted per copy rather than read once, so flipping the option applies to editors
+ * that are already open — matching the application-level handler that covers read-only surfaces.
*/
export interface ClipboardImageEmbedConfig {
- enabled?: boolean;
+ enabled?: () => boolean;
embedImage?: (src: string) => string | null;
}
@@ -62,7 +65,7 @@ export default class ClipboardImageEmbed extends Plugin {
private _embedImages(data: ViewDocumentClipboardOutputEventData) {
const config = this.editor.config.get("clipboardImageEmbed") as ClipboardImageEmbedConfig | undefined;
- if (!config?.enabled || !config.embedImage) {
+ if (!config?.enabled?.() || !config.embedImage) {
return;
}