@aiti-ai/ext-shared 0.1.12 → 0.1.16
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/README.zh-CN.md +27 -0
- package/agent-spawn-env.test.mjs +92 -0
- package/agent-spawn-env.ts +92 -1
- package/dev-pack.mjs +16 -3
- package/dock-icons.ts +145 -13
- package/ext-display-names.ts +108 -78
- package/ext-entry-contracts.test.mjs +8 -10
- package/ext-float-dock-core.ts +66 -9
- package/ext-float-dock.test.mjs +1 -0
- package/ext-float-dock.ts +181 -32
- package/ext-protocol.mjs +1 -1
- package/git-ops-client/api.mjs +3 -1
- package/git-ops-client/branch-panel.tsx +115 -20
- package/git-ops-client/graph-lanes.mjs +118 -0
- package/git-ops-client/graph-lanes.test.mjs +132 -0
- package/git-ops-client/i18n.mjs +9 -0
- package/icons/box.svg +1 -0
- package/icons/discussion.svg +1 -0
- package/icons/film.svg +1 -0
- package/icons/flag.svg +1 -0
- package/icons/message-square-text.svg +1 -0
- package/icons/qr-code.svg +1 -0
- package/icons/star.svg +1 -0
- package/menu-row.tsx +92 -0
- package/new-session-cwd.test.mjs +4 -4
- package/package.json +2 -2
- package/path-breadcrumb.tsx +363 -0
- package/path-crumbs.test.mjs +91 -0
- package/path-crumbs.ts +102 -0
- package/pi-fork-rpc/wire.test.mjs +4 -3
- package/pi-fork-rpc/wire.ts +39 -2
- package/popup-anchor.ts +27 -2
- package/soffice-panel.ts +39 -30
- package/typo-search.mjs +79 -0
- package/typo-search.test.mjs +57 -0
- package/ui-dialogs.test.mjs +59 -4
- package/ui-dialogs.ts +95 -9
- package/sync-gate.test.mjs +0 -93
- package/sync-mirror.test.mjs +0 -85
package/popup-anchor.ts
CHANGED
|
@@ -9,9 +9,15 @@
|
|
|
9
9
|
* (fixedOrigin)。
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
/**
|
|
13
|
-
* will-change
|
|
12
|
+
/** Whether an ancestor constitutes a fixed containing block (common CSS subset:
|
|
13
|
+
* transform/perspective/filter, will-change declaring the same properties, contain
|
|
14
|
+
* layout values, zoom≠1 — standardized CSS zoom makes fixed descendants position
|
|
15
|
+
* relative to the zoomed ancestor with insets scaled by it; verified on Chrome 157:
|
|
16
|
+
* an inset:0 overlay inside a zoom container renders at the container size, not the
|
|
17
|
+
* viewport size). */
|
|
14
18
|
function isFixedContainingBlock(cs: CSSStyleDeclaration): boolean {
|
|
19
|
+
const zoomValue = parseFloat(cs.zoom || "");
|
|
20
|
+
if (Number.isFinite(zoomValue) && zoomValue !== 1) return true;
|
|
15
21
|
if (cs.transform !== "none" || cs.perspective !== "none" || cs.filter !== "none") return true;
|
|
16
22
|
if (/transform|perspective|filter/.test(cs.willChange || "")) return true;
|
|
17
23
|
const c = cs.contain;
|
|
@@ -34,6 +40,25 @@ export function fixedOrigin(el: Element): { x: number; y: number } {
|
|
|
34
40
|
return { x: 0, y: 0 };
|
|
35
41
|
}
|
|
36
42
|
|
|
43
|
+
/** Product of `zoom` values on el's ancestor chain (excluding el itself; 1 when none).
|
|
44
|
+
* Standardized CSS zoom makes the nearest zoomed ancestor the containing block of
|
|
45
|
+
* fixed descendants and scales their insets — a popup rendered inside a zoomed panel
|
|
46
|
+
* must divide viewport-space coordinates by this factor after subtracting the
|
|
47
|
+
* containing-block origin (fixedOrigin, which now matches on zoomed ancestors too):
|
|
48
|
+
* local = (expectedViewport − origin) / zoomFactor. */
|
|
49
|
+
export function zoomFactorOf(el: Element): number {
|
|
50
|
+
let z = 1;
|
|
51
|
+
let n = el.parentElement;
|
|
52
|
+
while (n && n !== document.documentElement) {
|
|
53
|
+
try {
|
|
54
|
+
const zoomValue = parseFloat(getComputedStyle(n).zoom || "");
|
|
55
|
+
if (Number.isFinite(zoomValue) && zoomValue > 0 && zoomValue !== 1) z *= zoomValue;
|
|
56
|
+
} catch { /* ignore */ }
|
|
57
|
+
n = n.parentElement;
|
|
58
|
+
}
|
|
59
|
+
return z;
|
|
60
|
+
}
|
|
61
|
+
|
|
37
62
|
/** 视口内钳制(pad 内边距;宽/高超视口时贴 pad 对齐)。入参/返回均为视口坐标。 */
|
|
38
63
|
export function clampToViewport(left: number, top: number, width: number, height: number, pad = 8): { left: number; top: number } {
|
|
39
64
|
const vw = window.innerWidth;
|
package/soffice-panel.ts
CHANGED
|
@@ -9,8 +9,15 @@
|
|
|
9
9
|
* - 替代动作:⬇ 下载到本机编辑;🖥 虚拟环境打开(远程查看,需 aiti-ui-env)
|
|
10
10
|
*
|
|
11
11
|
* 纯 DOM(无 JSX/无框架依赖),aiti-editor 与 aiti-sheet-univer bundle 共用。
|
|
12
|
+
*
|
|
13
|
+
* i18n(2026-11-17 ext-i18n convention):copy uses Chinese raw text as t() keys; the
|
|
14
|
+
* consumer injects its own ctx.t via opts.t (per-extension ui.i18n table carries the
|
|
15
|
+
* zh-CN identity + en translation entries). Absent opts.t → raw text (current behavior).
|
|
12
16
|
*/
|
|
13
17
|
|
|
18
|
+
/** Consumer-injected translate fn (host ctx.t shape; params interpolation {name}). */
|
|
19
|
+
export type SofficePanelT = (key: string, params?: Record<string, unknown>) => string;
|
|
20
|
+
|
|
14
21
|
export interface SofficeStatus {
|
|
15
22
|
available?: boolean;
|
|
16
23
|
path?: string | null;
|
|
@@ -42,25 +49,27 @@ export interface SofficePanelOpts {
|
|
|
42
49
|
onClose?: () => void;
|
|
43
50
|
/** 转换重试:提供则在面板底部显示「重试转换」按钮 */
|
|
44
51
|
onRetry?: () => void;
|
|
52
|
+
/** i18n translate (consumer's ctx.t); absent → raw text fallback */
|
|
53
|
+
t?: SofficePanelT;
|
|
45
54
|
}
|
|
46
55
|
|
|
47
56
|
const shellQuote = (s: string) => "'" + String(s).replace(/'/g, "'\\''") + "'";
|
|
48
57
|
|
|
49
58
|
export function buildSofficeMissingPanel(opts: SofficePanelOpts): HTMLElement {
|
|
50
59
|
const { apiUrl, filePath } = opts;
|
|
60
|
+
const tr: SofficePanelT = opts.t || ((k) => k);
|
|
51
61
|
const el = document.createElement("div");
|
|
52
62
|
el.style.cssText = "position:absolute;inset:0;overflow:auto;padding:22px 26px;background:var(--bg,#fff);font-size:13px;line-height:1.55;color:var(--text,#1a1a1a);z-index:10;";
|
|
53
63
|
|
|
54
64
|
const title = document.createElement("div");
|
|
55
65
|
title.style.cssText = "font-weight:700;font-size:15px;margin-bottom:6px;";
|
|
56
|
-
title.textContent = "转换该文件需要 LibreOffice (soffice)";
|
|
66
|
+
title.textContent = tr("转换该文件需要 LibreOffice (soffice)");
|
|
57
67
|
el.appendChild(title);
|
|
58
68
|
|
|
59
69
|
const why = document.createElement("div");
|
|
60
70
|
why.style.cssText = "margin-bottom:12px;";
|
|
61
|
-
why.innerHTML = "服务器端未检测到 LibreOffice,因此无法把老格式"
|
|
62
|
-
|
|
63
|
-
+ "。两个解决方向:在<b>服务器</b>安装并配置 LibreOffice(推荐,装完即可在线转换);或把文件下载到<b>你本地</b>用 Office 编辑。";
|
|
71
|
+
why.innerHTML = tr("服务器端未检测到 LibreOffice,因此无法把老格式{target}。两个解决方向:在<b>服务器</b>安装并配置 LibreOffice(推荐,装完即可在线转换);或把文件下载到<b>你本地</b>用 Office 编辑。",
|
|
72
|
+
{ target: opts.targetLabel ? tr("转换成 {label}", { label: opts.targetLabel }) : "" });
|
|
64
73
|
el.appendChild(why);
|
|
65
74
|
|
|
66
75
|
// 安装指引
|
|
@@ -72,7 +81,7 @@ export function buildSofficeMissingPanel(opts: SofficePanelOpts): HTMLElement {
|
|
|
72
81
|
el.appendChild(d);
|
|
73
82
|
return d;
|
|
74
83
|
};
|
|
75
|
-
box("服务器安装 LibreOffice(任选其一)");
|
|
84
|
+
box(tr("服务器安装 LibreOffice(任选其一)"));
|
|
76
85
|
const cmds: Array<[string, string]> = [
|
|
77
86
|
["Linux (Debian/Ubuntu)", "sudo apt install libreoffice fonts-noto-cjk"],
|
|
78
87
|
["macOS", "brew install --cask libreoffice"],
|
|
@@ -99,15 +108,15 @@ export function buildSofficeMissingPanel(opts: SofficePanelOpts): HTMLElement {
|
|
|
99
108
|
const aiBtn = document.createElement("button");
|
|
100
109
|
aiBtn.type = "button";
|
|
101
110
|
aiBtn.style.cssText = btnStyle + "background:var(--bg-selected,#e8edff);border-color:var(--accent,#7c8cf8);";
|
|
102
|
-
aiBtn.innerHTML = "AI 帮我安装(推荐)";
|
|
103
|
-
aiBtn.title = "调起 aiti-helper 并自动选中「AI 帮我在服务器安装 LibreOffice」帮助点,AI 自动检测平台(Linux/Windows/macOS)、安装并验证";
|
|
111
|
+
aiBtn.innerHTML = tr("AI 帮我安装(推荐)");
|
|
112
|
+
aiBtn.title = tr("调起 aiti-helper 并自动选中「AI 帮我在服务器安装 LibreOffice」帮助点,AI 自动检测平台(Linux/Windows/macOS)、安装并验证");
|
|
104
113
|
const aiTipText = document.createElement("span");
|
|
105
114
|
aiTipText.style.cssText = "flex:1 1 200px;font-size:11px;color:var(--text-dim,#888);";
|
|
106
|
-
aiTipText.textContent = "不想手动装?点按钮唤起 AI 助手自动完成检测、安装、验证;也可点弹窗右上角 🤖 手动选帮助点。";
|
|
115
|
+
aiTipText.textContent = tr("不想手动装?点按钮唤起 AI 助手自动完成检测、安装、验证;也可点弹窗右上角 🤖 手动选帮助点。");
|
|
107
116
|
aiBtn.onclick = () => {
|
|
108
117
|
const w = window as any;
|
|
109
118
|
if (typeof w.__aitiHelperInvoke !== "function") {
|
|
110
|
-
aiTipText.textContent = "aiti-helper 未就绪(未部署或未加载):刷新页面后重试,或按上方命令手动安装。";
|
|
119
|
+
aiTipText.textContent = tr("aiti-helper 未就绪(未部署或未加载):刷新页面后重试,或按上方命令手动安装。");
|
|
111
120
|
return;
|
|
112
121
|
}
|
|
113
122
|
w.__aitiHelperInvoke({
|
|
@@ -118,23 +127,23 @@ export function buildSofficeMissingPanel(opts: SofficePanelOpts): HTMLElement {
|
|
|
118
127
|
extId: "aiti-editor",
|
|
119
128
|
extName: "aiti-editor",
|
|
120
129
|
});
|
|
121
|
-
aiTipText.textContent = "已唤起 AI 助手并自动发起安装任务——切到 AI 助手弹窗查看进度;装完回本页点「重试转换」。";
|
|
130
|
+
aiTipText.textContent = tr("已唤起 AI 助手并自动发起安装任务——切到 AI 助手弹窗查看进度;装完回本页点「重试转换」。");
|
|
122
131
|
};
|
|
123
132
|
aiRow.append(aiBtn, aiTipText);
|
|
124
133
|
el.appendChild(aiRow);
|
|
125
134
|
|
|
126
135
|
// 配置指引
|
|
127
|
-
box("已安装但不在 PATH?配置路径");
|
|
136
|
+
box(tr("已安装但不在 PATH?配置路径"));
|
|
128
137
|
const cfg = document.createElement("div");
|
|
129
138
|
cfg.style.cssText = "font-size:12px;";
|
|
130
|
-
cfg.textContent = "扩展管理器 → aiti-editor → 功能 → 「soffice 路径(LibreOffice)」填入绝对路径(如 /opt/libreoffice/program/soffice),保存后刷新页面重试。";
|
|
139
|
+
cfg.textContent = tr("扩展管理器 → aiti-editor → 功能 → 「soffice 路径(LibreOffice)」填入绝对路径(如 /opt/libreoffice/program/soffice),保存后刷新页面重试。");
|
|
131
140
|
el.appendChild(cfg);
|
|
132
141
|
|
|
133
142
|
// 分隔
|
|
134
143
|
const sep = document.createElement("div");
|
|
135
144
|
sep.style.cssText = "height:1px;background:var(--border,#eee);margin:14px 0;";
|
|
136
145
|
el.appendChild(sep);
|
|
137
|
-
box("替代方案");
|
|
146
|
+
box(tr("替代方案"));
|
|
138
147
|
|
|
139
148
|
const btns = document.createElement("div");
|
|
140
149
|
btns.style.cssText = "display:flex;flex-wrap:wrap;gap:8px;";
|
|
@@ -143,8 +152,8 @@ export function buildSofficeMissingPanel(opts: SofficePanelOpts): HTMLElement {
|
|
|
143
152
|
const dl = document.createElement("button");
|
|
144
153
|
dl.type = "button";
|
|
145
154
|
dl.style.cssText = btnStyle;
|
|
146
|
-
dl.innerHTML = "下载到本机编辑";
|
|
147
|
-
dl.title = "下载原文件,在你本地用 Office/WPS 编辑";
|
|
155
|
+
dl.innerHTML = tr("下载到本机编辑");
|
|
156
|
+
dl.title = tr("下载原文件,在你本地用 Office/WPS 编辑");
|
|
148
157
|
dl.onclick = async () => {
|
|
149
158
|
dl.disabled = true;
|
|
150
159
|
try {
|
|
@@ -153,7 +162,7 @@ export function buildSofficeMissingPanel(opts: SofficePanelOpts): HTMLElement {
|
|
|
153
162
|
method: "POST", headers: { "content-type": "application/json" },
|
|
154
163
|
body: JSON.stringify({ filePath }),
|
|
155
164
|
}).then((r) => r.json());
|
|
156
|
-
if (!t.ticket) throw new Error(t.error || "票据签发失败");
|
|
165
|
+
if (!t.ticket) throw new Error(t.error || tr("票据签发失败"));
|
|
157
166
|
const a = document.createElement("a");
|
|
158
167
|
a.href = base + "/t/" + t.ticket + "/file";
|
|
159
168
|
a.download = filePath.split("/").pop() || "download";
|
|
@@ -172,35 +181,35 @@ export function buildSofficeMissingPanel(opts: SofficePanelOpts): HTMLElement {
|
|
|
172
181
|
const venv = document.createElement("button");
|
|
173
182
|
venv.type = "button";
|
|
174
183
|
venv.style.cssText = btnStyle;
|
|
175
|
-
venv.innerHTML = "虚拟环境打开(远程查看)";
|
|
176
|
-
venv.title = "在服务器的虚拟桌面用 LibreOffice 打开本文件,经浏览器 VNC 远程查看/编辑(需 aiti-ui-env 扩展)";
|
|
184
|
+
venv.innerHTML = tr("虚拟环境打开(远程查看)");
|
|
185
|
+
venv.title = tr("在服务器的虚拟桌面用 LibreOffice 打开本文件,经浏览器 VNC 远程查看/编辑(需 aiti-ui-env 扩展)");
|
|
177
186
|
venv.onclick = async () => {
|
|
178
187
|
venv.disabled = true;
|
|
179
|
-
venv.textContent = "虚拟环境准备中…";
|
|
188
|
+
venv.textContent = tr("虚拟环境准备中…");
|
|
180
189
|
const progress = (m: string) => { venv.textContent = m; };
|
|
181
190
|
try {
|
|
182
191
|
const ui = apiUrl("/api/extensions/aiti-ui-env");
|
|
183
192
|
// 1) 虚拟屏就绪
|
|
184
193
|
const hd = await fetch(ui + "/headless", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ action: "start" }) }).then((r) => r.json());
|
|
185
|
-
if (!hd.ok) throw new Error("虚拟屏启动失败:"
|
|
194
|
+
if (!hd.ok) throw new Error(tr("虚拟屏启动失败:{msg}", { msg: hd.error || tr("未知") }));
|
|
186
195
|
// 2) 虚拟桌面里用 LibreOffice 打开文件
|
|
187
196
|
const run = await fetch(ui + "/run", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ cmd: "soffice --norestore " + shellQuote(filePath), prefer: "headless" }) }).then((r) => r.json());
|
|
188
|
-
if (!run.ok) throw new Error("LibreOffice 启动失败:"
|
|
197
|
+
if (!run.ok) throw new Error(tr("LibreOffice 启动失败:{msg}", { msg: run.error || tr("未知") }));
|
|
189
198
|
// 3) VNC 起桥
|
|
190
199
|
const vnc = await fetch(ui + "/vnc", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ action: "start" }) }).then((r) => r.json());
|
|
191
|
-
if (!vnc.ok) throw new Error("VNC 启动失败:"
|
|
200
|
+
if (!vnc.ok) throw new Error(tr("VNC 启动失败:{msg}", { msg: vnc.error || tr("未知") }));
|
|
192
201
|
// 4) 会话 cookie + 查看页路径
|
|
193
202
|
const sess = await fetch(ui + "/vnc-session", { method: "POST" }).then((r) => r.json());
|
|
194
|
-
if (!sess.ok || !sess.path) throw new Error("VNC 会话获取失败");
|
|
195
|
-
progress("已在虚拟环境打开,正在跳转远程查看…");
|
|
203
|
+
if (!sess.ok || !sess.path) throw new Error(tr("VNC 会话获取失败"));
|
|
204
|
+
progress(tr("已在虚拟环境打开,正在跳转远程查看…"));
|
|
196
205
|
window.open(apiUrl(sess.path), "_blank");
|
|
197
|
-
venv.textContent = "虚拟环境打开(远程查看)";
|
|
206
|
+
venv.textContent = tr("虚拟环境打开(远程查看)");
|
|
198
207
|
venv.disabled = false;
|
|
199
208
|
} catch (e) {
|
|
200
209
|
const msg = String((e as Error).message || e);
|
|
201
210
|
venv.textContent = msg.includes("404") || msg.includes("Failed to fetch")
|
|
202
|
-
? "虚拟环境打开(需 aiti-ui-env 扩展)"
|
|
203
|
-
: "虚拟环境打开(重试)";
|
|
211
|
+
? tr("虚拟环境打开(需 aiti-ui-env 扩展)")
|
|
212
|
+
: tr("虚拟环境打开(重试)");
|
|
204
213
|
venv.title = msg;
|
|
205
214
|
venv.disabled = false;
|
|
206
215
|
}
|
|
@@ -210,7 +219,7 @@ export function buildSofficeMissingPanel(opts: SofficePanelOpts): HTMLElement {
|
|
|
210
219
|
|
|
211
220
|
const note = document.createElement("div");
|
|
212
221
|
note.style.cssText = "margin-top:10px;font-size:11px;color:var(--text-dim,#888);";
|
|
213
|
-
note.textContent = "「虚拟环境打开」在服务器的虚拟桌面上运行 LibreOffice 并经浏览器 VNC 远程查看——适合不想在本地装 Office、且服务器已配置虚拟屏(aiti-ui-env)的场景。";
|
|
222
|
+
note.textContent = tr("「虚拟环境打开」在服务器的虚拟桌面上运行 LibreOffice 并经浏览器 VNC 远程查看——适合不想在本地装 Office、且服务器已配置虚拟屏(aiti-ui-env)的场景。");
|
|
214
223
|
el.appendChild(note);
|
|
215
224
|
|
|
216
225
|
// 重试 / 关闭
|
|
@@ -220,7 +229,7 @@ export function buildSofficeMissingPanel(opts: SofficePanelOpts): HTMLElement {
|
|
|
220
229
|
if (opts.onRetry) {
|
|
221
230
|
const retry = document.createElement("button");
|
|
222
231
|
retry.type = "button";
|
|
223
|
-
retry.textContent = "重试转换";
|
|
232
|
+
retry.textContent = tr("重试转换");
|
|
224
233
|
retry.style.cssText = btnStyle;
|
|
225
234
|
retry.onclick = () => opts.onRetry!();
|
|
226
235
|
row.appendChild(retry);
|
|
@@ -228,7 +237,7 @@ export function buildSofficeMissingPanel(opts: SofficePanelOpts): HTMLElement {
|
|
|
228
237
|
if (opts.onClose) {
|
|
229
238
|
const x = document.createElement("button");
|
|
230
239
|
x.type = "button";
|
|
231
|
-
x.textContent = "关闭";
|
|
240
|
+
x.textContent = tr("关闭");
|
|
232
241
|
x.style.cssText = btnStyle + "background:transparent;";
|
|
233
242
|
x.onclick = () => opts.onClose!();
|
|
234
243
|
row.appendChild(x);
|
package/typo-search.mjs
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 错字容忍搜索(typo-tolerant matching)。
|
|
3
|
+
*
|
|
4
|
+
* 场景:扩展搜索框输入 servcie / sevice 等错字也要命中 service-tunnels。
|
|
5
|
+
* 设计:
|
|
6
|
+
* - OSA 距离(Damerau-Levenshtein 受限版):相邻转置算 1 步——servcie→service=1、
|
|
7
|
+
* sevice→service=1(纯 Levenshtein 转置=2,短词阈值会漏)。
|
|
8
|
+
* - 阈值按 query 长度自适应(typo-tolerance 惯例):≤3 字符不启用模糊(噪音大),
|
|
9
|
+
* 4-7 允 1 错,8+ 允 2 错。
|
|
10
|
+
* - 只对无空格短 query 启用;候选文本切成 token(字母数字/中文段)逐个比距离,
|
|
11
|
+
* 长度差剪枝 + 超长 token(中文长句整段)跳过。
|
|
12
|
+
* 纯函数无依赖:client bundle(vite)与 node --test 直接跑同一份实现。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** OSA(optimal string alignment)编辑距离。 */
|
|
16
|
+
export function osaDistance(a, b) {
|
|
17
|
+
const m = a.length;
|
|
18
|
+
const n = b.length;
|
|
19
|
+
if (m === 0) return n;
|
|
20
|
+
if (n === 0) return m;
|
|
21
|
+
let prev2 = null;
|
|
22
|
+
let prev = Array.from({ length: n + 1 }, (_, j) => j);
|
|
23
|
+
for (let i = 1; i <= m; i++) {
|
|
24
|
+
const cur = [i];
|
|
25
|
+
for (let j = 1; j <= n; j++) {
|
|
26
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
27
|
+
let v = Math.min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + cost);
|
|
28
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
29
|
+
v = Math.min(v, prev2[j - 2] + 1);
|
|
30
|
+
}
|
|
31
|
+
cur[j] = v;
|
|
32
|
+
}
|
|
33
|
+
prev2 = prev;
|
|
34
|
+
prev = cur;
|
|
35
|
+
}
|
|
36
|
+
return prev[n];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** query 允许的错字数(按长度自适应;≤3 返回 0 = 不启用模糊)。 */
|
|
40
|
+
export function typoThreshold(q) {
|
|
41
|
+
const len = [...String(q)].length;
|
|
42
|
+
if (len <= 3) return 0;
|
|
43
|
+
if (len <= 7) return 1;
|
|
44
|
+
return 2;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const TOKEN_SPLIT = /[^a-z0-9\u4e00-\u9fa5]+/;
|
|
48
|
+
const MAX_TOKEN_LEN = 24; // 超长 token(无分隔的中文长句等)不做模糊:DP 开销与误命中噪音都不划算
|
|
49
|
+
|
|
50
|
+
function* candidateTokens(text) {
|
|
51
|
+
for (const tok of String(text || "").toLowerCase().split(TOKEN_SPLIT)) {
|
|
52
|
+
if (tok && tok.length <= MAX_TOKEN_LEN) yield tok;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 错字容忍命中判断:query 为无空格短词时,对候选文本的 token 逐个比 OSA 距离。
|
|
58
|
+
* 除常规 token 外还检查「去分隔符形态」(service-tunnels → servicetunnels),
|
|
59
|
+
* 让漏连字符/错连字符的 query 也能命中。不满足启用条件(多词/超短)一律返回
|
|
60
|
+
* false,调用方回退精确 includes 行为(本函数不含 includes 语义)。
|
|
61
|
+
*/
|
|
62
|
+
export function typoMatch(q, text) {
|
|
63
|
+
const query = String(q || "").trim().toLowerCase();
|
|
64
|
+
if (!query || /\s/.test(query)) return false;
|
|
65
|
+
const threshold = typoThreshold(query);
|
|
66
|
+
if (threshold <= 0) return false;
|
|
67
|
+
const raw = String(text || "");
|
|
68
|
+
for (const src of [raw, raw.replace(/[-_/]/g, "")]) {
|
|
69
|
+
for (const tok of candidateTokens(src)) {
|
|
70
|
+
if (Math.abs(tok.length - query.length) > 2) continue; // 长度差剪枝(上限 2:为双错漏字放宽保留)
|
|
71
|
+
const d = osaDistance(query, tok);
|
|
72
|
+
if (d <= threshold) return true;
|
|
73
|
+
// 漏字场景放宽到 2 错(sevic→service 删 r+删尾 e=2、tunel→tunnels=2),
|
|
74
|
+
// 但要求首字符一致(错字通常不在首位,锚定防误报:zzvic 不命中 service)
|
|
75
|
+
if (d <= 2 && query[0] === tok[0]) return true;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// typo-search unit tests: node --test _shared/typo-search.test.mjs
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import assert from "node:assert/strict";
|
|
4
|
+
import { osaDistance, typoThreshold, typoMatch } from "./typo-search.mjs";
|
|
5
|
+
|
|
6
|
+
const HAY = "service-tunnels 服务隧道 Service Tunnels 本机服务一键公网可达 @aiti-ai/aiti-service-tunnels";
|
|
7
|
+
|
|
8
|
+
test("osaDistance: 转置算 1 步(Damerau 语义,纯 Levenshtein 会是 2)", () => {
|
|
9
|
+
assert.equal(osaDistance("servcie", "service"), 1);
|
|
10
|
+
assert.equal(osaDistance("sevice", "service"), 1); // 删 r
|
|
11
|
+
assert.equal(osaDistance("service", "service"), 0);
|
|
12
|
+
assert.equal(osaDistance("abc", "yxa"), 3);
|
|
13
|
+
assert.equal(osaDistance("", "ab"), 2);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("typoThreshold: 按长度自适应", () => {
|
|
17
|
+
assert.equal(typoThreshold("abc"), 0); // ≤3 不启用
|
|
18
|
+
assert.equal(typoThreshold("sevice"), 1); // 4-7
|
|
19
|
+
assert.equal(typoThreshold("service-tunnels"), 2); // 8+
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("typoMatch: 用户用例——servcie / sevice 命中 service-tunnels", () => {
|
|
23
|
+
assert.equal(typoMatch("servcie", HAY), true);
|
|
24
|
+
assert.equal(typoMatch("sevice", HAY), true);
|
|
25
|
+
assert.equal(typoMatch("servie", HAY), true);
|
|
26
|
+
assert.equal(typoMatch("servicetunnels", HAY), true); // 对整串 id 距离 1(漏连字符)
|
|
27
|
+
// 完整拼错的复合 query(含连字符)对整串 token 命中
|
|
28
|
+
assert.equal(typoMatch("servcie-tunnels", HAY), true);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("typoMatch: 双错漏字——首字符锚定放宽(sevic→service 删 r+删尾 e)", () => {
|
|
32
|
+
assert.equal(typoMatch("sevic", HAY), true);
|
|
33
|
+
assert.equal(typoMatch("tunel", HAY), true); // tunnels 漏 n+漏 s
|
|
34
|
+
assert.equal(typoMatch("zzvic", HAY), false); // 首字符不同不放宽(防误报)
|
|
35
|
+
assert.equal(typoMatch("evics", HAY), false);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("typoMatch: 中文错字", () => {
|
|
39
|
+
assert.equal(typoMatch("服务随道", HAY), true); // 1 错
|
|
40
|
+
assert.equal(typoMatch("服物隧道", HAY), true); // 1 错
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("typoMatch: 不该命中的(防噪音)", () => {
|
|
44
|
+
assert.equal(typoMatch("zzzzzzz", HAY), false);
|
|
45
|
+
assert.equal(typoMatch("npm", HAY), false); // ≤3 字符不启用模糊
|
|
46
|
+
assert.equal(typoMatch("service tunnels", HAY), false); // 多词 query 不启用(includes 在调用方处理)
|
|
47
|
+
// 精确子串命中由调用方 includes 层负责,typoMatch 本身不含 includes 语义
|
|
48
|
+
assert.equal(typoMatch("aiti-service-tunnels", HAY), false);
|
|
49
|
+
assert.equal(typoMatch("draft-keeper", HAY), false); // 别的扩展名不该命中
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("typoMatch: 边界(空/非字符串)", () => {
|
|
53
|
+
assert.equal(typoMatch("", HAY), false);
|
|
54
|
+
assert.equal(typoMatch(" sevice ", HAY), true); // trim 后生效
|
|
55
|
+
assert.equal(typoMatch("sevice", ""), false);
|
|
56
|
+
assert.equal(typoMatch(null, HAY), false);
|
|
57
|
+
});
|
package/ui-dialogs.test.mjs
CHANGED
|
@@ -18,8 +18,11 @@ import { fileURLToPath, pathToFileURL } from "node:url";
|
|
|
18
18
|
|
|
19
19
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
20
20
|
|
|
21
|
-
/** DOM
|
|
22
|
-
|
|
21
|
+
/** DOM stub: enough for ui-dialogs' create / event / query paths.
|
|
22
|
+
* opts.dialog=true gives createElement("dialog") showModal/close (browser top-layer path);
|
|
23
|
+
* omitted by default (legacy browser / no showModal → z-index fallback path). */
|
|
24
|
+
function makeDomStub(opts) {
|
|
25
|
+
const withDialog = !!(opts && opts.dialog);
|
|
23
26
|
const mkEl = (tag = "div") => {
|
|
24
27
|
const el = {
|
|
25
28
|
tag, style: new Proxy({}, { get: (t, k) => (k === "cssText" ? t.cssText : t[k]), set: (t, k, v) => { t[k] = v; return true; } }),
|
|
@@ -52,6 +55,11 @@ function makeDomStub() {
|
|
|
52
55
|
return out;
|
|
53
56
|
},
|
|
54
57
|
};
|
|
58
|
+
if (withDialog && tag === "dialog") {
|
|
59
|
+
el.open = false;
|
|
60
|
+
el.showModal = function () { this.open = true; (this.modalCalls ||= []).push("showModal"); };
|
|
61
|
+
el.close = function () { this.open = false; (this.modalCalls ||= []).push("close"); };
|
|
62
|
+
}
|
|
55
63
|
return el;
|
|
56
64
|
};
|
|
57
65
|
const body = mkEl("body");
|
|
@@ -64,10 +72,10 @@ function makeDomStub() {
|
|
|
64
72
|
return { document, body };
|
|
65
73
|
}
|
|
66
74
|
|
|
67
|
-
async function loadModule() {
|
|
75
|
+
async function loadModule(opts = {}) {
|
|
68
76
|
const dir = mkdtempSync(join(tmpdir(), "uidlg-test-"));
|
|
69
77
|
const stub = `
|
|
70
|
-
const { document } = (${makeDomStubSource})();
|
|
78
|
+
const { document } = (${makeDomStubSource})(${JSON.stringify(opts)});
|
|
71
79
|
globalThis.document = document;
|
|
72
80
|
export * from ${JSON.stringify(join(here, "ui-dialogs.ts"))};
|
|
73
81
|
`;
|
|
@@ -130,6 +138,16 @@ test("ui-confirm-alert-prompt", async (t) => {
|
|
|
130
138
|
assert.equal(await p, false);
|
|
131
139
|
});
|
|
132
140
|
|
|
141
|
+
await t.test("no showModal: top-level body div + highest z-index fallback", async () => {
|
|
142
|
+
const p = mod.uiConfirm("fallback tier");
|
|
143
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
144
|
+
const overlay = overlayOf(body);
|
|
145
|
+
assert.equal(overlay.tag, "div", "without showModal the host is a plain div");
|
|
146
|
+
assert.ok(overlay.style.cssText.includes("2147483600"), "fallback tier must beat self-managed panel z-indices");
|
|
147
|
+
buttons(overlay.children[0])[0].click();
|
|
148
|
+
assert.equal(await p, false);
|
|
149
|
+
});
|
|
150
|
+
|
|
133
151
|
await t.test("confirm:Esc → false", async () => {
|
|
134
152
|
const p = mod.uiConfirm("继续吗?");
|
|
135
153
|
await new Promise((r) => setTimeout(r, 150));
|
|
@@ -196,3 +214,40 @@ test("ui-confirm-alert-prompt", async (t) => {
|
|
|
196
214
|
assert.equal(await p2, false);
|
|
197
215
|
});
|
|
198
216
|
});
|
|
217
|
+
|
|
218
|
+
test("top-layer path (<dialog>.showModal): escapes z-index stacking and closes on settle", async (t) => {
|
|
219
|
+
const { mod, cleanup } = await loadModule({ dialog: true });
|
|
220
|
+
t.after(cleanup);
|
|
221
|
+
const body = globalThis.document.body;
|
|
222
|
+
|
|
223
|
+
await t.test("showModal on mount, same semantics as the fallback (confirm/cancel/Esc)", async () => {
|
|
224
|
+
const p1 = mod.uiConfirm({ message: "Sign out this subscription?", danger: true, confirmText: "Sign out" });
|
|
225
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
226
|
+
const overlay = overlayOf(body);
|
|
227
|
+
assert.equal(overlay.tag, "dialog", "showModal available → dialog modal host");
|
|
228
|
+
assert.equal(overlay.open, true, "should enter modal (top layer)");
|
|
229
|
+
assert.deepEqual(overlay.modalCalls, ["showModal"]);
|
|
230
|
+
buttons(overlay.children[0]).at(-1).click();
|
|
231
|
+
assert.equal(await p1, true);
|
|
232
|
+
assert.equal(body.children.length, 0, "overlay leaves after confirm");
|
|
233
|
+
assert.equal(overlay.open, false, "settle must close() to exit the top layer");
|
|
234
|
+
assert.deepEqual(overlay.modalCalls, ["showModal", "close"]);
|
|
235
|
+
|
|
236
|
+
// cancel → null
|
|
237
|
+
const p2 = mod.uiPrompt({ message: "Input:", value: "x" });
|
|
238
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
239
|
+
buttons(overlayOf(body).children[0])[0].click();
|
|
240
|
+
assert.equal(await p2, null);
|
|
241
|
+
|
|
242
|
+
// native Esc (dialog cancel event) → cancel; browser must not close on its own
|
|
243
|
+
const p3 = mod.uiConfirm("Esc cancel");
|
|
244
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
245
|
+
const ov = overlayOf(body);
|
|
246
|
+
let prevented = false;
|
|
247
|
+
for (const fn of ov.handlers.cancel || []) fn({ preventDefault() { prevented = true; } });
|
|
248
|
+
assert.equal(await p3, false);
|
|
249
|
+
assert.equal(prevented, true, "cancel must preventDefault (this module owns the settle path)");
|
|
250
|
+
assert.equal(ov.open, false);
|
|
251
|
+
assert.equal(ov.removed, true);
|
|
252
|
+
});
|
|
253
|
+
});
|