234 lines
7.8 KiB
JavaScript
234 lines
7.8 KiB
JavaScript
"use strict";
|
||
|
||
const list = document.querySelector("#projects-view");
|
||
const grid = document.querySelector("#project-grid");
|
||
const empty = document.querySelector("#projects-empty");
|
||
const upload = document.querySelector("#upload-zip");
|
||
const input = document.querySelector("#zip-input");
|
||
const drop = document.querySelector("#drop-zone");
|
||
const create = document.querySelector("#new-project");
|
||
const message = document.querySelector("#toast");
|
||
|
||
let busy = false;
|
||
|
||
list.hidden = false;
|
||
message.setAttribute("role", "status");
|
||
document.querySelector("#header-actions").textContent = "原生 Beta";
|
||
document.querySelector("footer").textContent = "";
|
||
|
||
function setBusy(value) {
|
||
busy = value;
|
||
upload.disabled = value;
|
||
create.disabled = value;
|
||
grid.inert = value;
|
||
}
|
||
|
||
function showMessage(text, failed = false) {
|
||
message.textContent = text;
|
||
message.classList.toggle("error", failed);
|
||
message.hidden = false;
|
||
}
|
||
|
||
async function responseError(response) {
|
||
const body = await response.json().catch(() => null);
|
||
return new Error(body?.error || "请求失败(" + response.status + ")");
|
||
}
|
||
|
||
async function request(path, options) {
|
||
const response = await fetch(path, options);
|
||
if (!response.ok) throw await responseError(response);
|
||
return response.json();
|
||
}
|
||
|
||
async function openProject(project) {
|
||
if (!project.mainFile) throw new Error("项目没有可打开的 TeX 主文件");
|
||
const path = "/p/" + encodeURIComponent(project.id) + "/";
|
||
showMessage("正在打开「" + project.name + "」…");
|
||
// The project may have been saved even if preparing its editor fails.
|
||
// Surface that failure on the project list instead of navigating to JSON.
|
||
const response = await fetch(path, { cache: "no-store" });
|
||
if (!response.ok) throw await responseError(response);
|
||
location.assign(path);
|
||
}
|
||
|
||
function projectCard(project) {
|
||
const card = document.createElement("article");
|
||
card.className = "project-card";
|
||
const body = document.createElement("div");
|
||
body.className = "project-body";
|
||
const title = document.createElement("h3");
|
||
title.textContent = project.name;
|
||
const details = document.createElement("p");
|
||
details.className = "project-meta";
|
||
details.textContent = project.mainFile || "没有 TeX 主文件";
|
||
const actions = document.createElement("div");
|
||
actions.className = "project-actions";
|
||
const button = document.createElement("button");
|
||
button.type = "button";
|
||
button.className = "btn primary";
|
||
button.textContent = "打开";
|
||
button.disabled = !project.mainFile;
|
||
button.addEventListener("click", async () => {
|
||
if (busy) return;
|
||
setBusy(true);
|
||
try {
|
||
await openProject(project);
|
||
} catch (error) {
|
||
setBusy(false);
|
||
showMessage("打开项目失败:" + error.message, true);
|
||
}
|
||
});
|
||
actions.append(button);
|
||
body.append(title, details, actions);
|
||
card.append(body);
|
||
return card;
|
||
}
|
||
|
||
async function refresh() {
|
||
const { projects } = await request("/api/projects");
|
||
grid.replaceChildren(...projects.map(projectCard));
|
||
empty.hidden = projects.length > 0;
|
||
}
|
||
|
||
async function importFile(file) {
|
||
if (!file || busy) return;
|
||
if (!file.name.toLowerCase().endsWith(".zip")) {
|
||
showMessage("请选择 ZIP 压缩包", true);
|
||
return;
|
||
}
|
||
setBusy(true);
|
||
showMessage("正在导入「" + file.name + "」…");
|
||
let project;
|
||
try {
|
||
const name = file.name.slice(0, -4);
|
||
project = await request("/api/projects?name=" + encodeURIComponent(name), {
|
||
method: "POST",
|
||
headers: { "Content-Type": "application/zip" },
|
||
body: file,
|
||
});
|
||
await openProject(project);
|
||
} catch (error) {
|
||
showMessage((project ? "ZIP 已导入,打开项目失败:" : "导入 ZIP 失败:") + error.message, true);
|
||
if (project) await refresh().catch(() => {});
|
||
setBusy(false);
|
||
}
|
||
}
|
||
|
||
upload.addEventListener("click", () => input.click());
|
||
input.addEventListener("change", () => {
|
||
const file = input.files[0];
|
||
input.value = "";
|
||
importFile(file);
|
||
});
|
||
document.addEventListener("dragover", (event) => {
|
||
if (!Array.from(event.dataTransfer?.types ?? []).includes("Files")) return;
|
||
event.preventDefault();
|
||
event.dataTransfer.dropEffect = busy ? "none" : "copy";
|
||
drop.classList.toggle("dragging", !busy);
|
||
});
|
||
document.addEventListener("dragleave", (event) => {
|
||
if (!event.relatedTarget) drop.classList.remove("dragging");
|
||
});
|
||
document.addEventListener("drop", (event) => {
|
||
const transfer = event.dataTransfer;
|
||
if (!transfer || !(transfer.files.length || Array.from(transfer.types).includes("Files"))) return;
|
||
event.preventDefault();
|
||
drop.classList.remove("dragging");
|
||
if (busy) return;
|
||
if (transfer.files.length !== 1) {
|
||
showMessage("请一次拖入一个 ZIP 压缩包", true);
|
||
return;
|
||
}
|
||
importFile(transfer.files[0]);
|
||
});
|
||
|
||
// 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", () => {
|
||
if (busy) return;
|
||
projectName.value = "";
|
||
projectName.setCustomValidity("");
|
||
projectError.hidden = true;
|
||
projectDialog.showModal();
|
||
projectName.focus();
|
||
});
|
||
cancel.addEventListener("click", () => projectDialog.close());
|
||
projectDialog.addEventListener("cancel", (event) => {
|
||
if (busy) event.preventDefault();
|
||
});
|
||
projectName.addEventListener("input", () => projectName.setCustomValidity(""));
|
||
projectForm.addEventListener("submit", async (event) => {
|
||
event.preventDefault();
|
||
if (busy) return;
|
||
const name = projectName.value.trim();
|
||
if (!name) {
|
||
projectName.setCustomValidity("请输入项目名称");
|
||
projectName.reportValidity();
|
||
return;
|
||
}
|
||
setBusy(true);
|
||
submit.disabled = true;
|
||
cancel.disabled = true;
|
||
submit.textContent = "正在创建…";
|
||
projectError.hidden = true;
|
||
let project;
|
||
try {
|
||
project = await request("/api/projects/new", {
|
||
method: "POST",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify({ name }),
|
||
});
|
||
projectDialog.close();
|
||
await openProject(project);
|
||
} catch (error) {
|
||
if (project) {
|
||
showMessage("项目已创建,打开失败:" + error.message, true);
|
||
await refresh().catch(() => {});
|
||
} else {
|
||
projectError.textContent = error.message;
|
||
projectError.hidden = false;
|
||
}
|
||
setBusy(false);
|
||
} finally {
|
||
submit.disabled = false;
|
||
cancel.disabled = false;
|
||
submit.textContent = "创建项目";
|
||
}
|
||
});
|
||
|
||
refresh().catch((error) => {
|
||
if (!busy) showMessage("读取项目列表失败:" + error.message, true);
|
||
});
|