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(text): insert a dropped ICO as a picture instead of a reference link
Elian Doran2036ade0e7fb
diff --git a/apps/client/src/widgets/type_widgets/text/config.spec.ts b/apps/client/src/widgets/type_widgets/text/config.spec.tsindex c08791260a..ccbd059fd2 100644--- a/apps/client/src/widgets/type_widgets/text/config.spec.ts+++ b/apps/client/src/widgets/type_widgets/text/config.spec.ts@@ -163,6 +163,26 @@ describe("CK config", () => {// The `text-editor.ck` section of the English catalog is the registry of editor strings: each// entry names the English message id a plugin passes to `editor.t()`, and the value the editor// gets for it is that key resolved through the app's i18n.+ it("claims every media type the upload endpoint stores as an image", async () => {+ const config = await buildConfig(baseOpts());+ const types = (config.image as { upload: { types: string[] } }).upload.types;+ // The editor decides whether a dropped file is an image with this exact expression (see+ // createImageTypeRegExp) — anchored, so a subtype has to be listed in full.+ const claimed = new RegExp(`^image\\/(${types.map((type) => type.replace("+", "\\+")).join("|")})$`);++ // Kept in step with ACCEPTED_IMAGE_MIMES in trilium-core's image service. A type the server+ // stores as an image but the editor does not claim falls through to the file-upload plugin,+ // which builds a reference link out of it — and a reference link cannot be pointed at the+ // `api/attachments/.../image/...` URL the server answers with, so it renders as+ // "[missing note]" rather than as the picture.+ for (const mime of [+ "image/png", "image/jpeg", "image/gif", "image/webp", "image/svg+xml",+ "image/x-icon", "image/vnd.microsoft.icon"+ ]) {+ expect(claimed.test(mime), mime).toBe(true);+ }+ });+it("turns the English editor catalog into the dictionary the editor resolves messages through", async () => {catalogState.bundle = { "text-editor": { ck: { "insert-a-table": "Insert a table." } } };catalogState.entries["text-editor.ck.insert-a-table"] = "Tabelle einfügen";diff --git a/apps/client/src/widgets/type_widgets/text/config.ts b/apps/client/src/widgets/type_widgets/text/config.tsindex 1e5fe671c3..3c315e6b81 100644--- a/apps/client/src/widgets/type_widgets/text/config.ts+++ b/apps/client/src/widgets/type_widgets/text/config.ts@@ -111,7 +111,18 @@ export async function buildConfig(opts: BuildEditorOptions): Promise<EditorConfi"toggleImageCaption"],upload: {- types: ["jpeg", "png", "gif", "bmp", "webp", "tiff", "svg", "svg+xml", "avif"]+ // Matched against a dropped file's media type as `^image/(<one of these>)$`, which+ // is what decides whether the editor inserts a picture or hands the file to the+ // file-upload plugin to make a reference link out of. So this has to claim+ // everything the upload endpoint stores as an image (ACCEPTED_IMAGE_MIMES): a type+ // the server calls an image but the editor calls a file ends up as a reference link+ // pointed at an `api/attachments/.../image/...` URL, which is not a note URL and so+ // renders as "[missing note]". Both ICO spellings are listed for the same reason+ // the server accepts both — servers send either.+ types: [+ "jpeg", "png", "gif", "bmp", "webp", "tiff", "svg", "svg+xml", "avif",+ "x-icon", "vnd.microsoft.icon"+ ]}},heading: {diff --git a/packages/ckeditor5/src/plugins/file_upload/fileuploadediting.spec.ts b/packages/ckeditor5/src/plugins/file_upload/fileuploadediting.spec.tsindex 7f393f2678..cdd33c9336 100644--- a/packages/ckeditor5/src/plugins/file_upload/fileuploadediting.spec.ts+++ b/packages/ckeditor5/src/plugins/file_upload/fileuploadediting.spec.ts@@ -2,6 +2,8 @@ import {Clipboard,Essentials,FileRepository,+ Image,+ ImageUpload,Notification,Paragraph,Plugin,@@ -130,6 +132,51 @@ describe("FileUploadEditing", () => {expect(getModelData(editor.model)).toBe(before);});+ // -----------------------------------------------------------------+ // Which plugin claims a dropped file+ // -----------------------------------------------------------------++ describe("against ImageUpload", () => {+ /**+ * Both plugins listen for `clipboardInput` at the same priority, and this one takes any+ * file it is handed with no regard for what kind it is. So whether a picture is inserted as+ * a picture comes down entirely to whether ImageUpload recognised the media type first and+ * stopped the event — which is what `image.upload.types` decides.+ */+ async function editorClaiming(types: string[]) {+ const withImages = await createTestEditor(+ [Essentials, Paragraph, FileRepository, Notification, Clipboard, ReferenceSchema, Image, ImageUpload, FileUploadEditing],+ { image: { upload: { types } } }+ );+ installUploadAdapter(withImages);+ setModelData(withImages.model, "<paragraph>[]</paragraph>");++ return withImages;+ }++ function dropIcon(target: ClassicEditor) {+ target.editing.view.document.fire("clipboardInput", {+ dataTransfer: makeDataTransfer({ files: [ new File([ "\x00\x00\x01\x00" ], "favicon.ico", { type: "image/x-icon" }) ] }),+ targetRanges: null+ });++ return getModelData(target.model);+ }++ it("leaves a claimed media type to ImageUpload, so it becomes a picture", async () => {+ const model = dropIcon(await editorClaiming([ "png", "x-icon" ]));++ expect(model).toContain("image");+ expect(model).not.toContain("reference");+ });++ it("takes an unclaimed one itself, which is what made a dropped icon a reference link", async () => {+ const model = dropIcon(await editorClaiming([ "png" ]));++ expect(model).toContain("reference");+ });+ });+// -----------------------------------------------------------------// dragover handler// -----------------------------------------------------------------