Skip to content

Commit

Permalink
update: #378 uploadBefore event - return files
Browse files Browse the repository at this point in the history
  • Loading branch information
JiHong88 committed May 23, 2020
1 parent 57e8c0c commit 9520808
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 13 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,7 @@ editor.onDrop = function (e, core) { console.log('onDrop', e) }

// Called before the image is uploaded
// If false is returned, no image upload is performed.
// If new fileList are returned, replaced the previous fileList
/**
* files: Files array
* info: {
Expand All @@ -916,15 +917,19 @@ editor.onDrop = function (e, core) { console.log('onDrop', e) }
* - element: If isUpdate is true, the currently selected image.
* }
* core: Core object
* return {Boolean}
* return {Boolean|Array}
*/
editor.onImageUploadBefore: function (files, info, core) {
console.log('files', files);
console.log('info', info);
return Boolean

return Boolean;
// or
return [] // (new files);
}
// Called before the video is uploaded
// If false is returned, no video(iframe, video) upload is performed.
// If new fileList are returned, replaced the previous fileList
/**
* files: Files array
* info: {
Expand All @@ -940,10 +945,14 @@ editor.onImageUploadBefore: function (files, info, core) {
editor.onVideoUploadBefore: function (files, info, core) {
console.log('files', files);
console.log('info', info);
return Boolean

return Boolean;
// or
return [] // (new files);
}
// Called before the audio is uploaded
// If false is returned, no audio upload is performed.
// If new fileList are returned, replaced the previous fileList
/**
* files: Files array
* info: {
Expand All @@ -956,7 +965,10 @@ editor.onVideoUploadBefore: function (files, info, core) {
editor.onAudioUploadBefore: function (files, info, core) {
console.log('files', files);
console.log('info', info);
return Boolean

return Boolean;
// or
return [] // (new files);
}

// Called when the image is uploaded, updated, deleted.
Expand Down
6 changes: 3 additions & 3 deletions src/lib/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ export default class SunEditor {
* @param core Core object
* @returns
*/
onImageUploadBefore: (files: any[], info: imageInputInformation, core: Core) => boolean;
onImageUploadBefore: (files: any[], info: imageInputInformation, core: Core) => boolean | any[];

/**
* @description Called before the video is uploaded
Expand All @@ -623,7 +623,7 @@ export default class SunEditor {
* @param core Core object
* @returns
*/
onVideoUploadBefore: (files: any[], info: videoInputInformation, core: Core) => boolean;
onVideoUploadBefore: (files: any[], info: videoInputInformation, core: Core) => boolean | any[];

/**
* @description Called before the audio is uploaded
Expand All @@ -633,7 +633,7 @@ export default class SunEditor {
* @param core Core object
* @returns
*/
onAudioUploadBefore: (files: any[], info: audioInputInformation, core: Core) => boolean;
onAudioUploadBefore: (files: any[], info: audioInputInformation, core: Core) => boolean | any[];

/**
* @description Called when the image is uploaded, updated, deleted
Expand Down
3 changes: 3 additions & 0 deletions src/lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -6185,6 +6185,7 @@ export default function (context, pluginCallButtons, plugins, lang, options, _ic
/**
* @description Called before the image is uploaded
* If false is returned, no image upload is performed.
* If new fileList are returned, replaced the previous fileList
* @param {Array} files Files array
* @param {Object} info info: {
* - linkValue: Link url value
Expand All @@ -6202,6 +6203,7 @@ export default function (context, pluginCallButtons, plugins, lang, options, _ic
/**
* @description Called before the video is uploaded
* If false is returned, no video(iframe, video) upload is performed.
* If new fileList are returned, replaced the previous fileList
* @param {Array} files Files array
* @param {Object} info info: {
* - inputWidth: Value of width input
Expand All @@ -6217,6 +6219,7 @@ export default function (context, pluginCallButtons, plugins, lang, options, _ic
/**
* @description Called before the audio is uploaded
* If false is returned, no audio upload is performed.
* If new fileList are returned, replaced the previous fileList
* @param {Array} files Files array
* @param {Object} info info: {
* - isUpdate: Update audio if true, create audio if false
Expand Down
8 changes: 6 additions & 2 deletions src/plugins/dialog/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export default {
if (fileList.length === 0) return;

let fileSize = 0;
const files = [];
let files = [];
for (let i = 0, len = fileList.length; i < len; i++) {
if (/audio/i.test(fileList[i].type)) {
files.push(fileList[i]);
Expand Down Expand Up @@ -295,7 +295,11 @@ export default {
element: context._element
};

if (typeof this.functions.onAudioUploadBefore === 'function' && !this.functions.onAudioUploadBefore(files, info, this)) return;
if (typeof this.functions.onAudioUploadBefore === 'function') {
const result = this.functions.onAudioUploadBefore(files, info, this);
if (!result) return;
if (typeof result === 'object' && result.length > 0) files = result;
}

// create formData
const formData = new FormData();
Expand Down
8 changes: 6 additions & 2 deletions src/plugins/dialog/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export default {
if (fileList.length === 0) return;

let fileSize = 0;
const files = [];
let files = [];
for (let i = 0, len = fileList.length; i < len; i++) {
if (/image/i.test(fileList[i].type)) {
files.push(fileList[i]);
Expand Down Expand Up @@ -392,7 +392,11 @@ export default {
element: contextImage._element
};

if (typeof this.functions.onImageUploadBefore === 'function' && !this.functions.onImageUploadBefore(files, info, this)) return;
if (typeof this.functions.onImageUploadBefore === 'function') {
const result = this.functions.onImageUploadBefore(files, info, this);
if (!result) return;
if (typeof result === 'object' && result.length > 0) files = result;
}

// server upload
if (typeof imageUploadUrl === 'string' && imageUploadUrl.length > 0) {
Expand Down
8 changes: 6 additions & 2 deletions src/plugins/dialog/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export default {
if (fileList.length === 0) return;

let fileSize = 0;
const files = [];
let files = [];
for (let i = 0, len = fileList.length; i < len; i++) {
if (/video/i.test(fileList[i].type)) {
files.push(fileList[i]);
Expand Down Expand Up @@ -370,7 +370,11 @@ export default {
element: contextVideo._element
};

if (typeof this.functions.onVideoUploadBefore === 'function' && !this.functions.onVideoUploadBefore(files, info, this)) return;
if (typeof this.functions.onVideoUploadBefore === 'function') {
const result = this.functions.onVideoUploadBefore(files, info, this);
if (!result) return;
if (typeof result === 'object' && result.length > 0) files = result;
}

// server upload
if (typeof videoUploadUrl === 'string' && videoUploadUrl.length > 0) {
Expand Down

0 comments on commit 9520808

Please sign in to comment.