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
+9
-3
@@ -57,9 +57,10 @@ python3 target/local-beta/native-beta.py resume /path/to/saved-project --web-dir
|
||||
|
||||
## 输入公式
|
||||
|
||||
编辑源文件时用 $...$ 输入行内公式,用 \[...\] 输入陈列公式。
|
||||
使用 align 等环境时,在导言区加入 \usepackage{amsmath}。
|
||||
例如在正文中输入以下内容。
|
||||
在编辑区点击「行内公式」或「独立公式」可插入对应的 LaTeX 定界符,
|
||||
并把光标放在公式内部。有选区时,定界符包住已选中的内容。
|
||||
也可以直接键入 $...$ 或 \[...\]。使用 align 环境时,在导言区加入
|
||||
\usepackage{amsmath}。例如在正文中输入以下内容。
|
||||
|
||||
~~~latex
|
||||
行内公式 $E=mc^2$。
|
||||
@@ -68,6 +69,11 @@ python3 target/local-beta/native-beta.py resume /path/to/saved-project --web-dir
|
||||
\]
|
||||
~~~
|
||||
|
||||
Tab 输入两个空格或缩进所选行,Shift+Tab 将焦点移到上一个控件。
|
||||
切换文件会记住各自的光标、选区和滚动位置。输入法确认文字后才提交预览,
|
||||
连续输入时在短暂停顿后提交最新修改。编译出错时可继续查看上一次成功的预览,
|
||||
修正后页面会自动更新。
|
||||
|
||||
## 保存和重新打开
|
||||
|
||||
点击「保存源文件」将当前编辑与已声明的项目资源一起写入一个新目录。
|
||||
|
||||
+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);
|
||||
}
|
||||
|
||||
@@ -94,10 +94,60 @@ drop.addEventListener("drop", (event) => {
|
||||
event.preventDefault();
|
||||
importFile(event.dataTransfer.files[0]);
|
||||
});
|
||||
create.addEventListener("click", async () => {
|
||||
const name = prompt("项目名称");
|
||||
if (!name) return;
|
||||
create.disabled = true;
|
||||
// An in-page form works in embeddable browsers that suppress JS prompts.
|
||||
const projectDialog = document.createElement("dialog");
|
||||
projectDialog.style.cssText = "width:min(26rem, calc(100% - 2rem));padding:1.25rem;border:1px solid #d5dce3;border-radius:12px";
|
||||
const projectForm = document.createElement("form");
|
||||
projectForm.style.cssText = "display:grid;gap:.8rem";
|
||||
const projectName = document.createElement("input");
|
||||
projectName.type = "text";
|
||||
projectName.required = true;
|
||||
projectName.autocomplete = "off";
|
||||
projectName.id = "project-name";
|
||||
projectName.style.cssText = "width:100%;padding:.55rem .65rem;font:inherit";
|
||||
const projectLabel = document.createElement("label");
|
||||
projectLabel.htmlFor = projectName.id;
|
||||
projectLabel.id = "project-name-label";
|
||||
projectLabel.textContent = "项目名称";
|
||||
projectDialog.setAttribute("aria-labelledby", projectLabel.id);
|
||||
const projectError = document.createElement("p");
|
||||
projectError.hidden = true;
|
||||
projectError.setAttribute("role", "alert");
|
||||
const projectActions = document.createElement("div");
|
||||
projectActions.style.cssText = "display:flex;justify-content:flex-end;gap:.5rem";
|
||||
const cancel = document.createElement("button");
|
||||
cancel.type = "button";
|
||||
cancel.className = "btn";
|
||||
cancel.textContent = "取消";
|
||||
const submit = document.createElement("button");
|
||||
submit.type = "submit";
|
||||
submit.className = "btn primary";
|
||||
submit.textContent = "创建项目";
|
||||
projectActions.append(cancel, submit);
|
||||
projectForm.append(projectLabel, projectName, projectError, projectActions);
|
||||
projectDialog.append(projectForm);
|
||||
document.body.append(projectDialog);
|
||||
|
||||
create.addEventListener("click", () => {
|
||||
projectName.value = "";
|
||||
projectName.setCustomValidity("");
|
||||
projectError.hidden = true;
|
||||
projectDialog.showModal();
|
||||
projectName.focus();
|
||||
});
|
||||
cancel.addEventListener("click", () => projectDialog.close());
|
||||
projectName.addEventListener("input", () => projectName.setCustomValidity(""));
|
||||
projectForm.addEventListener("submit", async (event) => {
|
||||
event.preventDefault();
|
||||
if (submit.disabled) return;
|
||||
const name = projectName.value.trim();
|
||||
if (!name) {
|
||||
projectName.setCustomValidity("请输入项目名称");
|
||||
projectName.reportValidity();
|
||||
return;
|
||||
}
|
||||
submit.disabled = true;
|
||||
projectError.hidden = true;
|
||||
try {
|
||||
const project = await request("/api/projects/new", {
|
||||
method: "POST",
|
||||
@@ -106,9 +156,10 @@ create.addEventListener("click", async () => {
|
||||
});
|
||||
open(project);
|
||||
} catch (error) {
|
||||
showError(error);
|
||||
projectError.textContent = error.message;
|
||||
projectError.hidden = false;
|
||||
} finally {
|
||||
create.disabled = false;
|
||||
submit.disabled = false;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user