/** * @module modules/uploader */ import type { IJodit, IUploader, IUploaderAnswer, IUploaderData, IUploaderOptions, IUploaderPayload, MediaUploadType } from 'jodit/types'; import { Config } from 'jodit/config'; import { isArray } from 'jodit/core/helpers/checker/is-array'; import { isJoditObject } from 'jodit/core/helpers/checker/is-jodit-object'; import { css } from 'jodit/src/core/helpers'; declare module 'jodit/config' { interface Config { /** * Enable drag and drop file editor */ enableDragAndDropFileToEditor: boolean; uploader: IUploaderOptions; uploaderApi: string; } } /** * Module for processing download documents and images by Drag and Drop * Drag and Drop files */ Config.prototype.enableDragAndDropFileToEditor = true; Config.prototype.uploader = { url: (editor: IJodit, form: FormData) => { // Check file if (!(form && form.get('file'))) return; // Custom Server const file = form.get('file'); // @ts-ignore const matchType = file.type.match(/([a-z0-9]+)\//i) as string[]; // @ts-ignore const matchExtension = file.type.match(/\/([a-z0-9]+)/i) as string[]; const fileType: string = matchType && matchType[1] ? matchType[1].toLowerCase() : ''; const fileExtension: string = matchExtension && matchExtension[1] ? matchExtension[1].toLowerCase() : ''; let path; switch (fileType) { case 'image': if (fileExtension === 'gif') path = 'gif'; else path = 'image'; break; case 'video': path = 'video'; break; default: path = 'file'; break; } return new URL(path, editor.o.uploaderApi).href; }, insertImageAsBase64URI: false, imagesExtensions: ['jpg', 'png', 'jpeg', 'gif'], headers: (authToken: string) => { if (authToken) { return { token: 'Authorization: ' + authToken }; } else { return null; } }, data: null, authToken: null, filesVariableName(): string { // return 'files[0]'; return 'file'; }, withCredentials: false, pathVariableName: 'path', format: 'json', method: 'POST', prepareData(this: IUploader, formData: FormData) { return formData; }, isSuccess(this: IUploader, resp: IUploaderAnswer): boolean { return resp.success; }, getMessage(this: IUploader, resp: IUploaderAnswer) { return resp.data.message !== undefined && isArray(resp.data.message) ? resp.data.message.join(' ') : ''; }, /** * @see [[IUploader.processFileName]] */ processFileName( this: IUploader, key: string, file: File, name: string ): [string, File, string] { return [key, file, name]; }, process(this: IUploader, resp: IUploaderAnswer): IUploaderData { return resp.data; }, error(this: IUploader, e: Error) { this.j.e.fire('errorMessage', e.message, 'error', 4000); }, getDisplayName(this: IUploader, filename: string): string { return filename; }, defaultHandlerSuccess( this: IUploader, resp: IUploaderData, mediaUploadType?: MediaUploadType, payload?: IUploaderPayload ) { console.log( '-------- uploader config.ts defaultHandlerSuccess() --------' ); console.log(':::: response: ', resp); console.log(':::: mediaUploadType: ', mediaUploadType); console.log(':::: payload: ', payload); const j = this.j || this; if (!isJoditObject(j)) { return; } if (!resp.file) return; let element; if (mediaUploadType === 'file') { const downloadSVG = require('jodit/src/styles/icons/download.svg'); element = j.c.fromHTML( `${payload?.name}` ); } else { switch (resp.type) { case 'image': case 'gif': element = j.createInside.element('img'); element.setAttribute('src', resp.url); element.setAttribute('alt', resp.file); css(element, { display: 'block', width: `${j.o.imageDefaultWidth}px` }); element = j.createInside.fromHTML( `${
							resp.file
						}` ); break; case 'video': element = j.c.fromHTML( `
 
` ); break; case 'file': element = j.c.fromHTML( `${ payload?.name ?? resp.file }` ); break; // case 'audio': // element = j.c.fromHTML( // `

` // ); // break; default: element = j.c.fromHTML( `${resp.file}` ); break; } } const enterKeyEvent = new KeyboardEvent('keydown', { key: 'Enter', code: 'Enter', keyCode: 13, which: 13, bubbles: true }); j.editor.dispatchEvent(enterKeyEvent); j.s.insertHTML(element); }, defaultHandlerError(this: IUploader, e: Error) { this.j.e.fire('closeAllPopups'); }, contentType(this: IUploader, requestData: any) { return (this.ow as any).FormData !== undefined && typeof requestData !== 'string' ? false : 'application/x-www-form-urlencoded; charset=UTF-8'; } } as IUploaderOptions;