109 lines
6.3 KiB
JavaScript
109 lines
6.3 KiB
JavaScript
"use strict";
|
||
(() => {
|
||
const editors = new Map();
|
||
const drafts = [];
|
||
let sequence = 0, collapsed = false;
|
||
const MAX_DRAFTS = 12;
|
||
|
||
const dock = el("aside", undefined, "draft-dock"); dock.id = "draft-dock"; dock.hidden = true;
|
||
const heading = el("div", undefined, "draft-dock-heading");
|
||
const title = el("strong", t("draftDockTitle")); title.dataset.i18n = "draftDockTitle";
|
||
const headingActions = el("div", undefined, "draft-dock-heading-actions");
|
||
const count = el("span", "0", "pill");
|
||
const toggle = el("button", "−", "draft-dock-toggle"); toggle.type = "button"; toggle.setAttribute("aria-controls", "draft-dock-list");
|
||
headingActions.append(count, toggle); heading.append(title, headingActions);
|
||
const list = el("div", undefined, "draft-dock-list"); list.id = "draft-dock-list"; dock.append(heading, list); document.body.append(dock);
|
||
|
||
function editorLabel(type) { const key = editors.get(type)?.labelKey; return key ? t(key) : type; }
|
||
function draftTitle(value) { return String(value || t("draftUntitled")).trim() || t("draftUntitled"); }
|
||
function draftAge(createdAt) {
|
||
const seconds = Math.max(0, Math.floor((Date.now() - createdAt) / 1000));
|
||
if (seconds < 60) return t("draftAgeNow");
|
||
const minutes = Math.floor(seconds / 60); if (minutes < 60) return t("draftAgeMinutes").replace("{count}", minutes);
|
||
const hours = Math.floor(minutes / 60); if (hours < 24) return t("draftAgeHours").replace("{count}", hours);
|
||
return t("draftAgeDays").replace("{count}", Math.floor(hours / 24));
|
||
}
|
||
function addDraft(type, packed) {
|
||
if (!packed?.state) return;
|
||
drafts.push({ id: ++sequence, type, title: draftTitle(packed.title), state: packed.state, createdAt: Date.now() });
|
||
while (drafts.length > MAX_DRAFTS) drafts.shift();
|
||
render(); toast(t("draftMinimized"));
|
||
}
|
||
function stashOpen() {
|
||
for (const [type, api] of editors) {
|
||
if (!api.isOpen?.()) continue;
|
||
if (api.isDirty?.()) addDraft(type, api.suspend());
|
||
else api.close?.();
|
||
}
|
||
}
|
||
async function restore(id) {
|
||
const index = drafts.findIndex(item => item.id === id); if (index < 0) return;
|
||
const draft = drafts[index], api = editors.get(draft.type); if (!api?.restore) return;
|
||
stashOpen(); drafts.splice(index, 1); render();
|
||
try { api.restore(draft.state); }
|
||
catch (error) { drafts.splice(Math.min(index, drafts.length), 0, draft); render(); toast(error.message || String(error), true); }
|
||
}
|
||
async function discard(id) {
|
||
const index = drafts.findIndex(item => item.id === id); if (index < 0) return;
|
||
if (!(await appConfirm(t("draftDiscardConfirm"), { danger: true, confirmLabel: t("discardAction") }))) return;
|
||
drafts.splice(index, 1); render();
|
||
}
|
||
function render() {
|
||
dock.hidden = drafts.length === 0; count.textContent = String(drafts.length); dock.classList.toggle("is-collapsed", collapsed);
|
||
list.hidden = collapsed; toggle.textContent = collapsed ? "+" : "−"; toggle.setAttribute("aria-expanded", String(!collapsed)); toggle.setAttribute("aria-label", t(collapsed ? "draftDockExpand" : "draftDockCollapse"));
|
||
list.replaceChildren();
|
||
for (const draft of drafts) {
|
||
const row = el("div", undefined, "draft-dock-item");
|
||
const open = el("button", undefined, "draft-dock-open"); open.type = "button"; open.setAttribute("aria-label", t("draftRestore"));
|
||
const identity = el("span", undefined, "draft-dock-identity"), meta = el("span", undefined, "draft-dock-meta");
|
||
meta.append(el("small", editorLabel(draft.type)), el("time", draftAge(draft.createdAt)));
|
||
identity.append(meta, el("b", draft.title)); open.append(identity); open.addEventListener("click", () => void restore(draft.id));
|
||
const remove = el("button", "×", "draft-dock-remove"); remove.type = "button"; remove.setAttribute("aria-label", t("draftDiscard")); remove.addEventListener("click", () => void discard(draft.id));
|
||
row.append(open, remove); list.append(row);
|
||
}
|
||
window.rustProxyRefresh?.sync();
|
||
}
|
||
toggle.addEventListener("click", () => { collapsed = !collapsed; render(); });
|
||
function register(type, api) { editors.set(type, api); render(); }
|
||
function dockEditor(type) {
|
||
const api = editors.get(type); if (!api?.isOpen?.()) return false;
|
||
if (api.isDirty?.()) addDraft(type, api.suspend()); else api.close?.();
|
||
return true;
|
||
}
|
||
function prepare() { stashOpen(); }
|
||
function reset() { for (const api of editors.values()) api.close?.(); drafts.splice(0); collapsed = false; render(); }
|
||
|
||
function captureControls(form) {
|
||
return [...form.elements].map((control, index) => ({
|
||
index, id: control.id || "", name: control.name || "", type: control.type || control.tagName.toLowerCase(),
|
||
value: control.value, checked: "checked" in control ? control.checked : null,
|
||
}));
|
||
}
|
||
function restoreControls(form, state) {
|
||
const controls = [...form.elements];
|
||
for (const saved of state || []) {
|
||
let control = saved.id ? form.querySelector(`#${CSS.escape(saved.id)}`) : null;
|
||
if (!control && saved.name) {
|
||
const same = [...form.querySelectorAll(`[name="${CSS.escape(saved.name)}"]`)]; control = same.find(item => controls.indexOf(item) === saved.index) || same[0];
|
||
}
|
||
if (!control) control = controls[saved.index]; if (!control) continue;
|
||
if (saved.checked !== null && "checked" in control) control.checked = saved.checked;
|
||
if (control.type !== "file") control.value = saved.value;
|
||
}
|
||
}
|
||
const takeChildren = node => { const children = [...node.childNodes]; node.replaceChildren(); return children; };
|
||
const restoreChildren = (node, children) => node.replaceChildren(...(children || []));
|
||
window.rustProxyRefresh?.register("drafts", {
|
||
interval: () => 60, active: () => drafts.length > 0 && !collapsed,
|
||
run: async () => render()
|
||
});
|
||
|
||
document.addEventListener("click", async event => {
|
||
const button = event.target.closest?.("[data-editor-cancel]"); if (!button) return;
|
||
const api = editors.get(button.dataset.editorCancel); if (!api?.isOpen?.()) return;
|
||
if (api.isDirty?.() && !(await appConfirm(t("draftDiscardConfirm"), { danger: true, confirmLabel: t("discardAction") }))) return;
|
||
api.close?.();
|
||
});
|
||
window.rustProxyDrafts = { register, dock: dockEditor, prepare, reset, refresh: render, captureControls, restoreControls, takeChildren, restoreChildren };
|
||
})();
|