Improve LaTeX input in the native web editor
This commit is contained in:
1 parent
b081c951af
commit
aed7010cba
3 files changed
+204
-19
No files matched your search
+138
-10
@@ -13,11 +13,15 @@ const selector = document.querySelector("#project-files");
|
||||
const name = document.querySelector("#name, #open-file-path");
|
||||
const fileTree = document.querySelector("#file-tree");
|
||||
const edits = new Map();
|
||||
const fileViews = new Map();
|
||||
const EDIT_IDLE_MS = 70;
|
||||
let files;
|
||||
let activeFile;
|
||||
let sequence = 0;
|
||||
let publishedRevision = null;
|
||||
let draft = null;
|
||||
let composing = false;
|
||||
let compileTimer;
|
||||
let frame;
|
||||
const projectPrefix = location.pathname.startsWith("/p/") ? location.pathname.slice(0, -1) : "";
|
||||
const previewDraft = new URLSearchParams(location.hash.slice(1)).get("preview") !== "published";
|
||||
@@ -52,9 +56,31 @@ function attachWebControls() {
|
||||
}
|
||||
|
||||
function selectFile(filename) {
|
||||
if (filename === activeFile) return;
|
||||
if (activeFile !== undefined) {
|
||||
if (composing) {
|
||||
// Let the platform commit the old file before replacing its buffer.
|
||||
source.blur();
|
||||
if (composing) {
|
||||
composing = false;
|
||||
queueCompile();
|
||||
}
|
||||
}
|
||||
fileViews.set(activeFile, {
|
||||
start: source.selectionStart,
|
||||
end: source.selectionEnd,
|
||||
direction: source.selectionDirection,
|
||||
top: source.scrollTop,
|
||||
left: source.scrollLeft,
|
||||
});
|
||||
}
|
||||
activeFile = filename;
|
||||
name.textContent = filename;
|
||||
source.value = edits.get(filename) ?? files.get(filename);
|
||||
const view = fileViews.get(filename);
|
||||
source.setSelectionRange(view?.start ?? 0, view?.end ?? 0, view?.direction ?? "none");
|
||||
source.scrollTop = view?.top ?? 0;
|
||||
source.scrollLeft = view?.left ?? 0;
|
||||
if (fileTree) {
|
||||
for (const row of fileTree.children) {
|
||||
const selected = row.dataset.path === activeFile;
|
||||
@@ -215,14 +241,92 @@ async function compile(edit, submitted, changes) {
|
||||
}
|
||||
}
|
||||
|
||||
function queueCompile() {
|
||||
const edit = ++sequence;
|
||||
const submitted = performance.now();
|
||||
function invalidatePreview() {
|
||||
++sequence;
|
||||
clearTimeout(compileTimer);
|
||||
cancelAnimationFrame(frame);
|
||||
restorePublishedPages();
|
||||
status.textContent = "正在编译";
|
||||
const changes = Object.fromEntries(edits);
|
||||
frame = requestAnimationFrame(() => compile(edit, submitted, changes));
|
||||
}
|
||||
|
||||
function queueCompile(delay = EDIT_IDLE_MS) {
|
||||
invalidatePreview();
|
||||
const edit = sequence;
|
||||
const submitted = performance.now();
|
||||
if (status.textContent !== "正在编译") status.textContent = "正在编译";
|
||||
const submit = () => {
|
||||
if (edit !== sequence || composing) return;
|
||||
compile(edit, submitted, Object.fromEntries(edits));
|
||||
};
|
||||
if (delay) compileTimer = setTimeout(submit, delay);
|
||||
else frame = requestAnimationFrame(submit);
|
||||
}
|
||||
|
||||
function recordEdit(forceCompile = false) {
|
||||
const changed = source.value !== (edits.get(activeFile) ?? files.get(activeFile));
|
||||
if (changed) edits.set(activeFile, source.value);
|
||||
if (changed || forceCompile) queueCompile();
|
||||
}
|
||||
|
||||
function replaceSourceRange(text, start, end, selectionStart, selectionEnd, direction) {
|
||||
source.focus({ preventScroll: true });
|
||||
source.setSelectionRange(start, end);
|
||||
const before = source.value;
|
||||
try {
|
||||
// Use the browser's editing command when available so insertion is undoable.
|
||||
document.execCommand("insertText", false, text);
|
||||
} catch {
|
||||
// The range API also works when the editing command is unavailable.
|
||||
}
|
||||
if (source.value === before) source.setRangeText(text, start, end, "end");
|
||||
source.setSelectionRange(selectionStart, selectionEnd, direction);
|
||||
recordEdit();
|
||||
}
|
||||
|
||||
function indentSource() {
|
||||
const start = source.selectionStart;
|
||||
const end = source.selectionEnd;
|
||||
if (start === end) {
|
||||
replaceSourceRange(" ", start, end, start + 2, start + 2, "none");
|
||||
return;
|
||||
}
|
||||
const firstLine = start === 0 ? 0 : source.value.lastIndexOf("\n", start - 1) + 1;
|
||||
const selection = source.value.slice(firstLine, end);
|
||||
const indented = selection.split("\n").map((line, index, lines) =>
|
||||
index === lines.length - 1 && line === "" ? line : " " + line
|
||||
).join("\n");
|
||||
replaceSourceRange(indented, firstLine, end, start + 2,
|
||||
end + indented.length - selection.length, source.selectionDirection);
|
||||
}
|
||||
|
||||
function addFormulaButtons() {
|
||||
const heading = document.querySelector(".editor-panel .panel-head, .edit .file");
|
||||
if (!heading) return;
|
||||
const group = document.createElement("span");
|
||||
group.setAttribute("role", "group");
|
||||
group.setAttribute("aria-label", "插入公式");
|
||||
group.style.display = "inline-flex";
|
||||
group.style.gap = ".35rem";
|
||||
group.style.marginInlineStart = ".5rem";
|
||||
group.style.whiteSpace = "nowrap";
|
||||
for (const [label, open, close] of [
|
||||
["行内公式", "$", "$"],
|
||||
["独立公式", "\\[\n", "\n\\]"],
|
||||
]) {
|
||||
const button = document.createElement("button");
|
||||
button.type = "button";
|
||||
button.className = "btn small";
|
||||
button.textContent = label;
|
||||
button.addEventListener("click", () => {
|
||||
if (composing || source.disabled || source.readOnly) return;
|
||||
const start = source.selectionStart;
|
||||
const end = source.selectionEnd;
|
||||
const selection = source.value.slice(start, end);
|
||||
replaceSourceRange(open + selection + close, start, end,
|
||||
start + open.length, end + open.length, source.selectionDirection);
|
||||
});
|
||||
group.append(button);
|
||||
}
|
||||
heading.append(group);
|
||||
}
|
||||
|
||||
async function saveSources() {
|
||||
@@ -267,10 +371,15 @@ async function openProject() {
|
||||
}
|
||||
selectFile(project.name);
|
||||
source.disabled = false;
|
||||
source.setAttribute("autocapitalize", "off");
|
||||
source.setAttribute("autocorrect", "off");
|
||||
source.setAttribute("autocomplete", "off");
|
||||
addFormulaButtons();
|
||||
sequence = project.sequence;
|
||||
save.disabled = false;
|
||||
save.addEventListener("click", saveSources);
|
||||
document.addEventListener("keydown", (event) => {
|
||||
if (composing || event.isComposing) return;
|
||||
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "s") {
|
||||
event.preventDefault();
|
||||
if (!save.disabled) saveSources();
|
||||
@@ -283,11 +392,30 @@ async function openProject() {
|
||||
name.hidden = true;
|
||||
selector.addEventListener("change", () => selectFile(selector.value));
|
||||
}
|
||||
source.addEventListener("input", () => {
|
||||
edits.set(activeFile, source.value);
|
||||
queueCompile();
|
||||
source.addEventListener("compositionstart", () => {
|
||||
composing = true;
|
||||
invalidatePreview();
|
||||
});
|
||||
queueCompile();
|
||||
source.addEventListener("compositionend", () => {
|
||||
if (!composing) return;
|
||||
composing = false;
|
||||
recordEdit(true);
|
||||
});
|
||||
source.addEventListener("input", (event) => {
|
||||
if (composing) return;
|
||||
if (event.isComposing) {
|
||||
invalidatePreview();
|
||||
return;
|
||||
}
|
||||
recordEdit();
|
||||
});
|
||||
source.addEventListener("keydown", (event) => {
|
||||
if (event.key !== "Tab" || composing || event.isComposing
|
||||
|| event.shiftKey || event.ctrlKey || event.metaKey || event.altKey) return;
|
||||
event.preventDefault();
|
||||
indentSource();
|
||||
});
|
||||
queueCompile(0);
|
||||
} catch (failure) {
|
||||
showError(failure.message);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user