Connect the native Beta to the project Web workspace
This commit is contained in:
1 parent
29193c03bc
commit
8b6dab8d52
5 files changed
+155
-20
No files matched your search
@@ -1,12 +1,17 @@
|
||||
const workspace = document.querySelector("#workspace");
|
||||
if (workspace) attachWebControls();
|
||||
|
||||
const source = document.querySelector("#source");
|
||||
const pages = document.querySelector("#pages");
|
||||
const status = document.querySelector("#status");
|
||||
const error = document.querySelector("#error");
|
||||
const status = document.querySelector("#status, #preview-status");
|
||||
const error = document.querySelector("#error, #preview-error-text");
|
||||
const errorPanel = document.querySelector("#error, #preview-error");
|
||||
const pdf = document.querySelector("#pdf");
|
||||
const save = document.querySelector("#save");
|
||||
const saved = document.querySelector("#saved");
|
||||
const saved = document.querySelector("#saved, #file-panel-foot");
|
||||
const selector = document.querySelector("#project-files");
|
||||
const name = document.querySelector("#name");
|
||||
const name = document.querySelector("#name, #open-file-path");
|
||||
const fileTree = document.querySelector("#file-tree");
|
||||
const edits = new Map();
|
||||
let files;
|
||||
let activeFile;
|
||||
@@ -16,16 +21,60 @@ let draft = null;
|
||||
let frame;
|
||||
const previewDraft = new URLSearchParams(location.hash.slice(1)).get("preview") !== "published";
|
||||
|
||||
function attachWebControls() {
|
||||
const button = document.createElement("button");
|
||||
button.type = "button";
|
||||
button.id = "save";
|
||||
button.className = "btn";
|
||||
button.disabled = true;
|
||||
button.textContent = "保存源文件";
|
||||
document.querySelector(".file-tools").replaceChildren(button);
|
||||
|
||||
const download = document.createElement("a");
|
||||
download.id = "pdf";
|
||||
download.className = "btn small";
|
||||
download.download = "";
|
||||
download.hidden = true;
|
||||
download.textContent = "下载 PDF";
|
||||
document.querySelector("#export-pdf").replaceWith(download);
|
||||
|
||||
document.querySelector("#back-button").hidden = true;
|
||||
document.querySelector("#toggle-engine").hidden = true;
|
||||
document.querySelector("#header-actions").textContent = "原生 Beta";
|
||||
document.querySelector("footer").textContent = "Ctrl/Cmd+S 保存源码副本";
|
||||
document.querySelector("#source").disabled = true;
|
||||
document.querySelector("#file-panel-foot").setAttribute("role", "status");
|
||||
document.querySelector("#file-panel-foot").style.overflowWrap = "anywhere";
|
||||
workspace.hidden = false;
|
||||
}
|
||||
|
||||
function selectFile(filename) {
|
||||
activeFile = filename;
|
||||
name.textContent = filename;
|
||||
source.value = edits.get(filename) ?? files.get(filename);
|
||||
if (fileTree) {
|
||||
for (const row of fileTree.children) {
|
||||
const selected = row.dataset.path === activeFile;
|
||||
row.classList.toggle("active", selected);
|
||||
row.setAttribute("aria-pressed", selected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function showError(message) {
|
||||
status.textContent = "编译失败";
|
||||
error.textContent = message;
|
||||
error.hidden = false;
|
||||
errorPanel.hidden = false;
|
||||
}
|
||||
|
||||
function pageImage(page) {
|
||||
const img = new Image();
|
||||
img.alt = `第 ${page.index + 1} 页`;
|
||||
img.dataset.index = page.index;
|
||||
if (workspace) {
|
||||
img.className = "page";
|
||||
img.style.aspectRatio = "auto";
|
||||
}
|
||||
img.src = URL.createObjectURL(new Blob([page.svg], { type: "image/svg+xml" }));
|
||||
return img;
|
||||
}
|
||||
@@ -116,7 +165,7 @@ async function compile(edit, submitted, changes) {
|
||||
} else {
|
||||
acceptPublication(update.revision, previous);
|
||||
}
|
||||
error.hidden = true;
|
||||
errorPanel.hidden = true;
|
||||
await new Promise(requestAnimationFrame);
|
||||
await new Promise(requestAnimationFrame);
|
||||
if (edit !== sequence) return;
|
||||
@@ -199,22 +248,37 @@ async function openProject() {
|
||||
if (!response.ok) throw new Error("无法打开源文件");
|
||||
const project = await response.json();
|
||||
files = new Map(Object.entries(project.files));
|
||||
activeFile = project.name;
|
||||
name.textContent = activeFile;
|
||||
source.value = files.get(activeFile);
|
||||
if (fileTree) {
|
||||
document.querySelector("#project-title").textContent = project.name;
|
||||
for (const filename of files.keys()) {
|
||||
const row = document.createElement("button");
|
||||
row.type = "button";
|
||||
row.className = "btn tree-row file-row";
|
||||
row.style.width = "100%";
|
||||
row.dataset.path = filename;
|
||||
row.textContent = filename;
|
||||
row.title = filename;
|
||||
row.addEventListener("click", () => selectFile(filename));
|
||||
fileTree.append(row);
|
||||
}
|
||||
}
|
||||
selectFile(project.name);
|
||||
source.disabled = false;
|
||||
sequence = project.sequence;
|
||||
save.disabled = false;
|
||||
save.addEventListener("click", saveSources);
|
||||
if (files.size > 1) {
|
||||
document.addEventListener("keydown", (event) => {
|
||||
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "s") {
|
||||
event.preventDefault();
|
||||
if (!save.disabled) saveSources();
|
||||
}
|
||||
});
|
||||
if (selector && files.size > 1) {
|
||||
for (const filename of files.keys()) selector.add(new Option(filename, filename));
|
||||
selector.value = activeFile;
|
||||
selector.hidden = false;
|
||||
name.hidden = true;
|
||||
selector.addEventListener("change", () => {
|
||||
activeFile = selector.value;
|
||||
source.value = edits.get(activeFile) ?? files.get(activeFile);
|
||||
});
|
||||
selector.addEventListener("change", () => selectFile(selector.value));
|
||||
}
|
||||
source.addEventListener("input", () => {
|
||||
edits.set(activeFile, source.value);
|
||||
|
||||
Reference in new issue
Block a user