@lamberl-lee/file-preview 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/COPYING +674 -0
- package/LICENSE +165 -0
- package/README.md +165 -0
- package/dist/AudioPreview.d.ts +9 -0
- package/dist/AudioPreview.js +29 -0
- package/dist/AudioPreview.js.map +1 -0
- package/dist/CodePreview.d.ts +10 -0
- package/dist/CodePreview.js +121 -0
- package/dist/CodePreview.js.map +1 -0
- package/dist/CsvPreview.d.ts +9 -0
- package/dist/CsvPreview.js +117 -0
- package/dist/CsvPreview.js.map +1 -0
- package/dist/DocxPreview.d.ts +11 -0
- package/dist/DocxPreview.js +89 -0
- package/dist/DocxPreview.js.map +1 -0
- package/dist/EpubPreview.d.ts +9 -0
- package/dist/EpubPreview.js +693 -0
- package/dist/EpubPreview.js.map +1 -0
- package/dist/HtmlPreview.d.ts +9 -0
- package/dist/HtmlPreview.js +60 -0
- package/dist/HtmlPreview.js.map +1 -0
- package/dist/ImagePreview.d.ts +9 -0
- package/dist/ImagePreview.js +44 -0
- package/dist/ImagePreview.js.map +1 -0
- package/dist/LargeFileGate.d.ts +12 -0
- package/dist/LargeFileGate.js +88 -0
- package/dist/LargeFileGate.js.map +1 -0
- package/dist/MarkdownPreview.d.ts +8 -0
- package/dist/MarkdownPreview.js +140 -0
- package/dist/MarkdownPreview.js.map +1 -0
- package/dist/PdfPreview.d.ts +11 -0
- package/dist/PdfPreview.js +206 -0
- package/dist/PdfPreview.js.map +1 -0
- package/dist/PlainTextLargePreview.d.ts +9 -0
- package/dist/PlainTextLargePreview.js +62 -0
- package/dist/PlainTextLargePreview.js.map +1 -0
- package/dist/PluginPreviewRenderer.d.ts +13 -0
- package/dist/PluginPreviewRenderer.js +89 -0
- package/dist/PluginPreviewRenderer.js.map +1 -0
- package/dist/PptxPreview.d.ts +16 -0
- package/dist/PptxPreview.js +376 -0
- package/dist/PptxPreview.js.map +1 -0
- package/dist/PreviewErrorBoundary.d.ts +29 -0
- package/dist/PreviewErrorBoundary.js +53 -0
- package/dist/PreviewErrorBoundary.js.map +1 -0
- package/dist/PreviewFallback.d.ts +18 -0
- package/dist/PreviewFallback.js +143 -0
- package/dist/PreviewFallback.js.map +1 -0
- package/dist/PreviewLoading.d.ts +8 -0
- package/dist/PreviewLoading.js +14 -0
- package/dist/PreviewLoading.js.map +1 -0
- package/dist/RtfPreview.d.ts +10 -0
- package/dist/RtfPreview.js +240 -0
- package/dist/RtfPreview.js.map +1 -0
- package/dist/ShikiSourceView.d.ts +11 -0
- package/dist/ShikiSourceView.js +112 -0
- package/dist/ShikiSourceView.js.map +1 -0
- package/dist/SvgPreview.d.ts +9 -0
- package/dist/SvgPreview.js +89 -0
- package/dist/SvgPreview.js.map +1 -0
- package/dist/TextPreview.d.ts +9 -0
- package/dist/TextPreview.js +9 -0
- package/dist/TextPreview.js.map +1 -0
- package/dist/VideoPreview.d.ts +9 -0
- package/dist/VideoPreview.js +18 -0
- package/dist/VideoPreview.js.map +1 -0
- package/dist/XlsxPreview.d.ts +12 -0
- package/dist/XlsxPreview.js +856 -0
- package/dist/XlsxPreview.js.map +1 -0
- package/dist/ZipPreview.d.ts +9 -0
- package/dist/ZipPreview.js +153 -0
- package/dist/ZipPreview.js.map +1 -0
- package/dist/core/binary.d.ts +10 -0
- package/dist/core/binary.js +27 -0
- package/dist/core/binary.js.map +1 -0
- package/dist/core/config.d.ts +17 -0
- package/dist/core/config.js +19 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/download.d.ts +5 -0
- package/dist/core/download.js +20 -0
- package/dist/core/download.js.map +1 -0
- package/dist/core/i18n.d.ts +101 -0
- package/dist/core/i18n.js +200 -0
- package/dist/core/i18n.js.map +1 -0
- package/dist/core/plugin.d.ts +38 -0
- package/dist/core/plugin.js +46 -0
- package/dist/core/plugin.js.map +1 -0
- package/dist/core/registry.d.ts +13 -0
- package/dist/core/registry.js +33 -0
- package/dist/core/registry.js.map +1 -0
- package/dist/core/source.d.ts +14 -0
- package/dist/core/source.js +131 -0
- package/dist/core/source.js.map +1 -0
- package/dist/core/types.d.ts +88 -0
- package/dist/core/types.js +27 -0
- package/dist/core/types.js.map +1 -0
- package/dist/hooks/useObjectUrlFromSource.d.ts +9 -0
- package/dist/hooks/useObjectUrlFromSource.js +50 -0
- package/dist/hooks/useObjectUrlFromSource.js.map +1 -0
- package/dist/hooks/useSourceBase64.d.ts +10 -0
- package/dist/hooks/useSourceBase64.js +32 -0
- package/dist/hooks/useSourceBase64.js.map +1 -0
- package/dist/hooks/useSourceText.d.ts +10 -0
- package/dist/hooks/useSourceText.js +32 -0
- package/dist/hooks/useSourceText.js.map +1 -0
- package/dist/icons.d.ts +44 -0
- package/dist/icons.js +248 -0
- package/dist/icons.js.map +1 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +147 -0
- package/dist/index.js.map +1 -0
- package/dist/limits.d.ts +26 -0
- package/dist/limits.js +45 -0
- package/dist/limits.js.map +1 -0
- package/dist/performance-limits.d.ts +27 -0
- package/dist/performance-limits.js +54 -0
- package/dist/performance-limits.js.map +1 -0
- package/dist/plugins/audio-plugin.d.ts +7 -0
- package/dist/plugins/audio-plugin.js +11 -0
- package/dist/plugins/audio-plugin.js.map +1 -0
- package/dist/plugins/builtin-plugins.d.ts +9 -0
- package/dist/plugins/builtin-plugins.js +43 -0
- package/dist/plugins/builtin-plugins.js.map +1 -0
- package/dist/plugins/csv-plugin.d.ts +7 -0
- package/dist/plugins/csv-plugin.js +11 -0
- package/dist/plugins/csv-plugin.js.map +1 -0
- package/dist/plugins/docx-plugin.d.ts +7 -0
- package/dist/plugins/docx-plugin.js +15 -0
- package/dist/plugins/docx-plugin.js.map +1 -0
- package/dist/plugins/epub-plugin.d.ts +7 -0
- package/dist/plugins/epub-plugin.js +15 -0
- package/dist/plugins/epub-plugin.js.map +1 -0
- package/dist/plugins/html-plugin.d.ts +7 -0
- package/dist/plugins/html-plugin.js +11 -0
- package/dist/plugins/html-plugin.js.map +1 -0
- package/dist/plugins/image-plugin.d.ts +7 -0
- package/dist/plugins/image-plugin.js +11 -0
- package/dist/plugins/image-plugin.js.map +1 -0
- package/dist/plugins/markdown-plugin.d.ts +7 -0
- package/dist/plugins/markdown-plugin.js +11 -0
- package/dist/plugins/markdown-plugin.js.map +1 -0
- package/dist/plugins/pdf-plugin.d.ts +7 -0
- package/dist/plugins/pdf-plugin.js +15 -0
- package/dist/plugins/pdf-plugin.js.map +1 -0
- package/dist/plugins/pptx-plugin.d.ts +7 -0
- package/dist/plugins/pptx-plugin.js +15 -0
- package/dist/plugins/pptx-plugin.js.map +1 -0
- package/dist/plugins/rtf-plugin.d.ts +7 -0
- package/dist/plugins/rtf-plugin.js +15 -0
- package/dist/plugins/rtf-plugin.js.map +1 -0
- package/dist/plugins/source-code-plugin.d.ts +7 -0
- package/dist/plugins/source-code-plugin.js +11 -0
- package/dist/plugins/source-code-plugin.js.map +1 -0
- package/dist/plugins/svg-plugin.d.ts +7 -0
- package/dist/plugins/svg-plugin.js +11 -0
- package/dist/plugins/svg-plugin.js.map +1 -0
- package/dist/plugins/text-plugin.d.ts +7 -0
- package/dist/plugins/text-plugin.js +11 -0
- package/dist/plugins/text-plugin.js.map +1 -0
- package/dist/plugins/video-plugin.d.ts +7 -0
- package/dist/plugins/video-plugin.js +11 -0
- package/dist/plugins/video-plugin.js.map +1 -0
- package/dist/plugins/xlsx-plugin.d.ts +7 -0
- package/dist/plugins/xlsx-plugin.js +15 -0
- package/dist/plugins/xlsx-plugin.js.map +1 -0
- package/dist/plugins/zip-plugin.d.ts +7 -0
- package/dist/plugins/zip-plugin.js +15 -0
- package/dist/plugins/zip-plugin.js.map +1 -0
- package/dist/preview-adapters/AudioPreviewAdapter.d.ts +8 -0
- package/dist/preview-adapters/AudioPreviewAdapter.js +31 -0
- package/dist/preview-adapters/AudioPreviewAdapter.js.map +1 -0
- package/dist/preview-adapters/CsvPreviewAdapter.d.ts +8 -0
- package/dist/preview-adapters/CsvPreviewAdapter.js +28 -0
- package/dist/preview-adapters/CsvPreviewAdapter.js.map +1 -0
- package/dist/preview-adapters/DocxPreviewAdapter.d.ts +8 -0
- package/dist/preview-adapters/DocxPreviewAdapter.js +16 -0
- package/dist/preview-adapters/DocxPreviewAdapter.js.map +1 -0
- package/dist/preview-adapters/EpubPreviewAdapter.d.ts +8 -0
- package/dist/preview-adapters/EpubPreviewAdapter.js +28 -0
- package/dist/preview-adapters/EpubPreviewAdapter.js.map +1 -0
- package/dist/preview-adapters/HtmlPreviewAdapter.d.ts +8 -0
- package/dist/preview-adapters/HtmlPreviewAdapter.js +28 -0
- package/dist/preview-adapters/HtmlPreviewAdapter.js.map +1 -0
- package/dist/preview-adapters/ImagePreviewAdapter.d.ts +8 -0
- package/dist/preview-adapters/ImagePreviewAdapter.js +31 -0
- package/dist/preview-adapters/ImagePreviewAdapter.js.map +1 -0
- package/dist/preview-adapters/MarkdownPreviewAdapter.d.ts +8 -0
- package/dist/preview-adapters/MarkdownPreviewAdapter.js +28 -0
- package/dist/preview-adapters/MarkdownPreviewAdapter.js.map +1 -0
- package/dist/preview-adapters/PdfPreviewAdapter.d.ts +8 -0
- package/dist/preview-adapters/PdfPreviewAdapter.js +16 -0
- package/dist/preview-adapters/PdfPreviewAdapter.js.map +1 -0
- package/dist/preview-adapters/PptxPreviewAdapter.d.ts +8 -0
- package/dist/preview-adapters/PptxPreviewAdapter.js +16 -0
- package/dist/preview-adapters/PptxPreviewAdapter.js.map +1 -0
- package/dist/preview-adapters/RtfPreviewAdapter.d.ts +8 -0
- package/dist/preview-adapters/RtfPreviewAdapter.js +54 -0
- package/dist/preview-adapters/RtfPreviewAdapter.js.map +1 -0
- package/dist/preview-adapters/SourceCodePreviewAdapter.d.ts +8 -0
- package/dist/preview-adapters/SourceCodePreviewAdapter.js +35 -0
- package/dist/preview-adapters/SourceCodePreviewAdapter.js.map +1 -0
- package/dist/preview-adapters/SvgPreviewAdapter.d.ts +8 -0
- package/dist/preview-adapters/SvgPreviewAdapter.js +28 -0
- package/dist/preview-adapters/SvgPreviewAdapter.js.map +1 -0
- package/dist/preview-adapters/TextPreviewAdapter.d.ts +8 -0
- package/dist/preview-adapters/TextPreviewAdapter.js +28 -0
- package/dist/preview-adapters/TextPreviewAdapter.js.map +1 -0
- package/dist/preview-adapters/UnsupportedPluginPreview.d.ts +11 -0
- package/dist/preview-adapters/UnsupportedPluginPreview.js +34 -0
- package/dist/preview-adapters/UnsupportedPluginPreview.js.map +1 -0
- package/dist/preview-adapters/VideoPreviewAdapter.d.ts +8 -0
- package/dist/preview-adapters/VideoPreviewAdapter.js +31 -0
- package/dist/preview-adapters/VideoPreviewAdapter.js.map +1 -0
- package/dist/preview-adapters/XlsxPreviewAdapter.d.ts +8 -0
- package/dist/preview-adapters/XlsxPreviewAdapter.js +17 -0
- package/dist/preview-adapters/XlsxPreviewAdapter.js.map +1 -0
- package/dist/preview-adapters/ZipPreviewAdapter.d.ts +8 -0
- package/dist/preview-adapters/ZipPreviewAdapter.js +28 -0
- package/dist/preview-adapters/ZipPreviewAdapter.js.map +1 -0
- package/dist/remote-url.d.ts +20 -0
- package/dist/remote-url.js +478 -0
- package/dist/remote-url.js.map +1 -0
- package/dist/rtf/load-rtfjs.d.ts +42 -0
- package/dist/rtf/load-rtfjs.js +71 -0
- package/dist/rtf/load-rtfjs.js.map +1 -0
- package/dist/rtf/normalize-codepage.d.ts +33 -0
- package/dist/rtf/normalize-codepage.js +88 -0
- package/dist/rtf/normalize-codepage.js.map +1 -0
- package/dist/shiki.d.ts +35 -0
- package/dist/shiki.js +128 -0
- package/dist/shiki.js.map +1 -0
- package/dist/styles/AudioPreview.css +35 -0
- package/dist/styles/CsvPreview.css +106 -0
- package/dist/styles/DocxPreview.css +93 -0
- package/dist/styles/EpubPreview.css +509 -0
- package/dist/styles/HtmlPreview.css +15 -0
- package/dist/styles/ImagePreview.css +45 -0
- package/dist/styles/LargeFileGate.css +55 -0
- package/dist/styles/MarkdownPreview.css +291 -0
- package/dist/styles/PdfPreview.css +68 -0
- package/dist/styles/PlainTextLargePreview.css +85 -0
- package/dist/styles/PluginDebugBar.css +30 -0
- package/dist/styles/PptxPreview.css +207 -0
- package/dist/styles/PreviewFallback.css +88 -0
- package/dist/styles/PreviewLoading.css +13 -0
- package/dist/styles/RtfPreview.css +99 -0
- package/dist/styles/ShikiSourceView.css +159 -0
- package/dist/styles/SvgPreview.css +99 -0
- package/dist/styles/VideoPreview.css +19 -0
- package/dist/styles/ViewModeBar.css +38 -0
- package/dist/styles/XlsxPreview.css +361 -0
- package/dist/styles/ZipPreview.css +86 -0
- package/dist/styles/base.css +238 -0
- package/dist/styles/index.css +39 -0
- package/dist/support-status.d.ts +19 -0
- package/dist/support-status.js +174 -0
- package/dist/support-status.js.map +1 -0
- package/dist/utils.d.ts +17 -0
- package/dist/utils.js +205 -0
- package/dist/utils.js.map +1 -0
- package/package.json +125 -0
- package/scripts/copy-pdf-worker.mjs +31 -0
- package/scripts/copy-rtfjs-bundles.mjs +49 -0
|
@@ -0,0 +1,693 @@
|
|
|
1
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useState, useCallback, useRef } from "react";
|
|
3
|
+
import JSZip from "jszip";
|
|
4
|
+
import {
|
|
5
|
+
BookOpenIcon,
|
|
6
|
+
ChevronLeftIcon,
|
|
7
|
+
ChevronRightIcon,
|
|
8
|
+
ListIcon,
|
|
9
|
+
SearchIcon,
|
|
10
|
+
XIcon,
|
|
11
|
+
ChevronDownIcon
|
|
12
|
+
} from "./icons";
|
|
13
|
+
import { base64ToUint8Array } from "./utils";
|
|
14
|
+
import { useLocale } from "./core/i18n";
|
|
15
|
+
import "./styles/EpubPreview.css";
|
|
16
|
+
async function parseEpub(base64Content) {
|
|
17
|
+
const bytes = base64ToUint8Array(base64Content);
|
|
18
|
+
const zip = await JSZip.loadAsync(bytes);
|
|
19
|
+
const chapters = [];
|
|
20
|
+
const toc = [];
|
|
21
|
+
let bookTitle = "";
|
|
22
|
+
let bookAuthor = "";
|
|
23
|
+
const stylesheets = [];
|
|
24
|
+
const imageMap = {};
|
|
25
|
+
function getAttr(tag, attrName) {
|
|
26
|
+
const re = new RegExp(`${attrName}="([^"]*)"`, "i");
|
|
27
|
+
const m = tag.match(re);
|
|
28
|
+
return m ? m[1] : null;
|
|
29
|
+
}
|
|
30
|
+
function resolvePath(baseDir, relativePath) {
|
|
31
|
+
if (relativePath.startsWith("/")) return relativePath.substring(1);
|
|
32
|
+
const decoded = decodeURIComponent(relativePath);
|
|
33
|
+
const pathPart = decoded.split("#")[0];
|
|
34
|
+
if (!pathPart) return baseDir;
|
|
35
|
+
const baseParts = baseDir.split("/").filter(Boolean);
|
|
36
|
+
const relParts = pathPart.split("/");
|
|
37
|
+
for (const part of relParts) {
|
|
38
|
+
if (part === "..") {
|
|
39
|
+
baseParts.pop();
|
|
40
|
+
} else if (part !== "." && part !== "") {
|
|
41
|
+
baseParts.push(part);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return baseParts.join("/");
|
|
45
|
+
}
|
|
46
|
+
function findZipFile(path) {
|
|
47
|
+
let f = zip.file(path);
|
|
48
|
+
if (f) return f;
|
|
49
|
+
try {
|
|
50
|
+
f = zip.file(decodeURIComponent(path));
|
|
51
|
+
if (f) return f;
|
|
52
|
+
} catch {
|
|
53
|
+
}
|
|
54
|
+
const lowerPath = path.toLowerCase();
|
|
55
|
+
let found = null;
|
|
56
|
+
zip.forEach((p, file) => {
|
|
57
|
+
if (!found && p.toLowerCase() === lowerPath) {
|
|
58
|
+
found = file;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
return found;
|
|
62
|
+
}
|
|
63
|
+
const imageExtensions = [".jpg", ".jpeg", ".png", ".gif", ".svg", ".webp", ".bmp"];
|
|
64
|
+
const imagePromises = [];
|
|
65
|
+
zip.forEach((path, file) => {
|
|
66
|
+
if (file.dir) return;
|
|
67
|
+
const lowerPath = path.toLowerCase();
|
|
68
|
+
if (imageExtensions.some((ext) => lowerPath.endsWith(ext))) {
|
|
69
|
+
imagePromises.push(
|
|
70
|
+
(async () => {
|
|
71
|
+
try {
|
|
72
|
+
const blob = await file.async("blob");
|
|
73
|
+
const url = URL.createObjectURL(blob);
|
|
74
|
+
imageMap[path] = url;
|
|
75
|
+
const filename = path.split("/").pop();
|
|
76
|
+
imageMap[filename] = url;
|
|
77
|
+
imageMap[lowerPath] = url;
|
|
78
|
+
imageMap[filename.toLowerCase()] = url;
|
|
79
|
+
} catch {
|
|
80
|
+
}
|
|
81
|
+
})()
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
await Promise.all(imagePromises);
|
|
86
|
+
const containerFile = zip.file("META-INF/container.xml");
|
|
87
|
+
if (!containerFile) {
|
|
88
|
+
throw new Error("Invalid EPUB: missing container.xml");
|
|
89
|
+
}
|
|
90
|
+
const containerXml = await containerFile.async("string");
|
|
91
|
+
const rootfileMatch = containerXml.match(/full-path="([^"]+)"/);
|
|
92
|
+
if (!rootfileMatch) {
|
|
93
|
+
throw new Error("Invalid EPUB: no rootfile in container.xml");
|
|
94
|
+
}
|
|
95
|
+
const opfPath = rootfileMatch[1];
|
|
96
|
+
const opfFile = findZipFile(opfPath);
|
|
97
|
+
if (!opfFile) {
|
|
98
|
+
throw new Error("Invalid EPUB: OPF file not found");
|
|
99
|
+
}
|
|
100
|
+
const opfXml = await opfFile.async("string");
|
|
101
|
+
const opfDir = opfPath.includes("/") ? opfPath.substring(0, opfPath.lastIndexOf("/") + 1) : "";
|
|
102
|
+
const titleMatch = opfXml.match(/<dc:title[^>]*>([\s\S]*?)<\/dc:title>/);
|
|
103
|
+
if (titleMatch) bookTitle = titleMatch[1].trim();
|
|
104
|
+
const authorMatch = opfXml.match(/<dc:creator[^>]*>([\s\S]*?)<\/dc:creator>/);
|
|
105
|
+
if (authorMatch) bookAuthor = authorMatch[1].trim();
|
|
106
|
+
const manifestMatch = opfXml.match(/<manifest[^>]*>([\s\S]*?)<\/manifest>/);
|
|
107
|
+
const manifestMap = {};
|
|
108
|
+
const manifestHrefToId = {};
|
|
109
|
+
if (manifestMatch) {
|
|
110
|
+
const itemRegex = /<item\s+([^>]+?)\/?>/g;
|
|
111
|
+
let itemMatch;
|
|
112
|
+
while ((itemMatch = itemRegex.exec(manifestMatch[1])) !== null) {
|
|
113
|
+
const attrs = itemMatch[1];
|
|
114
|
+
const id = getAttr(attrs, "id");
|
|
115
|
+
const href = getAttr(attrs, "href");
|
|
116
|
+
const mediaType = getAttr(attrs, "media-type") || "";
|
|
117
|
+
if (id && href) {
|
|
118
|
+
manifestMap[id] = { href, mediaType };
|
|
119
|
+
manifestHrefToId[href] = id;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const spineMatch = opfXml.match(/<spine[^>]*>([\s\S]*?)<\/spine>/);
|
|
124
|
+
const spineItems = [];
|
|
125
|
+
if (spineMatch) {
|
|
126
|
+
const itemrefRegex = /<itemref\s+([^>]+?)\/?>/g;
|
|
127
|
+
let refMatch;
|
|
128
|
+
while ((refMatch = itemrefRegex.exec(spineMatch[1])) !== null) {
|
|
129
|
+
const idref = getAttr(refMatch[1], "idref");
|
|
130
|
+
if (idref) spineItems.push(idref);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const spineTocAttr = spineMatch ? spineMatch[0].match(/toc="([^"]+)"/)?.[1] ?? null : null;
|
|
134
|
+
if (spineTocAttr) {
|
|
135
|
+
const tocInfo = manifestMap[spineTocAttr];
|
|
136
|
+
if (tocInfo) {
|
|
137
|
+
const tocPath = resolvePath(opfDir, tocInfo.href);
|
|
138
|
+
const tocFile = findZipFile(tocPath);
|
|
139
|
+
if (tocFile) {
|
|
140
|
+
let parseNavPoints2 = function(xml, parentPath) {
|
|
141
|
+
const items = [];
|
|
142
|
+
const navRegex = /<navPoint[^>]*>([\s\S]*?)<\/navPoint>/g;
|
|
143
|
+
let navMatch;
|
|
144
|
+
while ((navMatch = navRegex.exec(xml)) !== null) {
|
|
145
|
+
const block = navMatch[1];
|
|
146
|
+
const labelMatch = block.match(
|
|
147
|
+
/<navLabel[^>]*>[\s\S]*?<text>([^<]*)<\/text>/
|
|
148
|
+
);
|
|
149
|
+
const contentMatch = block.match(/<content\s+src="([^"]+)"/);
|
|
150
|
+
if (labelMatch && contentMatch) {
|
|
151
|
+
const src = contentMatch[1].split("#")[0];
|
|
152
|
+
const item = {
|
|
153
|
+
title: labelMatch[1].trim(),
|
|
154
|
+
src: resolvePath(parentPath, src)
|
|
155
|
+
};
|
|
156
|
+
const children = parseNavPoints2(block, parentPath);
|
|
157
|
+
if (children.length > 0) item.children = children;
|
|
158
|
+
items.push(item);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return items;
|
|
162
|
+
};
|
|
163
|
+
var parseNavPoints = parseNavPoints2;
|
|
164
|
+
const tocXml = await tocFile.async("string");
|
|
165
|
+
const parsed = parseNavPoints2(tocXml, opfDir);
|
|
166
|
+
toc.push(...parsed);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
for (const [, info] of Object.entries(manifestMap)) {
|
|
171
|
+
if (info.mediaType === "text/css" || info.href.endsWith(".css")) {
|
|
172
|
+
const cssPath = resolvePath(opfDir, info.href);
|
|
173
|
+
const cssFile = findZipFile(cssPath);
|
|
174
|
+
if (cssFile) {
|
|
175
|
+
try {
|
|
176
|
+
const cssText = await cssFile.async("string");
|
|
177
|
+
stylesheets.push(cssText);
|
|
178
|
+
} catch {
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const tocTitleMap = {};
|
|
184
|
+
function flattenToc(items) {
|
|
185
|
+
for (const item of items) {
|
|
186
|
+
if (item.title) {
|
|
187
|
+
tocTitleMap[item.src] = item.title;
|
|
188
|
+
const filename = item.src.split("/").pop();
|
|
189
|
+
tocTitleMap[filename] = item.title;
|
|
190
|
+
}
|
|
191
|
+
if (item.children) flattenToc(item.children);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
flattenToc(toc);
|
|
195
|
+
for (let i = 0; i < spineItems.length; i++) {
|
|
196
|
+
const idref = spineItems[i];
|
|
197
|
+
const info = manifestMap[idref];
|
|
198
|
+
if (!info) continue;
|
|
199
|
+
const filePath = resolvePath(opfDir, info.href);
|
|
200
|
+
const chapterFile = findZipFile(filePath);
|
|
201
|
+
if (!chapterFile) continue;
|
|
202
|
+
const chapterXml = await chapterFile.async("string");
|
|
203
|
+
let chapterTitle = `Chapter ${i + 1}`;
|
|
204
|
+
const tocTitle = tocTitleMap[filePath] || tocTitleMap[filePath.toLowerCase()] || tocTitleMap[info.href] || tocTitleMap[info.href.toLowerCase()];
|
|
205
|
+
if (tocTitle) {
|
|
206
|
+
chapterTitle = tocTitle;
|
|
207
|
+
} else {
|
|
208
|
+
const hMatch = chapterXml.match(/<h[1-3][^>]*>([\s\S]*?)<\/h[1-3]>/);
|
|
209
|
+
if (hMatch && hMatch[1].trim()) {
|
|
210
|
+
chapterTitle = hMatch[1].replace(/<[^>]+>/g, "").trim();
|
|
211
|
+
} else {
|
|
212
|
+
const titleTagMatch = chapterXml.match(/<title>([^<]*)<\/title>/);
|
|
213
|
+
if (titleTagMatch && titleTagMatch[1].trim()) {
|
|
214
|
+
chapterTitle = titleTagMatch[1].trim();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
const bodyMatch = chapterXml.match(/<body[^>]*>([\s\S]*?)<\/body>/);
|
|
219
|
+
let bodyHtml = bodyMatch ? bodyMatch[1] : chapterXml;
|
|
220
|
+
bodyHtml = bodyHtml.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, "").replace(/<div\s+class="mbp_pagebreak"[^>]*><\/div>/gi, "");
|
|
221
|
+
const chapterDir = filePath.includes("/") ? filePath.substring(0, filePath.lastIndexOf("/") + 1) : "";
|
|
222
|
+
bodyHtml = bodyHtml.replace(
|
|
223
|
+
/(<img\s+[^>]*?)src="([^"]+)"/gi,
|
|
224
|
+
(_match, prefix, src) => {
|
|
225
|
+
if (src.startsWith("http://") || src.startsWith("https://") || src.startsWith("data:")) {
|
|
226
|
+
return `${prefix}src="${src}"`;
|
|
227
|
+
}
|
|
228
|
+
const resolvedSrc = resolvePath(chapterDir, src);
|
|
229
|
+
const blobUrl = imageMap[resolvedSrc] || imageMap[resolvedSrc.toLowerCase()] || imageMap[src] || imageMap[src.toLowerCase()] || imageMap[src.split("/").pop()] || imageMap[src.split("/").pop().toLowerCase()];
|
|
230
|
+
if (blobUrl) {
|
|
231
|
+
return `${prefix}src="${blobUrl}"`;
|
|
232
|
+
}
|
|
233
|
+
return `${prefix}src="${src}"`;
|
|
234
|
+
}
|
|
235
|
+
);
|
|
236
|
+
bodyHtml = bodyHtml.replace(
|
|
237
|
+
/(<a\s+[^>]*?)href="([^"]+)"/gi,
|
|
238
|
+
(_match, prefix, href) => {
|
|
239
|
+
if (href.startsWith("http://") || href.startsWith("https://") || href.startsWith("#") || href.startsWith("mailto:")) {
|
|
240
|
+
return `${prefix}href="${href}"`;
|
|
241
|
+
}
|
|
242
|
+
const resolvedHref = resolvePath(chapterDir, href.split("#")[0]);
|
|
243
|
+
const anchor = href.includes("#") ? "#" + href.split("#")[1] : "";
|
|
244
|
+
return `${prefix}href="${resolvedHref}${anchor}" data-epub-link="true"`;
|
|
245
|
+
}
|
|
246
|
+
);
|
|
247
|
+
chapters.push({
|
|
248
|
+
index: i,
|
|
249
|
+
title: chapterTitle,
|
|
250
|
+
htmlContent: bodyHtml,
|
|
251
|
+
filePath
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
if (chapters.length === 0) {
|
|
255
|
+
const htmlFiles = [];
|
|
256
|
+
zip.forEach((path) => {
|
|
257
|
+
if (path.endsWith(".html") || path.endsWith(".xhtml") || path.endsWith(".htm")) {
|
|
258
|
+
htmlFiles.push(path);
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
htmlFiles.sort();
|
|
262
|
+
for (let i = 0; i < htmlFiles.length; i++) {
|
|
263
|
+
const file = zip.file(htmlFiles[i]);
|
|
264
|
+
if (!file) continue;
|
|
265
|
+
const html = await file.async("string");
|
|
266
|
+
const bodyMatch = html.match(/<body[^>]*>([\s\S]*?)<\/body>/);
|
|
267
|
+
let bodyHtml = (bodyMatch ? bodyMatch[1] : html).replace(/<script[^>]*>[\s\S]*?<\/script>/gi, "").replace(/<div\s+class="mbp_pagebreak"[^>]*><\/div>/gi, "");
|
|
268
|
+
const chapterDir = htmlFiles[i].includes("/") ? htmlFiles[i].substring(0, htmlFiles[i].lastIndexOf("/") + 1) : "";
|
|
269
|
+
bodyHtml = bodyHtml.replace(
|
|
270
|
+
/(<img\s+[^>]*?)src="([^"]+)"/gi,
|
|
271
|
+
(_match, prefix, src) => {
|
|
272
|
+
if (src.startsWith("http://") || src.startsWith("https://") || src.startsWith("data:")) {
|
|
273
|
+
return `${prefix}src="${src}"`;
|
|
274
|
+
}
|
|
275
|
+
const resolvedSrc = resolvePath(chapterDir, src);
|
|
276
|
+
const blobUrl = imageMap[resolvedSrc] || imageMap[resolvedSrc.toLowerCase()] || imageMap[src] || imageMap[src.toLowerCase()];
|
|
277
|
+
if (blobUrl) {
|
|
278
|
+
return `${prefix}src="${blobUrl}"`;
|
|
279
|
+
}
|
|
280
|
+
return `${prefix}src="${src}"`;
|
|
281
|
+
}
|
|
282
|
+
);
|
|
283
|
+
const tocTitle = tocTitleMap[htmlFiles[i]] || tocTitleMap[htmlFiles[i].toLowerCase()];
|
|
284
|
+
const hMatch = html.match(/<h[1-3][^>]*>([\s\S]*?)<\/h[1-3]>/);
|
|
285
|
+
let chTitle = `Chapter ${i + 1}`;
|
|
286
|
+
if (tocTitle) {
|
|
287
|
+
chTitle = tocTitle;
|
|
288
|
+
} else if (hMatch && hMatch[1].trim()) {
|
|
289
|
+
chTitle = hMatch[1].replace(/<[^>]+>/g, "").trim();
|
|
290
|
+
}
|
|
291
|
+
chapters.push({
|
|
292
|
+
index: i,
|
|
293
|
+
title: chTitle,
|
|
294
|
+
htmlContent: bodyHtml,
|
|
295
|
+
filePath: htmlFiles[i]
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return { title: bookTitle, author: bookAuthor, chapters, toc, stylesheets, imageMap };
|
|
300
|
+
}
|
|
301
|
+
function EpubPreview({ content, fileName }) {
|
|
302
|
+
const [bookData, setBookData] = useState(null);
|
|
303
|
+
const [loading, setLoading] = useState(true);
|
|
304
|
+
const [error, setError] = useState(null);
|
|
305
|
+
const [currentChapter, setCurrentChapter] = useState(0);
|
|
306
|
+
const [showToc, setShowToc] = useState(false);
|
|
307
|
+
const [showChapterDropdown, setShowChapterDropdown] = useState(false);
|
|
308
|
+
const [searchQuery, setSearchQuery] = useState("");
|
|
309
|
+
const [searchResults, setSearchResults] = useState([]);
|
|
310
|
+
const contentRef = useRef(null);
|
|
311
|
+
const dropdownRef = useRef(null);
|
|
312
|
+
const t = useLocale();
|
|
313
|
+
const [prevContent, setPrevContent] = useState(content);
|
|
314
|
+
if (prevContent !== content) {
|
|
315
|
+
setPrevContent(content);
|
|
316
|
+
setLoading(true);
|
|
317
|
+
setError(null);
|
|
318
|
+
setBookData(null);
|
|
319
|
+
setCurrentChapter(0);
|
|
320
|
+
setSearchResults([]);
|
|
321
|
+
}
|
|
322
|
+
useEffect(() => {
|
|
323
|
+
let cancelled = false;
|
|
324
|
+
parseEpub(content).then(
|
|
325
|
+
(result) => {
|
|
326
|
+
if (cancelled) return;
|
|
327
|
+
setBookData(result);
|
|
328
|
+
setLoading(false);
|
|
329
|
+
},
|
|
330
|
+
(err) => {
|
|
331
|
+
if (cancelled) return;
|
|
332
|
+
console.error("Error parsing EPUB:", err);
|
|
333
|
+
setError(err instanceof Error ? err.message : "Failed to parse e-book");
|
|
334
|
+
setLoading(false);
|
|
335
|
+
}
|
|
336
|
+
);
|
|
337
|
+
return () => {
|
|
338
|
+
cancelled = true;
|
|
339
|
+
};
|
|
340
|
+
}, [content]);
|
|
341
|
+
useEffect(() => {
|
|
342
|
+
function handleClickOutside(e) {
|
|
343
|
+
if (dropdownRef.current && !dropdownRef.current.contains(e.target)) {
|
|
344
|
+
setShowChapterDropdown(false);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
document.addEventListener("mousedown", handleClickOutside);
|
|
348
|
+
return () => document.removeEventListener("mousedown", handleClickOutside);
|
|
349
|
+
}, []);
|
|
350
|
+
useEffect(() => {
|
|
351
|
+
return () => {
|
|
352
|
+
if (bookData?.imageMap) {
|
|
353
|
+
Object.values(bookData.imageMap).forEach((url) => {
|
|
354
|
+
if (url.startsWith("blob:")) URL.revokeObjectURL(url);
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
}, [bookData?.imageMap]);
|
|
359
|
+
useEffect(() => {
|
|
360
|
+
if (contentRef.current) {
|
|
361
|
+
contentRef.current.scrollTop = 0;
|
|
362
|
+
}
|
|
363
|
+
}, [currentChapter]);
|
|
364
|
+
const handleSearch = useCallback(
|
|
365
|
+
(query) => {
|
|
366
|
+
setSearchQuery(query);
|
|
367
|
+
if (!bookData || !query.trim()) {
|
|
368
|
+
setSearchResults([]);
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
const lowerQuery = query.toLowerCase();
|
|
372
|
+
const results = [];
|
|
373
|
+
for (let i = 0; i < bookData.chapters.length; i++) {
|
|
374
|
+
const text = bookData.chapters[i].htmlContent.replace(/<[^>]+>/g, "");
|
|
375
|
+
const idx = text.toLowerCase().indexOf(lowerQuery);
|
|
376
|
+
if (idx !== -1) {
|
|
377
|
+
const start = Math.max(0, idx - 30);
|
|
378
|
+
const end = Math.min(text.length, idx + query.length + 30);
|
|
379
|
+
const snippet = (start > 0 ? "..." : "") + text.substring(start, end) + (end < text.length ? "..." : "");
|
|
380
|
+
results.push({ chapterIndex: i, snippet });
|
|
381
|
+
if (results.length >= 20) break;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
setSearchResults(results);
|
|
385
|
+
},
|
|
386
|
+
[bookData]
|
|
387
|
+
);
|
|
388
|
+
const handleContentClick = useCallback(
|
|
389
|
+
(e) => {
|
|
390
|
+
const target = e.target;
|
|
391
|
+
const anchor = target.closest("a");
|
|
392
|
+
if (!anchor || !bookData) return;
|
|
393
|
+
const isEpubLink = anchor.getAttribute("data-epub-link") === "true";
|
|
394
|
+
if (isEpubLink) {
|
|
395
|
+
e.preventDefault();
|
|
396
|
+
const href = anchor.getAttribute("href") || "";
|
|
397
|
+
const targetPath = href.split("#")[0];
|
|
398
|
+
const chapterIdx = bookData.chapters.findIndex(
|
|
399
|
+
(ch) => ch.filePath === targetPath || ch.filePath.toLowerCase() === targetPath.toLowerCase()
|
|
400
|
+
);
|
|
401
|
+
if (chapterIdx !== -1) {
|
|
402
|
+
setCurrentChapter(chapterIdx);
|
|
403
|
+
setTimeout(() => {
|
|
404
|
+
if (contentRef.current) contentRef.current.scrollTop = 0;
|
|
405
|
+
}, 100);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
},
|
|
409
|
+
[bookData]
|
|
410
|
+
);
|
|
411
|
+
if (loading) {
|
|
412
|
+
return /* @__PURE__ */ jsxs("div", { className: "fv-epub__state fv-epub__state--loading", children: [
|
|
413
|
+
/* @__PURE__ */ jsx("div", { className: "fv-spinner fv-spinner--lg" }),
|
|
414
|
+
/* @__PURE__ */ jsx("p", { className: "fv-epub__state-msg", children: t.loadingEbook })
|
|
415
|
+
] });
|
|
416
|
+
}
|
|
417
|
+
if (error || !bookData) {
|
|
418
|
+
return /* @__PURE__ */ jsxs("div", { className: "fv-epub__state fv-epub__state--error", children: [
|
|
419
|
+
/* @__PURE__ */ jsx(BookOpenIcon, { size: 48, className: "fv-epub__state-icon" }),
|
|
420
|
+
/* @__PURE__ */ jsx("p", { className: "fv-epub__state-title", children: t.ebookLoadFailed }),
|
|
421
|
+
/* @__PURE__ */ jsx("p", { className: "fv-epub__state-msg", children: error || t.unknownError })
|
|
422
|
+
] });
|
|
423
|
+
}
|
|
424
|
+
if (bookData.chapters.length === 0) {
|
|
425
|
+
return /* @__PURE__ */ jsxs("div", { className: "fv-epub__state fv-epub__state--empty", children: [
|
|
426
|
+
/* @__PURE__ */ jsx(BookOpenIcon, { size: 48 }),
|
|
427
|
+
/* @__PURE__ */ jsx("p", { className: "fv-epub__state-title", children: t.noChaptersFound })
|
|
428
|
+
] });
|
|
429
|
+
}
|
|
430
|
+
const chapter = bookData.chapters[currentChapter];
|
|
431
|
+
const combinedCss = bookData.stylesheets.join("\n") + `
|
|
432
|
+
/* Override book styles that may cause horizontal overflow */
|
|
433
|
+
.fv-epub__article * {
|
|
434
|
+
max-width: 100% !important;
|
|
435
|
+
overflow-wrap: break-word !important;
|
|
436
|
+
word-wrap: break-word !important;
|
|
437
|
+
}
|
|
438
|
+
.fv-epub__article pre, .fv-epub__article code {
|
|
439
|
+
overflow-x: auto !important;
|
|
440
|
+
max-width: 100% !important;
|
|
441
|
+
}
|
|
442
|
+
.fv-epub__article img {
|
|
443
|
+
max-width: 100% !important;
|
|
444
|
+
height: auto !important;
|
|
445
|
+
}
|
|
446
|
+
.fv-epub__article table {
|
|
447
|
+
display: block;
|
|
448
|
+
overflow-x: auto;
|
|
449
|
+
max-width: 100%;
|
|
450
|
+
}
|
|
451
|
+
`;
|
|
452
|
+
return /* @__PURE__ */ jsxs("div", { className: "fv-epub", children: [
|
|
453
|
+
/* @__PURE__ */ jsx("div", { className: "fv-epub__info-bar", children: /* @__PURE__ */ jsxs("div", { className: "fv-epub__info-row", children: [
|
|
454
|
+
/* @__PURE__ */ jsxs("div", { className: "fv-epub__info-left", children: [
|
|
455
|
+
/* @__PURE__ */ jsx(BookOpenIcon, { size: 14, className: "fv-epub__info-icon" }),
|
|
456
|
+
/* @__PURE__ */ jsx("span", { className: "fv-epub__info-title", children: bookData.title || fileName }),
|
|
457
|
+
bookData.author && /* @__PURE__ */ jsxs("span", { className: "fv-epub__info-author", children: [
|
|
458
|
+
"\u2014 ",
|
|
459
|
+
bookData.author
|
|
460
|
+
] })
|
|
461
|
+
] }),
|
|
462
|
+
/* @__PURE__ */ jsxs("div", { className: "fv-epub__info-right", children: [
|
|
463
|
+
/* @__PURE__ */ jsxs("div", { className: "fv-epub__search-wrap", children: [
|
|
464
|
+
/* @__PURE__ */ jsx(SearchIcon, { size: 14, className: "fv-epub__search-icon" }),
|
|
465
|
+
/* @__PURE__ */ jsx(
|
|
466
|
+
"input",
|
|
467
|
+
{
|
|
468
|
+
type: "text",
|
|
469
|
+
value: searchQuery,
|
|
470
|
+
onChange: (e) => handleSearch(e.target.value),
|
|
471
|
+
placeholder: t.searchPlaceholder,
|
|
472
|
+
className: "fv-epub__search-input"
|
|
473
|
+
}
|
|
474
|
+
),
|
|
475
|
+
searchQuery && /* @__PURE__ */ jsx(
|
|
476
|
+
"button",
|
|
477
|
+
{
|
|
478
|
+
onClick: () => {
|
|
479
|
+
setSearchQuery("");
|
|
480
|
+
setSearchResults([]);
|
|
481
|
+
},
|
|
482
|
+
className: "fv-epub__search-clear",
|
|
483
|
+
children: /* @__PURE__ */ jsx(XIcon, { size: 12 })
|
|
484
|
+
}
|
|
485
|
+
)
|
|
486
|
+
] }),
|
|
487
|
+
/* @__PURE__ */ jsx(
|
|
488
|
+
"button",
|
|
489
|
+
{
|
|
490
|
+
onClick: () => setShowToc(!showToc),
|
|
491
|
+
className: `fv-epub__toc-btn ${showToc ? "fv-epub__toc-btn--active" : ""}`,
|
|
492
|
+
title: t.tableOfContents,
|
|
493
|
+
children: /* @__PURE__ */ jsx(ListIcon, { size: 16 })
|
|
494
|
+
}
|
|
495
|
+
)
|
|
496
|
+
] })
|
|
497
|
+
] }) }),
|
|
498
|
+
searchQuery && searchResults.length > 0 && /* @__PURE__ */ jsxs("div", { className: "fv-epub__search-results", children: [
|
|
499
|
+
/* @__PURE__ */ jsx("p", { className: "fv-epub__search-results-label", children: t.foundInChapters.replace("{count}", searchResults.length.toLocaleString()) }),
|
|
500
|
+
/* @__PURE__ */ jsx("div", { className: "fv-epub__search-results-list", children: searchResults.map((r, i) => /* @__PURE__ */ jsxs(
|
|
501
|
+
"button",
|
|
502
|
+
{
|
|
503
|
+
onClick: () => {
|
|
504
|
+
setCurrentChapter(r.chapterIndex);
|
|
505
|
+
setSearchQuery("");
|
|
506
|
+
setSearchResults([]);
|
|
507
|
+
},
|
|
508
|
+
className: "fv-epub__search-result-item",
|
|
509
|
+
children: [
|
|
510
|
+
/* @__PURE__ */ jsx("span", { className: "fv-epub__search-result-title", children: bookData.chapters[r.chapterIndex].title }),
|
|
511
|
+
/* @__PURE__ */ jsx("span", { className: "fv-epub__search-result-snippet", children: r.snippet })
|
|
512
|
+
]
|
|
513
|
+
},
|
|
514
|
+
i
|
|
515
|
+
)) })
|
|
516
|
+
] }),
|
|
517
|
+
searchQuery && searchResults.length === 0 && /* @__PURE__ */ jsx("div", { className: "fv-epub__search-empty", children: /* @__PURE__ */ jsx("p", { className: "fv-epub__search-empty-text", children: t.noResultsFound }) }),
|
|
518
|
+
/* @__PURE__ */ jsxs("div", { className: "fv-epub__body", children: [
|
|
519
|
+
showToc && /* @__PURE__ */ jsx("div", { className: "fv-epub__toc-sidebar", children: /* @__PURE__ */ jsxs("div", { className: "fv-epub__toc-inner", children: [
|
|
520
|
+
/* @__PURE__ */ jsx("h3", { className: "fv-epub__toc-heading", children: t.tableOfContents }),
|
|
521
|
+
/* @__PURE__ */ jsx(
|
|
522
|
+
TocTree,
|
|
523
|
+
{
|
|
524
|
+
items: bookData.toc,
|
|
525
|
+
chapters: bookData.chapters,
|
|
526
|
+
currentChapter,
|
|
527
|
+
onSelect: (idx) => {
|
|
528
|
+
setCurrentChapter(idx);
|
|
529
|
+
setShowChapterDropdown(false);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
),
|
|
533
|
+
bookData.toc.length === 0 && /* @__PURE__ */ jsx("div", { className: "fv-epub__toc-list", children: bookData.chapters.map((ch, i) => /* @__PURE__ */ jsx(
|
|
534
|
+
"button",
|
|
535
|
+
{
|
|
536
|
+
onClick: () => setCurrentChapter(i),
|
|
537
|
+
className: `fv-epub__toc-item ${i === currentChapter ? "fv-epub__toc-item--active" : ""}`,
|
|
538
|
+
children: ch.title
|
|
539
|
+
},
|
|
540
|
+
i
|
|
541
|
+
)) })
|
|
542
|
+
] }) }),
|
|
543
|
+
/* @__PURE__ */ jsxs("div", { className: "fv-epub__main", children: [
|
|
544
|
+
/* @__PURE__ */ jsxs("div", { className: "fv-epub__chapter-bar", children: [
|
|
545
|
+
/* @__PURE__ */ jsx(
|
|
546
|
+
"button",
|
|
547
|
+
{
|
|
548
|
+
onClick: () => setCurrentChapter(Math.max(0, currentChapter - 1)),
|
|
549
|
+
disabled: currentChapter === 0,
|
|
550
|
+
className: "fv-epub__chapter-nav-btn",
|
|
551
|
+
children: /* @__PURE__ */ jsx(ChevronLeftIcon, { size: 16 })
|
|
552
|
+
}
|
|
553
|
+
),
|
|
554
|
+
/* @__PURE__ */ jsxs("div", { className: "fv-epub__dropdown-wrap", ref: dropdownRef, children: [
|
|
555
|
+
/* @__PURE__ */ jsxs(
|
|
556
|
+
"button",
|
|
557
|
+
{
|
|
558
|
+
onClick: () => setShowChapterDropdown(!showChapterDropdown),
|
|
559
|
+
className: "fv-epub__dropdown-trigger",
|
|
560
|
+
children: [
|
|
561
|
+
/* @__PURE__ */ jsxs("span", { className: "fv-epub__dropdown-index", children: [
|
|
562
|
+
currentChapter + 1,
|
|
563
|
+
"/",
|
|
564
|
+
bookData.chapters.length
|
|
565
|
+
] }),
|
|
566
|
+
/* @__PURE__ */ jsx("span", { className: "fv-epub__dropdown-title", children: chapter.title }),
|
|
567
|
+
/* @__PURE__ */ jsx(
|
|
568
|
+
ChevronDownIcon,
|
|
569
|
+
{
|
|
570
|
+
size: 14,
|
|
571
|
+
className: `fv-epub__dropdown-chevron ${showChapterDropdown ? "fv-epub__dropdown-chevron--open" : ""}`
|
|
572
|
+
}
|
|
573
|
+
)
|
|
574
|
+
]
|
|
575
|
+
}
|
|
576
|
+
),
|
|
577
|
+
showChapterDropdown && /* @__PURE__ */ jsx("div", { className: "fv-epub__dropdown-list", children: bookData.chapters.map((ch, i) => /* @__PURE__ */ jsxs(
|
|
578
|
+
"button",
|
|
579
|
+
{
|
|
580
|
+
onClick: () => {
|
|
581
|
+
setCurrentChapter(i);
|
|
582
|
+
setShowChapterDropdown(false);
|
|
583
|
+
},
|
|
584
|
+
className: `fv-epub__dropdown-item ${i === currentChapter ? "fv-epub__dropdown-item--active" : ""}`,
|
|
585
|
+
children: [
|
|
586
|
+
/* @__PURE__ */ jsxs("span", { className: "fv-epub__dropdown-item-index", children: [
|
|
587
|
+
i + 1,
|
|
588
|
+
"."
|
|
589
|
+
] }),
|
|
590
|
+
ch.title
|
|
591
|
+
]
|
|
592
|
+
},
|
|
593
|
+
i
|
|
594
|
+
)) })
|
|
595
|
+
] }),
|
|
596
|
+
/* @__PURE__ */ jsx(
|
|
597
|
+
"button",
|
|
598
|
+
{
|
|
599
|
+
onClick: () => setCurrentChapter(Math.min(bookData.chapters.length - 1, currentChapter + 1)),
|
|
600
|
+
disabled: currentChapter === bookData.chapters.length - 1,
|
|
601
|
+
className: "fv-epub__chapter-nav-btn",
|
|
602
|
+
children: /* @__PURE__ */ jsx(ChevronRightIcon, { size: 16 })
|
|
603
|
+
}
|
|
604
|
+
)
|
|
605
|
+
] }),
|
|
606
|
+
/* @__PURE__ */ jsx("div", { className: "fv-epub__content", ref: contentRef, onClick: handleContentClick, children: /* @__PURE__ */ jsxs("div", { className: "fv-epub__content-inner", children: [
|
|
607
|
+
/* @__PURE__ */ jsx("h2", { className: "fv-epub__chapter-title", children: chapter.title }),
|
|
608
|
+
combinedCss && /* @__PURE__ */ jsx("style", { dangerouslySetInnerHTML: { __html: combinedCss } }),
|
|
609
|
+
/* @__PURE__ */ jsx(
|
|
610
|
+
"div",
|
|
611
|
+
{
|
|
612
|
+
className: "fv-epub__article",
|
|
613
|
+
dangerouslySetInnerHTML: { __html: chapter.htmlContent }
|
|
614
|
+
}
|
|
615
|
+
)
|
|
616
|
+
] }) }),
|
|
617
|
+
/* @__PURE__ */ jsxs("div", { className: "fv-epub__footer", children: [
|
|
618
|
+
/* @__PURE__ */ jsxs(
|
|
619
|
+
"button",
|
|
620
|
+
{
|
|
621
|
+
onClick: () => setCurrentChapter(Math.max(0, currentChapter - 1)),
|
|
622
|
+
disabled: currentChapter === 0,
|
|
623
|
+
className: "fv-epub__footer-btn",
|
|
624
|
+
children: [
|
|
625
|
+
/* @__PURE__ */ jsx(ChevronLeftIcon, { size: 16 }),
|
|
626
|
+
/* @__PURE__ */ jsx("span", { className: "fv-epub__footer-btn-text", children: t.previous })
|
|
627
|
+
]
|
|
628
|
+
}
|
|
629
|
+
),
|
|
630
|
+
/* @__PURE__ */ jsxs("span", { className: "fv-epub__footer-label", children: [
|
|
631
|
+
currentChapter + 1,
|
|
632
|
+
" / ",
|
|
633
|
+
bookData.chapters.length
|
|
634
|
+
] }),
|
|
635
|
+
/* @__PURE__ */ jsxs(
|
|
636
|
+
"button",
|
|
637
|
+
{
|
|
638
|
+
onClick: () => setCurrentChapter(Math.min(bookData.chapters.length - 1, currentChapter + 1)),
|
|
639
|
+
disabled: currentChapter === bookData.chapters.length - 1,
|
|
640
|
+
className: "fv-epub__footer-btn",
|
|
641
|
+
children: [
|
|
642
|
+
/* @__PURE__ */ jsx("span", { className: "fv-epub__footer-btn-text", children: t.next }),
|
|
643
|
+
/* @__PURE__ */ jsx(ChevronRightIcon, { size: 16 })
|
|
644
|
+
]
|
|
645
|
+
}
|
|
646
|
+
)
|
|
647
|
+
] })
|
|
648
|
+
] })
|
|
649
|
+
] })
|
|
650
|
+
] });
|
|
651
|
+
}
|
|
652
|
+
function TocTree({
|
|
653
|
+
items,
|
|
654
|
+
chapters,
|
|
655
|
+
currentChapter,
|
|
656
|
+
onSelect,
|
|
657
|
+
depth = 0
|
|
658
|
+
}) {
|
|
659
|
+
return /* @__PURE__ */ jsx("div", { className: depth > 0 ? "fv-epub__toc-indent" : "fv-epub__toc-list", children: items.map((item, i) => {
|
|
660
|
+
const chapterIdx = chapters.findIndex(
|
|
661
|
+
(ch) => ch.filePath === item.src || ch.filePath.toLowerCase() === item.src.toLowerCase() || item.src.endsWith(ch.filePath.split("/").pop()) || ch.filePath.endsWith(item.src.split("/").pop())
|
|
662
|
+
);
|
|
663
|
+
const isCurrent = chapterIdx === currentChapter;
|
|
664
|
+
return /* @__PURE__ */ jsxs("div", { children: [
|
|
665
|
+
/* @__PURE__ */ jsx(
|
|
666
|
+
"button",
|
|
667
|
+
{
|
|
668
|
+
onClick: () => {
|
|
669
|
+
if (chapterIdx !== -1) onSelect(chapterIdx);
|
|
670
|
+
},
|
|
671
|
+
disabled: chapterIdx === -1,
|
|
672
|
+
className: `fv-epub__toc-item ${isCurrent ? "fv-epub__toc-item--active" : ""}`,
|
|
673
|
+
style: { paddingLeft: `${8 + depth * 4}px` },
|
|
674
|
+
children: item.title
|
|
675
|
+
}
|
|
676
|
+
),
|
|
677
|
+
item.children && item.children.length > 0 && /* @__PURE__ */ jsx(
|
|
678
|
+
TocTree,
|
|
679
|
+
{
|
|
680
|
+
items: item.children,
|
|
681
|
+
chapters,
|
|
682
|
+
currentChapter,
|
|
683
|
+
onSelect,
|
|
684
|
+
depth: depth + 1
|
|
685
|
+
}
|
|
686
|
+
)
|
|
687
|
+
] }, `${depth}-${i}`);
|
|
688
|
+
}) });
|
|
689
|
+
}
|
|
690
|
+
export {
|
|
691
|
+
EpubPreview
|
|
692
|
+
};
|
|
693
|
+
//# sourceMappingURL=EpubPreview.js.map
|