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
chore(ckeditor5/plugins): integrate uploadimage
Elian Doran5cb5d8e5114e
diff --git a/packages/ckeditor5/src/augmentation.ts b/packages/ckeditor5/src/augmentation.tsindex 038be8f917..b596592558 100644--- a/packages/ckeditor5/src/augmentation.ts+++ b/packages/ckeditor5/src/augmentation.ts@@ -5,6 +5,10 @@ declare global {getComponentByEl(el: unknown): {triggerCommand(command: string): void;};+ getActiveContextNote(): {+ noteId: string;+ };+ getHeaders(): Promise<Record<string, string>>;}}diff --git a/packages/ckeditor5/src/plugins.ts b/packages/ckeditor5/src/plugins.tsindex 42ee4e13e5..901e0cf5d8 100644--- a/packages/ckeditor5/src/plugins.ts+++ b/packages/ckeditor5/src/plugins.ts@@ -1,9 +1,11 @@import { Autoformat, AutoLink, BlockQuote, Bold, CKFinderUploadAdapter, Clipboard, Code, CodeBlock, Enter, FindAndReplace, Font, FontBackgroundColor, FontColor, GeneralHtmlSupport, Heading, HeadingButtonsUI, HorizontalLine, Image, ImageCaption, ImageInline, ImageResize, ImageStyle, ImageToolbar, ImageUpload, Indent, IndentBlock, Italic, Link, List, ListProperties, Mention, PageBreak, Paragraph, ParagraphButtonUI, PasteFromOffice, PictureEditing, RemoveFormat, SelectAll, ShiftEnter, SpecialCharacters, SpecialCharactersEssentials, Strikethrough, Style, Subscript, Superscript, Table, TableCaption, TableCellProperties, TableColumnResize, TableProperties, TableSelection, TableToolbar, TextPartLanguage, TextTransformation, TodoList, Typing, Underline, Undo } from "ckeditor5";import type { Plugin } from "ckeditor5";import CutToNotePlugin from "./plugins/cuttonote.js";+import UploadimagePlugin from "./plugins/uploadimage.js";const TRILIUM_PLUGINS: typeof Plugin[] = [- CutToNotePlugin+ CutToNotePlugin,+ UploadimagePlugin];export const COMMON_PLUGINS: typeof Plugin[] = [@@ -46,8 +48,7 @@ export const COMMON_PLUGINS: typeof Plugin[] = [IndentBlock,ParagraphButtonUI,HeadingButtonsUI,- //Uploadfileplugin,- //UploadimagePlugin,+ //UploadfilepluginTextTransformation,Font,FontColor,diff --git a/packages/ckeditor5/src/plugins/cuttonote.ts b/packages/ckeditor5/src/plugins/cuttonote.tsindex fffe45abfd..28fb0a98a0 100644--- a/packages/ckeditor5/src/plugins/cuttonote.ts+++ b/packages/ckeditor5/src/plugins/cuttonote.ts@@ -12,7 +12,6 @@ export default class CutToNotePlugin extends Plugin {this.editor.ui.componentFactory.add( 'cutToNote', locale => {const view = new ButtonView( locale );- console.log("Got ", scissorsIcon);view.set( {label: 'Cut & paste selection to sub-note',icon: scissorsIcon,diff --git a/_regroup/ckeditor5-build-trilium/packages/ckeditor5-build-trilium/src/uploadimage.js b/packages/ckeditor5/src/plugins/uploadimage.tssimilarity index 68%rename from _regroup/ckeditor5-build-trilium/packages/ckeditor5-build-trilium/src/uploadimage.jsrename to packages/ckeditor5/src/plugins/uploadimage.tsindex 9566e566d2..e1bb00261b 100644--- a/_regroup/ckeditor5-build-trilium/packages/ckeditor5-build-trilium/src/uploadimage.js+++ b/packages/ckeditor5/src/plugins/uploadimage.ts@@ -1,10 +1,6 @@-import Plugin from '@ckeditor/ckeditor5-core/src/plugin';-import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';+import { FileRepository, Plugin, type FileLoader, type UploadAdapter } from "ckeditor5";export default class UploadimagePlugin extends Plugin {- /**- * @inheritDoc- */static get requires() {return [ FileRepository ];}@@ -18,17 +14,16 @@ export default class UploadimagePlugin extends Plugin {}}-class Adapter {+class Adapter implements UploadAdapter {+ private loader: FileLoader;+ private xhr?: XMLHttpRequest;+/*** Creates a new adapter instance.- *- * @param {module:upload/filerepository~FileLoader} loader*/- constructor(loader) {+ constructor(loader: FileLoader) {/*** FileLoader instance to use during the upload.- *- * @member {module:upload/filerepository~FileLoader} #loader*/this.loader = loader;}@@ -37,16 +32,15 @@ class Adapter {* Starts the upload process.** @see module:upload/filerepository~Adapter#upload- * @returns {Promise}*/upload() {return this.loader.file- .then( file => new Promise( ( resolve, reject ) => {+ .then( file => new Promise<File | null>( ( resolve, reject ) => {this._initRequest().then(() => {- this._initListeners( resolve, reject, file );- this._sendRequest( file );+ this._initListeners(resolve, reject);+ this._sendRequest();});- } ) );+ } ) ) as Promise<any>;}/**@@ -88,13 +82,23 @@ class Adapter {* Initializes XMLHttpRequest listeners.** @private- * @param {Function} resolve Callback function to be called when the request is successful.- * @param {Function} reject Callback function to be called when the request cannot be completed.+ * @param resolve Callback function to be called when the request is successful.+ * @param reject Callback function to be called when the request cannot be completed.*/- async _initListeners(resolve, reject) {+ async _initListeners(resolve: (value: File | PromiseLike<File | null> | null) => void, reject: (reason?: any) => void) {const xhr = this.xhr;+ if (!xhr) {+ reject("Missing XHR");+ return;+ }+const loader = this.loader;const file = await loader.file;+ if (!file) {+ reject("Missing file");+ return;+ }+const genericError = 'Cannot upload file:' + ` ${file.name}.`;xhr.addEventListener('error', () => reject(genericError));@@ -108,7 +112,7 @@ class Adapter {resolve({default: response.url- });+ } as unknown as File);});// Upload progress when it's supported.@@ -131,9 +135,13 @@ class Adapter {async _sendRequest() {// Prepare form data.const data = new FormData();- data.append('upload', await this.loader.file);- // Send request.- this.xhr.send(data);+ const file = await this.loader.file;+ if (file) {+ data.append('upload', file);++ // Send request.+ this.xhr?.send(data);+ }}}diff --git a/packages/ckeditor5/tsconfig.lib.json b/packages/ckeditor5/tsconfig.lib.jsonindex 73dac81be9..360e0c0235 100644--- a/packages/ckeditor5/tsconfig.lib.json+++ b/packages/ckeditor5/tsconfig.lib.json@@ -9,8 +9,8 @@"tsBuildInfoFile": "dist/tsconfig.lib.tsbuildinfo","emitDeclarationOnly": true,"forceConsistentCasingInFileNames": true,+ "lib": ["DOM", "ES2015"],"types": [- "node","vite/client"]},