fix: resolve build errors and complete missing files
Fix CSS issues, add missing files, and optimize documentation page. Co-authored-by: null <4804959+fnvtk@users.noreply.github.com>
This commit is contained in:
@@ -1,24 +1,4 @@
|
||||
/**
|
||||
* Word文档生成器
|
||||
* 使用docx库生成专业的Word文档
|
||||
*/
|
||||
|
||||
import {
|
||||
Document,
|
||||
Paragraph,
|
||||
TextRun,
|
||||
HeadingLevel,
|
||||
ImageRun,
|
||||
TableOfContents,
|
||||
PageBreak,
|
||||
AlignmentType,
|
||||
Table,
|
||||
TableRow,
|
||||
TableCell,
|
||||
WidthType,
|
||||
BorderStyle,
|
||||
} from "docx"
|
||||
import { Buffer } from "buffer"
|
||||
import { Document, Packer, Paragraph, ImageRun, TextRun, HeadingLevel } from "docx"
|
||||
|
||||
interface Screenshot {
|
||||
id: string
|
||||
@@ -41,445 +21,190 @@ interface DocumentSettings {
|
||||
orientation: string
|
||||
}
|
||||
|
||||
interface DocumentSection {
|
||||
title: string
|
||||
path: string
|
||||
description: string
|
||||
screenshot: string
|
||||
}
|
||||
|
||||
interface DocumentData {
|
||||
title: string
|
||||
author: string
|
||||
date: string
|
||||
sections: DocumentSection[]
|
||||
}
|
||||
|
||||
/**
|
||||
* 将base64字符串转换为Buffer
|
||||
* @param base64 base64字符串
|
||||
* @returns Buffer
|
||||
*/
|
||||
function base64ToBuffer(base64: string): Buffer {
|
||||
try {
|
||||
// 移除data URL前缀
|
||||
const base64Data = base64.includes("base64,") ? base64.split("base64,")[1] : base64
|
||||
|
||||
// 转换为Buffer
|
||||
return Buffer.from(base64Data, "base64")
|
||||
} catch (error) {
|
||||
console.error("base64转换为Buffer时出错:", error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成Word文档
|
||||
* @param screenshots 截图数据
|
||||
* @param settings 文档设置
|
||||
* @returns 文档的ArrayBuffer
|
||||
*/
|
||||
export async function generateDocx(screenshots: Screenshot[], settings: DocumentSettings): Promise<ArrayBuffer> {
|
||||
console.log("开始生成Word文档...")
|
||||
|
||||
try {
|
||||
// 验证输入数据
|
||||
if (!screenshots || !Array.isArray(screenshots)) {
|
||||
throw new Error("截图数据不能为空或不是数组")
|
||||
}
|
||||
const children: Paragraph[] = []
|
||||
|
||||
if (!settings) {
|
||||
throw new Error("文档设置不能为空")
|
||||
}
|
||||
|
||||
console.log(`文档标题: ${settings.title}`)
|
||||
console.log(`作者: ${settings.author}`)
|
||||
console.log(`描述: ${settings.description}`)
|
||||
console.log(`部分数量: ${screenshots.length}`)
|
||||
|
||||
// 创建文档
|
||||
const doc = new Document({
|
||||
title: settings.title,
|
||||
description: settings.description,
|
||||
creator: settings.author,
|
||||
styles: {
|
||||
paragraphStyles: [
|
||||
{
|
||||
id: "Normal",
|
||||
name: "Normal",
|
||||
run: {
|
||||
size: 24, // 12pt
|
||||
font: "Microsoft YaHei",
|
||||
},
|
||||
paragraph: {
|
||||
spacing: {
|
||||
line: 360, // 1.5倍行距
|
||||
before: 240, // 12pt
|
||||
after: 240, // 12pt
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "Heading1",
|
||||
name: "Heading 1",
|
||||
run: {
|
||||
size: 36, // 18pt
|
||||
bold: true,
|
||||
font: "Microsoft YaHei",
|
||||
},
|
||||
paragraph: {
|
||||
spacing: {
|
||||
before: 480, // 24pt
|
||||
after: 240, // 12pt
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "Heading2",
|
||||
name: "Heading 2",
|
||||
run: {
|
||||
size: 32, // 16pt
|
||||
bold: true,
|
||||
font: "Microsoft YaHei",
|
||||
},
|
||||
paragraph: {
|
||||
spacing: {
|
||||
before: 360, // 18pt
|
||||
after: 240, // 12pt
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "Caption",
|
||||
name: "Caption",
|
||||
run: {
|
||||
size: 20, // 10pt
|
||||
italic: true,
|
||||
font: "Microsoft YaHei",
|
||||
},
|
||||
paragraph: {
|
||||
alignment: AlignmentType.CENTER,
|
||||
spacing: {
|
||||
before: 120, // 6pt
|
||||
after: 240, // 12pt
|
||||
},
|
||||
},
|
||||
},
|
||||
// 添加标题
|
||||
children.push(
|
||||
new Paragraph({
|
||||
children: [
|
||||
new TextRun({
|
||||
text: settings.title,
|
||||
bold: true,
|
||||
size: 32,
|
||||
}),
|
||||
],
|
||||
})
|
||||
heading: HeadingLevel.TITLE,
|
||||
}),
|
||||
)
|
||||
|
||||
// 文档部分
|
||||
const sections = []
|
||||
|
||||
// 封面
|
||||
sections.push({
|
||||
properties: {},
|
||||
children: [
|
||||
// 添加描述
|
||||
if (settings.description) {
|
||||
children.push(
|
||||
new Paragraph({
|
||||
text: "",
|
||||
spacing: {\
|
||||
before: 3000, // 大约页面1/3处
|
||||
},\
|
||||
}),\
|
||||
new Paragraph({\
|
||||
text: settings.title,\
|
||||
heading: HeadingLevel.TITLE,
|
||||
alignment: AlignmentType.CENTER,
|
||||
spacing: {\
|
||||
after: 400,
|
||||
},
|
||||
}),
|
||||
new Paragraph({\
|
||||
text: "",\
|
||||
spacing: {\
|
||||
before: 800,
|
||||
},
|
||||
}),
|
||||
new Paragraph({
|
||||
alignment: AlignmentType.CENTER,\
|
||||
children: [
|
||||
new TextRun({
|
||||
text: \`作者: ${settings.author}`,\
|
||||
size: 24,\
|
||||
}),
|
||||
],
|
||||
}),
|
||||
new Paragraph({
|
||||
alignment: AlignmentType.CENTER,\
|
||||
children: [
|
||||
new TextRun({
|
||||
text: `生成日期: ${new Date().toLocaleString()}`,\
|
||||
text: settings.description,
|
||||
size: 24,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
new Paragraph({
|
||||
text: "",\
|
||||
break: PageBreak.AFTER,
|
||||
}),
|
||||
],
|
||||
})
|
||||
|
||||
// 目录
|
||||
sections.push({
|
||||
properties: {},
|
||||
children: [\
|
||||
new Paragraph({
|
||||
text: "目录",
|
||||
heading: HeadingLevel.HEADING_1,\
|
||||
alignment: AlignmentType.CENTER,
|
||||
}),
|
||||
new TableOfContents("目录", {
|
||||
hyperlink: true,
|
||||
headingStyleRange: "1-3",\
|
||||
}),\
|
||||
new Paragraph({
|
||||
text: "",
|
||||
break: PageBreak.AFTER,
|
||||
}),\
|
||||
],
|
||||
})
|
||||
|
||||
// 正文
|
||||
const contentSection = {
|
||||
properties: {},
|
||||
children: [] as any[],
|
||||
)
|
||||
}
|
||||
|
||||
// 添加简介
|
||||
contentSection.children.push(\
|
||||
// 添加生成信息
|
||||
if (settings.includeTimestamp) {
|
||||
children.push(
|
||||
new Paragraph({
|
||||
children: [
|
||||
new TextRun({
|
||||
text: `生成时间: ${new Date().toLocaleString()}`,
|
||||
size: 20,
|
||||
italics: true,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
children.push(
|
||||
new Paragraph({
|
||||
text: "1. 简介",
|
||||
heading: HeadingLevel.HEADING_1,
|
||||
}),
|
||||
new Paragraph({
|
||||
text: "本文档由文档生成工具自动生成,包含应用程序的所有主要页面截图和功能说明。",
|
||||
}),
|
||||
new Paragraph({
|
||||
text: "文档目的是帮助用户了解系统功能和使用方法,为系统管理员和最终用户提供参考。",
|
||||
}),\
|
||||
new Paragraph({
|
||||
text: "",
|
||||
children: [
|
||||
new TextRun({
|
||||
text: `作者: ${settings.author}`,
|
||||
size: 20,
|
||||
italics: true,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
)
|
||||
|
||||
// 添加页面内容
|
||||
contentSection.children.push(
|
||||
// 添加分隔符
|
||||
children.push(
|
||||
new Paragraph({
|
||||
text: "2. 系统功能",
|
||||
heading: HeadingLevel.HEADING_1,
|
||||
children: [
|
||||
new TextRun({
|
||||
text: "─".repeat(50),
|
||||
size: 20,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
)
|
||||
|
||||
// 处理每个部分
|
||||
console.log("开始处理文档部分...")
|
||||
|
||||
// 使用for循环而不是forEach,以便更好地处理错误
|
||||
for (let i = 0; i < screenshots.length; i++) {
|
||||
try {
|
||||
const screenshot = screenshots[i]
|
||||
console.log(\`处理部分 ${i + 1}/${screenshots.length}: ${screenshot.name}`)
|
||||
|
||||
// 添加标题
|
||||
contentSection.children.push(
|
||||
new Paragraph({
|
||||
text: `2.${i + 1} ${screenshot.name}`,
|
||||
heading: HeadingLevel.HEADING_2,
|
||||
}),
|
||||
)
|
||||
|
||||
// 添加页面URL和截图时间
|
||||
if (settings.includePageUrls) {
|
||||
contentSection.children.push(
|
||||
new Paragraph({
|
||||
text: `页面URL: ${screenshot.url}`,
|
||||
// 添加每个截图
|
||||
for (const screenshot of screenshots) {
|
||||
// 页面标题
|
||||
children.push(
|
||||
new Paragraph({
|
||||
children: [
|
||||
new TextRun({
|
||||
text: screenshot.name,
|
||||
bold: true,
|
||||
size: 28,
|
||||
}),
|
||||
)
|
||||
}
|
||||
],
|
||||
heading: HeadingLevel.HEADING_1,
|
||||
}),
|
||||
)
|
||||
|
||||
if (settings.includeTimestamp) {
|
||||
contentSection.children.push(
|
||||
new Paragraph({
|
||||
text: `截图时间: ${screenshot.timestamp.toLocaleString()}`,
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
// 添加状态
|
||||
contentSection.children.push(
|
||||
// 页面URL
|
||||
if (settings.includePageUrls) {
|
||||
children.push(
|
||||
new Paragraph({
|
||||
text: `状态: ${screenshot.status}`,
|
||||
}),
|
||||
)
|
||||
|
||||
// 添加截图
|
||||
try {
|
||||
if (screenshot.dataUrl && screenshot.dataUrl.startsWith("data:image/")) {
|
||||
console.log(`处理截图: ${screenshot.name}`)
|
||||
|
||||
// 从base64数据URL中提取图像数据
|
||||
const base64Data = screenshot.dataUrl.split(",")[1]
|
||||
if (!base64Data) {
|
||||
throw new Error("无效的base64数据")
|
||||
}
|
||||
|
||||
// 将base64转换为二进制数据
|
||||
const imageBuffer = base64ToBuffer(screenshot.dataUrl)
|
||||
|
||||
// 添加图像
|
||||
contentSection.children.push(
|
||||
new Paragraph({
|
||||
children: [
|
||||
new ImageRun({
|
||||
data: imageBuffer,
|
||||
transformation: {
|
||||
width: 600,
|
||||
height: 400,
|
||||
},
|
||||
}),
|
||||
],
|
||||
alignment: AlignmentType.CENTER,
|
||||
children: [
|
||||
new TextRun({
|
||||
text: `页面地址: ${screenshot.url}`,
|
||||
size: 20,
|
||||
color: "666666",
|
||||
}),
|
||||
new Paragraph({
|
||||
text: `图 ${i + 1}: ${screenshot.name} 页面截图`,
|
||||
style: "Caption",
|
||||
}),
|
||||
new Paragraph({
|
||||
text: "",
|
||||
}),
|
||||
)
|
||||
} else {
|
||||
console.warn(`部分 ${i + 1} 缺少有效的截图`)
|
||||
contentSection.children.push(
|
||||
new Paragraph({
|
||||
text: "[截图不可用]",
|
||||
alignment: AlignmentType.CENTER,
|
||||
}),
|
||||
new Paragraph({
|
||||
text: "",
|
||||
}),
|
||||
)
|
||||
}
|
||||
} catch (imageError) {
|
||||
console.error(`处理部分 ${i + 1} 的截图时出错:`, imageError)
|
||||
contentSection.children.push(
|
||||
new Paragraph({
|
||||
text: "[处理截图时出错]",
|
||||
alignment: AlignmentType.CENTER,
|
||||
}),
|
||||
new Paragraph({
|
||||
text: "",
|
||||
}),
|
||||
)
|
||||
}
|
||||
} catch (sectionError) {
|
||||
console.error(`处理部分 ${i + 1} 时出错:`, sectionError)
|
||||
contentSection.children.push(
|
||||
new Paragraph({
|
||||
text: `[处理部分 ${i + 1} 时出错: ${sectionError instanceof Error ? sectionError.message : String(sectionError)}]`,
|
||||
alignment: AlignmentType.CENTER,
|
||||
}),
|
||||
new Paragraph({
|
||||
text: "",
|
||||
],
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
// 截图时间
|
||||
if (settings.includeTimestamp) {
|
||||
children.push(
|
||||
new Paragraph({
|
||||
children: [
|
||||
new TextRun({
|
||||
text: `截图时间: ${screenshot.timestamp.toLocaleString()}`,
|
||||
size: 20,
|
||||
color: "666666",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
// 添加截图
|
||||
if (screenshot.dataUrl) {
|
||||
try {
|
||||
// 将 dataUrl 转换为 ArrayBuffer
|
||||
const base64Data = screenshot.dataUrl.split(",")[1]
|
||||
const binaryString = atob(base64Data)
|
||||
const bytes = new Uint8Array(binaryString.length)
|
||||
for (let i = 0; i < binaryString.length; i++) {
|
||||
bytes[i] = binaryString.charCodeAt(i)
|
||||
}
|
||||
|
||||
children.push(
|
||||
new Paragraph({
|
||||
children: [
|
||||
new ImageRun({
|
||||
data: bytes,
|
||||
transformation: {
|
||||
width: 600,
|
||||
height: 400,
|
||||
},
|
||||
}),
|
||||
],
|
||||
}),
|
||||
)
|
||||
} catch (error) {
|
||||
console.error("添加图片失败:", error)
|
||||
children.push(
|
||||
new Paragraph({
|
||||
children: [
|
||||
new TextRun({
|
||||
text: "[图片加载失败]",
|
||||
color: "FF0000",
|
||||
italics: true,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// 添加分隔符
|
||||
children.push(
|
||||
new Paragraph({
|
||||
children: [
|
||||
new TextRun({
|
||||
text: "",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
// 添加附录
|
||||
contentSection.children.push(
|
||||
new Paragraph({
|
||||
text: "3. 附录",
|
||||
heading: HeadingLevel.HEADING_1,
|
||||
}),
|
||||
new Paragraph({
|
||||
text: "3.1 文档信息",
|
||||
heading: HeadingLevel.HEADING_2,
|
||||
}),
|
||||
)
|
||||
|
||||
// 创建文档信息表格
|
||||
const infoTable = new Table({
|
||||
width: {
|
||||
size: 100,
|
||||
type: WidthType.PERCENTAGE,
|
||||
},
|
||||
borders: {
|
||||
top: { style: BorderStyle.SINGLE, size: 1, color: "auto" },
|
||||
bottom: { style: BorderStyle.SINGLE, size: 1, color: "auto" },
|
||||
left: { style: BorderStyle.SINGLE, size: 1, color: "auto" },
|
||||
right: { style: BorderStyle.SINGLE, size: 1, color: "auto" },
|
||||
insideHorizontal: { style: BorderStyle.SINGLE, size: 1, color: "auto" },
|
||||
insideVertical: { style: BorderStyle.SINGLE, size: 1, color: "auto" },
|
||||
},
|
||||
rows: [
|
||||
new TableRow({
|
||||
children: [
|
||||
new TableCell({
|
||||
width: {
|
||||
size: 30,
|
||||
type: WidthType.PERCENTAGE,
|
||||
},
|
||||
children: [new Paragraph("文档标题")],
|
||||
}),
|
||||
new TableCell({
|
||||
width: {
|
||||
size: 70,
|
||||
type: WidthType.PERCENTAGE,
|
||||
},
|
||||
children: [new Paragraph(settings.title)],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
new TableRow({
|
||||
children: [
|
||||
new TableCell({
|
||||
children: [new Paragraph("作者")],
|
||||
}),
|
||||
new TableCell({
|
||||
children: [new Paragraph(settings.author)],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
new TableRow({
|
||||
children: [
|
||||
new TableCell({
|
||||
children: [new Paragraph("生成日期")],
|
||||
}),
|
||||
new TableCell({
|
||||
children: [new Paragraph(new Date().toLocaleString())],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
new TableRow({
|
||||
children: [
|
||||
new TableCell({
|
||||
children: [new Paragraph("页面数量")],
|
||||
}),
|
||||
new TableCell({
|
||||
children: [new Paragraph(String(screenshots.length))],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
// 创建文档
|
||||
const doc = new Document({
|
||||
sections: [
|
||||
{
|
||||
properties: {},
|
||||
children: children,
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
contentSection.children.push(infoTable)
|
||||
|
||||
// 添加内容部分到文档
|
||||
doc.addSection({
|
||||
children: sections[0].children.concat(sections[1].children, contentSection.children),
|
||||
})
|
||||
|
||||
console.log("文档生成完成,准备导出...")
|
||||
|
||||
// 生成blob
|
||||
const buffer = await doc.save()
|
||||
console.log("文档生成完成")
|
||||
|
||||
// 生成文档
|
||||
const buffer = await Packer.toBuffer(doc)
|
||||
return buffer
|
||||
} catch (error) {
|
||||
console.error("生成Word文档时出错:", error)
|
||||
throw error
|
||||
console.error("生成文档失败:", error)
|
||||
throw new Error(`文档生成失败: ${error instanceof Error ? error.message : "未知错误"}`)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user