chore(pdfjs): manage requested changes
Some checks are pending
Checks / main (push) Waiting to run

This commit is contained in:
Elian Doran 2026-01-01 12:36:33 +02:00
parent b2378f2a53
commit 7ec7b6bd7b
No known key found for this signature in database
10 changed files with 36 additions and 52 deletions

View File

@ -122,12 +122,17 @@ class NoteContext extends Component implements EventListener<"entitiesReloaded">
// Clear context data when switching notes and notify subscribers
const oldKeys = Array.from(this.contextData.keys());
this.contextData.clear();
for (const key of oldKeys) {
this.triggerEvent("contextDataChanged", {
noteContext: this,
key,
value: undefined
});
if (oldKeys.length > 0) {
// Notify subscribers asynchronously to avoid blocking navigation
window.setTimeout(() => {
for (const key of oldKeys) {
this.triggerEvent("contextDataChanged", {
noteContext: this,
key,
value: undefined
});
}
}, 0);
}
this.saveToRecentNotes(resolvedNotePath);

View File

@ -192,7 +192,7 @@ export function formatSize(size: number | null | undefined) {
}
const k = 1024;
const sizes = ["B", "KB", "MB", "GB"];
const sizes = ["B", "KiB", "MiB", "GiB"];
const i = Math.floor(Math.log(size) / Math.log(k));
return `${Math.round((size / Math.pow(k, i)) * 100) / 100} ${sizes[i]}`;

View File

@ -4,7 +4,7 @@
*/
import { NoteType } from "@triliumnext/commons";
import { type JSX,VNode } from "preact";
import { type JSX, VNode } from "preact";
import { TypeWidgetProps } from "./type_widgets/type_widget";

View File

@ -1213,23 +1213,18 @@ export function useSetContextData<K extends keyof NoteContextDataMap>(
key: K,
value: NoteContextDataMap[K] | undefined
) {
const valueRef = useRef<NoteContextDataMap[K] | undefined>(value);
valueRef.current = value;
useEffect(() => {
if (!noteContext || valueRef.current === undefined) return;
if (!noteContext) return;
noteContext.setContextData(key, valueRef.current);
if (value !== undefined) {
noteContext.setContextData(key, value);
} else {
noteContext.clearContextData(key);
}
return () => {
noteContext.clearContextData(key);
};
}, [noteContext, key]);
// Update when value changes
useEffect(() => {
if (!noteContext || value === undefined) return;
noteContext.setContextData(key, value);
}, [noteContext, key, value]);
}
@ -1253,23 +1248,7 @@ export function useSetContextData<K extends keyof NoteContextDataMap>(
*/
export function useGetContextData<K extends keyof NoteContextDataMap>(key: K): NoteContextDataMap[K] | undefined {
const { noteContext } = useActiveNoteContext();
const [data, setData] = useState<NoteContextDataMap[K] | undefined>(() =>
noteContext?.getContextData(key)
);
// Update initial value when noteContext changes
useEffect(() => {
setData(noteContext?.getContextData(key));
}, [noteContext, key]);
// Subscribe to changes via Trilium event system
useTriliumEvent("contextDataChanged", ({ noteContext: eventNoteContext, key: changedKey, value }) => {
if (eventNoteContext === noteContext && changedKey === key) {
setData(value as NoteContextDataMap[K]);
}
});
return data;
return useGetContextDataFrom(noteContext, key);
}
/**

View File

@ -61,7 +61,7 @@ describe("PDF sidebar", () => {
await expect(attachmentsList.locator(".pdf-attachment-item")).toHaveCount(2);
const attachmentInfo = attachmentsList.locator(".pdf-attachment-item", { hasText: "Note.trilium" });
await expect(attachmentInfo).toContainText("3.36 MB");
await expect(attachmentInfo).toContainText("3.36 MiB");
// Download the attachment and check its size.
const [ download ] = await Promise.all([

View File

@ -1,11 +1,12 @@
export async function setupPdfAttachments() {
const app = window.PDFViewerApplication;
// Extract immediately since we're called after documentloaded
await extractAndSendAttachments();
// Listen for download requests
window.addEventListener("message", async (event) => {
// Only accept messages from the same origin to prevent malicious iframes
if (event.origin !== window.location.origin) return;
if (event.data?.type === "trilium-download-attachment") {
const filename = event.data.filename;
await downloadAttachment(filename);
@ -18,7 +19,6 @@ async function extractAndSendAttachments() {
try {
const attachments = await app.pdfDocument.getAttachments();
console.log("Got attachments:", attachments);
if (!attachments) {
window.parent.postMessage({

View File

@ -35,7 +35,7 @@ function hideSidebar() {
const toggleButtonEl = document.getElementById("viewsManagerToggleButton");
if (toggleButtonEl) {
const spacer = toggleButtonEl.nextElementSibling.nextElementSibling;
if (spacer.classList.contains("toolbarButtonSpacer")) {
if (spacer instanceof HTMLElement && spacer.classList.contains("toolbarButtonSpacer")) {
spacer.remove();
}
toggleButtonEl.remove();
@ -47,10 +47,7 @@ function getCustomAppOptions(urlParams: URLSearchParams) {
localeProperties: {
// Read from URL query
lang: urlParams.get("lang") || "en"
},
// Control sidebar visibility via query parameter
// sidebarViewOnLoad: -1 disables sidebar, 0 = NONE (default)
viewsManager: null
}
};
}

View File

@ -1,11 +1,12 @@
export async function setupPdfLayers() {
const app = window.PDFViewerApplication;
// Extract immediately since we're called after documentloaded
await extractAndSendLayers();
// Listen for layer visibility toggle requests
window.addEventListener("message", async (event) => {
// Only accept messages from the same origin to prevent malicious iframes
if (event.origin !== window.location.origin) return;
if (event.data?.type === "trilium-toggle-layer") {
const layerId = event.data.layerId;
const visible = event.data.visible;

View File

@ -19,16 +19,15 @@ export function setupPdfPages() {
}, window.location.origin);
});
// Listen for scroll-to-page requests
window.addEventListener("message", (event) => {
window.addEventListener("message", async(event) => {
// Only accept messages from the same origin to prevent malicious iframes
if (event.origin !== window.location.origin) return;
if (event.data?.type === "trilium-scroll-to-page") {
const pageNumber = event.data.pageNumber;
app.pdfViewer.currentPageNumber = pageNumber;
}
});
// Listen for thumbnail requests
window.addEventListener("message", async (event) => {
if (event.data?.type === "trilium-request-thumbnail") {
const pageNumber = event.data.pageNumber;
await generateThumbnail(pageNumber);

View File

@ -55,6 +55,9 @@ function convertOutlineToToc(outline: any[], level = 0, outlineMap?: Map<string,
export function setupScrollToHeading() {
window.addEventListener("message", async (event) => {
// Only accept messages from the same origin to prevent malicious iframes
if (event.origin !== window.location.origin) return;
if (event.data?.type === "trilium-scroll-to-heading") {
const headingId = event.data.headingId;