Apply sparse preview page patches
This commit is contained in:
1 parent
ad2b4dbdb1
commit
b7b6c65684
1 file changed
+35
-8
@@ -484,12 +484,16 @@ async function refreshPreview() {
|
||||
state.previewController = controller;
|
||||
setPreviewStatus("排版中…");
|
||||
try {
|
||||
const response = await fetch(projectPath("/preview") + engineQuery() + "&buffer=1", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "text/plain; charset=utf-8" },
|
||||
body: sourceEditor.value,
|
||||
signal: controller.signal,
|
||||
});
|
||||
const patchMode = state.engine === "route-d";
|
||||
const response = await fetch(
|
||||
projectPath("/preview") + engineQuery() + "&buffer=1" + (patchMode ? "&patch=1" : ""),
|
||||
{
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "text/plain; charset=utf-8" },
|
||||
body: sourceEditor.value,
|
||||
signal: controller.signal,
|
||||
}
|
||||
);
|
||||
const actualEngine =
|
||||
response.headers.get("X-Preview-Engine") ||
|
||||
(state.engine === "route-d" ? "route-d" : "v1");
|
||||
@@ -497,9 +501,13 @@ async function refreshPreview() {
|
||||
const payload = await response.json();
|
||||
if (requestId !== state.previewRequest) return;
|
||||
if (payload.status === "ok") {
|
||||
applyPages(payload.pages);
|
||||
if (patchMode) {
|
||||
applyPagePatches(payload);
|
||||
} else {
|
||||
applyPages(payload.pages);
|
||||
}
|
||||
hidePreviewError();
|
||||
const count = payload.pages.length;
|
||||
const count = payload.pageCount ?? payload.pages.length;
|
||||
const warnings = Array.isArray(payload.warnings) ? payload.warnings : [];
|
||||
const duration =
|
||||
typeof payload.durationMs === "number"
|
||||
@@ -542,6 +550,25 @@ function applyPages(list) {
|
||||
}
|
||||
}
|
||||
|
||||
function applyPagePatches(payload) {
|
||||
pages.hidden = false;
|
||||
const count = payload.pageCount ?? pages.children.length;
|
||||
while (pages.children.length > count) pages.lastElementChild.remove();
|
||||
while (pages.children.length < count) {
|
||||
const page = document.createElement("div");
|
||||
page.className = "page";
|
||||
pages.appendChild(page);
|
||||
}
|
||||
for (const next of payload.pages) {
|
||||
const index = Number(next.index);
|
||||
if (!Number.isInteger(index) || index < 0 || index >= count) continue;
|
||||
const page = pages.children[index];
|
||||
if (page.dataset.hash === next.hash) continue;
|
||||
page.innerHTML = next.svg;
|
||||
page.dataset.hash = next.hash;
|
||||
}
|
||||
}
|
||||
|
||||
function showPreviewError(message) {
|
||||
previewErrorText.textContent = message;
|
||||
previewError.hidden = false;
|
||||
|
||||
Reference in new issue
Block a user