Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add drag&drop support #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,57 @@ export default class AttachesTool {
this.nodes.button.click();
}

/**
* Specify paste substitutes
*
* @see {@link https://github.com/codex-team/editor.js/blob/master/docs/tools.md#paste-handling}
* @returns {{tags: string[], patterns: object<string, RegExp>, files: {extensions: string[], mimeTypes: string[]}}}
*/
static get pasteConfig() {
return {
/**
* Paste HTML into Editor
*/
tags: [],

/**
* Paste URL of file into the Editor
*/
patterns: {},

/**
* Drag n drop file from into the Editor
*/
files: {
mimeTypes: [ 'application/*' ],
},
};
}

/**
* Specify paste handlers
*
* @public
* @see {@link https://github.com/codex-team/editor.js/blob/master/docs/tools.md#paste-handling}
* @param {CustomEvent} event - editor.js custom paste event
* {@link https://github.com/codex-team/editor.js/blob/master/types/tools/paste-events.d.ts}
* @returns {void}
*/
async onPaste(event) {
switch (event.type) {
case 'file': {
const file = event.detail.file;

this.uploader.uploadByFile(file, {
onPreview: () => {
this.nodes.wrapper.classList.add(this.CSS.wrapperLoading, this.CSS.loader);
},
});
break;
}
}
}

/**
* Checks if any of Tool's fields have data
*
Expand Down
42 changes: 42 additions & 0 deletions src/uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,48 @@ export default class Uploader {
}).then((response) => response.body);
}

upload.then((response) => {
this.onUpload(response);
}).catch((errorResponse) => {
const error = errorResponse.body;

const message = (error && error.message) ? error.message : this.config.errorMessage;

this.onError(message);
});
}
/**
* Handle clicks on the upload file button
* Fires ajax.post()
*
* @param {File} file - file pasted by drag-n-drop
* @param {Function} onPreview - file pasted by drag-n-drop
*/
uploadByFile(file, { onPreview }) {
/**
* Custom uploading
* or default uploading
*/
let upload;

// custom uploading
if (this.config.uploader && typeof this.config.uploader.uploadByFile === 'function') {
upload = this.config.uploader.uploadByFile(file);

if (!isPromise(upload)) {
console.warn('Custom uploader method uploadByFile should return a Promise');
}
// default uploading
} else {
upload = ajax.transport({
url: this.config.endpoint,
accept: this.config.types,
beforeSend: () => onPreview(),
fieldName: this.config.field,
headers: this.config.additionalRequestHeaders || {},
}).then((response) => response.body);
}

upload.then((response) => {
this.onUpload(response);
}).catch((errorResponse) => {
Expand Down