// 引入工具 import PizZip from 'pizzip'; import Docxtemplater from 'docxtemplater'; import JSZipUtils from 'jszip-utils'; import { saveAs } from 'file-saver'; // 加载 .docx 模板文件 function loadFile(url, callback) { JSZipUtils.getBinaryContent(url, callback); } // 下载生成的文档 export function download(file, name) { saveAs(file, name); } // 处理富文本,提取段落和缩进信息 function processRichText(html) { if (!html) return ''; // 将HTML字符串转换为DOM对象 const parser = new DOMParser(); const doc = parser.parseFromString(html, 'text/html'); let result = []; // 处理普通段落 const paragraphs = doc.querySelectorAll('p'); paragraphs.forEach(p => { const style = p.getAttribute('style') || ''; const indentMatch = style.match(/text-indent:\s*(\d+)pt/); const indent = indentMatch ? parseInt(indentMatch[1]) / 24 : 0; const text = p.textContent.trim(); if (text) { const indentStr = indent > 0 ? ' '.repeat(indent) : ''; result.push(indentStr + text); } }); // 处理列表(ul/li) const lists = doc.querySelectorAll('ul'); lists.forEach(ul => { const lis = ul.querySelectorAll('li'); lis.forEach(li => { // 计算缩进层级 let parent = li.parentElement; let indentLevel = 0; while (parent && parent !== ul) { if (parent.tagName === 'UL') indentLevel++; parent = parent.parentElement; } const text = li.textContent.trim(); if (text) { // 使用不同符号表示不同层级 const bullets = ['▪', '•', '▫', '◦']; const bullet = bullets[Math.min(indentLevel, bullets.length - 1)]; const indentStr = ' '.repeat(indentLevel); result.push(indentStr + bullet + ' ' + text); } }); }); return result.join('\n\n'); // 用两个换行符分隔段落 } function convertTreeToHtml(data) { let html = ''; function buildList(items) { let listHtml = '