配置功能完善

This commit is contained in:
jiangdong.cheng
2026-02-12 15:37:49 +08:00
parent 67cd9501de
commit 56c685b579
14 changed files with 1259 additions and 701 deletions

View File

@@ -139,14 +139,22 @@
const uploadEndpoint = '{{ route('admin.uploads.markdown-image') }}';
const csrfToken = document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '';
const insertTextAtCursor = (textarea, text) => {
const start = textarea.selectionStart ?? textarea.value.length;
const end = textarea.selectionEnd ?? textarea.value.length;
textarea.value = textarea.value.substring(0, start) + text + textarea.value.substring(end);
const insertTextAtCursor = (field, text) => {
if (!(field instanceof HTMLInputElement) && !(field instanceof HTMLTextAreaElement)) {
return;
}
const start = field.selectionStart ?? field.value.length;
const end = field.selectionEnd ?? field.value.length;
field.value = field.value.substring(0, start) + text + field.value.substring(end);
const nextPos = start + text.length;
textarea.selectionStart = nextPos;
textarea.selectionEnd = nextPos;
textarea.focus();
if (typeof field.selectionStart === 'number' && typeof field.selectionEnd === 'number') {
field.selectionStart = nextPos;
field.selectionEnd = nextPos;
}
field.focus();
};
const setButtonLoading = (button, loading) => {
@@ -205,8 +213,9 @@
const attachUploadButton = (button) => {
button.addEventListener('click', function () {
const selector = button.getAttribute('data-target');
const textarea = selector ? document.querySelector(selector) : null;
if (!textarea) {
const targetField = selector ? document.querySelector(selector) : null;
if (!(targetField instanceof HTMLInputElement) && !(targetField instanceof HTMLTextAreaElement)) {
return;
}
@@ -222,7 +231,10 @@
setButtonLoading(button, true);
try {
const payload = await uploadImage(file);
insertTextAtCursor(textarea, buildMarkdownImageText(payload, file.name || 'image'));
const imageText = targetField instanceof HTMLTextAreaElement
? buildMarkdownImageText(payload, file.name || 'image')
: payload.url;
insertTextAtCursor(targetField, imageText);
} catch (_) {
alert('图片上传失败,请稍后重试');
} finally {