feat: 更新 editor.md 的图片上传功能:支持用户自定义 imageUploadURL

This commit is contained in:
Frankie Huang 2025-04-07 13:30:30 +08:00
parent d7e01521ab
commit 75314c2aed
2 changed files with 56 additions and 6 deletions

View File

@ -47,9 +47,17 @@
}
var imageFileName = classPrefix + "image-file";
// 删除 method="post" 让表单不会触发提交。后面使用 ajax 重写图片上传请求
var dialogContent = ( (settings.imageUpload) ? "<form action=\"" + action +"\" target=\"" + iframeName + "\" enctype=\"multipart/form-data\" class=\"" + classPrefix + "form\">" : "<div class=\"" + classPrefix + "form\">" ) +
// 将 action 置为 # 号。后面使用 ajax 重写图片上传请求
var dialogContent = ( (settings.imageUpload) ? "<form action=\"#\" target=\"" + iframeName + "\" method=\"post\" enctype=\"multipart/form-data\" class=\"" + classPrefix + "form\">" : "<div class=\"" + classPrefix + "form\">" ) +
( (settings.imageUpload) ? "<iframe name=\"" + iframeName + "\" id=\"" + iframeName + "\" guid=\"" + guid + "\"></iframe>" : "" ) +
// [begin] add by Frankie. for change imageUploadURL
( (settings.imageUpload) ? "<label>Service</label><input type=\"text\" id=\"upload-url-input\" title=\"" + settings.imageUploadURL + "\" placeholder=\"" + settings.imageUploadURL + "\" disabled />" : "" ) +
(function() {
return (settings.imageUpload) ? "<div style=\"margin-left: 8px; display: inline-block\">" +
"<button type=\"button\" id=\"lock-or-unlock\">Unlock</button>" +
"</div><br/>" : "";
})() +
// [end] add by Frankie.
"<label>" + imageLang.url + "</label>" +
"<input type=\"text\" data-url />" + (function(){
return (settings.imageUpload) ? "<div class=\"" + classPrefix + "file-input\">" +
@ -72,7 +80,7 @@
dialog = this.createDialog({
title : imageLang.title,
width : (settings.imageUpload) ? 465 : 380,
height : 254,
height : (settings.imageUpload) ? 304 : 254,
name : dialogName,
content : dialogContent,
mask : settings.dialogShowMask,
@ -134,6 +142,27 @@
return ;
}
// 给按钮绑定事件,支持手动更新 settings.imageUploadURL
$('#lock-or-unlock').on('click', function() {
let uploadURLInput = $('#upload-url-input')
if (uploadURLInput.attr('disabled') == undefined) {
// 如果输入框未被禁用,则禁用按钮
$(this).text('Unlock');
uploadURLInput.attr('disabled', 'disabled');
// 用输入框内容更新 imageUploadURL
if (uploadURLInput.val().trim().length > 0) {
settings.imageUploadURL = uploadURLInput.val().trim()
// 触发回调事件,支持向上暴露
settings.imageUploadURLChange(settings.imageUploadURL)
}
} else {
// 如果输入框为禁用状态,则解锁输入框
$(this).text('Update');
uploadURLInput.removeAttr('disabled');
uploadURLInput.trigger('focus')
}
})
var fileInput = dialog.find("[name=\"" + imageFileName + "\"]");
fileInput.bind("change", function() {
@ -202,6 +231,9 @@
alert(data.message);
}
},
error: function(xhr) {
alert("Upload image failed. HTTP Status Code: " + xhr.status);
},
});
return false;
};

View File

@ -33,12 +33,26 @@ export default {
required: false,
default: '720px'
},
//
//
imageUpload: {
type: Boolean,
required: false,
default: false
},
//
imageUploadURL: {
type: String,
required: false,
default: ''
},
// image-dialog imageUploadURL
imageUploadURLChange: {
type: Function,
required: false,
default: (newURL) => {
console.log("new imageUploadURL: " + newURL)
}
},
//
onload: {
type: Function,
@ -109,9 +123,13 @@ export default {
taskList: true,
//
imageUpload: vm.imageUploadURL == '' ? false : true, // imageUploadURL
imageUpload: vm.imageUpload, // imageUploadURL
imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp", "ico"],
imageUploadURL: vm.imageUploadURL, //
imageUploadURL: vm.imageUploadURL, //
imageUploadURLChange: function (newURL) {
// image-dialog imageUploadURL
vm.imageUploadURLChange(newURL)
},
//
// theme: (localStorage.theme) ? localStorage.theme : "dark",