From 5e42e3db3d99d0b9eaf5108997eb310bf4feb774 Mon Sep 17 00:00:00 2001 From: windingwind <33902321+windingwind@users.noreply.github.com> Date: Thu, 7 Nov 2024 20:29:27 +0100 Subject: [PATCH] add: use `note.css` for note export to PDF update: lint --- addon/chrome/content/printTemplate.xhtml | 4 ++++ src/modules/export/api.ts | 13 ++++++++----- src/modules/export/docx.ts | 9 ++++++--- src/modules/export/exportWindow.ts | 20 ++++++++++---------- src/modules/export/pdf.ts | 5 ++++- src/modules/template/editorWindow.ts | 24 ++++++++++++++++-------- src/modules/workspace/content.ts | 5 ++++- src/utils/note.ts | 9 ++++++--- 8 files changed, 58 insertions(+), 31 deletions(-) diff --git a/addon/chrome/content/printTemplate.xhtml b/addon/chrome/content/printTemplate.xhtml index ac91b1d..a61f047 100644 --- a/addon/chrome/content/printTemplate.xhtml +++ b/addon/chrome/content/printTemplate.xhtml @@ -23,6 +23,10 @@ var parser = new DOMParser(); var serializer = new XMLSerializer(); var htmlString = e.data.html; + const styleString = e.data.style; + const style = document.createElement("style"); + style.innerHTML = styleString; + document.head.appendChild(style); var htmlDoc = parser.parseFromString(htmlString, "text/html"); var xhtmlString = serializer.serializeToString(htmlDoc); document.querySelector(".markdown-body").innerHTML = xhtmlString; diff --git a/src/modules/export/api.ts b/src/modules/export/api.ts index 2350991..b315dbd 100644 --- a/src/modules/export/api.ts +++ b/src/modules/export/api.ts @@ -221,7 +221,9 @@ async function embedLinkedNotes(noteItem: Zotero.Item): Promise { for (const i in noteLines) { newLines.push(noteLines[i]); const doc = parser.parseFromString(noteLines[i], "text/html"); - const linkParams = (Array.from(doc.querySelectorAll("a")) as HTMLAnchorElement[]) + const linkParams = ( + Array.from(doc.querySelectorAll("a")) as HTMLAnchorElement[] + ) .filter((a) => a?.href.startsWith("zotero://note/")) .map((a) => getNoteLinkParams(a?.href)) .filter((p) => p.noteItem && !p.ignore); @@ -248,10 +250,11 @@ async function embedLinkedNotes(noteItem: Zotero.Item): Promise { seenCitationItemIDs.push(currentID); } } - return `
${newLines.join("\n")}
`; + return `
${newLines.join("\n")}
`; } function getNoteCitationData(noteItem: Zotero.Item) { diff --git a/src/modules/export/docx.ts b/src/modules/export/docx.ts index 0e9b1e9..fa1f277 100644 --- a/src/modules/export/docx.ts +++ b/src/modules/export/docx.ts @@ -49,7 +49,9 @@ async function parseDocxFields(html: string, worker: HTMLIFrameElement) { const mathCache = {} as MathCache; - for (const elem of (Array.from(doc.querySelectorAll("math")) as MathMLElement[])) { + for (const elem of Array.from( + doc.querySelectorAll("math"), + ) as MathMLElement[]) { let str = (await sendWorkerTask( worker, "parseMML", @@ -122,8 +124,9 @@ async function parseDocxFields(html: string, worker: HTMLIFrameElement) { citationItems.push(item); } const properties = citation.properties; - const formattedCitation = `${elem.textContent || "Zotero Citation" - } - Please click Zotero - Refresh in Word/LibreOffice to update all fields.`; + const formattedCitation = `${ + elem.textContent || "Zotero Citation" + } - Please click Zotero - Refresh in Word/LibreOffice to update all fields.`; properties.formattedCitation = formattedCitation; properties.plainCitation = formattedCitation + " "; properties.noteIndex = 0; diff --git a/src/modules/export/exportWindow.ts b/src/modules/export/exportWindow.ts index 49fb452..50ddd5f 100644 --- a/src/modules/export/exportWindow.ts +++ b/src/modules/export/exportWindow.ts @@ -177,11 +177,11 @@ function makeCheckboxLine(dataKey: string, callback?: (ev: Event) => void) { }, listeners: callback ? [ - { - type: "change", - listener: callback, - }, - ] + { + type: "change", + listener: callback, + }, + ] : [], }, ], @@ -223,11 +223,11 @@ function makeRadioLine( }, listeners: callback ? [ - { - type: "change", - listener: callback, - }, - ] + { + type: "change", + listener: callback, + }, + ] : [], }, ], diff --git a/src/modules/export/pdf.ts b/src/modules/export/pdf.ts index 4e5038d..2211d12 100644 --- a/src/modules/export/pdf.ts +++ b/src/modules/export/pdf.ts @@ -17,7 +17,10 @@ export async function savePDF(noteId: number) { args, )!; await args._initPromise.promise; - args.browser?.contentWindow.postMessage({ type: "print", html }, "*"); + args.browser?.contentWindow.postMessage( + { type: "print", html, style: Zotero.Prefs.get("note.css") || "" }, + "*", + ); win.print(); showHint("Note Saved as PDF"); } diff --git a/src/modules/template/editorWindow.ts b/src/modules/template/editorWindow.ts index f92a684..b0c07c1 100644 --- a/src/modules/template/editorWindow.ts +++ b/src/modules/template/editorWindow.ts @@ -283,10 +283,18 @@ function updateEditor() { "#editor-name", ) as HTMLInputElement; const editor = win?.document.getElementById("editor") as HTMLIFrameElement; - const saveTemplate = win?.document.getElementById("save") as XULButtonElement | null; - const deleteTemplate = win?.document.getElementById("delete") as XULButtonElement | null; - const resetTemplate = win?.document.getElementById("reset") as XULButtonElement | null; - const shareTemplate = win?.document.getElementById("share") as XULButtonElement | null; + const saveTemplate = win?.document.getElementById( + "save", + ) as XULButtonElement | null; + const deleteTemplate = win?.document.getElementById( + "delete", + ) as XULButtonElement | null; + const resetTemplate = win?.document.getElementById( + "reset", + ) as XULButtonElement | null; + const shareTemplate = win?.document.getElementById( + "share", + ) as XULButtonElement | null; const formats = win?.document.getElementById( "formats-container", ) as HTMLDivElement; @@ -608,7 +616,7 @@ function resetSelectedTemplate() { if (addon.api.template.SYSTEM_TEMPLATE_NAMES.includes(name)) { addon.data.template.editor.editor.setValue( addon.api.template.DEFAULT_TEMPLATES.find((t) => t.name === name)?.text || - "", + "", ); showHint(`Template ${name} is reset. Please save before leaving.`); } @@ -631,9 +639,9 @@ pluginVersion: "${version}" savedAt: "${new Date().toISOString()}" content: |- ${content - .split("\n") - .map((line) => ` ${line}`) - .join("\n")} + .split("\n") + .map((line) => ` ${line}`) + .join("\n")} `; new ztoolkit.Clipboard().addText(yaml, "text/plain").copy(); showHint( diff --git a/src/modules/workspace/content.ts b/src/modules/workspace/content.ts index acae7ca..9a218aa 100644 --- a/src/modules/workspace/content.ts +++ b/src/modules/workspace/content.ts @@ -1,7 +1,10 @@ import { Workspace } from "../../elements/workspace/workspace"; import { waitUtilAsync } from "../../utils/wait"; -export async function initWorkspace(container: XULBoxElement, item: Zotero.Item) { +export async function initWorkspace( + container: XULBoxElement, + item: Zotero.Item, +) { if (!container) { return; } diff --git a/src/utils/note.ts b/src/utils/note.ts index 9859334..4e3b54b 100644 --- a/src/utils/note.ts +++ b/src/utils/note.ts @@ -58,7 +58,8 @@ async function setLinesToNote(note: Zotero.Item, lines: string[]) { } else { const noteHead = noteText.substring(0, containerIndex); note.setNote( - `${noteHead}data-schema-version="${config.dataSchemaVersion + `${noteHead}data-schema-version="${ + config.dataSchemaVersion }">${lines.join("\n")}`, ); } @@ -368,8 +369,10 @@ async function copyEmbeddedImagesInHTML( note: targetNote, }); }); - nodes.forEach((node) => - node?.setAttribute("data-attachment-key", copiedAttachment.key), Node + nodes.forEach( + (node) => + node?.setAttribute("data-attachment-key", copiedAttachment.key), + Node, ); } }