Skip to content

Build your personal knowledge base with Trilium Notes

refactor(pdfjs): extract toc logic to separate file

Elian Dorane2d29aadca7b
diff --git a/packages/pdfjs-viewer/src/custom.ts b/packages/pdfjs-viewer/src/custom.ts
index 1f3d922eb1..90c24df361 100644
--- a/packages/pdfjs-viewer/src/custom.ts
+++ b/packages/pdfjs-viewer/src/custom.ts
@@ -1,4 +1,5 @@
import interceptPersistence from "./persistence";
+import { extractAndSendToc, setupScrollToHeading } from "./toc";
const LOG_EVENT_BUS = false;
@@ -78,85 +79,5 @@ function patchEventBus() {
};
}
-async function extractAndSendToc() {
- const app = window.PDFViewerApplication;
-
- try {
- const outline = await app.pdfDocument.getOutline();
-
- if (!outline || outline.length === 0) {
- window.parent.postMessage({
- type: "pdfjs-viewer-toc",
- data: null
- }, "*");
- return;
- }
-
- // Store outline items with their destinations for later scrolling
- const outlineMap = new Map();
- const toc = convertOutlineToToc(outline, 0, outlineMap);
-
- // Store the map globally so setupScrollToHeading can access it
- (window as any).TRILIUM_OUTLINE_MAP = outlineMap;
-
- window.parent.postMessage({
- type: "pdfjs-viewer-toc",
- data: toc
- }, "*");
- } catch (error) {
- window.parent.postMessage({
- type: "pdfjs-viewer-toc",
- data: null
- }, "*");
- }
-}
-
-function convertOutlineToToc(outline: any[], level = 0, outlineMap?: Map<string, any>, parentId = ""): any[] {
- return outline.map((item, index) => {
- const id = parentId ? `${parentId}-${index}` : `pdf-outline-${index}`;
-
- if (outlineMap) {
- outlineMap.set(id, item);
- }
-
- return {
- title: item.title,
- level: level,
- dest: item.dest,
- id: id,
- items: item.items && item.items.length > 0 ? convertOutlineToToc(item.items, level + 1, outlineMap, id) : []
- };
- });
-}
-
main();
console.log("Custom script loaded");
-
-function setupScrollToHeading() {
- window.addEventListener("message", async (event) => {
- if (event.data?.type === "trilium-scroll-to-heading") {
- const headingId = event.data.headingId;
- const outlineMap = (window as any).TRILIUM_OUTLINE_MAP as Map<string, any>;
-
- if (!outlineMap) return;
-
- const outlineItem = outlineMap.get(headingId);
- if (!outlineItem || !outlineItem.dest) return;
-
- const app = window.PDFViewerApplication;
-
- // Navigate to the destination
- try {
- const dest = typeof outlineItem.dest === 'string'
- ? await app.pdfDocument.getDestination(outlineItem.dest)
- : outlineItem.dest;
-
- if (dest) {
- app.pdfLinkService.goToDestination(dest);
- }
- } catch (error) {
- console.error("Error navigating to heading:", error);
- }
- }
- });
-}
diff --git a/packages/pdfjs-viewer/src/toc.ts b/packages/pdfjs-viewer/src/toc.ts
new file mode 100644
index 0000000000..fe261e4534
--- /dev/null
+++ b/packages/pdfjs-viewer/src/toc.ts
@@ -0,0 +1,77 @@
+let outlineMap: Map<string, any> | null = null;
+
+export async function extractAndSendToc() {
+ const app = window.PDFViewerApplication;
+
+ try {
+ const outline = await app.pdfDocument.getOutline();
+
+ if (!outline || outline.length === 0) {
+ window.parent.postMessage({
+ type: "pdfjs-viewer-toc",
+ data: null
+ }, "*");
+ return;
+ }
+
+ // Store outline items with their destinations for later scrolling
+ outlineMap = new Map();
+ const toc = convertOutlineToToc(outline, 0, outlineMap);
+
+ window.parent.postMessage({
+ type: "pdfjs-viewer-toc",
+ data: toc
+ }, "*");
+ } catch (error) {
+ window.parent.postMessage({
+ type: "pdfjs-viewer-toc",
+ data: null
+ }, "*");
+ }
+}
+
+function convertOutlineToToc(outline: any[], level = 0, outlineMap?: Map<string, any>, parentId = ""): any[] {
+ return outline.map((item, index) => {
+ const id = parentId ? `${parentId}-${index}` : `pdf-outline-${index}`;
+
+ if (outlineMap) {
+ outlineMap.set(id, item);
+ }
+
+ return {
+ title: item.title,
+ level: level,
+ dest: item.dest,
+ id: id,
+ items: item.items && item.items.length > 0 ? convertOutlineToToc(item.items, level + 1, outlineMap, id) : []
+ };
+ });
+}
+
+export function setupScrollToHeading() {
+ window.addEventListener("message", async (event) => {
+ if (event.data?.type === "trilium-scroll-to-heading") {
+ const headingId = event.data.headingId;
+
+ if (!outlineMap) return;
+
+ const outlineItem = outlineMap.get(headingId);
+ if (!outlineItem || !outlineItem.dest) return;
+
+ const app = window.PDFViewerApplication;
+
+ // Navigate to the destination
+ try {
+ const dest = typeof outlineItem.dest === 'string'
+ ? await app.pdfDocument.getDestination(outlineItem.dest)
+ : outlineItem.dest;
+
+ if (dest) {
+ app.pdfLinkService.goToDestination(dest);
+ }
+ } catch (error) {
+ console.error("Error navigating to heading:", error);
+ }
+ }
+ });
+}