Skip to content

Commit

Permalink
fix: export docx
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Aug 25, 2023
1 parent 0ec220c commit c603a6e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 45 deletions.
11 changes: 11 additions & 0 deletions addon/chrome/content/docxExport.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<script
src="chrome://__addonRef__/content/scripts/docxWorker.js"
type="application/javascript"
></script>
</head>
<body></body>
</html>
27 changes: 15 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@
"crypto-js": "^4.1.1",
"diff": "^5.1.0",
"hast-util-to-html": "^9.0.0",
"hast-util-to-mdast": "^10.0.0",
"hast-util-to-mdast": "^8.4.1",
"hast-util-to-text": "^4.0.0",
"hastscript": "^8.0.0",
"html-docx-js": "^0.3.1",
"html-docx-js-typescript": "^0.1.5",
"katex": "^0.16.8",
"rehype-format": "^4.0.1",
Expand All @@ -69,29 +70,31 @@
"zotero-plugin-toolkit": "^2.2.5"
},
"devDependencies": {
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
"@prettier/plugin-xml": "^3.2.0",
"@types/browser-or-node": "^1.3.0",
"@types/diff": "^5.0.3",
"@types/node": "^20.4.9",
"@types/html-docx-js": "^0.3.1",
"@types/node": "^20.5.6",
"@types/seedrandom": "^3.0.5",
"@types/yamljs": "^0.2.31",
"@typescript-eslint/eslint-plugin": "^6.3.0",
"@typescript-eslint/parser": "^6.3.0",
"@typescript-eslint/eslint-plugin": "^6.4.1",
"@typescript-eslint/parser": "^6.4.1",
"chokidar-cli": "^3.0.0",
"compressing": "^1.9.1",
"concurrently": "^8.2.0",
"compressing": "^1.10.0",
"concurrently": "^8.2.1",
"cross-env": "^7.0.3",
"esbuild": "^0.19.2",
"eslint": "^8.46.0",
"eslint": "^8.47.0",
"eslint-config-prettier": "^9.0.0",
"prettier": "^3.0.1",
"prettier": "^3.0.2",
"prosemirror-model": "^1.19.3",
"prosemirror-state": "^1.4.3",
"prosemirror-transform": "^1.7.4",
"prosemirror-transform": "^1.7.5",
"prosemirror-view": "^1.31.7",
"release-it": "^16.1.4",
"release-it": "^16.1.5",
"replace-in-file": "^7.0.1",
"typescript": "^5.1.6",
"zotero-types": "^1.2.1"
"typescript": "^5.2.2",
"zotero-types": "^1.2.2"
}
}
2 changes: 0 additions & 2 deletions src/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class Addon {
};
export: {
pdf: { promise?: _ZoteroTypes.PromiseObject };
docx: { worker?: HTMLIFrameElement };
};
sync: {
lock: boolean;
Expand Down Expand Up @@ -92,7 +91,6 @@ class Addon {
// ztoolkit: new ZoteroToolkit(),
export: {
pdf: { promise: undefined },
docx: { worker: undefined },
},
sync: {
lock: false,
Expand Down
15 changes: 6 additions & 9 deletions src/extras/docxWorker.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { asBlob } from "html-docx-js-typescript";
// @ts-ignore
import htmlDocx from "html-docx-js/dist/html-docx";

// this runs in a webworker. accept input message
// this runs in a iframe. accept input message
// and return output message
onmessage = ({ data: { type, jobId, message } }) => {
if (type === "parseDocx") {
console.log("DOCX Worker", type, jobId, message);
asBlob(message)
.then((blob) => {
postMessage({ type: "parseDocxReturn", jobId, message: blob }, "*");
})
.catch((err) => {
console.log(err);
});
const blob = htmlDocx.asBlob(message);
console.log("DOCX Worker", blob);
postMessage({ type: "parseDocxReturn", jobId, message: blob }, "*");
}
};
43 changes: 21 additions & 22 deletions src/modules/export/docx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { showHintWithLink } from "../../utils/hint";
import { renderNoteHTML } from "../../utils/note";
import { getFileContent, randomString } from "../../utils/str";
import { randomString } from "../../utils/str";
import { waitUtilAsync } from "../../utils/wait";
import { config } from "../../../package.json";

export async function saveDocx(filename: string, noteId: number) {
const noteItem = Zotero.Items.get(noteId);
Expand Down Expand Up @@ -39,30 +40,28 @@ async function note2docx(noteItem: Zotero.Item) {
);
await lock.promise;
worker.contentWindow?.removeEventListener("message", listener);
destroyWorker(worker);
return blob!;
}

async function getWorker() {
if (addon.data.export.docx.worker) {
return addon.data.export.docx.worker;
}
const worker = Zotero.Browser.createHiddenBrowser(
window,
) as HTMLIFrameElement;
await waitUtilAsync(() => worker.contentDocument?.readyState === "complete");

const doc = worker.contentDocument;
ztoolkit.UI.appendElement(
{
tag: "script",
properties: {
innerHTML: await getFileContent(
rootURI + "chrome/content/scripts/docxWorker.js",
),
},
async function getWorker(): Promise<HTMLIFrameElement> {
const worker = ztoolkit.UI.createElement(document, "iframe", {
properties: {
src: `chrome://${config.addonRef}/content/docxExport.html`,
},
doc!.head,
);
addon.data.export.docx.worker = worker;
styles: {
width: "0",
height: "0",
border: "0",
position: "absolute",
},
});
window.document.documentElement.appendChild(worker);
await waitUtilAsync(() => worker.contentDocument?.readyState === "complete");
return worker;
}

function destroyWorker(worker: any) {
worker.parentNode.removeChild(worker);
worker = null;
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"target": "ES2016",
"resolveJsonModule": true,
"skipLibCheck": true,
"alwaysStrict": false,
"strict": true
},
"include": ["src", "typings", "node_modules/zotero-types"],
Expand Down

0 comments on commit c603a6e

Please sign in to comment.