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. 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 - Page cards take their aspect ratio and width from the preview payload, so A4
and Letter documents are both displayed without distortion. 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 ## 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 | | `PUT /api/projects/<id>/file?path=` | Write a text file |
| `POST /api/projects/<id>/files?path=` | Upload a file | | `POST /api/projects/<id>/files?path=` | Upload a file |
| `DELETE /api/projects/<id>/file?path=` | Delete 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>/preview?engine=route-d\|v1` | Live preview of the main file |
| `POST /api/projects/<id>/pdf` | PDF 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`, Preview and PDF responses carry `X-Preview-Engine: route-d|v1`. The preview
`pageWidthMm`, `pageHeightMm`, and `pages` (one inline SVG and hash per page). response is a JSON object with `status`, `durationMs`, `pageWidthMm`,
Font faces are loaded from `/fonts.css` and `/fonts/*.ttf`. `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 ## Design notes
+39 -10
View File
@@ -25,6 +25,14 @@ const toastElement = $("#toast");
const IMAGE_EXTENSIONS = ["png", "jpg", "jpeg", "gif", "bmp", "webp", "svg"]; 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"]; 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 = { const state = {
projects: [], projects: [],
project: null, project: null,
@@ -36,7 +44,7 @@ const state = {
previewController: null, previewController: null,
previewRequest: 0, previewRequest: 0,
toastTimer: 0, toastTimer: 0,
routeD: localStorage.getItem("texrra-route-d") === "1", engine: initialEngine(),
}; };
function escapeHtml(value) { function escapeHtml(value) {
@@ -105,6 +113,7 @@ function setSaveStatus(text) {
function setPreviewStatus(text) { function setPreviewStatus(text) {
previewStatus.textContent = text || ""; previewStatus.textContent = text || "";
previewStatus.title = "";
} }
function route() { function route() {
@@ -452,9 +461,14 @@ function scheduleSave() {
state.saveTimer = setTimeout(saveFile, 450); state.saveTimer = setTimeout(saveFile, 450);
} }
function engineQuery() {
return state.engine === "route-d" ? "?engine=route-d" : "?engine=v1";
}
function updateEngineButton() { function updateEngineButton() {
engineButton.textContent = state.routeD ? "引擎: Route-D" : "引擎: v1"; engineButton.textContent =
engineButton.classList.toggle("primary", state.routeD); state.engine === "route-d" ? "引擎: Route-D (实验)" : "引擎: v1";
engineButton.classList.toggle("primary", state.engine === "route-d");
} }
function schedulePreview(delay) { function schedulePreview(delay) {
@@ -470,11 +484,13 @@ async function refreshPreview() {
state.previewController = controller; state.previewController = controller;
setPreviewStatus("排版中…"); setPreviewStatus("排版中…");
try { try {
const engineQuery = state.routeD ? "?engine=route-d" : ""; const response = await fetch(projectPath("/preview") + engineQuery(), {
const response = await fetch(projectPath("/preview") + engineQuery, {
method: "POST", method: "POST",
signal: controller.signal, signal: controller.signal,
}); });
const actualEngine =
response.headers.get("X-Preview-Engine") ||
(state.engine === "route-d" ? "route-d" : "v1");
if (requestId !== state.previewRequest) return; if (requestId !== state.previewRequest) return;
const payload = await response.json(); const payload = await response.json();
if (requestId !== state.previewRequest) return; if (requestId !== state.previewRequest) return;
@@ -482,12 +498,21 @@ async function refreshPreview() {
applyPages(payload.pages); applyPages(payload.pages);
hidePreviewError(); hidePreviewError();
const count = payload.pages.length; 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") { } else if (payload.status === "fallback") {
applyPages([]); applyPages([]);
showPreviewError(payload.message); showPreviewError(payload.message);
setPreviewStatus("需要 PDF 回退"); setPreviewStatus("需要 PDF 回退");
} else { } else {
previewStatus.title = "";
showPreviewError(payload.message || "编译失败"); showPreviewError(payload.message || "编译失败");
setPreviewStatus("编译错误"); setPreviewStatus("编译错误");
} }
@@ -630,12 +655,15 @@ async function createFile() {
async function exportPdf() { async function exportPdf() {
if (!state.project) return; if (!state.project) return;
try { 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) { if (!response.ok) {
toast(`PDF 编译失败:${await response.text()}`, true); toast(`PDF 编译失败:${await response.text()}`, true);
return; return;
} }
const actualEngine = response.headers.get("X-Preview-Engine");
if (actualEngine && actualEngine !== state.engine) {
toast(`PDF 由 ${actualEngine} 生成(已回退)`);
}
const blob = await response.blob(); const blob = await response.blob();
const url = URL.createObjectURL(blob); const url = URL.createObjectURL(blob);
const anchor = document.createElement("a"); const anchor = document.createElement("a");
@@ -739,8 +767,9 @@ window.addEventListener("hashchange", route);
route(); route();
engineButton.addEventListener("click", () => { engineButton.addEventListener("click", () => {
state.routeD = !state.routeD; state.engine = state.engine === "route-d" ? "v1" : "route-d";
localStorage.setItem("texrra-route-d", state.routeD ? "1" : "0"); localStorage.setItem("texrra-engine", state.engine);
localStorage.removeItem("texrra-route-d");
updateEngineButton(); updateEngineButton();
schedulePreview(0); schedulePreview(0);
}); });
+1 -1
View File
@@ -66,7 +66,7 @@
<span>实时预览</span> <span>实时预览</span>
<span class="head-right"> <span class="head-right">
<span class="status" id="preview-status"></span> <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> <button class="btn small" id="export-pdf">导出 PDF</button>
</span> </span>
</div> </div>