Show native preview during durable file publication
This commit is contained in:
1 parent
75d156f0cf
commit
eda01fb689
5 files changed
+439
-58
No files matched your search
@@ -4,8 +4,10 @@ const status = document.querySelector("#status");
|
||||
const error = document.querySelector("#error");
|
||||
const pdf = document.querySelector("#pdf");
|
||||
let sequence = 0;
|
||||
let visibleRevision = null;
|
||||
let publishedRevision = null;
|
||||
let draft = null;
|
||||
let frame;
|
||||
const previewDraft = new URLSearchParams(location.hash.slice(1)).get("preview") !== "published";
|
||||
|
||||
function showError(message) {
|
||||
status.textContent = "编译失败";
|
||||
@@ -21,60 +23,99 @@ function pageImage(page) {
|
||||
return img;
|
||||
}
|
||||
|
||||
function removeTrailingPages(count) {
|
||||
while (pages.children.length > count) {
|
||||
const old = pages.lastElementChild;
|
||||
URL.revokeObjectURL(old.src);
|
||||
old.remove();
|
||||
function revokeDetached(images) {
|
||||
for (const image of images) {
|
||||
if (!image.isConnected) URL.revokeObjectURL(image.src);
|
||||
}
|
||||
}
|
||||
|
||||
function applyPages(update, replacements) {
|
||||
const previous = [...pages.children];
|
||||
if (update.base_revision === null) {
|
||||
pages.replaceChildren(...replacements);
|
||||
} else {
|
||||
for (const image of replacements) {
|
||||
const old = pages.children[Number(image.dataset.index)];
|
||||
if (old) old.replaceWith(image);
|
||||
else pages.append(image);
|
||||
}
|
||||
while (pages.children.length > update.page_count) pages.lastElementChild.remove();
|
||||
}
|
||||
document.body.dataset.visibleRevision = update.revision;
|
||||
return previous;
|
||||
}
|
||||
|
||||
function restorePublishedPages() {
|
||||
if (!draft) return;
|
||||
const discarded = [...pages.children];
|
||||
pages.replaceChildren(...draft.previous);
|
||||
revokeDetached(discarded);
|
||||
if (publishedRevision === null) delete document.body.dataset.visibleRevision;
|
||||
else document.body.dataset.visibleRevision = publishedRevision;
|
||||
pdf.hidden = publishedRevision === null;
|
||||
draft = null;
|
||||
}
|
||||
|
||||
function acceptPublication(revision, previous) {
|
||||
revokeDetached(previous);
|
||||
publishedRevision = revision;
|
||||
pdf.href = `/pdf?revision=${revision}`;
|
||||
pdf.hidden = false;
|
||||
draft = null;
|
||||
}
|
||||
|
||||
async function publicationStatus(edit, revision) {
|
||||
while (edit === sequence) {
|
||||
const response = await fetch(`/publication?revision=${revision}`);
|
||||
const result = await response.json();
|
||||
if (!response.ok || result.state === "failed") throw new Error(result.error);
|
||||
if (result.state === "published") return result;
|
||||
await new Promise((resolve) => setTimeout(resolve, 40));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function compile(edit, submitted) {
|
||||
const replacements = [];
|
||||
try {
|
||||
const response = await fetch("/preview", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ sequence: edit, visible_revision: visibleRevision, source: source.value }),
|
||||
body: JSON.stringify({
|
||||
sequence: edit,
|
||||
visible_revision: publishedRevision,
|
||||
source: source.value,
|
||||
preview_draft: previewDraft,
|
||||
}),
|
||||
});
|
||||
const result = await response.json();
|
||||
if (edit !== sequence) return;
|
||||
if (!response.ok) throw new Error(result.error);
|
||||
const update = result.update;
|
||||
if (result.sequence !== edit || (update.base_revision !== null && update.base_revision !== visibleRevision)) {
|
||||
if (result.sequence !== edit || (update.base_revision !== null && update.base_revision !== publishedRevision)) {
|
||||
throw new Error("预览版本与当前页面不一致");
|
||||
}
|
||||
for (const page of update.pages) replacements.push(pageImage(page));
|
||||
await Promise.all(replacements.map((page) => page.decode()));
|
||||
if (edit !== sequence) {
|
||||
for (const img of replacements) URL.revokeObjectURL(img.src);
|
||||
revokeDetached(replacements);
|
||||
return;
|
||||
}
|
||||
if (update.base_revision === null) {
|
||||
removeTrailingPages(0);
|
||||
pages.replaceChildren(...replacements);
|
||||
const previous = applyPages(update, replacements);
|
||||
if (result.pending_publication) {
|
||||
draft = { previous };
|
||||
pdf.hidden = true;
|
||||
status.textContent = "正在写入文件";
|
||||
} else {
|
||||
for (const img of replacements) {
|
||||
const old = pages.children[Number(img.dataset.index)];
|
||||
if (old) {
|
||||
old.replaceWith(img);
|
||||
URL.revokeObjectURL(old.src);
|
||||
} else {
|
||||
pages.append(img);
|
||||
}
|
||||
}
|
||||
removeTrailingPages(update.page_count);
|
||||
acceptPublication(update.revision, previous);
|
||||
}
|
||||
visibleRevision = update.revision;
|
||||
pdf.href = `/pdf?revision=${visibleRevision}`;
|
||||
pdf.hidden = false;
|
||||
error.hidden = true;
|
||||
await new Promise(requestAnimationFrame);
|
||||
await new Promise(requestAnimationFrame);
|
||||
if (edit !== sequence) return;
|
||||
const sample = {
|
||||
sequence: edit,
|
||||
revision: visibleRevision,
|
||||
revision: update.revision,
|
||||
changed: update.pages.map((page) => page.index + 1),
|
||||
count: update.page_count,
|
||||
compileMs: result.compile_ms,
|
||||
@@ -86,15 +127,29 @@ async function compile(edit, submitted) {
|
||||
reusedSteps: result.reused_steps,
|
||||
reusedFrontier: result.reused_frontier,
|
||||
replayedCalls: result.replayed_calls,
|
||||
pendingPublication: result.pending_publication === true,
|
||||
};
|
||||
status.textContent = `${sample.count} 页 · ${Math.round(sample.visibleMs)} 毫秒`;
|
||||
document.body.dataset.visibleRevision = visibleRevision;
|
||||
window.dispatchEvent(new CustomEvent("texrra-preview-painted", { detail: sample }));
|
||||
if (result.pending_publication) {
|
||||
const committed = await publicationStatus(edit, update.revision);
|
||||
if (edit !== sequence || !committed) return;
|
||||
acceptPublication(update.revision, draft.previous);
|
||||
window.dispatchEvent(new CustomEvent("texrra-preview-published", {
|
||||
detail: {
|
||||
sequence: edit,
|
||||
revision: update.revision,
|
||||
compileMs: committed.compile_ms,
|
||||
publicationMs: committed.publication_ms,
|
||||
},
|
||||
}));
|
||||
}
|
||||
status.textContent = `${sample.count} 页 · ${Math.round(sample.visibleMs)} 毫秒`;
|
||||
} catch (failure) {
|
||||
for (const img of replacements) {
|
||||
if (!img.isConnected) URL.revokeObjectURL(img.src);
|
||||
revokeDetached(replacements);
|
||||
if (edit === sequence) {
|
||||
restorePublishedPages();
|
||||
showError(failure.message);
|
||||
}
|
||||
if (edit === sequence) showError(failure.message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,6 +157,7 @@ function queueCompile() {
|
||||
const edit = ++sequence;
|
||||
const submitted = performance.now();
|
||||
cancelAnimationFrame(frame);
|
||||
restorePublishedPages();
|
||||
status.textContent = "正在编译";
|
||||
frame = requestAnimationFrame(() => compile(edit, submitted));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user