mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-30 01:27:26 +08:00
This commit is contained in:
1 parent
786b1a41c2
commit
b85b3bc27d
4 files changed
+68
-16
No files matched your search
+21
-4
@@ -3215,6 +3215,14 @@ async function loadWASM() {
|
||||
}
|
||||
if (data.type === "progress") {
|
||||
setProgress(data.text, data.percent);
|
||||
} else if (data.type === "conversion") {
|
||||
if (wasmRequests.get(data.id)?.openSeq === state.openSeq && !el.cancelExportButton.disabled) {
|
||||
if (data.phase === "open") setProgress("正在读取 PDF", null);
|
||||
else if (data.phase === "pages") setProgress(data.completed ? `正在整理 ${data.completed} 页` : "正在整理页面", null);
|
||||
else if (data.phase === "convert") setProgress(`正在转换 ${data.completed} / ${data.total} 页`, data.total ? 28 + data.completed / data.total * 22 : null);
|
||||
else if (data.phase === "write.fonts") setProgress("正在处理字体", null);
|
||||
else setProgress("正在生成 OFD", null);
|
||||
}
|
||||
} else if (data.type === "export") {
|
||||
if (wasmRequests.get(data.id)?.openSeq === state.openSeq && state.exporting) {
|
||||
if (data.stage === "save") {
|
||||
@@ -3344,7 +3352,15 @@ async function openOFD(file) {
|
||||
setProgress("正在转换 PDF", 25);
|
||||
await ensureWASM();
|
||||
if (openSeq !== state.openSeq) return;
|
||||
const converted = await callWASM("ofdgoConvertPDF", bytes);
|
||||
let converted;
|
||||
try {
|
||||
converted = await callWASM("ofdgoConvertPDF", bytes);
|
||||
} finally {
|
||||
if (openSeq === state.openSeq) {
|
||||
state.exportRequestID = 0;
|
||||
el.cancelExportButton.hidden = true;
|
||||
}
|
||||
}
|
||||
if (openSeq !== state.openSeq) return;
|
||||
bytes = converted.bytes;
|
||||
warnings = JSON.parse(converted.warnings || "null") || [];
|
||||
@@ -3384,7 +3400,8 @@ async function openOFD(file) {
|
||||
await openDocument({ pageIndex: 0, resetScroll: true, openSeq });
|
||||
} catch (err) {
|
||||
if (openSeq === state.openSeq) {
|
||||
showError(err, !state.doc);
|
||||
if (err.name === "AbortError") setStatus("转换已取消");
|
||||
else showError(err, !state.doc);
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
@@ -7299,11 +7316,11 @@ async function callWASM(name, ...args) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const id = ++wasmRequestID;
|
||||
wasmRequests.set(id, { resolve, reject, openSeq: state.openSeq });
|
||||
if (name === "ofdgoExportPage" || name === "ofdgoExportDocument" || name === "ofdgoExportAttachment" || name === "ofdgoSaveDocument" || name === "ofdgoSaveEncrypted" || name === "ofdgoSaveSigned" || name === "ofdgoImportPages") {
|
||||
if (name === "ofdgoConvertPDF" || name === "ofdgoExportPage" || name === "ofdgoExportDocument" || name === "ofdgoExportAttachment" || name === "ofdgoSaveDocument" || name === "ofdgoSaveEncrypted" || name === "ofdgoSaveSigned" || name === "ofdgoImportPages") {
|
||||
state.exportRequestID = id;
|
||||
el.cancelExportButton.hidden = false;
|
||||
el.cancelExportButton.disabled = false;
|
||||
el.cancelExportButton.title = { ofdgoSaveDocument: "取消保存", ofdgoSaveEncrypted: "取消保存", ofdgoSaveSigned: "取消签署", ofdgoImportPages: "取消导入", ofdgoExportAttachment: "取消下载" }[name] || "取消导出";
|
||||
el.cancelExportButton.title = { ofdgoConvertPDF: "取消转换", ofdgoSaveDocument: "取消保存", ofdgoSaveEncrypted: "取消保存", ofdgoSaveSigned: "取消签署", ofdgoImportPages: "取消导入", ofdgoExportAttachment: "取消下载" }[name] || "取消导出";
|
||||
el.cancelExportButton.setAttribute("aria-label", el.cancelExportButton.title);
|
||||
}
|
||||
try {
|
||||
|
||||
@@ -7,7 +7,7 @@ self.onmessage = ({ data }) => {
|
||||
operations.get(data.id)?.abort();
|
||||
return;
|
||||
}
|
||||
if (data.name === "ofdgoExportPage" || data.name === "ofdgoExportDocument" || data.name === "ofdgoExportAttachment" || data.name === "ofdgoSaveDocument" || data.name === "ofdgoSaveEncrypted" || data.name === "ofdgoSaveSigned" || data.name === "ofdgoImportPages") {
|
||||
if (data.name === "ofdgoConvertPDF" || data.name === "ofdgoExportPage" || data.name === "ofdgoExportDocument" || data.name === "ofdgoExportAttachment" || data.name === "ofdgoSaveDocument" || data.name === "ofdgoSaveEncrypted" || data.name === "ofdgoSaveSigned" || data.name === "ofdgoImportPages") {
|
||||
operations.set(data.id, new AbortController());
|
||||
}
|
||||
pending = pending.then(() => handleMessage(data));
|
||||
@@ -17,6 +17,7 @@ async function handleMessage({ id, name, args }) {
|
||||
let channel;
|
||||
const signal = operations.get(id)?.signal;
|
||||
const importing = name === "ofdgoImportPages";
|
||||
const converting = name === "ofdgoConvertPDF";
|
||||
const saving = name === "ofdgoSaveDocument" || name === "ofdgoSaveEncrypted" || name === "ofdgoSaveSigned";
|
||||
const key = name === "ofdgoOpen" ? args[3]?.key : name === "ofdgoSaveSigned" || name === "ofdgoLoadImport" ? args[1]?.key : null;
|
||||
const keyPassword = name === "ofdgoOpen" ? args[3]?.keyPassword : name === "ofdgoSaveSigned" || name === "ofdgoLoadImport" ? args[1]?.keyPassword : null;
|
||||
@@ -34,10 +35,10 @@ async function handleMessage({ id, name, args }) {
|
||||
};
|
||||
channel.port2.postMessage(null);
|
||||
};
|
||||
if (importing) {
|
||||
if (importing || converting) {
|
||||
args.push((phase, completed, total, done) => {
|
||||
self.postMessage({ id, type: "import", phase, completed, total });
|
||||
finish(done, null, phase === "commit");
|
||||
self.postMessage({ id, type: importing ? "import" : "conversion", phase, completed, total });
|
||||
finish(done, null, importing && phase === "commit");
|
||||
});
|
||||
} else {
|
||||
const file = args.pop();
|
||||
@@ -74,7 +75,7 @@ async function handleMessage({ id, name, args }) {
|
||||
const payload = await globalThis[name](...args);
|
||||
signal?.throwIfAborted();
|
||||
const result = typeof payload === "string" ? JSON.parse(payload) : payload;
|
||||
if (signal && !importing && result.ok) {
|
||||
if (signal && !importing && !converting && result.ok) {
|
||||
operations.delete(id);
|
||||
result.data.size = size;
|
||||
self.postMessage({ id, type: "export", stage: "save" });
|
||||
@@ -93,7 +94,7 @@ async function handleMessage({ id, name, args }) {
|
||||
await output?.abort().catch(() => {});
|
||||
const result = { id, ok: false, error: err.message };
|
||||
if (signal?.aborted) {
|
||||
result.error = importing ? "导入已取消" : "导出已取消";
|
||||
result.error = importing ? "导入已取消" : converting ? "转换已取消" : "导出已取消";
|
||||
result.canceled = true;
|
||||
}
|
||||
self.postMessage(result);
|
||||
|
||||
Reference in new issue
Block a user