chore(ckeditor5/plugins): integrate uploadimage

This commit is contained in:
Elian Doran 2025-05-03 16:40:32 +03:00
parent 444e33628c
commit 5cb5d8e511
No known key found for this signature in database
5 changed files with 40 additions and 28 deletions

View File

@ -5,6 +5,10 @@ declare global {
getComponentByEl(el: unknown): { getComponentByEl(el: unknown): {
triggerCommand(command: string): void; triggerCommand(command: string): void;
}; };
getActiveContextNote(): {
noteId: string;
};
getHeaders(): Promise<Record<string, string>>;
} }
} }

View File

@ -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 { 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 type { Plugin } from "ckeditor5";
import CutToNotePlugin from "./plugins/cuttonote.js"; import CutToNotePlugin from "./plugins/cuttonote.js";
import UploadimagePlugin from "./plugins/uploadimage.js";
const TRILIUM_PLUGINS: typeof Plugin[] = [ const TRILIUM_PLUGINS: typeof Plugin[] = [
CutToNotePlugin CutToNotePlugin,
UploadimagePlugin
]; ];
export const COMMON_PLUGINS: typeof Plugin[] = [ export const COMMON_PLUGINS: typeof Plugin[] = [
@ -46,8 +48,7 @@ export const COMMON_PLUGINS: typeof Plugin[] = [
IndentBlock, IndentBlock,
ParagraphButtonUI, ParagraphButtonUI,
HeadingButtonsUI, HeadingButtonsUI,
//Uploadfileplugin, //Uploadfileplugin
//UploadimagePlugin,
TextTransformation, TextTransformation,
Font, Font,
FontColor, FontColor,

View File

@ -12,7 +12,6 @@ export default class CutToNotePlugin extends Plugin {
this.editor.ui.componentFactory.add( 'cutToNote', locale => { this.editor.ui.componentFactory.add( 'cutToNote', locale => {
const view = new ButtonView( locale ); const view = new ButtonView( locale );
console.log("Got ", scissorsIcon);
view.set( { view.set( {
label: 'Cut & paste selection to sub-note', label: 'Cut & paste selection to sub-note',
icon: scissorsIcon, icon: scissorsIcon,

View File

@ -1,10 +1,6 @@
import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; import { FileRepository, Plugin, type FileLoader, type UploadAdapter } from "ckeditor5";
import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';
export default class UploadimagePlugin extends Plugin { export default class UploadimagePlugin extends Plugin {
/**
* @inheritDoc
*/
static get requires() { static get requires() {
return [ FileRepository ]; 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. * Creates a new adapter instance.
*
* @param {module:upload/filerepository~FileLoader} loader
*/ */
constructor(loader) { constructor(loader: FileLoader) {
/** /**
* FileLoader instance to use during the upload. * FileLoader instance to use during the upload.
*
* @member {module:upload/filerepository~FileLoader} #loader
*/ */
this.loader = loader; this.loader = loader;
} }
@ -37,16 +32,15 @@ class Adapter {
* Starts the upload process. * Starts the upload process.
* *
* @see module:upload/filerepository~Adapter#upload * @see module:upload/filerepository~Adapter#upload
* @returns {Promise}
*/ */
upload() { upload() {
return this.loader.file return this.loader.file
.then( file => new Promise( ( resolve, reject ) => { .then( file => new Promise<File | null>( ( resolve, reject ) => {
this._initRequest().then(() => { this._initRequest().then(() => {
this._initListeners( resolve, reject, file ); this._initListeners(resolve, reject);
this._sendRequest( file ); this._sendRequest();
}); });
} ) ); } ) ) as Promise<any>;
} }
/** /**
@ -88,13 +82,23 @@ class Adapter {
* Initializes XMLHttpRequest listeners. * Initializes XMLHttpRequest listeners.
* *
* @private * @private
* @param {Function} resolve Callback function to be called when the request is successful. * @param 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 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; const xhr = this.xhr;
if (!xhr) {
reject("Missing XHR");
return;
}
const loader = this.loader; const loader = this.loader;
const file = await loader.file; const file = await loader.file;
if (!file) {
reject("Missing file");
return;
}
const genericError = 'Cannot upload file:' + ` ${file.name}.`; const genericError = 'Cannot upload file:' + ` ${file.name}.`;
xhr.addEventListener('error', () => reject(genericError)); xhr.addEventListener('error', () => reject(genericError));
@ -108,7 +112,7 @@ class Adapter {
resolve({ resolve({
default: response.url default: response.url
}); } as unknown as File);
}); });
// Upload progress when it's supported. // Upload progress when it's supported.
@ -131,9 +135,13 @@ class Adapter {
async _sendRequest() { async _sendRequest() {
// Prepare form data. // Prepare form data.
const data = new FormData(); const data = new FormData();
data.append('upload', await this.loader.file);
// Send request. const file = await this.loader.file;
this.xhr.send(data); if (file) {
data.append('upload', file);
// Send request.
this.xhr?.send(data);
}
} }
} }

View File

@ -9,8 +9,8 @@
"tsBuildInfoFile": "dist/tsconfig.lib.tsbuildinfo", "tsBuildInfoFile": "dist/tsconfig.lib.tsbuildinfo",
"emitDeclarationOnly": true, "emitDeclarationOnly": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"lib": ["DOM", "ES2015"],
"types": [ "types": [
"node",
"vite/client" "vite/client"
] ]
}, },