167 lines
5.5 KiB
JavaScript
167 lines
5.5 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");
|
|
|
|
list.hidden = false;
|
|
document.querySelector("#header-actions").textContent = "原生 Beta";
|
|
document.querySelector("footer").textContent = "";
|
|
|
|
async function request(path, options) {
|
|
const response = await fetch(path, options);
|
|
const result = await response.json();
|
|
if (!response.ok) throw new Error(result.error);
|
|
return result;
|
|
}
|
|
|
|
function open(project) {
|
|
location.href = "/p/" + encodeURIComponent(project.id) + "/";
|
|
}
|
|
|
|
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", () => open(project));
|
|
actions.append(button);
|
|
body.append(title, details, actions);
|
|
card.append(body);
|
|
return card;
|
|
}
|
|
|
|
async function refresh() {
|
|
try {
|
|
const { projects } = await request("/api/projects");
|
|
grid.replaceChildren(...projects.map(projectCard));
|
|
empty.hidden = projects.length > 0;
|
|
} catch (error) {
|
|
showError(error);
|
|
}
|
|
}
|
|
|
|
function showError(error) {
|
|
message.hidden = false;
|
|
message.textContent = error.message;
|
|
}
|
|
|
|
async function importFile(file) {
|
|
if (!file || upload.disabled) return;
|
|
if (!file.name.toLowerCase().endsWith(".zip")) {
|
|
showError(new Error("请选择 ZIP 压缩包"));
|
|
return;
|
|
}
|
|
upload.disabled = true;
|
|
message.hidden = true;
|
|
try {
|
|
const name = file.name.slice(0, -4);
|
|
const project = await request("/api/projects?name=" + encodeURIComponent(name), {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/zip" },
|
|
body: file,
|
|
});
|
|
open(project);
|
|
} catch (error) {
|
|
showError(error);
|
|
} finally {
|
|
input.value = "";
|
|
upload.disabled = false;
|
|
}
|
|
}
|
|
|
|
upload.addEventListener("click", () => input.click());
|
|
input.addEventListener("change", () => importFile(input.files[0]));
|
|
drop.addEventListener("dragover", (event) => event.preventDefault());
|
|
drop.addEventListener("drop", (event) => {
|
|
event.preventDefault();
|
|
importFile(event.dataTransfer.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", () => {
|
|
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",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ name }),
|
|
});
|
|
open(project);
|
|
} catch (error) {
|
|
projectError.textContent = error.message;
|
|
projectError.hidden = false;
|
|
} finally {
|
|
submit.disabled = false;
|
|
}
|
|
});
|
|
|
|
refresh();
|