feat(client/pdf): respect locale
Some checks failed
Checks / main (push) Has been cancelled

This commit is contained in:
Elian Doran 2025-12-29 19:10:14 +02:00
parent 07a1734d4b
commit 0a9ce84cf2
No known key found for this signature in database
3 changed files with 25 additions and 7 deletions

View File

@ -5,6 +5,7 @@ import FBlob from "../../../entities/fblob";
import FNote from "../../../entities/fnote";
import server from "../../../services/server";
import { useViewModeConfig } from "../../collections/NoteList";
import { useTriliumOption } from "../../react/hooks";
const VARIABLE_WHITELIST = new Set([
"root-background",
@ -21,6 +22,7 @@ export default function PdfPreview({ note, blob, componentId }: {
const iframeRef = useRef<HTMLIFrameElement>(null);
const { onLoad } = useStyleInjection(iframeRef);
const historyConfig = useViewModeConfig(note, "pdfHistory");
const [ locale ] = useTriliumOption("locale");
useEffect(() => {
function handleMessage(event: MessageEvent) {
@ -51,10 +53,11 @@ export default function PdfPreview({ note, blob, componentId }: {
<iframe
ref={iframeRef}
class="pdf-preview"
src={`pdfjs/web/viewer.html?file=../../api/notes/${note.noteId}/open`}
src={`pdfjs/web/viewer.html?file=../../api/notes/${note.noteId}/open&lang=${locale}`}
onLoad={() => {
if (iframeRef.current?.contentWindow) {
iframeRef.current.contentWindow.TRILIUM_VIEW_HISTORY_STORE = historyConfig.config;
const win = iframeRef.current?.contentWindow;
if (win) {
win.TRILIUM_VIEW_HISTORY_STORE = historyConfig.config;
}
onLoad();
}}

View File

@ -1,15 +1,15 @@
import interceptViewHistory from "./history";
import interceptPersistence from "./persistence";
const LOG_EVENT_BUS = false;
async function main() {
interceptPersistence(getCustomAppOptions());
// Wait for the PDF viewer application to be available.
while (!window.PDFViewerApplication) {
await new Promise(r => setTimeout(r, 50));
}
const app = window.PDFViewerApplication;
interceptViewHistory();
if (LOG_EVENT_BUS) {
patchEventBus();
@ -20,6 +20,17 @@ async function main() {
await app.initializedPromise;
};
function getCustomAppOptions() {
const urlParams = new URLSearchParams(window.location.search);
return {
localeProperties: {
// Read from URL query
lang: urlParams.get("lang") || "en"
}
};
}
function manageSave() {
const app = window.PDFViewerApplication;
const storage = app.pdfDocument.annotationStorage;

View File

@ -1,4 +1,4 @@
export default function interceptViewHistory() {
export default function interceptViewHistory(customOptions?: object) {
const originalSetItem = Storage.prototype.setItem;
Storage.prototype.setItem = function (key: string, value: string) {
if (key === "pdfjs.history") {
@ -11,6 +11,10 @@ export default function interceptViewHistory() {
const originalGetItem = Storage.prototype.getItem;
Storage.prototype.getItem = function (key: string) {
if (key === "pdfjs.preferences") {
return JSON.stringify(customOptions);
}
if (key === "pdfjs.history") {
return JSON.stringify(window.TRILIUM_VIEW_HISTORY_STORE || {});
}