From b1de8868289b9d860796eb61821c23ecc2fc63c0 Mon Sep 17 00:00:00 2001 From: zwt13703 Date: Mon, 6 Jul 2026 22:41:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20TinyMCE=20=E5=85=81=E8=AE=B8=20style=20?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=20+=20=E5=AF=B9=E9=BD=90=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E5=88=B0=E6=A0=87=E9=A2=98=E5=92=8C=E5=88=97?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Editor: valid_elements + extended_valid_elements 保留内联 style - docx_to_html: 对齐注入覆盖 p/h1-h6/li 块级元素 --- backend/app/services/document_processor.py | 12 ++++++------ web/src/pages/TemplateEditor.tsx | 2 ++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/backend/app/services/document_processor.py b/backend/app/services/document_processor.py index f540c76..df16857 100644 --- a/backend/app/services/document_processor.py +++ b/backend/app/services/document_processor.py @@ -21,18 +21,18 @@ async def docx_to_html(file_content: bytes) -> str: doc = Document(BytesIO(file_content)) soup = BeautifulSoup(html, "html.parser") paragraphs = doc.paragraphs - html_paras = soup.find_all("p") + html_blocks = soup.find_all(["p", "h1", "h2", "h3", "h4", "h5", "h6", "li"]) for i, para in enumerate(paragraphs): - if i >= len(html_paras): + if i >= len(html_blocks): break if para.alignment and para.alignment in ALIGN_MAP: css = ALIGN_MAP[para.alignment] - existing_style = html_paras[i].get("style", "") + existing = html_blocks[i].get("style", "") styles = f"text-align:{css}" - if existing_style: - styles = existing_style.rstrip(";") + ";" + styles - html_paras[i]["style"] = styles + if existing: + styles = existing.rstrip(";") + ";" + styles + html_blocks[i]["style"] = styles html = str(soup) except Exception: diff --git a/web/src/pages/TemplateEditor.tsx b/web/src/pages/TemplateEditor.tsx index da05ee7..1d079e5 100644 --- a/web/src/pages/TemplateEditor.tsx +++ b/web/src/pages/TemplateEditor.tsx @@ -363,6 +363,8 @@ export default function TemplateEditor() { toolbar: "undo redo | blocks | bold italic forecolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | removeformat | help", content_style: "body { font-family: Arial, sans-serif; font-size: 14px; }", + valid_elements: "*[*]", + extended_valid_elements: "p[style],h1[style],h2[style],h3[style],h4[style],h5[style],h6[style],div[style],span[style],li[style]", }} />