Skip to content

Commit

Permalink
feat: file d&d into richtext
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian ALEXANDRE committed May 29, 2024
1 parent 9af825c commit d1dc672
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit d1dc672

Please sign in to comment.