Make the engine choice explicit and show route-D telemetry

The toolbar now treats the engine as route-d|v1 instead of a boolean whose v1
state omitted the query parameter (which now means route-d on the server). Both
preview and PDF always send an explicit engine query, the old localStorage key
migrates, and route-D is the default with a v1 escape hatch.

The preview status tolerates a missing duration, displays the warning count with
details on hover, and reports when X-Preview-Engine differs from the selected
engine; PDF export shows a fallback notice. The README documents the toggle and
the X-Preview-Engine response header.
This commit is contained in:
gloamxun committed 2026-10-27 12:23:32 +08:00
1 parent 24c95938d2
commit cecad51f91
3 files changed
+53 -16

No files matched your search

+13 -5
View File
@@ -29,6 +29,12 @@ The landing page manages uploaded projects; opening one switches to the editor.
hash changed, so typing does not reload the view or jump the scroll position.
- Page cards take their aspect ratio and width from the preview payload, so A4
and Letter documents are both displayed without distortion.
- The preview toolbar switches between the experimental **Route-D** core and the
legacy **v1** pipeline. The choice is stored in `localStorage` and applies to
both preview and PDF export; Route-D is the default. The response header
`X-Preview-Engine` reports the engine that actually rendered (including a
fallback), and the status line shows preview duration, page count, warning
count (details on hover) and any engine fallback.
## Run
@@ -72,12 +78,14 @@ The frontend assumes the engine is the same origin and exposes:
| `PUT /api/projects/<id>/file?path=` | Write a text file |
| `POST /api/projects/<id>/files?path=` | Upload a file |
| `DELETE /api/projects/<id>/file?path=` | Delete a file |
| `POST /api/projects/<id>/preview` | Live preview of the main file |
| `POST /api/projects/<id>/pdf` | PDF of the main file |
| `POST /api/projects/<id>/preview?engine=route-d\|v1` | Live preview of the main file |
| `POST /api/projects/<id>/pdf?engine=route-d\|v1` | PDF of the main file |
The preview response is a JSON object with `status`, `durationMs`,
`pageWidthMm`, `pageHeightMm`, and `pages` (one inline SVG and hash per page).
Font faces are loaded from `/fonts.css` and `/fonts/*.ttf`.
Preview and PDF responses carry `X-Preview-Engine: route-d|v1`. The preview
response is a JSON object with `status`, `durationMs`, `pageWidthMm`,
`pageHeightMm`, and `pages` (one inline SVG and hash per page); Route-D
responses additionally carry `warnings` and edit metadata (`changedPages`,
`recomputedPages`). Font faces are loaded from `/fonts.css` and `/fonts/*.ttf`.
## Design notes
+39 -10
View File
@@ -25,6 +25,14 @@ const toastElement = $("#toast");
const IMAGE_EXTENSIONS = ["png", "jpg", "jpeg", "gif", "bmp", "webp", "svg"];
const TEXT_EXTENSIONS = ["tex", "sty", "cls", "bib", "bst", "txt", "md", "csv", "json", "yaml", "yml", "cfg", "dat", "tikz", "ltx"];
function initialEngine() {
const stored = localStorage.getItem("texrra-engine");
if (stored === "v1") return "v1";
if (stored === "route-d") return "route-d";
// Old boolean key: "0" meant v1, "1" or absent meant route-D.
return localStorage.getItem("texrra-route-d") === "0" ? "v1" : "route-d";
}
const state = {
projects: [],
project: null,
@@ -36,7 +44,7 @@ const state = {
previewController: null,
previewRequest: 0,
toastTimer: 0,
routeD: localStorage.getItem("texrra-route-d") === "1",
engine: initialEngine(),
};
function escapeHtml(value) {
@@ -105,6 +113,7 @@ function setSaveStatus(text) {
function setPreviewStatus(text) {
previewStatus.textContent = text || "";
previewStatus.title = "";
}
function route() {
@@ -452,9 +461,14 @@ function scheduleSave() {
state.saveTimer = setTimeout(saveFile, 450);
}
function engineQuery() {
return state.engine === "route-d" ? "?engine=route-d" : "?engine=v1";
}
function updateEngineButton() {
engineButton.textContent = state.routeD ? "引擎: Route-D" : "引擎: v1";
engineButton.classList.toggle("primary", state.routeD);
engineButton.textContent =
state.engine === "route-d" ? "引擎: Route-D (实验)" : "引擎: v1";
engineButton.classList.toggle("primary", state.engine === "route-d");
}
function schedulePreview(delay) {
@@ -470,11 +484,13 @@ async function refreshPreview() {
state.previewController = controller;
setPreviewStatus("排版中…");
try {
const engineQuery = state.routeD ? "?engine=route-d" : "";
const response = await fetch(projectPath("/preview") + engineQuery, {
const response = await fetch(projectPath("/preview") + engineQuery(), {
method: "POST",
signal: controller.signal,
});
const actualEngine =
response.headers.get("X-Preview-Engine") ||
(state.engine === "route-d" ? "route-d" : "v1");
if (requestId !== state.previewRequest) return;
const payload = await response.json();
if (requestId !== state.previewRequest) return;
@@ -482,12 +498,21 @@ async function refreshPreview() {
applyPages(payload.pages);
hidePreviewError();
const count = payload.pages.length;
setPreviewStatus(`${payload.durationMs.toFixed(1)} ms · ${count} 页`);
const warnings = Array.isArray(payload.warnings) ? payload.warnings : [];
const duration =
typeof payload.durationMs === "number"
? `${payload.durationMs.toFixed(1)} ms`
: "—";
const fallback = actualEngine !== state.engine ? ` · 实际 ${actualEngine}` : "";
const warningText = warnings.length ? ` · ${warnings.length} 条警告` : "";
setPreviewStatus(`${duration} · ${count} 页${warningText}${fallback}`);
previewStatus.title = warnings.join("\n");
} else if (payload.status === "fallback") {
applyPages([]);
showPreviewError(payload.message);
setPreviewStatus("需要 PDF 回退");
} else {
previewStatus.title = "";
showPreviewError(payload.message || "编译失败");
setPreviewStatus("编译错误");
}
@@ -630,12 +655,15 @@ async function createFile() {
async function exportPdf() {
if (!state.project) return;
try {
const engineQuery = state.routeD ? "?engine=route-d" : "";
const response = await fetch(projectPath("/pdf") + engineQuery, { method: "POST" });
const response = await fetch(projectPath("/pdf") + engineQuery(), { method: "POST" });
if (!response.ok) {
toast(`PDF 编译失败:${await response.text()}`, true);
return;
}
const actualEngine = response.headers.get("X-Preview-Engine");
if (actualEngine && actualEngine !== state.engine) {
toast(`PDF 由 ${actualEngine} 生成(已回退)`);
}
const blob = await response.blob();
const url = URL.createObjectURL(blob);
const anchor = document.createElement("a");
@@ -739,8 +767,9 @@ window.addEventListener("hashchange", route);
route();
engineButton.addEventListener("click", () => {
state.routeD = !state.routeD;
localStorage.setItem("texrra-route-d", state.routeD ? "1" : "0");
state.engine = state.engine === "route-d" ? "v1" : "route-d";
localStorage.setItem("texrra-engine", state.engine);
localStorage.removeItem("texrra-route-d");
updateEngineButton();
schedulePreview(0);
});
+1 -1
View File
@@ -66,7 +66,7 @@
<span>实时预览</span>
<span class="head-right">
<span class="status" id="preview-status"></span>
<button class="btn small" id="toggle-engine" title="切换预览使用的排版内核">引擎: v1</button>
<button class="btn small" id="toggle-engine" title="切换预览使用的排版内核(Route-D 为实验引擎)">引擎: Route-D (实验)</button>
<button class="btn small" id="export-pdf">导出 PDF</button>
</span>
</div>