Apply sparse preview page patches
This commit is contained in:
1 parent
ad2b4dbdb1
commit
b7b6c65684
1 file changed
+30
-3
@@ -484,12 +484,16 @@ async function refreshPreview() {
|
|||||||
state.previewController = controller;
|
state.previewController = controller;
|
||||||
setPreviewStatus("排版中…");
|
setPreviewStatus("排版中…");
|
||||||
try {
|
try {
|
||||||
const response = await fetch(projectPath("/preview") + engineQuery() + "&buffer=1", {
|
const patchMode = state.engine === "route-d";
|
||||||
|
const response = await fetch(
|
||||||
|
projectPath("/preview") + engineQuery() + "&buffer=1" + (patchMode ? "&patch=1" : ""),
|
||||||
|
{
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "text/plain; charset=utf-8" },
|
headers: { "Content-Type": "text/plain; charset=utf-8" },
|
||||||
body: sourceEditor.value,
|
body: sourceEditor.value,
|
||||||
signal: controller.signal,
|
signal: controller.signal,
|
||||||
});
|
}
|
||||||
|
);
|
||||||
const actualEngine =
|
const actualEngine =
|
||||||
response.headers.get("X-Preview-Engine") ||
|
response.headers.get("X-Preview-Engine") ||
|
||||||
(state.engine === "route-d" ? "route-d" : "v1");
|
(state.engine === "route-d" ? "route-d" : "v1");
|
||||||
@@ -497,9 +501,13 @@ async function refreshPreview() {
|
|||||||
const payload = await response.json();
|
const payload = await response.json();
|
||||||
if (requestId !== state.previewRequest) return;
|
if (requestId !== state.previewRequest) return;
|
||||||
if (payload.status === "ok") {
|
if (payload.status === "ok") {
|
||||||
|
if (patchMode) {
|
||||||
|
applyPagePatches(payload);
|
||||||
|
} else {
|
||||||
applyPages(payload.pages);
|
applyPages(payload.pages);
|
||||||
|
}
|
||||||
hidePreviewError();
|
hidePreviewError();
|
||||||
const count = payload.pages.length;
|
const count = payload.pageCount ?? payload.pages.length;
|
||||||
const warnings = Array.isArray(payload.warnings) ? payload.warnings : [];
|
const warnings = Array.isArray(payload.warnings) ? payload.warnings : [];
|
||||||
const duration =
|
const duration =
|
||||||
typeof payload.durationMs === "number"
|
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) {
|
function showPreviewError(message) {
|
||||||
previewErrorText.textContent = message;
|
previewErrorText.textContent = message;
|
||||||
previewError.hidden = false;
|
previewError.hidden = false;
|
||||||
|
|||||||
Reference in new issue
Block a user