Add workbench recompile, LaTeX assistance, and selectable preview
This commit is contained in:
1 parent
aaf2550c68
commit
e0d9b3a91f
12 files changed
+1059
-28
No files matched your search
@@ -1,3 +1,6 @@
|
||||
import { attachCodeEditor } from '/code_editor.js';
|
||||
import { attachPreviewView } from '/preview_view.js';
|
||||
|
||||
const workspace = document.querySelector("#workspace");
|
||||
if (workspace) attachWebControls();
|
||||
|
||||
@@ -15,8 +18,10 @@ const fileTree = document.querySelector("#file-tree");
|
||||
const edits = new Map();
|
||||
const fileViews = new Map();
|
||||
const EDIT_IDLE_MS = 70;
|
||||
const TEXT_LAYER_IDLE_MS = 180;
|
||||
const PREVIEW_SOCKET_RETRY_MS = 1000;
|
||||
let files;
|
||||
let projectFiles = [];
|
||||
let activeFile;
|
||||
let sequence = 0;
|
||||
let publishedRevision = null;
|
||||
@@ -29,9 +34,24 @@ let previewSocketUrl;
|
||||
let previewSocket;
|
||||
let previewSocketRetryAt = 0;
|
||||
let pendingSocketPreview;
|
||||
let textLayerTimer;
|
||||
let textLayerRequest;
|
||||
let textLayerRevision;
|
||||
const projectPrefix = location.pathname.startsWith("/p/") ? location.pathname.slice(0, -1) : "";
|
||||
const previewDraft = new URLSearchParams(location.hash.slice(1)).get("preview") !== "published";
|
||||
|
||||
const codeEditor = attachCodeEditor(source, { getFileNames: () => projectFiles });
|
||||
const previewView = attachPreviewView(pages);
|
||||
const recompile = document.createElement("button");
|
||||
recompile.type = "button";
|
||||
recompile.id = "recompile";
|
||||
recompile.className = "btn small";
|
||||
recompile.textContent = "重新编译";
|
||||
recompile.setAttribute("aria-keyshortcuts", "Control+Enter Meta+Enter");
|
||||
recompile.disabled = true;
|
||||
previewView.toolbar.prepend(recompile);
|
||||
recompile.addEventListener("click", manualCompile);
|
||||
|
||||
function attachWebControls() {
|
||||
const button = document.createElement("button");
|
||||
button.type = "button";
|
||||
@@ -87,6 +107,7 @@ function selectFile(filename) {
|
||||
source.setSelectionRange(view?.start ?? 0, view?.end ?? 0, view?.direction ?? "none");
|
||||
source.scrollTop = view?.top ?? 0;
|
||||
source.scrollLeft = view?.left ?? 0;
|
||||
codeEditor.sync();
|
||||
if (fileTree) {
|
||||
for (const row of fileTree.children) {
|
||||
const selected = row.dataset.path === activeFile;
|
||||
@@ -102,7 +123,7 @@ function showError(message) {
|
||||
errorPanel.hidden = false;
|
||||
}
|
||||
|
||||
function pageImage(page) {
|
||||
function pageFrame(page) {
|
||||
const img = new Image();
|
||||
img.alt = `第 ${page.index + 1} 页`;
|
||||
img.dataset.index = page.index;
|
||||
@@ -111,12 +132,12 @@ function pageImage(page) {
|
||||
img.style.aspectRatio = "auto";
|
||||
}
|
||||
img.src = URL.createObjectURL(new Blob([page.svg], { type: "image/svg+xml" }));
|
||||
return img;
|
||||
return previewView.wrap(img, page);
|
||||
}
|
||||
|
||||
function revokeDetached(images) {
|
||||
for (const image of images) {
|
||||
if (!image.isConnected) URL.revokeObjectURL(image.src);
|
||||
function revokeDetached(frames) {
|
||||
for (const frame of frames) {
|
||||
if (!frame.isConnected) URL.revokeObjectURL(frame.querySelector("img").src);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,10 +146,10 @@ function applyPages(update, replacements) {
|
||||
if (update.base_revision === null) {
|
||||
pages.replaceChildren(...replacements);
|
||||
} else {
|
||||
for (const image of replacements) {
|
||||
const old = pages.children[Number(image.dataset.index)];
|
||||
if (old) old.replaceWith(image);
|
||||
else pages.append(image);
|
||||
for (const frame of replacements) {
|
||||
const old = pages.children[Number(frame.dataset.index)];
|
||||
if (old) old.replaceWith(frame);
|
||||
else pages.append(frame);
|
||||
}
|
||||
while (pages.children.length > update.page_count) pages.lastElementChild.remove();
|
||||
}
|
||||
@@ -145,6 +166,32 @@ function restorePublishedPages() {
|
||||
else document.body.dataset.visibleRevision = publishedRevision;
|
||||
pdf.hidden = publishedRevision === null;
|
||||
draft = null;
|
||||
if (publishedRevision !== null && textLayerRevision !== publishedRevision) {
|
||||
requestPreviewText(publishedRevision);
|
||||
}
|
||||
}
|
||||
|
||||
function requestPreviewText(revision) {
|
||||
clearTimeout(textLayerTimer);
|
||||
textLayerRequest?.abort();
|
||||
const request = new AbortController();
|
||||
textLayerRequest = request;
|
||||
// Text extraction runs after the SVG is published and quiet, so it stays
|
||||
// off the visible editing path. Discard data if another revision appears.
|
||||
textLayerTimer = setTimeout(async () => {
|
||||
try {
|
||||
const response = await fetch(projectPrefix + "/text-layer?revision=" + revision,
|
||||
{ signal: request.signal });
|
||||
if (!response.ok) return;
|
||||
const result = await response.json();
|
||||
if (!request.signal.aborted && !draft && publishedRevision === revision && result.revision === revision) {
|
||||
previewView.setTextPages(result.pages);
|
||||
textLayerRevision = revision;
|
||||
}
|
||||
} catch {
|
||||
// The visual preview can still be used when its optional text layer is unavailable.
|
||||
}
|
||||
}, TEXT_LAYER_IDLE_MS);
|
||||
}
|
||||
|
||||
function acceptPublication(revision, previous) {
|
||||
@@ -153,6 +200,7 @@ function acceptPublication(revision, previous) {
|
||||
pdf.href = projectPrefix + `/pdf?revision=${revision}`;
|
||||
pdf.hidden = false;
|
||||
draft = null;
|
||||
requestPreviewText(revision);
|
||||
}
|
||||
|
||||
async function publicationStatus(edit, revision) {
|
||||
@@ -271,8 +319,8 @@ async function compile(edit, submitted, changes) {
|
||||
if (result.sequence !== edit || (update.base_revision !== null && update.base_revision !== publishedRevision)) {
|
||||
throw new Error("预览版本与当前页面不一致");
|
||||
}
|
||||
for (const page of update.pages) replacements.push(pageImage(page));
|
||||
await Promise.all(replacements.map((page) => page.decode()));
|
||||
for (const page of update.pages) replacements.push(pageFrame(page));
|
||||
await Promise.all(replacements.map((frame) => frame.querySelector("img").decode()));
|
||||
if (edit !== sequence) {
|
||||
revokeDetached(replacements);
|
||||
return;
|
||||
@@ -359,6 +407,11 @@ function queueCompile(immediate = false) {
|
||||
}, prompt ? 0 : EDIT_IDLE_MS);
|
||||
}
|
||||
|
||||
function manualCompile() {
|
||||
if (recompile.disabled || composing) return;
|
||||
recordEdit(true);
|
||||
}
|
||||
|
||||
function recordEdit(forceCompile = false) {
|
||||
const changed = source.value !== (edits.get(activeFile) ?? files.get(activeFile));
|
||||
if (changed) edits.set(activeFile, source.value);
|
||||
@@ -377,6 +430,7 @@ function replaceSourceRange(text, start, end, selectionStart, selectionEnd, dire
|
||||
}
|
||||
if (source.value === before) source.setRangeText(text, start, end, "end");
|
||||
source.setSelectionRange(selectionStart, selectionEnd, direction);
|
||||
codeEditor.sync();
|
||||
recordEdit();
|
||||
}
|
||||
|
||||
@@ -453,6 +507,7 @@ async function openProject() {
|
||||
if (!response.ok) throw new Error("无法打开源文件");
|
||||
const project = await response.json();
|
||||
files = new Map(Object.entries(project.files));
|
||||
projectFiles = project.project_files ?? [...files.keys()];
|
||||
if (fileTree) {
|
||||
document.querySelector("#project-title").textContent = project.name;
|
||||
for (const filename of files.keys()) {
|
||||
@@ -476,9 +531,15 @@ async function openProject() {
|
||||
sequence = project.sequence;
|
||||
advertisePreviewSocket(project.preview_socket);
|
||||
save.disabled = false;
|
||||
recompile.disabled = false;
|
||||
recompile.title = "重新编译主文件 " + project.name + "(Ctrl/Cmd+Enter)";
|
||||
save.addEventListener("click", saveSources);
|
||||
document.addEventListener("keydown", (event) => {
|
||||
if (composing || event.isComposing) return;
|
||||
if (event.defaultPrevented || composing || event.isComposing) return;
|
||||
if ((event.ctrlKey || event.metaKey) && event.key === "Enter") {
|
||||
event.preventDefault();
|
||||
if (!event.repeat) manualCompile();
|
||||
}
|
||||
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "s") {
|
||||
event.preventDefault();
|
||||
if (!save.disabled) saveSources();
|
||||
@@ -509,7 +570,7 @@ async function openProject() {
|
||||
recordEdit();
|
||||
});
|
||||
source.addEventListener("keydown", (event) => {
|
||||
if (event.key !== "Tab" || composing || event.isComposing
|
||||
if (event.defaultPrevented || event.key !== "Tab" || composing || event.isComposing
|
||||
|| event.shiftKey || event.ctrlKey || event.metaKey || event.altKey) return;
|
||||
event.preventDefault();
|
||||
indentSource();
|
||||
|
||||
Reference in new issue
Block a user