Skip to content

Build your personal knowledge base with Trilium Notes

chore(text): convert images to drawings through editor config

Elian Dorane6506e501471
diff --git a/.claude/skills/ckeditor5-testing/references/patterns.md b/.claude/skills/ckeditor5-testing/references/patterns.md
index fe77778d3f..f784da8c00 100644
--- a/.claude/skills/ckeditor5-testing/references/patterns.md
+++ b/.claude/skills/ckeditor5-testing/references/patterns.md
@@ -43,10 +43,11 @@ Host services that need no component come through the editor config, so a spec p
button hands over to the host: `internalLink: { openDialog }`, `markdownImport: { open }`,
`contentEmbed` (`pickNote`, the `load…` renderers, `getTools`, `enter` and the rest of
`ContentEmbedHost`), `cutToNote: { cut, saveNow }`, `inlineIcon: { showPicker }`,
-`blockReference: { openHandleMenu, copyReference }` and `tabs: { copyReference }`. Each falls
-back to a placeholder or a no-op when left out (an upload without `getNoteId` is rejected), so
-a spec that does not assert on it can omit it. A spec sharing one editor across tests can swap
-an entry with `editor.config.set()`, which the plugins read at call time.
+`blockReference: { openHandleMenu, copyReference }`, `tabs: { copyReference }` and
+`imageToDrawing: { convert }`. Each falls back to a placeholder or a no-op when left out (an
+upload without `getNoteId` is rejected), so a spec that does not assert on it can omit it. A
+spec sharing one editor across tests can swap an entry with `editor.config.set()`, which the
+plugins read at call time.
Many in-aggregate plugins still reach for a global `glob` (the Trilium bridge — typed in
`packages/ckeditor5/src/augmentation.ts` with `getComponentByEl`, which finds the host component), some
diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
index 6dec31cc32..13c24650f7 100644
--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
@@ -30,7 +30,6 @@ import {
useSearchTermsConsumer, useTriliumEvent, useTriliumOption, useTriliumOptionBool
} from "../../react/hooks";
import { setEditorNoteId } from "../../react/NoteStore";
-import { convertImageToDrawing } from "../canvas/image_to_drawing";
import { TypeWidgetProps } from "../type_widget";
import AttachmentSaves, { InlineSourceSaves, useNoteEditor } from "./attachment_saves";
import CKEditor, { CKEditorApi, NotificationEventData, NotificationEventInfo } from "./CKEditor";
@@ -278,9 +277,6 @@ export default function EditableText({
sizeEditable: true
});
},
- convertImageToDrawing(src: string) {
- return convertImageToDrawing(note.noteId, src, parentComponent?.componentId);
- },
// Keyboard shortcut
async followLinkUnderCursorCommand() {
const editor = await waitForEditor();
diff --git a/apps/client/src/widgets/type_widgets/text/config.spec.ts b/apps/client/src/widgets/type_widgets/text/config.spec.ts
index f80371430d..cc24360b49 100644
--- a/apps/client/src/widgets/type_widgets/text/config.spec.ts
+++ b/apps/client/src/widgets/type_widgets/text/config.spec.ts
@@ -95,6 +95,10 @@ vi.mock("../../../services/dialog.js", () => ({
vi.mock("@triliumnext/highlightjs", () => ({ default: { name: "hljs-stub" } }));
vi.mock("../../../services/math.js", () => ({ default: { name: "katex-stub" } }));
vi.mock("../../../services/content_renderer.js", () => ({ getUploadBoxSize: vi.fn(() => "medium") }));
+const { convertImageToDrawing } = vi.hoisted(() => ({
+ convertImageToDrawing: vi.fn(async () => "d1")
+}));
+vi.mock("../canvas/image_to_drawing.js", () => ({ convertImageToDrawing }));
vi.mock("../../../services/server.js", async (importOriginal) => {
const original = await importOriginal<typeof import("../../../services/server.js")>();
return {
@@ -433,6 +437,20 @@ describe("CK config - tables", () => {
expect(bare.tabs).toBeUndefined();
});
+ it("converts images to drawings in the note open at the time, as changes of the host", async () => {
+ let noteId: string | undefined = "n1";
+ const config = await buildConfig(baseOpts({
+ getNoteId: () => noteId,
+ getComponentId: () => "component1"
+ }));
+
+ expect(await config.imageToDrawing?.convert?.("api/images/a.png")).toBe("d1");
+ noteId = undefined;
+ expect(await config.imageToDrawing?.convert?.("api/images/b.png")).toBeNull();
+
+ expect(convertImageToDrawing).toHaveBeenCalledExactlyOnceWith("n1", "api/images/a.png", "component1");
+ });
+
it("hands embeds to the content embed handlers of the moment, and falls back without any", async () => {
const tools = { getTools: () => [], execute: vi.fn(), subscribe: () => () => {} };
const first = {
diff --git a/apps/client/src/widgets/type_widgets/text/config.ts b/apps/client/src/widgets/type_widgets/text/config.ts
index 9d46b81ddd..e1674bf638 100644
--- a/apps/client/src/widgets/type_widgets/text/config.ts
+++ b/apps/client/src/widgets/type_widgets/text/config.ts
@@ -22,6 +22,7 @@ import { ensureMimeTypesForHighlighting, isSyntaxHighlightEnabled } from "../../
import { getTaskStateDefinitions, openCustomTaskStateConfig } from "../../../services/task_states.js";
import { formatDateTime, isMac, isMobile } from "../../../services/utils.js";
import { resolveContentLanguage } from "../../../utils/formatters.js";
+import { convertImageToDrawing } from "../canvas/image_to_drawing.js";
import SAMPLE_DIAGRAMS from "../mermaid/sample_diagrams.js";
import buildAiAssistantStream, { type AiNoteLocationProvider, buildAiAssistantQuickActions } from "./ai_assistant_stream.js";
import diffAiResponse from "./ai_diff.js";
@@ -236,6 +237,12 @@ export async function buildConfig(opts: BuildEditorOptions): Promise<EditorConfi
markdownImport: { open: bindHostCommand(opts, "pasteMarkdownIntoText") },
inlineIcon: { showPicker: (request) => opts.showIconPicker?.(request) ?? null },
...(opts.referenceSource && buildReferenceConfig(opts.referenceSource)),
+ imageToDrawing: {
+ async convert(src) {
+ const noteId = opts.getNoteId?.();
+ return noteId ? convertImageToDrawing(noteId, src, opts.getComponentId?.()) : null;
+ }
+ },
cutToNote: {
cut: bindHostCommand(opts, "cutIntoNote"),
saveNow: bindHostCommand(opts, "saveNoteDetailNow")
diff --git a/packages/ckeditor5/src/augmentation.ts b/packages/ckeditor5/src/augmentation.ts
index 20cde65b20..074caf496e 100644
--- a/packages/ckeditor5/src/augmentation.ts
+++ b/packages/ckeditor5/src/augmentation.ts
@@ -89,11 +89,6 @@ declare global {
* starting from `storedTitle`, and calls `fix()` with the new href.
*/
fixReferenceLink?(storedTitle: string, fix: (href: string) => void): void;
- /**
- * Attaches a drawing canvas that shows the image at `src`, and answers with the ID of its
- * attachment, or `null` when that fails. Hosts without drawings leave it out.
- */
- convertImageToDrawing?(src: string): Promise<string | null>;
}
var glob: {
diff --git a/packages/ckeditor5/src/index.ts b/packages/ckeditor5/src/index.ts
index fd43af7215..8ca1d40f4e 100644
--- a/packages/ckeditor5/src/index.ts
+++ b/packages/ckeditor5/src/index.ts
@@ -57,6 +57,7 @@ import "./plugins/content_embed/content_embed.js";
import "./plugins/content_embed/content_embed_caret.js";
import "./plugins/find_in_link_widgets.js";
import "./plugins/find_reveal.js";
+import "./plugins/image_to_drawing.js";
import "./plugins/insert_date_time.js";
import "./plugins/markdownimport.js";
import "./plugins/math/math.js";
diff --git a/packages/ckeditor5/src/plugins/image_to_drawing.spec.ts b/packages/ckeditor5/src/plugins/image_to_drawing.spec.ts
index a8cba81ec2..c958a78731 100644
--- a/packages/ckeditor5/src/plugins/image_to_drawing.spec.ts
+++ b/packages/ckeditor5/src/plugins/image_to_drawing.spec.ts
@@ -13,7 +13,6 @@ import {
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createTestEditor } from "../../test/editor-kit.js";
-import { installGlobMock } from "../../test/globals-test-kit.js";
import ContentEmbed from "./content_embed/content_embed.js";
import ImageToDrawing from "./image_to_drawing.js";
@@ -34,15 +33,11 @@ describe("ImageToDrawing", () => {
calls.push(`convert ${src}`);
return "d1";
});
- installGlobMock({
- getComponentByEl: () => ({
- convertImageToDrawing
- })
- });
editor = await createTestEditor([
Essentials, Paragraph, Image, ImageBlock, ImageInline, ImageCaption, ContentEmbed,
ImageToDrawing
], {
+ imageToDrawing: { convert: convertImageToDrawing },
contentEmbed: {
focusAttachment: (id: string) => calls.push(`focus ${id}`),
loadAttachment: (id: string) => calls.push(`load ${id}`)
@@ -132,6 +127,12 @@ describe("ImageToDrawing", () => {
expect(getModelData(editor.model, { withoutSelection: true }))
.toBe(`<paragraph>a</paragraph>${IMAGE}`);
+ editor.config.set("imageToDrawing", { convert: undefined });
+ await editor.execute("convertImageToDrawing");
+ expect(getModelData(editor.model, { withoutSelection: true }))
+ .toBe(`<paragraph>a</paragraph>${IMAGE}`);
+ editor.config.set("imageToDrawing", { convert: convertImageToDrawing });
+
const finish = holdConversion();
const conversion = editor.execute("convertImageToDrawing");
editor.model.change((writer) => {
diff --git a/packages/ckeditor5/src/plugins/image_to_drawing.ts b/packages/ckeditor5/src/plugins/image_to_drawing.ts
index 8bfe2dd62d..339be5a912 100644
--- a/packages/ckeditor5/src/plugins/image_to_drawing.ts
+++ b/packages/ckeditor5/src/plugins/image_to_drawing.ts
@@ -66,11 +66,10 @@ export class ConvertImageToDrawingCommand extends Command {
if (!image || typeof src !== "string") return;
const size = this.getShownSize(image);
- const host = glob.getComponentByEl<EditorComponent>(editor.editing.view.getDomRoot());
this.forceDisabled(CONVERTING);
let attachmentId: string | null | undefined;
try {
- attachmentId = await host.convertImageToDrawing?.(src);
+ attachmentId = await editor.config.get("imageToDrawing")?.convert?.(src);
} finally {
this.clearForceDisabled(CONVERTING);
}
@@ -143,3 +142,15 @@ function isInDocument(element: ModelElement) {
const root = element.root;
return root.is("rootElement") && root.rootName !== "$graveyard";
}
+
+declare module "ckeditor5" {
+ interface EditorConfig {
+ imageToDrawing?: {
+ /**
+ * Attaches a drawing canvas that shows the image at `src`, and answers with the ID of
+ * its attachment, or `null` when that fails.
+ */
+ convert?(src: string): Promise<string | null>;
+ };
+ }
+}