Skip to content

Build your personal knowledge base with Trilium Notes

feat(markdown): add error management to image uploading

Elian Doranf9f9ba129332
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 765001f7c6..7bf254a394 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -241,6 +241,9 @@
"search_placeholder": "Search for note by its name or type > for commands...",
"search_button": "Search in full text"
},
+ "markdown_editor": {
+ "image_upload_failed": "Cannot upload image \"{{name}}\"."
+ },
"markdown_import": {
"dialog_title": "Markdown import",
"modal_body_text": "Because of browser sandbox it's not possible to directly read clipboard from JavaScript. Please paste the Markdown to import to textarea below and click on Import button",
diff --git a/apps/client/src/widgets/type_widgets/code/Markdown.tsx b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
index a147561275..9ea9c62bf8 100644
--- a/apps/client/src/widgets/type_widgets/code/Markdown.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
@@ -20,6 +20,7 @@ import note_create from "../../../services/note_create";
import options from "../../../services/options";
import server from "../../../services/server";
import { removeIndividualBinding } from "../../../services/shortcuts";
+import toast from "../../../services/toast";
import tree from "../../../services/tree";
import utils, { isDesktop } from "../../../services/utils";
import { useLegacyImperativeHandlers, useTriliumEvent } from "../../react/hooks";
@@ -254,15 +255,29 @@ function useSyncedHighlight(view: VanillaCodeMirror | null, preview: HTMLDivElem
}, [ view, preview, html ]);
}
-/** Uploads the image as a note attachment and inserts a markdown image reference at `pos` (or the cursor). */
+/**
+ * Uploads the image as a note attachment and inserts a markdown image reference at `pos` (or the cursor).
+ * Mirrors CKEditor's image upload adapter: on failure, surfaces the server's error message as a
+ * toast, falling back to a generic "Cannot upload" message for network errors.
+ */
async function uploadImageAndInsert(view: VanillaCodeMirror, note: FNote, file: File, pos?: number) {
- const result = await server.upload(
- `notes/${note.noteId}/attachments/upload`,
- file, undefined, "POST"
- ) as { uploaded: boolean; url?: string };
- if (!result?.uploaded || !result.url) return;
+ let detail: string | undefined;
+ try {
+ const result = await server.upload(
+ `notes/${note.noteId}/attachments/upload`,
+ file, undefined, "POST"
+ ) as { uploaded?: boolean; url?: string; message?: string };
+ if (result?.uploaded && result.url) {
+ insertText(view, `![${file.name}](${result.url})`, pos);
+ return;
+ }
+ detail = result?.message;
+ } catch (e) {
+ detail = e instanceof Error ? e.message : undefined;
+ }
- insertText(view, `![${file.name}](${result.url})`, pos);
+ const base = t("markdown_editor.image_upload_failed", { name: file.name });
+ toast.showError(detail ? `${base} ${detail}` : base);
}
/** Inserts text at the given position (or cursor) and moves the cursor to the end of the inserted text. */