Load new math resources in the native Web editor
This commit is contained in:
1 parent
8b6dab8d52
commit
fe86163767
12 files changed
+838
-106
No files matched your search
@@ -0,0 +1,115 @@
|
||||
"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]);
|
||||
});
|
||||
create.addEventListener("click", async () => {
|
||||
const name = prompt("项目名称");
|
||||
if (!name) return;
|
||||
create.disabled = true;
|
||||
try {
|
||||
const project = await request("/api/projects/new", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name }),
|
||||
});
|
||||
open(project);
|
||||
} catch (error) {
|
||||
showError(error);
|
||||
} finally {
|
||||
create.disabled = false;
|
||||
}
|
||||
});
|
||||
|
||||
refresh();
|
||||
Reference in new issue
Block a user