mirror of
https://github.com/zadam/trilium.git
synced 2026-01-01 12:14:24 +01:00
chore(client/right_pane): get raw ToC data to show up
This commit is contained in:
parent
257f6c5994
commit
e36049cd43
@ -36,13 +36,22 @@ export default function PdfPreview({ note, blob, componentId, noteContext }: {
|
||||
if (event.data.type === "pdfjs-viewer-save-view-history" && event.data?.data) {
|
||||
historyConfig?.storeFn(JSON.parse(event.data.data));
|
||||
}
|
||||
|
||||
if (event.data.type === "pdfjs-viewer-toc") {
|
||||
if (event.data.data) {
|
||||
noteContext.setContextData("toc", event.data.data);
|
||||
} else {
|
||||
// No ToC available, fallback to note title
|
||||
noteContext.setContextData("toc", note.title);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener("message", handleMessage);
|
||||
return () => {
|
||||
window.removeEventListener("message", handleMessage);
|
||||
};
|
||||
}, [ note, historyConfig, componentId, blob ]);
|
||||
}, [ note, historyConfig, componentId, blob, noteContext ]);
|
||||
|
||||
// Refresh when blob changes.
|
||||
useEffect(() => {
|
||||
@ -51,6 +60,7 @@ export default function PdfPreview({ note, blob, componentId, noteContext }: {
|
||||
}
|
||||
}, [ blob ]);
|
||||
|
||||
// Initial ToC is set to note.title, will be replaced by actual ToC when received
|
||||
useSetContextData(noteContext, "toc", note.title);
|
||||
|
||||
return (historyConfig &&
|
||||
|
||||
@ -17,6 +17,7 @@ async function main() {
|
||||
}
|
||||
app.eventBus.on("documentloaded", () => {
|
||||
manageSave();
|
||||
extractAndSendToc();
|
||||
});
|
||||
await app.initializedPromise;
|
||||
};
|
||||
@ -76,5 +77,41 @@ 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;
|
||||
}
|
||||
|
||||
// Convert PDF.js outline format to a simpler structure
|
||||
const toc = convertOutlineToToc(outline);
|
||||
|
||||
window.parent.postMessage({
|
||||
type: "pdfjs-viewer-toc",
|
||||
data: toc
|
||||
}, "*");
|
||||
} catch (error) {
|
||||
data: null
|
||||
}, "*");
|
||||
}
|
||||
}
|
||||
|
||||
function convertOutlineToToc(outline: any[], level = 0): any[] {
|
||||
return outline.map(item => ({
|
||||
title: item.title,
|
||||
level: level,
|
||||
dest: item.dest,
|
||||
items: item.items && item.items.length > 0 ? convertOutlineToToc(item.items, level + 1) : []
|
||||
}));
|
||||
}
|
||||
|
||||
main();
|
||||
console.log("Custom script loaded");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user