Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(client/pdf): respect locale
Elian Doran0a9ce84cf216
diff --git a/apps/client/src/widgets/type_widgets/file/Pdf.tsx b/apps/client/src/widgets/type_widgets/file/Pdf.tsxindex 5d2767bb95..fcce704cff 100644--- a/apps/client/src/widgets/type_widgets/file/Pdf.tsx+++ b/apps/client/src/widgets/type_widgets/file/Pdf.tsx@@ -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 }: {<iframeref={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();}}diff --git a/packages/pdfjs-viewer/src/custom.ts b/packages/pdfjs-viewer/src/custom.tsindex 22caff2b66..aab78afc92 100644--- a/packages/pdfjs-viewer/src/custom.ts+++ b/packages/pdfjs-viewer/src/custom.ts@@ -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;diff --git a/packages/pdfjs-viewer/src/history.ts b/packages/pdfjs-viewer/src/persistence.tssimilarity index 88%rename from packages/pdfjs-viewer/src/history.tsrename to packages/pdfjs-viewer/src/persistence.tsindex 21c2966d42..965b5a54b1 100644--- a/packages/pdfjs-viewer/src/history.ts+++ b/packages/pdfjs-viewer/src/persistence.ts@@ -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 || {});}