diff --git a/README.md b/README.md index 32e898a..3d00c70 100644 --- a/README.md +++ b/README.md @@ -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//file?path=` | Write a text file | | `POST /api/projects//files?path=` | Upload a file | | `DELETE /api/projects//file?path=` | Delete a file | -| `POST /api/projects//preview` | Live preview of the main file | -| `POST /api/projects//pdf` | PDF of the main file | +| `POST /api/projects//preview?engine=route-d\|v1` | Live preview of the main file | +| `POST /api/projects//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 diff --git a/app.js b/app.js index 6177ac6..cb29d90 100644 --- a/app.js +++ b/app.js @@ -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); }); diff --git a/index.html b/index.html index 5b19049..abd2211 100644 --- a/index.html +++ b/index.html @@ -66,7 +66,7 @@ 实时预览 - +