Cache published project previews for the next desktop launch

This commit is contained in:
gloamxun committed 2026-12-02 16:20:18 +08:00
1 parent 11ec5c853c
commit f727188c41
8 files changed
+350 -17

No files matched your search

+35
View File
@@ -54,6 +54,14 @@ recompile.disabled = true;
previewView.toolbar.prepend(recompile);
recompile.addEventListener("click", manualCompile);
const cachedPreviewStatus = document.createElement("span");
cachedPreviewStatus.id = "cached-preview-status";
cachedPreviewStatus.className = "texrra-preview-history";
cachedPreviewStatus.setAttribute("role", "status");
cachedPreviewStatus.textContent = "上次成功预览";
cachedPreviewStatus.hidden = true;
previewView.toolbar.prepend(cachedPreviewStatus);
function attachWebControls() {
const button = document.createElement("button");
button.type = "button";
@@ -158,6 +166,7 @@ function applyPages(update, replacements) {
while (pages.children.length > update.page_count) pages.lastElementChild.remove();
}
visibleRevision = update.revision;
cachedPreviewStatus.hidden = true;
document.body.dataset.visibleRevision = visibleRevision;
return previous;
}
@@ -169,6 +178,7 @@ function restorePublishedPages() {
draft = null;
revokeDetached(discarded);
visibleRevision = publishedRevision;
cachedPreviewStatus.hidden = visibleRevision !== null || pages.childElementCount === 0;
if (visibleRevision === null) delete document.body.dataset.visibleRevision;
else document.body.dataset.visibleRevision = visibleRevision;
pdf.hidden = publishedRevision === null;
@@ -177,6 +187,30 @@ function restorePublishedPages() {
}
}
async function showCachedPreview() {
const frames = [];
try {
const response = await fetch(projectPrefix + "/cached-preview");
if (!response.ok || visibleRevision !== null) return;
const { page_count, pages: savedPages } = await response.json();
if (visibleRevision !== null || !Array.isArray(savedPages) || savedPages.length === 0
|| savedPages.length !== page_count || savedPages.some((page, index) =>
page?.index !== index || typeof page.svg !== "string")) return;
const first = pageFrame(savedPages[0]);
frames.push(first);
// The rest of the saved document can decode after the first page is ready.
await first.querySelector("img").decode();
if (visibleRevision !== null) return;
for (const page of savedPages.slice(1)) frames.push(pageFrame(page));
pages.replaceChildren(...frames);
cachedPreviewStatus.hidden = false;
} catch {
// Cache errors do not interrupt the live compile.
} finally {
revokeDetached(frames);
}
}
function requestPreviewText(revision) {
clearTimeout(textLayerTimer);
textLayerRequest?.abort();
@@ -589,6 +623,7 @@ async function openProject() {
event.preventDefault();
indentSource();
});
if (project.cached_preview === true) void showCachedPreview();
if (preparingProject) {
status.textContent = "正在准备编译资源";
const ready = await fetch(projectPrefix + "/ready");