From 82b17e65c52b54b49c4d920a7802b0af6b9dea06 Mon Sep 17 00:00:00 2001 From: superz3 Date: Tue, 9 Jul 2024 15:48:17 +0800 Subject: [PATCH 1/3] feat: Make PDFRender Support non-latin characters --- package-lock.json | 19 +++++++++++++++++++ package.json | 1 + .../pdf/components/pages/PDFPages.tsx | 8 ++++++++ .../pdf/components/pages/PDFSinglePage.tsx | 7 ++++++- tsconfig.json | 1 + vite.config.ts | 19 ++++++++++++++++++- 6 files changed, 53 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8ff648f9..144ef702 100644 --- a/package-lock.json +++ b/package-lock.json @@ -54,6 +54,7 @@ "vite": "^5.2.12", "vite-plugin-dts": "^3.9.1", "vite-plugin-node-polyfills": "^0.22.0", + "vite-plugin-static-copy": "^1.0.6", "vitest": "^1.6.0", "vitest-fetch-mock": "^0.2.2", "webpack": "^5.91.0" @@ -23772,6 +23773,24 @@ "vite": "^2.0.0 || ^3.0.0 || ^4.0.0 || ^5.0.0" } }, + "node_modules/vite-plugin-static-copy": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/vite-plugin-static-copy/-/vite-plugin-static-copy-1.0.6.tgz", + "integrity": "sha512-3uSvsMwDVFZRitqoWHj0t4137Kz7UynnJeq1EZlRW7e25h2068fyIZX4ORCCOAkfp1FklGxJNVJBkBOD+PZIew==", + "dev": true, + "dependencies": { + "chokidar": "^3.5.3", + "fast-glob": "^3.2.11", + "fs-extra": "^11.1.0", + "picocolors": "^1.0.0" + }, + "engines": { + "node": "^18.0.0 || >=20.0.0" + }, + "peerDependencies": { + "vite": "^5.0.0" + } + }, "node_modules/vitest": { "version": "1.6.0", "resolved": "https://registry.npmjs.org/vitest/-/vitest-1.6.0.tgz", diff --git a/package.json b/package.json index 605193bb..171f3c5f 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,7 @@ "vite": "^5.2.12", "vite-plugin-dts": "^3.9.1", "vite-plugin-node-polyfills": "^0.22.0", + "vite-plugin-static-copy": "^1.0.6", "vitest": "^1.6.0", "vitest-fetch-mock": "^0.2.2", "webpack": "^5.91.0" diff --git a/src/renderers/pdf/components/pages/PDFPages.tsx b/src/renderers/pdf/components/pages/PDFPages.tsx index 2520149e..dd4a4595 100644 --- a/src/renderers/pdf/components/pages/PDFPages.tsx +++ b/src/renderers/pdf/components/pages/PDFPages.tsx @@ -8,6 +8,13 @@ import { setNumPages } from "../../state/actions"; import { initialPDFState } from "../../state/reducer"; import { PDFAllPages } from "./PDFAllPages"; import PDFSinglePage from "./PDFSinglePage"; +import { pdfjs } from "react-pdf"; +import "react-pdf/dist/Page/TextLayer.css"; +import "react-pdf/dist/Page/AnnotationLayer.css"; + +const options = { + cMapUrl: `https://unpkg.com/pdfjs-dist@${pdfjs.version}/cmaps/`, +}; const PDFPages: FC<{}> = () => { const { @@ -26,6 +33,7 @@ const PDFPages: FC<{}> = () => { return ( dispatch(setNumPages(numPages))} loading={{t("pdfPluginLoading")}} diff --git a/src/renderers/pdf/components/pages/PDFSinglePage.tsx b/src/renderers/pdf/components/pages/PDFSinglePage.tsx index 38334790..5c8ea32d 100644 --- a/src/renderers/pdf/components/pages/PDFSinglePage.tsx +++ b/src/renderers/pdf/components/pages/PDFSinglePage.tsx @@ -1,9 +1,14 @@ import React, { FC, useContext } from "react"; -import { Page } from "react-pdf"; +import { Document, Page } from "react-pdf"; import styled from "styled-components"; import { IStyledProps } from "../../../.."; import { useTranslation } from "../../../../hooks/useTranslation"; import { PDFContext } from "../../state"; +import { pdfjs } from "react-pdf"; + +const options = { + cMapUrl: `https://unpkg.com/pdfjs-dist@${pdfjs.version}/cmaps/`, +}; interface Props { pageNum?: number; diff --git a/tsconfig.json b/tsconfig.json index bc40bf4b..613bbe80 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,6 +7,7 @@ "skipLibCheck": true, "noEmit": true, "resolveJsonModule": true, + "module": "Preserve", "types": ["vitest/globals"] }, "exclude": ["node_modules", "use-cases"] diff --git a/vite.config.ts b/vite.config.ts index 965068ea..c7875bb9 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,7 +1,16 @@ -import { defineConfig } from "vitest/config"; +import path from 'node:path'; +import { createRequire } from 'node:module'; + +import { defineConfig, normalizePath } from 'vite'; import dsv from "@rollup/plugin-dsv"; import dts from "vite-plugin-dts"; import { nodePolyfills } from "vite-plugin-node-polyfills"; +import { viteStaticCopy } from 'vite-plugin-static-copy'; + +const require = createRequire(import.meta.url); + +const pdfjsDistPath = path.dirname(require.resolve('pdfjs-dist/package.json')); +const cMapsDir = normalizePath(path.join(pdfjsDistPath, 'cmaps')); export default defineConfig({ plugins: [ @@ -10,6 +19,14 @@ export default defineConfig({ }), dsv(), nodePolyfills(), + viteStaticCopy({ + targets: [ + { + src: cMapsDir, + dest: '', + }, + ], + }), ], build: { lib: { From 0a3a5aa60b5214ac0c620cff033c9d3a42d2d032 Mon Sep 17 00:00:00 2001 From: superz3 Date: Tue, 9 Jul 2024 16:06:27 +0800 Subject: [PATCH 2/3] feat: remove useless code --- src/renderers/pdf/components/pages/PDFSinglePage.tsx | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/renderers/pdf/components/pages/PDFSinglePage.tsx b/src/renderers/pdf/components/pages/PDFSinglePage.tsx index 5c8ea32d..7864a9eb 100644 --- a/src/renderers/pdf/components/pages/PDFSinglePage.tsx +++ b/src/renderers/pdf/components/pages/PDFSinglePage.tsx @@ -4,11 +4,6 @@ import styled from "styled-components"; import { IStyledProps } from "../../../.."; import { useTranslation } from "../../../../hooks/useTranslation"; import { PDFContext } from "../../state"; -import { pdfjs } from "react-pdf"; - -const options = { - cMapUrl: `https://unpkg.com/pdfjs-dist@${pdfjs.version}/cmaps/`, -}; interface Props { pageNum?: number; From ee42486c10517f2dc7b827a38d2684bc3b65e6a6 Mon Sep 17 00:00:00 2001 From: superz3 Date: Tue, 9 Jul 2024 16:08:35 +0800 Subject: [PATCH 3/3] feat: remove useless import --- src/renderers/pdf/components/pages/PDFSinglePage.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderers/pdf/components/pages/PDFSinglePage.tsx b/src/renderers/pdf/components/pages/PDFSinglePage.tsx index 7864a9eb..38334790 100644 --- a/src/renderers/pdf/components/pages/PDFSinglePage.tsx +++ b/src/renderers/pdf/components/pages/PDFSinglePage.tsx @@ -1,5 +1,5 @@ import React, { FC, useContext } from "react"; -import { Document, Page } from "react-pdf"; +import { Page } from "react-pdf"; import styled from "styled-components"; import { IStyledProps } from "../../../.."; import { useTranslation } from "../../../../hooks/useTranslation";