Skip to content

Build your personal knowledge base with Trilium Notes

feat(text): add upload image to slash commands

Elian Dorane20869f6d88d
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 7a3fe0c0b6..d800bcfb99 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -4256,6 +4256,7 @@
"type-the-content-here": "Type the content here...",
"undefined-custom-task-state": "Undefined custom task state",
"url": "URL",
+ "upload-image": "Upload image",
"warning": "Warning",
"working": "Working…"
},
diff --git a/apps/standalone/.env b/apps/standalone/.env
index dc6392a2f2..bd1aff470d 100644
--- a/apps/standalone/.env
+++ b/apps/standalone/.env
@@ -1 +1 @@
-VITE_CKEDITOR_ENABLE_INSPECTOR=false
\ No newline at end of file
+VITE_CKEDITOR_ENABLE_INSPECTOR=true
\ No newline at end of file
diff --git a/packages/ckeditor5/src/plugins/mention/slash_commands.ts b/packages/ckeditor5/src/plugins/mention/slash_commands.ts
index 058b8f07fa..23db5bb57f 100644
--- a/packages/ckeditor5/src/plugins/mention/slash_commands.ts
+++ b/packages/ckeditor5/src/plugins/mention/slash_commands.ts
@@ -21,7 +21,8 @@ import {
IconParagraph,
IconQuote,
IconTable,
- IconTodoList
+ IconTodoList,
+ IconImage
} from "@ckeditor/ckeditor5-icons";
import bxBookmark from "boxicons/svg/regular/bx-bookmark.svg?raw";
import bxBulb from "boxicons/svg/regular/bx-bulb.svg?raw";
@@ -140,7 +141,11 @@ export default class TriliumSlashCommands extends Plugin {
const config = (editor.config.get("slashCommand") ?? {}) as SlashCommandConfig;
const removed = new Set(config.removeCommands ?? []);
- return [ ...buildDefaultSlashCommands(editor), ...buildTriliumSlashCommands(editor), ...buildSnippetSlashCommands(editor) ]
+ return [
+ ...buildDefaultSlashCommands(editor),
+ ...buildTriliumSlashCommands(editor),
+ ...buildSnippetSlashCommands(editor)
+ ]
.filter((definition) => !removed.has(definition.id))
.filter((definition) => isSlashCommandEnabled(editor, definition));
}
@@ -295,7 +300,8 @@ export function buildDefaultSlashCommands(editor: Editor): SlashCommandDefinitio
description: t("Decrease the indentation of the current block."),
icon: IconOutdent,
commandName: "outdent"
- }
+ },
+ buildImageUploadCommand(editor)
];
}
@@ -627,6 +633,50 @@ function buildHeadingSlashCommands(editor: Editor): SlashCommandDefinition[] {
});
}
+function buildImageUploadCommand(editor: Editor): SlashCommandDefinition {
+ const t = editor.locale.t;
+
+ return {
+ id: "uploadImage",
+ title: t("Upload image"),
+ icon: IconImage,
+ execute(editor) {
+ const input = document.createElement("input");
+
+ input.type = "file";
+ input.accept = "*/*";
+
+ const imageTypes = editor.config.get( 'image.upload.types' )!;
+ const imageTypesRegExp = createImageTypeRegExp( imageTypes );
+
+ input.addEventListener( "change", () => {
+ const imagesToUpload = Array.from(input.files ?? [])
+ .filter( file => imageTypesRegExp.test(file.type));
+
+ if (imagesToUpload) {
+ editor.execute('uploadImage', { file: imagesToUpload });
+ editor.editing.view.focus();
+ }
+
+ input.remove();
+ }, { once: true } );
+
+ input.style.display = 'none';
+ document.body.appendChild( input );
+
+ input.click();
+ },
+ };
+}
+
+// Source: https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-image/src/imageupload/utils.ts
+function createImageTypeRegExp( types: Array<string> ): RegExp {
+ // Sanitize the MIME type name which may include: "+", "-" or ".".
+ const regExpSafeNames = types.map( type => type.replace( '+', '\\+' ) );
+
+ return new RegExp( `^image\\/(${ regExpSafeNames.join( '|' ) })$` );
+}
+
/**
* Renders one palette row: icon, title and description. The class names match premium's, so the
* overrides Trilium already carries in `style.css` and the Next theme keep applying unchanged.