Files
Netcatty/examples/plugins/vault-scratchpad/page.js
T
陈大猫 d5a03606b7 feat: rebuild plugin UI with host rendering and standard settings (#4053)
* feat: rebuild plugin pages with host rendering and native settings

* fix(plugins): contain host-view errors, isolate editing, and migrate fixtures

* fix(plugins): migrate remaining permission fixture hosts
2026-10-28 23:08:31 -07:00

29 lines
1.3 KiB
JavaScript

export function mount(root, context, viewId, stateScope) {
const cleanup = [];
const draft = root.querySelector("#" + "draft");
const status = root.querySelector("#" + "status");
let busy = false;
async function save(clear) {
if (busy) return;
busy = true;
try {
const text = clear ? "" : draft.value;
await context.views.setState(viewId, stateScope, { version: 1, text });
if (clear) draft.value = "";
status.textContent = clear ? "Draft cleared." : "Draft saved in this plugin only.";
} catch { status.textContent = "Could not save this draft."; }
finally { busy = false; }
}
root.querySelector("#" + "save").addEventListener("click", () => { void save(false); });
root.querySelector("#" + "clear").addEventListener("click", () => { void save(true); });
context.views.getState(viewId, stateScope).then((state) => {
if (state?.version === 1 && typeof state.text === "string" && state.text.length <= 4096) draft.value = state.text;
status.textContent = "Ready. Use synthetic text for this example.";
draft.disabled = false;
root.querySelector("#" + "save").disabled = false;
root.querySelector("#" + "clear").disabled = false;
}).catch(() => { status.textContent = "Could not load this plugin draft."; });
return () => { for (const stop of cleanup) stop(); };
}