@aiti-ai/ext-shared 0.0.0-stage → 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +36 -2
- package/agent-accounts.test.mjs +227 -0
- package/agent-accounts.ts +324 -0
- package/agent-capability-contract.test.mjs +129 -0
- package/agent-interaction.test.mjs +194 -0
- package/agent-interaction.ts +340 -0
- package/agent-launch.test.mjs +262 -0
- package/agent-spawn-env.test.mjs +169 -0
- package/agent-spawn-env.ts +548 -0
- package/aiti-editor-inject.mjs +49 -0
- package/backend-queue.test.mjs +336 -0
- package/backend-queue.ts +469 -0
- package/blob-save-route.ts +128 -0
- package/cbc-model-catalog.ts +86 -0
- package/check-client.mjs +273 -0
- package/check-ext-globals.mjs +144 -0
- package/controlled-login-dialog.mjs +270 -0
- package/controlled-login.mjs +398 -0
- package/controlled-login.test.mjs +466 -0
- package/convert-saveas.test.mjs +39 -0
- package/convert-saveas.ts +170 -0
- package/copy-client-bundle.mjs +39 -0
- package/cursor-cli-store.ts +377 -0
- package/dedupe-fetch.test.mjs +71 -0
- package/dedupe-fetch.ts +58 -0
- package/deploy-commit.mjs +52 -0
- package/dev-pack.mjs +246 -0
- package/dock-icons.ts +127 -0
- package/esbuild-bin.test.mjs +53 -0
- package/esbuild-bin.ts +66 -0
- package/exec-async.test.mjs +86 -0
- package/exec-async.ts +179 -0
- package/ext-display-names.ts +91 -0
- package/ext-entry-contracts.test.mjs +131 -0
- package/ext-float-dock-core.test.mjs +348 -0
- package/ext-float-dock-core.ts +783 -0
- package/ext-float-dock-lite.ts +52 -0
- package/ext-float-dock.test.mjs +286 -0
- package/ext-float-dock.ts +740 -0
- package/ext-icons-deploy.mjs +24 -0
- package/ext-lazy.test.mjs +186 -0
- package/ext-lazy.tsx +183 -0
- package/ext-model-state.test.mjs +272 -0
- package/ext-model-state.ts +324 -0
- package/ext-protocol.mjs +113 -0
- package/ext-protocol.test.mjs +88 -0
- package/ext-session-usage.test.mjs +219 -0
- package/ext-session-usage.ts +507 -0
- package/external-cli-governance.ts +153 -0
- package/git-ops-client/api.mjs +56 -0
- package/git-ops-client/branch-panel.tsx +987 -0
- package/git-ops-client/i18n.mjs +328 -0
- package/host-tools-allow.test.mjs +46 -0
- package/host-tools-allow.ts +84 -0
- package/icons/alert.svg +1 -0
- package/icons/bookmark.svg +1 -0
- package/icons/bot.svg +1 -0
- package/icons/chart-column.svg +1 -0
- package/icons/check-circle.svg +1 -0
- package/icons/clipboard-list.svg +1 -0
- package/icons/clock.svg +1 -0
- package/icons/coffee.svg +1 -0
- package/icons/edit.svg +1 -0
- package/icons/eye.svg +1 -0
- package/icons/git-branch.svg +1 -0
- package/icons/git-commit-horizontal.svg +1 -0
- package/icons/globe.svg +1 -0
- package/icons/history.svg +1 -0
- package/icons/import.svg +1 -0
- package/icons/inbox.svg +1 -0
- package/icons/key-round.svg +1 -0
- package/icons/layout-grid.svg +1 -0
- package/icons/mail.svg +1 -0
- package/icons/message-circle.svg +1 -0
- package/icons/message-square.svg +1 -0
- package/icons/meter-vertical.svg +1 -0
- package/icons/mic.svg +1 -0
- package/icons/monitor.svg +1 -0
- package/icons/pen-line.svg +1 -0
- package/icons/plug.svg +1 -0
- package/icons/radio-dot.svg +1 -0
- package/icons/radio.svg +1 -0
- package/icons/refresh.svg +1 -0
- package/icons/rocket.svg +1 -0
- package/icons/search.svg +1 -0
- package/icons/shield.svg +1 -0
- package/icons/sparkles.svg +1 -0
- package/icons/square-kanban.svg +1 -0
- package/icons/terminal.svg +1 -0
- package/icons/users.svg +1 -0
- package/icons/volume-2.svg +1 -0
- package/icons/wand.svg +1 -0
- package/icons/wrench.svg +1 -0
- package/icons/x-circle.svg +1 -0
- package/icons/x.svg +1 -0
- package/ignore-semantics.ts +76 -0
- package/lazy-loading-metrics.mjs +61 -0
- package/new-session-cwd.test.mjs +389 -0
- package/new-session-cwd.ts +253 -0
- package/open-viewer-window.ts +65 -0
- package/overlay-back-client.test.mjs +123 -0
- package/overlay-back-client.ts +87 -0
- package/package.json +65 -4
- package/pi-fork-rpc/README.md +12 -0
- package/pi-fork-rpc/cli-resolve.test.mjs +42 -0
- package/pi-fork-rpc/cli-resolve.ts +79 -0
- package/pi-fork-rpc/remote-queue.test.mjs +100 -0
- package/pi-fork-rpc/remote-queue.ts +140 -0
- package/pi-fork-rpc/spec.ts +106 -0
- package/pi-fork-rpc/wire.test.mjs +260 -0
- package/pi-fork-rpc/wire.ts +367 -0
- package/popup-anchor.ts +45 -0
- package/provider-priority-contract.test.mjs +55 -0
- package/reconcile-deps.mjs +41 -0
- package/reload-instances.mjs +60 -0
- package/seed-tool.test.mjs +95 -0
- package/seed-tool.ts +88 -0
- package/session-recycle.ts +109 -0
- package/shared-copy-sync.test.mjs +115 -0
- package/soffice-locate.ts +171 -0
- package/soffice-panel.ts +239 -0
- package/sync-dir.mjs +91 -0
- package/sync-dir.test.mjs +101 -0
- package/sync-gate.test.mjs +93 -0
- package/sync-mirror.test.mjs +85 -0
- package/task-list.test.mjs +91 -0
- package/task-list.ts +118 -0
- package/test-rm-rf.mjs +33 -0
- package/ui-dialogs.test.mjs +198 -0
- package/ui-dialogs.ts +310 -0
package/ui-dialogs.ts
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 内部弹窗共享模块(替代浏览器原生 alert/confirm/prompt)。
|
|
3
|
+
*
|
|
4
|
+
* 背景:原生 window.confirm/alert/prompt 是浏览器默认弹窗——阻塞主线程、样式与系统脱节、
|
|
5
|
+
* 且在部分嵌入环境(webview/无头)表现不一致。本模块提供 Promise 风格替代:
|
|
6
|
+
*
|
|
7
|
+
* const ok = await uiConfirm({ message: "删除分组 X?", danger: true });
|
|
8
|
+
* await uiAlert("已创建计划"); // 字符串快捷
|
|
9
|
+
* await uiAlert({ message: "保存失败", kind: "error" });
|
|
10
|
+
* const name = await uiPrompt({ message: "输入名称:", value: "草稿" }); // 取消 → null
|
|
11
|
+
*
|
|
12
|
+
* 实现:零依赖纯 DOM(React 扩展与原生 h() 扩展通用;事件处理器里直接 await)。
|
|
13
|
+
* 契约(详见 docs/aiti-web-ext-float-dialogs.md):
|
|
14
|
+
* - data-ext-float="1" → aiti-float 接管拖拽/置前/持久化;未装时回退 overlay flex 居中
|
|
15
|
+
* - data-ext-float-full="1" → 小屏(≤640px)自动铺满视口(物理适配)
|
|
16
|
+
* - data-ext-float-keepmask="1" → 真模态:遮罩不去除、点外不关闭(副作用即所求)
|
|
17
|
+
* - data-ext-float-max="0" → 豁免最大化按钮注入(小确认框不需要)
|
|
18
|
+
* - data-ext-float-persist="0" → 不做位置持久化(一次性弹窗不污染持久化槽)
|
|
19
|
+
* 同一时刻只显示一个弹窗(内部 Promise 链排队),保证不叠罗汉。
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
export type UiDialogKind = "info" | "success" | "error" | "warn";
|
|
23
|
+
|
|
24
|
+
export interface UiConfirmOptions {
|
|
25
|
+
title?: string;
|
|
26
|
+
message?: string;
|
|
27
|
+
/** 附加等宽代码块(如待执行的命令文本) */
|
|
28
|
+
detail?: string;
|
|
29
|
+
confirmText?: string;
|
|
30
|
+
cancelText?: string;
|
|
31
|
+
/** 危险确认:⚠ 图标 + 红色确认按钮 */
|
|
32
|
+
danger?: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface UiAlertOptions {
|
|
36
|
+
title?: string;
|
|
37
|
+
message?: string;
|
|
38
|
+
detail?: string;
|
|
39
|
+
kind?: UiDialogKind;
|
|
40
|
+
confirmText?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface UiPromptOptions {
|
|
44
|
+
title?: string;
|
|
45
|
+
message?: string;
|
|
46
|
+
value?: string;
|
|
47
|
+
placeholder?: string;
|
|
48
|
+
/** 输入为空时禁用确定按钮(默认 false:允许空串确认) */
|
|
49
|
+
required?: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const Z_BASE = 6000;
|
|
53
|
+
|
|
54
|
+
/** 常用 CSS 变量兜底(宿主一定是 pi-web 主题页;fallback 仅为防御) */
|
|
55
|
+
const V = {
|
|
56
|
+
panel: "var(--bg-panel, #1c1c20)",
|
|
57
|
+
hover: "var(--bg-hover, #2a2a30)",
|
|
58
|
+
border: "var(--border, #3a3a42)",
|
|
59
|
+
text: "var(--text, #e8e8ec)",
|
|
60
|
+
muted: "var(--text-muted, #9a9aa4)",
|
|
61
|
+
accent: "var(--accent, #4f8ef7)",
|
|
62
|
+
mono: "var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace)",
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const KIND_COLOR: Record<UiDialogKind, string> = {
|
|
66
|
+
info: V.accent,
|
|
67
|
+
success: "#10b981",
|
|
68
|
+
error: "var(--danger, #ef4444)",
|
|
69
|
+
warn: "var(--warn, #f59e0b)",
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/** 线性 stroke 图标(24 viewBox),随色 */
|
|
73
|
+
function iconSvg(kind: "warn" | "info" | "success" | "error", color: string): SVGSVGElement {
|
|
74
|
+
const NS = "http://www.w3.org/2000/svg";
|
|
75
|
+
const svg = document.createElementNS(NS, "svg") as SVGSVGElement;
|
|
76
|
+
svg.setAttribute("viewBox", "0 0 24 24");
|
|
77
|
+
svg.setAttribute("width", "18");
|
|
78
|
+
svg.setAttribute("height", "18");
|
|
79
|
+
svg.style.flexShrink = "0";
|
|
80
|
+
const common = { fill: "none", stroke: color, "stroke-width": "2", "stroke-linecap": "round", "stroke-linejoin": "round" } as const;
|
|
81
|
+
const add = (tag: string, attrs: Record<string, string>) => {
|
|
82
|
+
const el = document.createElementNS(NS, tag);
|
|
83
|
+
for (const [k, v] of Object.entries(attrs)) el.setAttribute(k, v);
|
|
84
|
+
svg.appendChild(el);
|
|
85
|
+
};
|
|
86
|
+
if (kind === "warn") {
|
|
87
|
+
add("path", { ...common, d: "M12 3 L22 20 H2 Z" });
|
|
88
|
+
add("line", { ...common, x1: "12", y1: "9.5", x2: "12", y2: "14" });
|
|
89
|
+
add("line", { ...common, x1: "12", y1: "16.8", x2: "12", y2: "16.9" });
|
|
90
|
+
} else if (kind === "error") {
|
|
91
|
+
add("circle", { ...common, cx: "12", cy: "12", r: "9" });
|
|
92
|
+
add("line", { ...common, x1: "9", y1: "9", x2: "15", y2: "15" });
|
|
93
|
+
add("line", { ...common, x1: "15", y1: "9", x2: "9", y2: "15" });
|
|
94
|
+
} else if (kind === "success") {
|
|
95
|
+
add("circle", { ...common, cx: "12", cy: "12", r: "9" });
|
|
96
|
+
add("path", { ...common, d: "M8 12.5 L11 15.5 L16.5 9.5" });
|
|
97
|
+
} else {
|
|
98
|
+
add("circle", { ...common, cx: "12", cy: "12", r: "9" });
|
|
99
|
+
add("line", { ...common, x1: "12", y1: "11", x2: "12", y2: "16" });
|
|
100
|
+
add("line", { ...common, x1: "12", y1: "8", x2: "12", y2: "8.01" });
|
|
101
|
+
}
|
|
102
|
+
return svg;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const btnBase = {
|
|
106
|
+
border: "1px solid " + V.border,
|
|
107
|
+
background: V.hover,
|
|
108
|
+
borderRadius: 6,
|
|
109
|
+
padding: "6px 16px",
|
|
110
|
+
cursor: "pointer",
|
|
111
|
+
fontSize: 13,
|
|
112
|
+
color: V.text,
|
|
113
|
+
fontFamily: "inherit",
|
|
114
|
+
} as const;
|
|
115
|
+
|
|
116
|
+
interface PendingSpec {
|
|
117
|
+
kind: "confirm" | "alert" | "prompt";
|
|
118
|
+
opts: any;
|
|
119
|
+
resolve: (v: unknown) => void;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 弹窗队列:同一时刻只显示一个(先到先显,后续排队) */
|
|
123
|
+
let chain: Promise<unknown> = Promise.resolve();
|
|
124
|
+
const queue: PendingSpec[] = [];
|
|
125
|
+
let showing = false;
|
|
126
|
+
|
|
127
|
+
function enqueue<T>(specKind: PendingSpec["kind"], opts: any): Promise<T> {
|
|
128
|
+
return new Promise<T>((resolve) => {
|
|
129
|
+
queue.push({ kind: specKind, opts, resolve: resolve as (v: unknown) => void });
|
|
130
|
+
drain();
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function drain() {
|
|
135
|
+
if (showing) return;
|
|
136
|
+
const next = queue.shift();
|
|
137
|
+
if (!next) return;
|
|
138
|
+
showing = true;
|
|
139
|
+
try {
|
|
140
|
+
showDialog(next, () => {
|
|
141
|
+
showing = false;
|
|
142
|
+
drain();
|
|
143
|
+
});
|
|
144
|
+
} catch (e) {
|
|
145
|
+
// 极端防御:DOM 不可用时按取消收场,不让队列卡死
|
|
146
|
+
next.resolve(next.kind === "prompt" ? null : next.kind === "confirm" ? false : undefined);
|
|
147
|
+
showing = false;
|
|
148
|
+
drain();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** 顶层挂载点:直接 document.body(最高层级,不依赖扩展面板 DOM) */
|
|
153
|
+
function mount(): { overlay: HTMLDivElement; panel: HTMLDivElement } {
|
|
154
|
+
const overlay = document.createElement("div");
|
|
155
|
+
overlay.setAttribute("data-ui-dialog-overlay", "1");
|
|
156
|
+
overlay.style.cssText =
|
|
157
|
+
"position:fixed;inset:0;background:rgba(0,0,0,0.45);display:flex;align-items:center;justify-content:center;padding:20px;z-index:" + Z_BASE + ";";
|
|
158
|
+
const panel = document.createElement("div");
|
|
159
|
+
panel.setAttribute("data-ext-float", "1");
|
|
160
|
+
panel.setAttribute("data-ext-float-w", "440");
|
|
161
|
+
panel.setAttribute("data-ext-float-autoh", "1");
|
|
162
|
+
panel.setAttribute("data-ext-float-full", "1");
|
|
163
|
+
panel.setAttribute("data-ext-float-keepmask", "1");
|
|
164
|
+
panel.setAttribute("data-ext-float-max", "0");
|
|
165
|
+
panel.setAttribute("data-ext-float-persist", "0");
|
|
166
|
+
panel.tabIndex = -1;
|
|
167
|
+
panel.style.cssText =
|
|
168
|
+
"width:440px;max-width:100%;border-radius:12px;background:" + V.panel + ";border:1px solid " + V.border +
|
|
169
|
+
";color:" + V.text + ";font-family:inherit;font-size:13px;box-shadow:0 20px 60px rgba(0,0,0,0.35);outline:none;overflow:hidden;";
|
|
170
|
+
overlay.appendChild(panel);
|
|
171
|
+
document.body.appendChild(overlay);
|
|
172
|
+
return { overlay, panel };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function buildBody(panel: HTMLDivElement, spec: PendingSpec, finish: (v: unknown) => void): HTMLInputElement | null {
|
|
176
|
+
const o = spec.opts || {};
|
|
177
|
+
const kind: UiDialogKind = spec.kind === "confirm"
|
|
178
|
+
? "info"
|
|
179
|
+
: ((o.kind === "success" || o.kind === "error" || o.kind === "warn" || o.kind === "info" ? o.kind : "info") as UiDialogKind);
|
|
180
|
+
const color = spec.kind === "confirm" && o.danger ? KIND_COLOR.error : KIND_COLOR[kind];
|
|
181
|
+
const iconKind: "warn" | UiDialogKind = spec.kind === "confirm" && o.danger ? "warn" : kind;
|
|
182
|
+
|
|
183
|
+
// 标题栏(aiti-float 把首个子元素当拖拽把手)
|
|
184
|
+
const head = document.createElement("div");
|
|
185
|
+
head.style.cssText = "display:flex;align-items:center;gap:10px;padding:14px 18px 0;";
|
|
186
|
+
const icon = iconSvg(iconKind, color);
|
|
187
|
+
icon.style.width = "20px";
|
|
188
|
+
icon.style.height = "20px";
|
|
189
|
+
head.appendChild(icon);
|
|
190
|
+
const titleEl = document.createElement("div");
|
|
191
|
+
titleEl.style.cssText = "flex:1;min-width:0;font-size:15px;font-weight:700;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;";
|
|
192
|
+
titleEl.textContent = o.title || (spec.kind === "confirm" ? (o.danger ? "危险操作确认" : "确认操作") : spec.kind === "prompt" ? "输入" : "提示");
|
|
193
|
+
head.appendChild(titleEl);
|
|
194
|
+
panel.appendChild(head);
|
|
195
|
+
|
|
196
|
+
// 正文
|
|
197
|
+
if (o.message) {
|
|
198
|
+
const msg = document.createElement("div");
|
|
199
|
+
msg.style.cssText = "padding:12px 18px 0;line-height:1.55;white-space:pre-wrap;word-break:break-word;color:" + V.text + ";";
|
|
200
|
+
msg.textContent = String(o.message);
|
|
201
|
+
panel.appendChild(msg);
|
|
202
|
+
}
|
|
203
|
+
// 等宽 detail 块(命令等)
|
|
204
|
+
if (o.detail) {
|
|
205
|
+
const det = document.createElement("pre");
|
|
206
|
+
det.style.cssText =
|
|
207
|
+
"margin:10px 18px 0;padding:10px 12px;border-radius:8px;background:" + V.hover + ";border:1px solid " + V.border +
|
|
208
|
+
";font-family:" + V.mono + ";font-size:12px;line-height:1.5;white-space:pre-wrap;word-break:break-all;max-height:180px;overflow:auto;color:" + V.text + ";";
|
|
209
|
+
det.textContent = String(o.detail);
|
|
210
|
+
panel.appendChild(det);
|
|
211
|
+
}
|
|
212
|
+
// prompt 输入框
|
|
213
|
+
let input: HTMLInputElement | null = null;
|
|
214
|
+
if (spec.kind === "prompt") {
|
|
215
|
+
input = document.createElement("input");
|
|
216
|
+
input.type = "text";
|
|
217
|
+
input.value = typeof o.value === "string" ? o.value : "";
|
|
218
|
+
if (o.placeholder) input.placeholder = String(o.placeholder);
|
|
219
|
+
input.style.cssText =
|
|
220
|
+
"display:block;margin:12px 18px 0;width:calc(100% - 36px);box-sizing:border-box;border:1px solid " + V.border +
|
|
221
|
+
";border-radius:6px;padding:7px 10px;font-size:13px;background:" + V.hover + ";color:" + V.text + ";font-family:inherit;outline:none;";
|
|
222
|
+
input.addEventListener("focus", () => { input!.style.borderColor = V.accent; });
|
|
223
|
+
input.addEventListener("blur", () => { input!.style.borderColor = V.border; });
|
|
224
|
+
input.addEventListener("keydown", (e) => {
|
|
225
|
+
if (e.key === "Enter") { e.preventDefault(); finish(input && o.required && !input.value.trim() ? undefined : input!.value); }
|
|
226
|
+
});
|
|
227
|
+
panel.appendChild(input);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// 页脚
|
|
231
|
+
const foot = document.createElement("div");
|
|
232
|
+
foot.style.cssText = "display:flex;align-items:center;justify-content:flex-end;gap:8px;padding:16px 18px;";
|
|
233
|
+
const mk = (label: string, primary: boolean, dangerStyle: boolean, onClick: () => void) => {
|
|
234
|
+
const b = document.createElement("button");
|
|
235
|
+
b.textContent = label;
|
|
236
|
+
b.style.cssText = Object.entries({
|
|
237
|
+
...btnBase,
|
|
238
|
+
...(primary ? { background: dangerStyle ? KIND_COLOR.error : V.accent, borderColor: "transparent", color: "#fff", fontWeight: "600" } : {}),
|
|
239
|
+
}).map(([k, v]) => k + ":" + v).join(";") + ";";
|
|
240
|
+
b.addEventListener("click", onClick);
|
|
241
|
+
foot.appendChild(b);
|
|
242
|
+
return b;
|
|
243
|
+
};
|
|
244
|
+
if (spec.kind === "alert") {
|
|
245
|
+
const ok = mk(o.confirmText || "确定", true, false, () => finish(undefined));
|
|
246
|
+
(ok as any).__primary = true;
|
|
247
|
+
} else {
|
|
248
|
+
mk(o.cancelText || "取消", false, false, () => finish(spec.kind === "prompt" ? null : false));
|
|
249
|
+
const ok = mk(o.confirmText || (spec.kind === "confirm" && o.danger ? "确认执行" : "确定"), true, !!(spec.kind === "confirm" && o.danger), () =>
|
|
250
|
+
finish(spec.kind === "prompt" ? (input && o.required && !input.value.trim() ? undefined : input!.value) : true));
|
|
251
|
+
(ok as any).__primary = true;
|
|
252
|
+
}
|
|
253
|
+
panel.appendChild(foot);
|
|
254
|
+
|
|
255
|
+
// 键盘:Esc=取消;Enter=确认(焦点在非按钮元素时)
|
|
256
|
+
panel.addEventListener("keydown", (e) => {
|
|
257
|
+
if (e.key === "Escape") { e.stopPropagation(); finish(spec.kind === "prompt" ? null : spec.kind === "confirm" ? false : undefined); }
|
|
258
|
+
else if (e.key === "Enter" && !(e.target instanceof HTMLButtonElement)) {
|
|
259
|
+
e.preventDefault();
|
|
260
|
+
(foot.querySelector("button") ? Array.from(foot.querySelectorAll("button")).at(-1) : null)?.click();
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
return input;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** 渲染一个弹窗实例;done 由队列在 resolve 后回调(放下一拍,让 DOM 先离场) */
|
|
267
|
+
function showDialog(spec: PendingSpec, done: () => void) {
|
|
268
|
+
const { overlay, panel } = mount();
|
|
269
|
+
const prevFocus = document.activeElement as HTMLElement | null;
|
|
270
|
+
let settled = false;
|
|
271
|
+
const finish = (v: unknown) => {
|
|
272
|
+
if (settled) return;
|
|
273
|
+
settled = true;
|
|
274
|
+
spec.resolve(v);
|
|
275
|
+
try { overlay.remove(); } catch {}
|
|
276
|
+
try { prevFocus && typeof prevFocus.focus === "function" && prevFocus.focus(); } catch {}
|
|
277
|
+
setTimeout(done, 0);
|
|
278
|
+
};
|
|
279
|
+
const input = buildBody(panel, spec, finish);
|
|
280
|
+
// 初始焦点:prompt → 输入框(全选);confirm/alert → 主按钮
|
|
281
|
+
setTimeout(() => {
|
|
282
|
+
try {
|
|
283
|
+
if (input) { input.focus(); input.select(); }
|
|
284
|
+
else {
|
|
285
|
+
const btns = panel.querySelectorAll("button");
|
|
286
|
+
(btns.length ? btns[btns.length - 1] : panel).focus();
|
|
287
|
+
}
|
|
288
|
+
} catch {}
|
|
289
|
+
}, 0);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// ---------- 公开 API ----------
|
|
293
|
+
|
|
294
|
+
/** 确认框(替代 window.confirm):确定 → true,取消/Esc → false */
|
|
295
|
+
export function uiConfirm(opts: UiConfirmOptions | string): Promise<boolean> {
|
|
296
|
+
const o = typeof opts === "string" ? { message: opts } : opts;
|
|
297
|
+
return enqueue<boolean>("confirm", o);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** 提示框(替代 window.alert):确定/Esc 关闭 */
|
|
301
|
+
export function uiAlert(opts: UiAlertOptions | string): Promise<void> {
|
|
302
|
+
const o = typeof opts === "string" ? { message: opts } : opts;
|
|
303
|
+
return enqueue<void>("alert", o);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** 输入框(替代 window.prompt):确定 → 字符串(可能空串),取消/Esc → null */
|
|
307
|
+
export function uiPrompt(opts: UiPromptOptions | string): Promise<string | null> {
|
|
308
|
+
const o = typeof opts === "string" ? { message: opts } : opts;
|
|
309
|
+
return enqueue<string | null>("prompt", o);
|
|
310
|
+
}
|