Skip to content
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, remove irrelevant changes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "irrelevant changes"? It make a while, don't use this package anymore. So I don't remember a lot about it. Feel free to build on this.

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