From d1dc672f56b7f166e1a7cf6915dbc8ae353a1e0b Mon Sep 17 00:00:00 2001 From: Florian ALEXANDRE Date: Thu, 25 Apr 2024 11:32:00 +0200 Subject: [PATCH] feat: file d&d into richtext --- .../upload-file/upload-file-editing.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/bundle/Resources/public/js/CKEditor/upload-file/upload-file-editing.js b/src/bundle/Resources/public/js/CKEditor/upload-file/upload-file-editing.js index b1ed5e6..0edf39b 100644 --- a/src/bundle/Resources/public/js/CKEditor/upload-file/upload-file-editing.js +++ b/src/bundle/Resources/public/js/CKEditor/upload-file/upload-file-editing.js @@ -8,6 +8,36 @@ class UploadFileEditing extends Plugin { } addListeners() { + this.listenTo(this.editor.editing.view.document, 'drop', (event, data) => { + if (data.dataTransfer.effectAllowed === 'copyMove') { + return; + } + + const { files } = data.dataTransfer; + + if (!files.length) { + return; + } + + this.editor.model.change((writer) => { + writer.setSelection(this.editor.editing.mapper.toModelRange(data.dropRange)); + }); + + const allowedMimeTypes = ibexa.adminUiConfig.fileUpload.mime_types || [] + const readFile = function(file, resolve, reject) { + this.addEventListener('load', () => resolve({fileReader: this, file}), false); + this.addEventListener('error', () => reject(), false); + this.readAsDataURL(file); + } + + files.forEach((file) => { + if (allowedMimeTypes.includes(file.type)) { + new Promise(readFile.bind(new FileReader(), file)).then(({file}) => { + this.editor.execute('insertIbexaUploadFile', { file }); + }) + } + }); + }); } init() {