自定义打印内容
toggle("translation")}
+ id="border"
+ checked={opts.border}
+ onCheckedChange={() => toggle("border")}
/>
-
+
toggle("py")}
/>
@@ -39,11 +57,11 @@ export const ToggleOption = (props: {
toggle("border")}
+ id="translation"
+ checked={opts.translation}
+ onCheckedChange={() => toggle("translation")}
/>
-
+
);
diff --git a/src/app/tools/print/layout.tsx b/src/app/tools/print/layout.tsx
index 45dd6d9d..9ca46cd4 100644
--- a/src/app/tools/print/layout.tsx
+++ b/src/app/tools/print/layout.tsx
@@ -1,26 +1,14 @@
-import Root from "../../root";
+import { Aside } from "./components/aside";
-export default function RootLayout({
+export default async function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
-
- 诗词打印、导出为PDF工具
-
-
- >
- }
- >
- {children}
-
+
);
}
diff --git a/src/app/tools/print/page.tsx b/src/app/tools/print/page.tsx
index 4281223a..b87241a0 100644
--- a/src/app/tools/print/page.tsx
+++ b/src/app/tools/print/page.tsx
@@ -1,232 +1,40 @@
-"use client";
+import { api } from "~/trpc/server";
+import PreviewPrint from "./components/preview-print";
+import { cache } from "react";
-import { chunk } from "lodash-es";
-import { useRef, useState } from "react";
-import { useReactToPrint } from "react-to-print";
-
-import { Button } from "~/components/ui/button";
-import { type Locale } from "~/dictionaries";
-import { api } from "~/trpc/react";
-import { cn } from "~/utils";
-import { type Options, ToggleOption } from "./toggle-option";
-import Link from "next/link";
-
-const Row = ({
- text,
- align = "center",
- className,
- border,
-}: {
- text: string;
- align?: "left" | "right" | "center";
- className?: string;
- border?: boolean;
-}) => {
- const num = 12;
- const left = Math.floor((num - text.length) / 2) + text.length;
-
- let data = text.padStart(left).padEnd(num).split("");
- if (align === "right") {
- data = text.padStart(num).split("");
- }
-
- if (align === "left") {
- data = text.padEnd(num).split("");
- }
-
- return (
-
- {data.map((item, index) => (
-
- {item}
-
- ))}
-
- );
-};
-
-const ChoosePoem = () => {
- return (
-
- );
-};
-
-const PyRow = ({
- py,
- className,
- border,
- align,
-}: {
- py: string;
- className?: string;
- align?: "left" | "right" | "center";
- border?: boolean;
-}) => {
- const num = 12;
- const arr = py.split(" ");
-
- let left = Math.floor((num - arr.length) / 2);
-
- if (align === "right") {
- left = num - arr.length;
- }
-
- const data = new Array(12).fill("").map((_, index) => {
- return index < left ? "" : arr[index - left];
- });
-
- return (
-
- {data.map((item, index) => (
-
- {border && (
- <>
-
-
- >
- )}
-
{item}
-
- ))}
-
- );
-};
-
-export default function PrintPage({
- searchParams,
-}: {
- searchParams: { id: string; lang: Locale };
-}) {
- const componentRef = useRef
(null);
- const handlePrint = useReactToPrint({
- content: () => componentRef.current,
- pageStyle: `padding:24px`,
- });
+interface Props {
+ searchParams: { id: string };
+}
- const [opts, setOpts] = useState({
- translation: true,
- py: false,
- border: true,
- });
+const getItem = cache(async ({ searchParams }: Props) => {
+ if (searchParams.id === "null" || !searchParams.id) return null;
- const { data: poem } = api.poem.findById.useQuery({
+ return api.poem.findById.query({
id: Number(searchParams.id),
- lang: searchParams.lang,
});
+});
- if (!poem) return ;
-
- const title = poem.title;
- const author = `${poem.author.dynasty}·${poem.author.name}`;
+export async function generateMetadata(props: Props) {
+ const poem = await getItem(props);
- const content = poem.content
- .replaceAll("\n", "")
- .match(/[^。|!|?|,|;]+[。|!|?|,|;]+/g);
-
- if (!content) return ;
-
- const translation = chunk(
- poem.translation?.replaceAll("\n", "").split(""),
- 12,
- );
-
- const arr = [title, author, ...content];
- const py = [
- poem.titlePinYin,
- poem.author.namePinYin,
- ...(poem.contentPinYin ?? "").split("."),
- ];
-
- return (
- <>
-
-
-
+ return {
+ title: `诗词《${poem.title}》打印`,
+ description: `诗词《${poem.title}》作者${poem.author.name},支持在线打印诗词田字格、内容、译文、拼音。允许你自由组合打印方式。`,
+ };
+}
-
-
-
- {arr.map((item, index) => (
-
- ))}
-
+export default async function Page({ searchParams }: Props) {
+ const poem = await getItem({ searchParams });
- {opts.translation && (
-
- 译文
- aspoem.com | 现代化中国诗词学习网站
-
- )}
+ if (!poem) return null;
- {opts.translation &&
- translation.map((item) =>
- Row({
- text: item.join(""),
- border: opts.border,
- align: "left",
- className: "h-20 w-20",
- }),
- )}
-
-
-
- >
- );
+ return ;
}
diff --git a/src/dictionaries/zh-Hans.json b/src/dictionaries/zh-Hans.json
index 085a5175..72156739 100644
--- a/src/dictionaries/zh-Hans.json
+++ b/src/dictionaries/zh-Hans.json
@@ -33,7 +33,8 @@
"contact": "联系方式",
"theme": "主题",
"language": "语言",
- "feedback": "留言"
+ "feedback": "留言",
+ "print": "打印"
},
"home": {
"poem": "推荐",
diff --git a/src/dictionaries/zh-Hant.json b/src/dictionaries/zh-Hant.json
index 9e96f0eb..8db44b26 100644
--- a/src/dictionaries/zh-Hant.json
+++ b/src/dictionaries/zh-Hant.json
@@ -33,7 +33,8 @@
"contact": "聯繫方式",
"theme": "主題",
"language": "語言",
- "feedback": "留言"
+ "feedback": "留言",
+ "print": "打印"
},
"home": {
"poem": "推薦",
diff --git a/src/server/api/routers/poem.ts b/src/server/api/routers/poem.ts
index c5063799..1a5416eb 100644
--- a/src/server/api/routers/poem.ts
+++ b/src/server/api/routers/poem.ts
@@ -90,7 +90,7 @@ export const poemRouter = createTRPCRouter({
),
search: publicProcedure
- .input(z.string().optional())
+ .input(z.string().default(""))
.query(({ input, ctx }) => {
return ctx.db.poem.findMany({
where: {