From 735a4947efa7c6c1537a1e60d51028470b54935c Mon Sep 17 00:00:00 2001 From: zpc Date: Wed, 4 Feb 2026 01:51:19 +0800 Subject: [PATCH] 21 --- .../danye/components/DanyeFormDialog.vue | 72 +++++++++++++++---- 1 file changed, 57 insertions(+), 15 deletions(-) diff --git a/server/HoneyBox/src/HoneyBox.Admin/admin-web/src/views/business/danye/components/DanyeFormDialog.vue b/server/HoneyBox/src/HoneyBox.Admin/admin-web/src/views/business/danye/components/DanyeFormDialog.vue index 83b3257a..851f34a9 100644 --- a/server/HoneyBox/src/HoneyBox.Admin/admin-web/src/views/business/danye/components/DanyeFormDialog.vue +++ b/server/HoneyBox/src/HoneyBox.Admin/admin-web/src/views/business/danye/components/DanyeFormDialog.vue @@ -157,6 +157,61 @@ const formRules: FormRules = { ] } +// 清理 HTML 内容,修复可能导致编辑器解析错误的格式问题 +const cleanHtmlContent = (html: string): string => { + if (!html) return '' + + // 修复以分号开头的 style 属性(如 style=";font-family:宋体") + let cleaned = html.replace(/style=";/g, 'style="') + + // 移除空的 style 属性 + cleaned = cleaned.replace(/style=""/g, '') + + // 修复连续的分号 + cleaned = cleaned.replace(/;;+/g, ';') + + // 移除 style 属性开头的分号 + cleaned = cleaned.replace(/style=";\s*/g, 'style="') + + return cleaned +} + +// 安全地设置编辑器内容 +const safeSetEditorContent = (editor: IDomEditor, content: string) => { + if (!editor || editor.isDestroyed) return + + try { + // 清理 HTML 内容 + const cleanedContent = cleanHtmlContent(content) + + // 先清空编辑器 + editor.clear() + + // 使用 setTimeout 确保 clear 操作完成 + setTimeout(() => { + if (!editor.isDestroyed) { + try { + editor.setHtml(cleanedContent) + } catch (e) { + console.error('[DanyeFormDialog] Error setting HTML content:', e) + // 如果设置失败,尝试设置纯文本 + try { + // 提取纯文本内容 + const tempDiv = document.createElement('div') + tempDiv.innerHTML = cleanedContent + const textContent = tempDiv.textContent || tempDiv.innerText || '' + editor.insertText(textContent) + } catch (e2) { + console.error('[DanyeFormDialog] Error setting text content:', e2) + } + } + } + }, 50) + } catch (e) { + console.error('[DanyeFormDialog] Error in safeSetEditorContent:', e) + } +} + // 编辑器创建完成 const handleCreated = (editor: IDomEditor) => { console.log('[DanyeFormDialog] handleCreated called, editor:', editor) @@ -164,17 +219,9 @@ const handleCreated = (editor: IDomEditor) => { editorRef.value = editor // 如果有内容,设置到编辑器 - // 使用 nextTick 确保编辑器完全初始化后再设置内容 if (formData.content) { console.log('[DanyeFormDialog] Setting initial content to editor:', formData.content.substring(0, 100)) - // 先清空编辑器,再设置新内容,避免 operations 历史记录冲突 - editor.clear() - // 使用 setTimeout 确保 clear 操作完成后再设置内容 - setTimeout(() => { - if (!editor.isDestroyed) { - editor.setHtml(formData.content) - } - }, 0) + safeSetEditorContent(editor, formData.content) } } @@ -190,12 +237,7 @@ watch(() => props.modelValue, (visible) => { // 如果编辑器已存在且未销毁,直接设置内容 if (editorRef.value && !editorRef.value.isDestroyed) { - editorRef.value.clear() - setTimeout(() => { - if (editorRef.value && !editorRef.value.isDestroyed) { - editorRef.value.setHtml(formData.content) - } - }, 0) + safeSetEditorContent(editorRef.value, formData.content) } } })