import "./styles.css";
const state = {
tab: "catalog",
notes: [],
content: "服务层把文件读写、shell 与 dialog 从 IPC 里拆出来。",
noteId: "welcome",
shellUrl: "https://example.com/docs",
response: null,
shellResult: null,
picked: null,
busy: false,
};
const tabs = [
{ id: "catalog", label: "服务目录" },
{ id: "notes", label: "笔记服务" },
{ id: "shell", label: "Shell 与 Dialog" },
];
const services = [
{
name: "notes-service",
deps: "fs/promises · path · crypto",
methods: [
"list()",
"read(id)",
"write(id, content)",
"remove(id)",
"reset()",
],
note: "原子写:临时文件 + rename;id 用正则限制;单文件 64KB 上限。",
},
{
name: "shell-service",
deps: "openExternal(注入)",
methods: ["open(url)"],
note: "只允许 https + host 白名单;dryRun 时不真的打开浏览器。",
},
{
name: "dialog-service",
deps: "showOpenDialog(注入)",
methods: ["pick()"],
note: "只接受 .md / .txt;取消返回 null,不抛异常。",
},
];
function escapeHtml(value) {
return value
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """);
}
function json(value) {
return escapeHtml(JSON.stringify(value, null, 2));
}
function time(at) {
return new Date(at).toLocaleTimeString("zh-CN", { hour12: false });
}
function catalogTab() {
return `
三个服务,一个薄 IPC 层
IPC 只做参数校验与转发;文件、URL、对话框逻辑都在 main/services/,可以用 Vitest 直接测。
${services
.map((service) => `
${service.name}
${service.deps}
${service.methods.map((method) => `${method} `).join("")}
${service.note}
`)
.join("")}
为什么不是把 fs 写在 ipcMain.handle 里
service 不依赖窗口,测试可以直接 new 一个临时目录;IPC 只有 30 行,改通道不会碰业务逻辑;同一服务可以被菜单、托盘、CLI 复用。
`;
}
function notesTab() {
return ``;
}
function shellTab() {
const allowed = state.shellResult?.allowed;
const blocked = state.shellResult?.blocked;
const allowedText = allowed
? allowed.ok
? `OK · ${allowed.value.url}${allowed.value.dryRun ? "(dryRun)" : ""}`
: allowed.error.code
: "—";
const blockedText = blocked
? blocked.ok
? "OK(不应出现)"
: blocked.error.code
: "—";
return `
Shell 白名单
允许:example.com、www.electronjs.org;只允许 https。
| 白名单链接 | ${escapeHtml(allowedText)} |
| 非白名单链接 | ${escapeHtml(blockedText)} |
被拒绝时的结构化错误
${blocked && !blocked.ok ? json(blocked.error) : "(还没有调用)"}
文件对话框
${escapeHtml(state.picked?.name ?? "未选择")}
smoke 模式用固定的 welcome.md 替代真实对话框;生产才调用系统选择器。
`;
}
function render() {
const root = document.querySelector("#app");
if (!root)
return;
const body = state.tab === "catalog"
? catalogTab()
: state.tab === "notes"
? notesTab()
: shellTab();
root.innerHTML = `
${body}`;
for (const button of root.querySelectorAll("[data-tab]")) {
button.addEventListener("click", () => {
const tab = button.dataset.tab;
if (tab === "catalog" || tab === "notes" || tab === "shell") {
state.tab = tab;
render();
}
});
}
const idInput = () => root.querySelector("#note-id")?.value ?? state.noteId;
const contentInput = () => root.querySelector("#note-content")?.value ??
state.content;
root.querySelector("#save-button")?.addEventListener("click", () => {
state.noteId = idInput();
state.content = contentInput();
void save();
});
root.querySelector("#read-button")?.addEventListener("click", () => {
state.noteId = idInput();
void read();
});
root.querySelector("#delete-button")?.addEventListener("click", () => {
state.noteId = idInput();
void remove();
});
for (const row of root.querySelectorAll("[data-id]")) {
row.addEventListener("click", () => {
state.noteId = row.dataset.id ?? "";
void read();
});
}
root.querySelector("#open-button")?.addEventListener("click", () => {
state.shellUrl =
root.querySelector("#shell-url")?.value ?? "";
void openUrl();
});
root.querySelector("#pick-button")?.addEventListener("click", () => {
void pick();
});
}
async function reload() {
const response = await window.api.listNotes();
if (response.ok)
state.notes = response.value;
}
async function save() {
if (state.busy)
return;
state.busy = true;
try {
state.response = await window.api.writeNote(state.noteId, state.content);
await reload();
}
finally {
state.busy = false;
render();
}
}
async function read() {
if (state.busy)
return;
state.busy = true;
try {
const response = await window.api.readNote(state.noteId);
state.response = response;
if (response.ok) {
state.content = response.value.content;
state.tab = "notes";
}
}
finally {
state.busy = false;
render();
}
}
async function remove() {
if (state.busy)
return;
state.busy = true;
try {
state.response = await window.api.removeNote(state.noteId);
await reload();
}
finally {
state.busy = false;
render();
}
}
async function openUrl() {
const result = await window.api.openUrl(state.shellUrl);
if (result.ok) {
state.shellResult = { ...(state.shellResult ?? {}), allowed: result };
}
else {
state.shellResult = { ...(state.shellResult ?? {}), blocked: result };
}
render();
}
async function pick() {
const response = await window.api.pickFile();
state.picked = response.ok ? response.value : null;
render();
}
function boot() {
render();
window.__setTab = (tab) => {
state.tab = tab;
render();
};
window.__snapshot = () => ({
tab: state.tab,
notes: state.notes.length,
picked: state.picked?.name ?? null,
});
window.__smoke = async () => {
await window.api.resetNotes();
await window.api.writeNote("welcome", "# 你好\n服务层把 fs 放在 main/services,IPC 只做校验。");
await window.api.writeNote("second-note", "第二条笔记");
const list = await window.api.listNotes();
const read = await window.api.readNote("welcome");
const bad = await window.api.readNote("../escape");
const allowed = await window.api.openUrl("https://example.com/docs");
const blocked = await window.api.openUrl("http://example.com");
const picked = await window.api.pickFile();
state.notes = list.ok ? list.value : [];
state.response = read;
state.content = read.ok ? read.value.content : "";
state.shellResult = { allowed, blocked };
state.picked = picked.ok ? picked.value : null;
state.tab = "catalog";
render();
return {
notes: state.notes.length,
readBytes: read.ok ? read.value.content.length : 0,
badId: bad.ok ? "OK" : bad.error.code,
allowed: allowed.ok,
blocked: blocked.ok ? "OK" : blocked.error.code,
picked: state.picked?.name ?? "none",
};
};
}
boot();
//# sourceMappingURL=renderer.js.map