Skip to content

Commit

Permalink
update: #378 add callback argument to uploadBefore
Browse files Browse the repository at this point in the history
fix: uploadHandler
  • Loading branch information
JiHong88 committed Jun 15, 2020
1 parent 0b25964 commit af2f625
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ editor.onAudioUpload = function (targetElement, index, state, info, remainingFil
// If you return false, the default notices are not called.
/**
* errorMessage: Error message
* result: Response Objectz
* result: Response Object
* core: Core object
* return {Boolean}
*/
Expand Down
93 changes: 93 additions & 0 deletions sample/html/out/document-user.html
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,53 @@ <h4 class="name" id="onAudioUploadBefore"><span class="type-signature"></span>on
<dl class="details"></dl>


<h4 class="name" id="videoUploadHandler"><span class="type-signature"></span>videoUploadHandler<span
class="signature">(xmlHttpRequest, info, core)</span><span class="type-signature"></span></h4>
<div class="description">
It replaces the default callback function of the video upload.
</div>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>xmlHttpRequest</code></td>
<td class="type">
<span class="param-type">XMLHttpRequest</span>
</td>
<td class="description last">xmlHttpRequest object</td>
</tr>
<tr>
<td class="name"><code>info</code></td>
<td class="type">
<span class="param-type">Object</span>
</td>
<td class="description last">
-- Input information --<br>
<strong>inputWidth:</strong> Value of width input<br>
<strong>inputHeight:</strong> Value of height input<br>
<strong>align:</strong> Align Check Value<br>
<strong>isUpdate:</strong> Update video if true, create video if false<br>
<strong>element:</strong> If isUpdate is true, the currently selected video.<br>
</td>
</tr>
<tr>
<td class="name"><code>core</code></td>
<td class="type">
<span class="param-type">Object</span>
</td>
<td class="description last"><a href="document-editor.html">Core Object</a></td>
</tr>
</tbody>
</table>
<dl class="details"></dl>


<h4 class="name" id="onAudioUpload"><span class="type-signature"></span>onAudioUpload<span
class="signature">(targetElement, index, state, info, remainingFilesCount, core)</span><span class="type-signature"></span></h4>
<div class="description">
Expand Down Expand Up @@ -1414,6 +1461,50 @@ <h4 class="name" id="onAudioUploadError"><span class="type-signature"></span>onA
<dl class="details"></dl>


<h4 class="name" id="videoUploadHandler"><span class="type-signature"></span>videoUploadHandler<span
class="signature">(xmlHttpRequest, info, core)</span><span class="type-signature"></span></h4>
<div class="description">
It replaces the default callback function of the audio upload.
</div>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>xmlHttpRequest</code></td>
<td class="type">
<span class="param-type">XMLHttpRequest</span>
</td>
<td class="description last">xmlHttpRequest object</td>
</tr>
<tr>
<td class="name"><code>info</code></td>
<td class="type">
<span class="param-type">Object</span>
</td>
<td class="description last">
-- Input information --<br>
<strong>isUpdate:</strong> Update audio if true, create audio if false<br>
<strong>element:</strong> If isUpdate is true, the currently selected audio.<br>
</td>
</tr>
<tr>
<td class="name"><code>core</code></td>
<td class="type">
<span class="param-type">Object</span>
</td>
<td class="description last"><a href="document-editor.html">Core Object</a></td>
</tr>
</tbody>
</table>
<dl class="details"></dl>


<h4 class="name" id="showInline"><span class="type-signature"></span>showInline<span
class="signature">(toolbar, context, core)</span><span class="type-signature"></span></h4>
<div class="description">
Expand Down Expand Up @@ -1611,9 +1702,11 @@ <h3>Functions & Events</h3>
<li><a href="document-user.html#onVideoUploadBefore">onVideoUploadBefore</a></li>
<li><a href="document-user.html#onVideoUpload">onVideoUpload</a></li>
<li><a href="document-user.html#onVideoUploadError">onVideoUploadError</a></li>
<li><a href="document-user.html#videoUploadHandler">videoUploadHandler</a></li>
<li><a href="document-user.html#onAudioUploadBefore">onAudioUploadBefore</a></li>
<li><a href="document-user.html#onAudioUpload">onAudioUpload</a></li>
<li><a href="document-user.html#onAudioUploadError">onAudioUploadError</a></li>
<li><a href="document-user.html#audioUploadHandler">audioUploadHandler</a></li>
<li><a href="document-user.html#toggleCodeView">toggleCodeView</a></li>
<li><a href="document-user.html#toggleFullScreen">toggleFullScreen</a></li>
<li><a href="document-user.html#showInline">showInline</a></li>
Expand Down
23 changes: 23 additions & 0 deletions src/lib/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,29 @@ export default class SunEditor {
*/
imageUploadHandler: (xmlHttpRequest: XMLHttpRequest, info: imageInputInformation, core: Core) => void;

/**
* @description It replaces the default callback function of the video upload
* @param xmlHttpRequest xmlHttpRequest object
* @param info Input information
* - inputWidth: Value of width input
* - inputHeight: Value of height input
* - align: Align Check Value
* - isUpdate: Update video if true, create video if false
* - element: If isUpdate is true, the currently selected video.
* @param core Core object
*/
videoUploadHandler: (xmlHttpRequest: XMLHttpRequest, info: videoInputInformation, core: Core) => void;

/**
* @description It replaces the default callback function of the audio upload
* @param xmlHttpRequest xmlHttpRequest object
* @param info Input information
* - isUpdate: Update audio if true, create audio if false
* - element: If isUpdate is true, the currently selected audio.
* @param core Core object
*/
audioUploadHandler: (xmlHttpRequest: XMLHttpRequest, info: audioInputInformation, core: Core) => void;

/**
* @description Called before the image is uploaded
* If false is returned, no image upload is performed.
Expand Down
22 changes: 22 additions & 0 deletions src/lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -6438,6 +6438,28 @@ export default function (context, pluginCallButtons, plugins, lang, options, _ic
* @param {Object} core Core object
*/
imageUploadHandler: null,
/**
* @description It replaces the default callback function of the video upload
* @param xmlHttpRequest xmlHttpRequest object
* @param info Input information
* - inputWidth: Value of width input
* - inputHeight: Value of height input
* - align: Align Check Value
* - isUpdate: Update video if true, create video if false
* - element: If isUpdate is true, the currently selected video.
* @param core Core object
*/
videoUploadHandler: null,

/**
* @description It replaces the default callback function of the audio upload
* @param xmlHttpRequest xmlHttpRequest object
* @param info Input information
* - isUpdate: Update audio if true, create audio if false
* - element: If isUpdate is true, the currently selected audio.
* @param core Core object
*/
audioUploadHandler: null,

/**
* @description Called before the image is uploaded
Expand Down
49 changes: 35 additions & 14 deletions src/plugins/dialog/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ export default {
};

if (typeof this.functions.onAudioUploadBefore === 'function') {
const result = this.functions.onAudioUploadBefore(files, info, this);
const result = this.functions.onAudioUploadBefore(files, info, this, this.plugins.audio.upload.bind(this, info));
if (typeof result === 'undefined') return;
if (!result) {
this.closeLoading();
return;
Expand All @@ -307,7 +308,24 @@ export default {
this.plugins.audio.upload.call(this, info, files);
},

error: function (message, response) {
this.closeLoading();
if (typeof this.functions.onAudioUploadError !== 'function' || this.functions.onAudioUploadError(message, response, this)) {
this.functions.noticeOpen(message);
throw Error('[SUNEDITOR.plugin.audio.exception] response: ' + message);
}
},

upload: function (info, files) {
if (!files) {
this.closeLoading();
return;
}
if (typeof files === 'string') {
this.plugins.audio.error.call(this, files, null);
return;
}

const audioUploadUrl = this.context.option.audioUploadUrl;
const filesLen = this.context.dialog.updateModal ? 1 : files.length;

Expand All @@ -322,20 +340,23 @@ export default {
},

callBack_upload: function (info, xmlHttp) {
const response = JSON.parse(xmlHttp.responseText);

if (response.errorMessage) {
if (this.functions.onAudioUploadError !== 'function' || this.functions.onAudioUploadError(response.errorMessage, response, this)) {
this.functions.noticeOpen(response.errorMessage);
}
if (typeof this.functions.audioUploadHandler === 'function') {
this.functions.audioUploadHandler(xmlHttp, info, this);
} else {
const fileList = response.result;
for (let i = 0, len = fileList.length, file, oAudio; i < len; i++) {
if (info.isUpdate) oAudio = info.element;
else oAudio = this.plugins.audio._createAudioTag.call(this);

file = { name: fileList[i].name, size: fileList[i].size };
this.plugins.audio.create_audio.call(this, oAudio, fileList[i].url, file, info.isUpdate);
const response = JSON.parse(xmlHttp.responseText);
if (response.errorMessage) {
if (this.functions.onAudioUploadError !== 'function' || this.functions.onAudioUploadError(response.errorMessage, response, this)) {
this.functions.noticeOpen(response.errorMessage);
}
} else {
const fileList = response.result;
for (let i = 0, len = fileList.length, file, oAudio; i < len; i++) {
if (info.isUpdate) oAudio = info.element;
else oAudio = this.plugins.audio._createAudioTag.call(this);

file = { name: fileList[i].name, size: fileList[i].size };
this.plugins.audio.create_audio.call(this, oAudio, fileList[i].url, file, info.isUpdate);
}
}
}
},
Expand Down
25 changes: 20 additions & 5 deletions src/plugins/dialog/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ export default {
};

if (typeof this.functions.onImageUploadBefore === 'function') {
const result = this.functions.onImageUploadBefore(files, info, this);
const result = this.functions.onImageUploadBefore(files, info, this, this.plugins.image.upload.bind(this, info));
if (typeof result === 'undefined') return;
if (!result) {
this.closeLoading();
return;
Expand All @@ -424,7 +425,24 @@ export default {
this.plugins.image.upload.call(this, info, files);
},

error: function (message, response) {
this.closeLoading();
if (typeof this.functions.onImageUploadError !== 'function' || this.functions.onImageUploadError(message, response, this)) {
this.functions.noticeOpen(message);
throw Error('[SUNEDITOR.plugin.image.error] response: ' + message);
}
},

upload: function (info, files) {
if (!files) {
this.closeLoading();
return;
}
if (typeof files === 'string') {
this.plugins.image.error.call(this, files, null);
return;
}

const imageUploadUrl = this.context.option.imageUploadUrl;
const filesLen = this.context.dialog.updateModal ? 1 : files.length;

Expand All @@ -445,11 +463,8 @@ export default {
this.functions.imageUploadHandler(xmlHttp, info, this);
} else {
const response = JSON.parse(xmlHttp.responseText);

if (response.errorMessage) {
if (this.functions.onImageUploadError !== 'function' || this.functions.onImageUploadError(response.errorMessage, response, this)) {
this.functions.noticeOpen(response.errorMessage);
}
this.plugins.image.error.call(this, response.errorMessage, response);
} else {
const fileList = response.result;
for (let i = 0, len = fileList.length, file; i < len; i++) {
Expand Down
25 changes: 20 additions & 5 deletions src/plugins/dialog/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ export default {
};

if (typeof this.functions.onVideoUploadBefore === 'function') {
const result = this.functions.onVideoUploadBefore(files, info, this);
const result = this.functions.onVideoUploadBefore(files, info, this, this.plugins.video.upload.bind(this, info));
if (typeof result === 'undefined') return;
if (!result) {
this.closeLoading();
return;
Expand All @@ -386,7 +387,24 @@ export default {
this.plugins.video.upload.call(this, info, files);
},

error: function (message, response) {
this.closeLoading();
if (typeof this.functions.onVideoUploadError !== 'function' || this.functions.onVideoUploadError(message, response, this)) {
this.functions.noticeOpen(message);
throw Error('[SUNEDITOR.plugin.video.error] response: ' + message);
}
},

upload: function (info, files) {
if (!files) {
this.closeLoading();
return;
}
if (typeof files === 'string') {
this.plugins.video.error.call(this, files, null);
return;
}

const videoUploadUrl = this.context.option.videoUploadUrl;
const filesLen = this.context.dialog.updateModal ? 1 : files.length;

Expand All @@ -407,11 +425,8 @@ export default {
this.functions.videoUploadHandler(xmlHttp, info, this);
} else {
const response = JSON.parse(xmlHttp.responseText);

if (response.errorMessage) {
if (this.functions.onVideoUploadError !== 'function' || this.functions.onVideoUploadError(response.errorMessage, response, this)) {
this.functions.noticeOpen(response.errorMessage);
}
this.plugins.video.error.call(this, response.errorMessage, response);
} else {
const fileList = response.result;
const videoTag = this.plugins.video.createVideoTag.call(this);
Expand Down
Loading

0 comments on commit af2f625

Please sign in to comment.