feat: add export settings for Word document generation

- Add export configuration (untranslated text color, page orientation)
- Add second page header option for multi-page exports
- Integrate export settings into main export flow
- Separate export settings page with visual preview
This commit is contained in:
2026-04-12 23:54:00 +08:00
parent a249fb038c
commit 030fce2813
5 changed files with 296 additions and 24 deletions
+40 -8
View File
@@ -1,6 +1,6 @@
"use client";
import {useState, useCallback} from "react";
import {useState, useCallback, useEffect} from "react";
import {
Upload,
Sun,
@@ -116,6 +116,31 @@ export default function RecipeTranslationPage() {
const [data, setData] = useState<WeeklyMenu | null>(null);
const [error, setError] = useState<string | null>(null);
const [fileName, setFileName] = useState<string>("");
const [exportConfig, setExportConfig] = useState<{
untranslatedColor: string;
secondPageHeader: boolean;
orientation: "portrait" | "landscape";
}>({
untranslatedColor: "CC0000",
secondPageHeader: false,
orientation: "landscape",
});
// 加载导出配置
useEffect(() => {
fetch("/api/config")
.then((res) => res.json())
.then((result) => {
if (result.success && result.data?.export) {
setExportConfig({
untranslatedColor: result.data.export.untranslatedColor || "CC0000",
secondPageHeader: result.data.export.secondPageHeader || false,
orientation: result.data.export.orientation || "landscape",
});
}
})
.catch(console.error);
}, []);
// 收集所有未翻译的菜品名称
const getAllDishNames = useCallback((menu: WeeklyMenu): string[] => {
@@ -195,7 +220,10 @@ export default function RecipeTranslationPage() {
if (!data) return;
try {
// 1. 收集所有需要翻译的内容(星期、餐食类型、菜品)
// 1. 获取导出设置
const { untranslatedColor, secondPageHeader, orientation } = exportConfig;
// 2. 收集所有需要翻译的内容(星期、餐食类型、菜品)
const allNames: string[] = [];
// 收集星期名称(使用实际数据中的星期)
@@ -321,7 +349,7 @@ export default function RecipeTranslationPage() {
} else {
cellContent.push(
new Paragraph({
children: [new TextRun({text: zhText, color: "CC0000"})],
children: [new TextRun({text: zhText, color: untranslatedColor})],
})
);
}
@@ -340,16 +368,20 @@ export default function RecipeTranslationPage() {
tableRows.push(new TableRow({children: dayCells}));
}
// 5. 创建文档 - 横向 A4
// 5. 创建文档 - 根据设置选择页面方向
const isLandscape = orientation === "landscape";
const pageWidth = isLandscape ? 11906 : 16838;
const pageHeight = isLandscape ? 16838 : 11906;
const doc = new Document({
sections: [
{
properties: {
page: {
size: {
orientation: PageOrientation.LANDSCAPE,
width: 11906,
height: 16838,
orientation: isLandscape ? PageOrientation.LANDSCAPE : PageOrientation.PORTRAIT,
width: pageWidth,
height: pageHeight,
},
margin: {
top: 700, // 约1.27厘米
@@ -395,7 +427,7 @@ export default function RecipeTranslationPage() {
console.error("Export error:", err);
alert("导出失败,请重试");
}
}, [data, fileName]);
}, [data, fileName, exportConfig]);
const handleFileUpload = useCallback(
async (file: File) => {