@ai-matrx/alchemy 0.6.2 → 0.8.2
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/CHANGELOG.md +58 -0
- package/dist/actions.cjs +374 -0
- package/dist/actions.cjs.map +1 -0
- package/dist/actions.d.cts +773 -0
- package/dist/actions.d.ts +773 -0
- package/dist/actions.js +351 -0
- package/dist/actions.js.map +1 -0
- package/dist/checks.cjs +31 -2
- package/dist/checks.cjs.map +1 -1
- package/dist/checks.d.cts +24 -1
- package/dist/checks.d.ts +24 -1
- package/dist/checks.js +31 -2
- package/dist/checks.js.map +1 -1
- package/dist/core.cjs +1757 -2
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.cts +595 -1
- package/dist/core.d.ts +595 -1
- package/dist/core.js +1749 -2
- package/dist/core.js.map +1 -1
- package/dist/declare.cjs +12 -10
- package/dist/declare.cjs.map +1 -1
- package/dist/declare.d.cts +18 -2
- package/dist/declare.d.ts +18 -2
- package/dist/declare.js +12 -10
- package/dist/declare.js.map +1 -1
- package/dist/index.cjs +1757 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +595 -1
- package/dist/index.d.ts +595 -1
- package/dist/index.js +1749 -2
- package/dist/index.js.map +1 -1
- package/dist/menu.cjs +436 -0
- package/dist/menu.cjs.map +1 -0
- package/dist/menu.d.cts +834 -0
- package/dist/menu.d.ts +834 -0
- package/dist/menu.js +413 -0
- package/dist/menu.js.map +1 -0
- package/dist/operate-document.cjs +163 -0
- package/dist/operate-document.cjs.map +1 -0
- package/dist/operate-document.d.cts +132 -0
- package/dist/operate-document.d.ts +132 -0
- package/dist/operate-document.js +130 -0
- package/dist/operate-document.js.map +1 -0
- package/dist/operate-rich-html.cjs +129 -0
- package/dist/operate-rich-html.cjs.map +1 -0
- package/dist/operate-rich-html.d.cts +136 -0
- package/dist/operate-rich-html.d.ts +136 -0
- package/dist/operate-rich-html.js +96 -0
- package/dist/operate-rich-html.js.map +1 -0
- package/dist/operate-server.cjs +157 -0
- package/dist/operate-server.cjs.map +1 -0
- package/dist/operate-server.d.cts +183 -0
- package/dist/operate-server.d.ts +183 -0
- package/dist/operate-server.js +134 -0
- package/dist/operate-server.js.map +1 -0
- package/dist/operate-xlsx.cjs +248 -0
- package/dist/operate-xlsx.cjs.map +1 -0
- package/dist/operate-xlsx.d.cts +129 -0
- package/dist/operate-xlsx.d.ts +129 -0
- package/dist/operate-xlsx.js +215 -0
- package/dist/operate-xlsx.js.map +1 -0
- package/dist/operate.cjs +1774 -0
- package/dist/operate.cjs.map +1 -0
- package/dist/operate.d.cts +595 -0
- package/dist/operate.d.ts +595 -0
- package/dist/operate.js +1750 -0
- package/dist/operate.js.map +1 -0
- package/dist/ports.cjs.map +1 -1
- package/dist/ports.d.cts +361 -3
- package/dist/ports.d.ts +361 -3
- package/dist/react-bar.cjs +686 -0
- package/dist/react-bar.cjs.map +1 -0
- package/dist/react-bar.d.cts +379 -0
- package/dist/react-bar.d.ts +379 -0
- package/dist/react-bar.js +665 -0
- package/dist/react-bar.js.map +1 -0
- package/dist/react-host.cjs +301 -0
- package/dist/react-host.cjs.map +1 -0
- package/dist/react-host.d.cts +820 -0
- package/dist/react-host.d.ts +820 -0
- package/dist/react-host.js +269 -0
- package/dist/react-host.js.map +1 -0
- package/dist/react-menu.cjs +735 -0
- package/dist/react-menu.cjs.map +1 -0
- package/dist/react-menu.d.cts +370 -0
- package/dist/react-menu.d.ts +370 -0
- package/dist/react-menu.js +722 -0
- package/dist/react-menu.js.map +1 -0
- package/dist/react-overflow.cjs +592 -0
- package/dist/react-overflow.cjs.map +1 -0
- package/dist/react-overflow.d.cts +817 -0
- package/dist/react-overflow.d.ts +817 -0
- package/dist/react-overflow.js +571 -0
- package/dist/react-overflow.js.map +1 -0
- package/dist/react-palette.cjs +489 -0
- package/dist/react-palette.cjs.map +1 -0
- package/dist/react-palette.d.cts +359 -0
- package/dist/react-palette.d.ts +359 -0
- package/dist/react-palette.js +457 -0
- package/dist/react-palette.js.map +1 -0
- package/dist/react-selection.cjs +617 -0
- package/dist/react-selection.cjs.map +1 -0
- package/dist/react-selection.d.cts +357 -0
- package/dist/react-selection.d.ts +357 -0
- package/dist/react-selection.js +596 -0
- package/dist/react-selection.js.map +1 -0
- package/dist/react-sheet.cjs +616 -0
- package/dist/react-sheet.cjs.map +1 -0
- package/dist/react-sheet.d.cts +361 -0
- package/dist/react-sheet.d.ts +361 -0
- package/dist/react-sheet.js +595 -0
- package/dist/react-sheet.js.map +1 -0
- package/package.json +148 -5
|
@@ -0,0 +1,665 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/react/bar.tsx
|
|
4
|
+
import * as React4 from "react";
|
|
5
|
+
import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger, TooltipProvider } from "@ai-matrx/design-system";
|
|
6
|
+
|
|
7
|
+
// src/menu/layouts.ts
|
|
8
|
+
var INLINE_SECTION_MAX = 3;
|
|
9
|
+
function sectionLabel(section) {
|
|
10
|
+
return section.label ?? null;
|
|
11
|
+
}
|
|
12
|
+
function fold(section) {
|
|
13
|
+
return {
|
|
14
|
+
kind: "submenu",
|
|
15
|
+
id: `${section.id}:fold`,
|
|
16
|
+
label: section.label,
|
|
17
|
+
...section.icon !== void 0 ? { icon: section.icon } : {},
|
|
18
|
+
children: section.nodes
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function rowCount(nodes) {
|
|
22
|
+
return nodes.filter((n) => n.kind !== "separator" && n.kind !== "label").length;
|
|
23
|
+
}
|
|
24
|
+
function placementOf(node) {
|
|
25
|
+
return "placement" in node && node.placement ? node.placement : "overflow";
|
|
26
|
+
}
|
|
27
|
+
function foldNamed(sections) {
|
|
28
|
+
return sections.map(
|
|
29
|
+
(s) => s.label && !s.primary && rowCount(s.nodes) > INLINE_SECTION_MAX ? { id: s.id, group: s.group, nodes: [fold({ ...s, label: s.label })] } : s
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
function bar(model, _options = {}) {
|
|
33
|
+
const primary = [];
|
|
34
|
+
const overflowSections = [];
|
|
35
|
+
for (const s of model.sections) {
|
|
36
|
+
const rest = [];
|
|
37
|
+
for (const n of s.nodes) {
|
|
38
|
+
if (n.kind === "separator" || n.kind === "label") {
|
|
39
|
+
rest.push(n);
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
const p = placementOf(n);
|
|
43
|
+
if (p === "primary" || p === "both") primary.push(n);
|
|
44
|
+
if (p === "overflow" || p === "both") rest.push(n);
|
|
45
|
+
}
|
|
46
|
+
if (rowCount(rest) > 0) overflowSections.push({ ...s, nodes: rest });
|
|
47
|
+
}
|
|
48
|
+
return { primary, overflow: foldNamed(overflowSections) };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/react/engine.tsx
|
|
52
|
+
import * as React2 from "react";
|
|
53
|
+
|
|
54
|
+
// src/declare/sensitivity.ts
|
|
55
|
+
var DEFAULT_SENSITIVITY = Object.freeze({
|
|
56
|
+
exportable: true,
|
|
57
|
+
classification: "ordinary",
|
|
58
|
+
includedByDefault: true
|
|
59
|
+
});
|
|
60
|
+
function resolveSensitivity(value) {
|
|
61
|
+
return {
|
|
62
|
+
exportable: value.sensitivity?.exportable ?? DEFAULT_SENSITIVITY.exportable,
|
|
63
|
+
classification: value.sensitivity?.classification ?? DEFAULT_SENSITIVITY.classification,
|
|
64
|
+
includedByDefault: value.sensitivity?.includedByDefault ?? DEFAULT_SENSITIVITY.includedByDefault
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// src/actions/projection.ts
|
|
69
|
+
function toJson(value) {
|
|
70
|
+
if (value === void 0 || typeof value === "function" || typeof value === "symbol") return void 0;
|
|
71
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") return value;
|
|
72
|
+
if (typeof value === "number") return Number.isFinite(value) ? value : null;
|
|
73
|
+
if (typeof value === "bigint") return value.toString();
|
|
74
|
+
if (value instanceof Date) return value.toISOString();
|
|
75
|
+
if (Array.isArray(value)) return value.map((v) => toJson(v) ?? null);
|
|
76
|
+
if (typeof value === "object") {
|
|
77
|
+
const out = {};
|
|
78
|
+
for (const [k, v] of Object.entries(value)) {
|
|
79
|
+
const j = toJson(v);
|
|
80
|
+
if (j !== void 0) out[k] = j;
|
|
81
|
+
}
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
return void 0;
|
|
85
|
+
}
|
|
86
|
+
function projectValues(raw, declared, pathPrefix = "") {
|
|
87
|
+
const byName = new Map(declared.map((d) => [d.name, d]));
|
|
88
|
+
const values = {};
|
|
89
|
+
const omissions = [];
|
|
90
|
+
for (const [name, value] of Object.entries(raw)) {
|
|
91
|
+
const path = pathPrefix ? `${pathPrefix}.${name}` : name;
|
|
92
|
+
const decl = byName.get(name);
|
|
93
|
+
if (!decl) {
|
|
94
|
+
omissions.push({ path, reason: "filtered" });
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const sensitivity = resolveSensitivity(decl);
|
|
98
|
+
if (sensitivity.classification !== "ordinary") {
|
|
99
|
+
omissions.push({ path, reason: "excluded", classification: sensitivity.classification });
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (!sensitivity.exportable) {
|
|
103
|
+
omissions.push({ path, reason: "excluded" });
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const json = toJson(value);
|
|
107
|
+
if (json === void 0) continue;
|
|
108
|
+
values[name] = json;
|
|
109
|
+
}
|
|
110
|
+
return { values, omissions };
|
|
111
|
+
}
|
|
112
|
+
function createProjectedReads(source) {
|
|
113
|
+
return {
|
|
114
|
+
async values(signal) {
|
|
115
|
+
if (!source.surface || !source.declaration) return { values: {}, omissions: [] };
|
|
116
|
+
const raw = await source.surface.readValues(signal);
|
|
117
|
+
const screenValues = source.declaration.values;
|
|
118
|
+
return projectValues(raw, screenValues);
|
|
119
|
+
},
|
|
120
|
+
async item(signal) {
|
|
121
|
+
const item = source.item;
|
|
122
|
+
if (!item || !source.declaration) return null;
|
|
123
|
+
const type = source.declaration.itemTypes?.find((t) => t.name === item.itemType);
|
|
124
|
+
if (!type) return { values: {}, omissions: [{ path: item.itemType, reason: "filtered" }] };
|
|
125
|
+
const raw = await item.readValues(signal);
|
|
126
|
+
return projectValues(raw, type.values, item.itemType);
|
|
127
|
+
},
|
|
128
|
+
async live(signal) {
|
|
129
|
+
if (!source.surface?.readLive) return {};
|
|
130
|
+
return source.surface.readLive(signal);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// src/actions/run.ts
|
|
136
|
+
async function runAction(action, target, options) {
|
|
137
|
+
const { ports } = options;
|
|
138
|
+
if (action.requiresAuth && !target.auth.authenticated) {
|
|
139
|
+
if (!ports.auth) {
|
|
140
|
+
ports.diagnostics.capture(new Error(`requiresAuth action "${action.id}" ran signed out with no auth port`), {
|
|
141
|
+
area: "actions",
|
|
142
|
+
detail: { actionId: action.id }
|
|
143
|
+
});
|
|
144
|
+
return { status: "failed", error: new Error("sign-in unavailable") };
|
|
145
|
+
}
|
|
146
|
+
ports.auth.signInThenResume({ actionId: action.id, contentFingerprint: options.contentFingerprint ?? action.id });
|
|
147
|
+
return { status: "auth-pending" };
|
|
148
|
+
}
|
|
149
|
+
const reads = createProjectedReads(options.source ?? { surface: null, declaration: target.declaration, item: null });
|
|
150
|
+
const signal = options.signal ?? new AbortController().signal;
|
|
151
|
+
try {
|
|
152
|
+
const outcome = await action.run(target, { reads, ports, signal });
|
|
153
|
+
return { status: "ran", outcome };
|
|
154
|
+
} catch (error) {
|
|
155
|
+
ports.diagnostics.capture(error, { area: "actions", detail: { actionId: action.id, stage: "run" } });
|
|
156
|
+
const label = typeof action.label === "string" ? action.label : action.id;
|
|
157
|
+
ports.notify?.error(`${label} did not finish.`, "Try again; if it keeps failing, report it from the feedback button.");
|
|
158
|
+
return { status: "failed", error };
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// src/menu/model.ts
|
|
163
|
+
var CATEGORY_GROUP = {
|
|
164
|
+
clipboard: "clipboard",
|
|
165
|
+
copy: "transfer",
|
|
166
|
+
export: "transfer",
|
|
167
|
+
save: "document",
|
|
168
|
+
share: "document",
|
|
169
|
+
edit: "editable",
|
|
170
|
+
history: "history",
|
|
171
|
+
ai: "ai",
|
|
172
|
+
ask: "ai",
|
|
173
|
+
listen: "tools",
|
|
174
|
+
study: "tools",
|
|
175
|
+
app: "tools",
|
|
176
|
+
feedback: "quick",
|
|
177
|
+
creator: "admin",
|
|
178
|
+
admin: "admin",
|
|
179
|
+
"surface-info": "surface-info"
|
|
180
|
+
};
|
|
181
|
+
var GROUP_ORDER = [
|
|
182
|
+
"clipboard",
|
|
183
|
+
"editable",
|
|
184
|
+
"transfer",
|
|
185
|
+
"document",
|
|
186
|
+
"history",
|
|
187
|
+
"ai",
|
|
188
|
+
"tools",
|
|
189
|
+
"quick",
|
|
190
|
+
"surface",
|
|
191
|
+
"admin",
|
|
192
|
+
"surface-info"
|
|
193
|
+
];
|
|
194
|
+
var CATEGORY_ORDER = [
|
|
195
|
+
"clipboard",
|
|
196
|
+
"edit",
|
|
197
|
+
"copy",
|
|
198
|
+
"export",
|
|
199
|
+
"save",
|
|
200
|
+
"share",
|
|
201
|
+
"history",
|
|
202
|
+
"ask",
|
|
203
|
+
"ai",
|
|
204
|
+
"listen",
|
|
205
|
+
"study",
|
|
206
|
+
"app",
|
|
207
|
+
"feedback",
|
|
208
|
+
"creator",
|
|
209
|
+
"admin",
|
|
210
|
+
"surface-info"
|
|
211
|
+
];
|
|
212
|
+
function translate(i18n, key, fallback) {
|
|
213
|
+
if (!i18n) return fallback;
|
|
214
|
+
try {
|
|
215
|
+
return i18n.t(key, fallback);
|
|
216
|
+
} catch {
|
|
217
|
+
return fallback;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
function actionToNode(resolved, target, options = {}) {
|
|
221
|
+
const { action, eligibility } = resolved;
|
|
222
|
+
const rawLabel = typeof action.label === "function" ? action.label(target) : action.label;
|
|
223
|
+
const label = translate(options.i18n, action.labelKey ?? action.id, rawLabel);
|
|
224
|
+
const base = {
|
|
225
|
+
id: action.id,
|
|
226
|
+
actionId: action.id,
|
|
227
|
+
...action.icon !== void 0 ? { icon: action.icon } : {},
|
|
228
|
+
...action.placement !== void 0 ? { placement: action.placement } : {}
|
|
229
|
+
};
|
|
230
|
+
if (action.expand) {
|
|
231
|
+
const node = {
|
|
232
|
+
...base,
|
|
233
|
+
kind: "submenu",
|
|
234
|
+
label,
|
|
235
|
+
lazy: true,
|
|
236
|
+
loading: false,
|
|
237
|
+
children: [],
|
|
238
|
+
...action.emptyLabel !== void 0 ? { emptyLabel: action.emptyLabel } : {}
|
|
239
|
+
};
|
|
240
|
+
return node;
|
|
241
|
+
}
|
|
242
|
+
if (action.href !== void 0) {
|
|
243
|
+
const href = typeof action.href === "function" ? action.href(target) : action.href;
|
|
244
|
+
return { ...base, kind: "link", label, href };
|
|
245
|
+
}
|
|
246
|
+
if (action.toggle) {
|
|
247
|
+
return {
|
|
248
|
+
...base,
|
|
249
|
+
kind: "checkbox",
|
|
250
|
+
label,
|
|
251
|
+
checked: action.pressed?.(target) ?? false,
|
|
252
|
+
...action.description !== void 0 ? { description: action.description } : {}
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
const state = action.stateIcon?.(target) ?? null;
|
|
256
|
+
const pressed = action.pressed?.(target);
|
|
257
|
+
const hint = action.hint !== void 0 ? translate(options.i18n, action.hintKey ?? `${action.id}.hint`, action.hint) : void 0;
|
|
258
|
+
return {
|
|
259
|
+
...base,
|
|
260
|
+
kind: "item",
|
|
261
|
+
label,
|
|
262
|
+
...action.iconTone !== void 0 ? { iconTone: action.iconTone } : {},
|
|
263
|
+
...state ? { stateIcon: state } : {},
|
|
264
|
+
...action.description !== void 0 ? { description: action.description } : {},
|
|
265
|
+
...hint !== void 0 ? { hint } : {},
|
|
266
|
+
...pressed !== void 0 ? { pressed } : {},
|
|
267
|
+
...action.destructive ? { destructive: true } : {},
|
|
268
|
+
...action.preserveSelection ? { preserveSelection: true } : {},
|
|
269
|
+
...eligibility.status === "unavailable-verb" ? { unavailable: { sentence: eligibility.sentence } } : {}
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
function buildMenuModel(target, resolved, options = {}) {
|
|
273
|
+
const ordered = resolved.map((r, index) => ({ r, index })).sort((a, b) => {
|
|
274
|
+
const ca = CATEGORY_ORDER.indexOf(a.r.action.category);
|
|
275
|
+
const cb = CATEGORY_ORDER.indexOf(b.r.action.category);
|
|
276
|
+
if (ca !== cb) return ca - cb;
|
|
277
|
+
const oa = a.r.action.order ?? 0;
|
|
278
|
+
const ob = b.r.action.order ?? 0;
|
|
279
|
+
if (oa !== ob) return oa - ob;
|
|
280
|
+
return a.index - b.index;
|
|
281
|
+
}).map((x) => x.r);
|
|
282
|
+
const sections = /* @__PURE__ */ new Map();
|
|
283
|
+
const roles = {};
|
|
284
|
+
const explicit = /* @__PURE__ */ new Map();
|
|
285
|
+
resolved.forEach((r, index) => {
|
|
286
|
+
const sec = r.action.section;
|
|
287
|
+
if (!sec) return;
|
|
288
|
+
const entry = explicit.get(sec.id) ?? { group: CATEGORY_GROUP[r.action.category], rows: [] };
|
|
289
|
+
entry.rows.push({ r, index });
|
|
290
|
+
explicit.set(sec.id, entry);
|
|
291
|
+
});
|
|
292
|
+
const placed = /* @__PURE__ */ new Set();
|
|
293
|
+
for (const r of ordered) {
|
|
294
|
+
const sec = r.action.section;
|
|
295
|
+
if (sec && placed.has(sec.id)) continue;
|
|
296
|
+
const group = sec ? explicit.get(sec.id)?.group ?? CATEGORY_GROUP[r.action.category] : CATEGORY_GROUP[r.action.category];
|
|
297
|
+
const sectionId = sec ? `section:${sec.id}` : group;
|
|
298
|
+
let section = sections.get(sectionId);
|
|
299
|
+
if (!section) {
|
|
300
|
+
section = {
|
|
301
|
+
id: sectionId,
|
|
302
|
+
group,
|
|
303
|
+
nodes: [],
|
|
304
|
+
...sec ? { label: translate(options.i18n, sec.labelKey ?? `section.${sec.id}`, sec.label) } : {},
|
|
305
|
+
...sec?.icon !== void 0 ? { icon: sec.icon } : {},
|
|
306
|
+
...sec?.primary ? { primary: true } : {}
|
|
307
|
+
};
|
|
308
|
+
sections.set(sectionId, section);
|
|
309
|
+
}
|
|
310
|
+
const rows = sec ? (explicit.get(sec.id)?.rows ?? []).sort((x, y) => (x.r.action.order ?? 0) - (y.r.action.order ?? 0) || x.index - y.index).map((x) => x.r) : [r];
|
|
311
|
+
if (sec) placed.add(sec.id);
|
|
312
|
+
for (const row of rows) {
|
|
313
|
+
section.nodes.push(actionToNode(row, target, options));
|
|
314
|
+
if (row.action.verb) roles[row.action.verb] = row.action.id;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
const list = [...sections.values()].sort((a, b) => {
|
|
318
|
+
if (Boolean(a.primary) !== Boolean(b.primary)) return a.primary ? -1 : 1;
|
|
319
|
+
return GROUP_ORDER.indexOf(a.group) - GROUP_ORDER.indexOf(b.group);
|
|
320
|
+
});
|
|
321
|
+
const selected = target.selection?.text?.trim();
|
|
322
|
+
const content = options.content?.trim();
|
|
323
|
+
const header = selected ? { label: "Selected", text: target.selection?.text ?? "" } : content ? { label: "Content", text: options.content ?? "" } : null;
|
|
324
|
+
return { header, sections: list, roles };
|
|
325
|
+
}
|
|
326
|
+
function withExpandedChildren(model, nodeId, children) {
|
|
327
|
+
const visit = (nodes) => nodes.map((n) => {
|
|
328
|
+
if (n.kind !== "submenu") return n;
|
|
329
|
+
if (n.id === nodeId) {
|
|
330
|
+
return children === "loading" ? { ...n, loading: true } : { ...n, lazy: false, loading: false, children };
|
|
331
|
+
}
|
|
332
|
+
return { ...n, children: visit(n.children) };
|
|
333
|
+
});
|
|
334
|
+
return { ...model, sections: model.sections.map((s) => ({ ...s, nodes: visit(s.nodes) })) };
|
|
335
|
+
}
|
|
336
|
+
function findNode(nodes, id) {
|
|
337
|
+
for (const n of nodes) {
|
|
338
|
+
if (n.id === id) return n;
|
|
339
|
+
if (n.kind === "submenu") {
|
|
340
|
+
const hit = findNode(n.children, id);
|
|
341
|
+
if (hit) return hit;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// src/react/context.ts
|
|
348
|
+
import * as React from "react";
|
|
349
|
+
var KEY = /* @__PURE__ */ Symbol.for("ai-matrx.alchemy.actions-context");
|
|
350
|
+
var AlchemyActionsContext = globalThis[KEY] ??= React.createContext(null);
|
|
351
|
+
function useAlchemyActions() {
|
|
352
|
+
const value = React.useContext(AlchemyActionsContext);
|
|
353
|
+
if (!value) throw new Error("Alchemy actions: render inside <AlchemyActionsProvider> (@ai-matrx/alchemy/react/host).");
|
|
354
|
+
return value;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// src/react/engine.tsx
|
|
358
|
+
import { jsx } from "react/jsx-runtime";
|
|
359
|
+
function useMenuEngine(target, options = {}) {
|
|
360
|
+
const { registry, ports } = useAlchemyActions();
|
|
361
|
+
const [resolved, setResolved] = React2.useState([]);
|
|
362
|
+
const [model, setModel] = React2.useState(null);
|
|
363
|
+
const [tick, setTick] = React2.useState(0);
|
|
364
|
+
const expanded = React2.useRef(/* @__PURE__ */ new Map());
|
|
365
|
+
const active = options.active ?? true;
|
|
366
|
+
const optionsRef = React2.useRef(options);
|
|
367
|
+
optionsRef.current = options;
|
|
368
|
+
React2.useEffect(() => registry.subscribe(() => setTick((t) => t + 1)), [registry]);
|
|
369
|
+
React2.useEffect(() => {
|
|
370
|
+
if (!target || !active) return;
|
|
371
|
+
let live = true;
|
|
372
|
+
void registry.resolve(target, optionsRef.current.restrict).then((list) => {
|
|
373
|
+
if (!live) return;
|
|
374
|
+
expanded.current.clear();
|
|
375
|
+
setResolved(list);
|
|
376
|
+
setModel(buildMenuModel(target, list, { ...ports.i18n ? { i18n: ports.i18n } : {}, content: optionsRef.current.content ?? null }));
|
|
377
|
+
});
|
|
378
|
+
return () => {
|
|
379
|
+
live = false;
|
|
380
|
+
};
|
|
381
|
+
}, [registry, ports, target, active, tick, options.revision]);
|
|
382
|
+
React2.useEffect(() => {
|
|
383
|
+
if (!target) return;
|
|
384
|
+
const offs = resolved.map((r) => r.action.subscribe?.(() => setTick((t) => t + 1), target)).filter((off) => typeof off === "function");
|
|
385
|
+
return () => offs.forEach((off) => off());
|
|
386
|
+
}, [resolved, target]);
|
|
387
|
+
const findAction = React2.useCallback(
|
|
388
|
+
(id) => resolved.find((r) => r.action.id === id)?.action ?? expanded.current.get(id),
|
|
389
|
+
[resolved]
|
|
390
|
+
);
|
|
391
|
+
const run = React2.useCallback(
|
|
392
|
+
async (nodeId) => {
|
|
393
|
+
if (!target || !model) return null;
|
|
394
|
+
const node = model.sections.map((s) => findNode(s.nodes, nodeId)).find(Boolean) ?? null;
|
|
395
|
+
const action = findAction(node && "actionId" in node && node.actionId ? node.actionId : nodeId);
|
|
396
|
+
if (!action) return null;
|
|
397
|
+
const o = optionsRef.current;
|
|
398
|
+
const result = await runAction(action, target, {
|
|
399
|
+
ports,
|
|
400
|
+
...o.source ? { source: o.source } : {},
|
|
401
|
+
...o.contentFingerprint ? { contentFingerprint: o.contentFingerprint } : {}
|
|
402
|
+
});
|
|
403
|
+
o.onRan?.(nodeId, result);
|
|
404
|
+
return result;
|
|
405
|
+
},
|
|
406
|
+
[target, model, findAction, ports]
|
|
407
|
+
);
|
|
408
|
+
const expand = React2.useCallback(
|
|
409
|
+
async (nodeId) => {
|
|
410
|
+
const action = findAction(nodeId);
|
|
411
|
+
if (!target || !action?.expand) return;
|
|
412
|
+
setModel((m) => m ? withExpandedChildren(m, nodeId, "loading") : m);
|
|
413
|
+
try {
|
|
414
|
+
const children = await action.expand(target, new AbortController().signal);
|
|
415
|
+
const nodes = children.map((child) => {
|
|
416
|
+
expanded.current.set(child.id, child);
|
|
417
|
+
return actionToNode({ action: child, eligibility: { status: "available" } }, target, ports.i18n ? { i18n: ports.i18n } : {});
|
|
418
|
+
});
|
|
419
|
+
setModel((m) => m ? withExpandedChildren(m, nodeId, nodes) : m);
|
|
420
|
+
} catch (error) {
|
|
421
|
+
ports.diagnostics.capture(error, { area: "menu", detail: { nodeId, stage: "expand" } });
|
|
422
|
+
setModel((m) => m ? withExpandedChildren(m, nodeId, []) : m);
|
|
423
|
+
}
|
|
424
|
+
},
|
|
425
|
+
[findAction, target, ports]
|
|
426
|
+
);
|
|
427
|
+
const icon = React2.useCallback(
|
|
428
|
+
(key, tone) => {
|
|
429
|
+
if (!key || !ports.icons) return null;
|
|
430
|
+
const Resolved = ports.icons.resolve(key);
|
|
431
|
+
return Resolved ? /* @__PURE__ */ jsx(Resolved, { className: `h-4 w-4 shrink-0 ${tone ?? ""}`, "aria-hidden": true }) : null;
|
|
432
|
+
},
|
|
433
|
+
[ports.icons]
|
|
434
|
+
);
|
|
435
|
+
return { model, resolved, run, expand, icon };
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// src/react/dropdown-nodes.tsx
|
|
439
|
+
import * as React3 from "react";
|
|
440
|
+
import {
|
|
441
|
+
DropdownMenuCheckboxItem,
|
|
442
|
+
DropdownMenuItem,
|
|
443
|
+
DropdownMenuLabel,
|
|
444
|
+
DropdownMenuSeparator,
|
|
445
|
+
DropdownMenuSub,
|
|
446
|
+
DropdownMenuSubContent,
|
|
447
|
+
DropdownMenuSubTrigger,
|
|
448
|
+
Tooltip,
|
|
449
|
+
TooltipContent,
|
|
450
|
+
TooltipTrigger
|
|
451
|
+
} from "@ai-matrx/design-system";
|
|
452
|
+
import { Fragment as Fragment2, jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
453
|
+
function NodeList({ nodes, engine }) {
|
|
454
|
+
return /* @__PURE__ */ jsx2(Fragment2, { children: nodes.map((node) => /* @__PURE__ */ jsx2(MenuNodeView, { node, engine }, node.id)) });
|
|
455
|
+
}
|
|
456
|
+
function SectionList({
|
|
457
|
+
sections,
|
|
458
|
+
engine
|
|
459
|
+
}) {
|
|
460
|
+
return /* @__PURE__ */ jsx2(Fragment2, { children: sections.map((section, index) => /* @__PURE__ */ jsxs(React3.Fragment, { children: [
|
|
461
|
+
index > 0 ? /* @__PURE__ */ jsx2(DropdownMenuSeparator, {}) : null,
|
|
462
|
+
sectionLabel(section) ? /* @__PURE__ */ jsx2(DropdownMenuLabel, { className: "text-xs font-medium text-muted-foreground", children: sectionLabel(section) }) : null,
|
|
463
|
+
/* @__PURE__ */ jsx2(NodeList, { nodes: section.nodes, engine })
|
|
464
|
+
] }, section.id)) });
|
|
465
|
+
}
|
|
466
|
+
function iconOf(node, engine) {
|
|
467
|
+
if (node.kind === "item" && node.stateIcon) {
|
|
468
|
+
const glyph = engine.icon(node.stateIcon.icon);
|
|
469
|
+
return node.stateIcon.spin ? /* @__PURE__ */ jsx2("span", { className: "inline-flex animate-spin", children: glyph }) : glyph;
|
|
470
|
+
}
|
|
471
|
+
if (!("icon" in node)) return null;
|
|
472
|
+
return engine.icon(node.icon, node.kind === "item" ? node.iconTone : void 0);
|
|
473
|
+
}
|
|
474
|
+
function MenuNodeView({ node, engine }) {
|
|
475
|
+
switch (node.kind) {
|
|
476
|
+
case "separator":
|
|
477
|
+
return /* @__PURE__ */ jsx2(DropdownMenuSeparator, {});
|
|
478
|
+
case "label":
|
|
479
|
+
return /* @__PURE__ */ jsx2(DropdownMenuLabel, { className: "text-xs font-medium text-muted-foreground", children: node.label });
|
|
480
|
+
case "submenu":
|
|
481
|
+
return /* @__PURE__ */ jsxs(
|
|
482
|
+
DropdownMenuSub,
|
|
483
|
+
{
|
|
484
|
+
onOpenChange: (open) => {
|
|
485
|
+
if (open && node.lazy && !node.loading) void engine.expand(node.id);
|
|
486
|
+
},
|
|
487
|
+
children: [
|
|
488
|
+
/* @__PURE__ */ jsxs(DropdownMenuSubTrigger, { "data-alchemy-node": node.id, className: "gap-2", children: [
|
|
489
|
+
iconOf(node, engine),
|
|
490
|
+
/* @__PURE__ */ jsx2("span", { className: "truncate", children: node.label })
|
|
491
|
+
] }),
|
|
492
|
+
/* @__PURE__ */ jsx2(DropdownMenuSubContent, { className: "max-h-[var(--radix-dropdown-menu-content-available-height)] overflow-y-auto", children: node.loading ? /* @__PURE__ */ jsx2(DropdownMenuLabel, { className: "text-xs text-muted-foreground", children: "Loading\u2026" }) : node.children.length === 0 && !node.lazy ? /* @__PURE__ */ jsx2(DropdownMenuLabel, { className: "text-xs text-muted-foreground", children: node.emptyLabel ?? "Nothing here yet" }) : /* @__PURE__ */ jsx2(NodeList, { nodes: node.children, engine }) })
|
|
493
|
+
]
|
|
494
|
+
}
|
|
495
|
+
);
|
|
496
|
+
case "checkbox":
|
|
497
|
+
return /* @__PURE__ */ jsxs(
|
|
498
|
+
DropdownMenuCheckboxItem,
|
|
499
|
+
{
|
|
500
|
+
"data-alchemy-node": node.id,
|
|
501
|
+
checked: node.checked,
|
|
502
|
+
onSelect: () => void engine.run(node.id),
|
|
503
|
+
className: "gap-2",
|
|
504
|
+
children: [
|
|
505
|
+
iconOf(node, engine),
|
|
506
|
+
/* @__PURE__ */ jsx2("span", { className: "truncate", children: node.label })
|
|
507
|
+
]
|
|
508
|
+
}
|
|
509
|
+
);
|
|
510
|
+
case "link":
|
|
511
|
+
return /* @__PURE__ */ jsx2(DropdownMenuItem, { asChild: true, "data-alchemy-node": node.id, children: /* @__PURE__ */ jsxs("a", { href: node.href, target: node.target, className: "gap-2", children: [
|
|
512
|
+
iconOf(node, engine),
|
|
513
|
+
/* @__PURE__ */ jsx2("span", { className: "truncate", children: node.label })
|
|
514
|
+
] }) });
|
|
515
|
+
case "item":
|
|
516
|
+
return /* @__PURE__ */ jsx2(LeafItem, { node, engine });
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
function LeafItem({ node, engine }) {
|
|
520
|
+
const row = /* @__PURE__ */ jsxs(
|
|
521
|
+
DropdownMenuItem,
|
|
522
|
+
{
|
|
523
|
+
"data-alchemy-node": node.id,
|
|
524
|
+
"aria-disabled": node.unavailable ? true : void 0,
|
|
525
|
+
"aria-pressed": node.pressed,
|
|
526
|
+
onSelect: (event) => {
|
|
527
|
+
if (node.unavailable) {
|
|
528
|
+
event.preventDefault();
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
void engine.run(node.id);
|
|
532
|
+
},
|
|
533
|
+
className: `gap-2 ${node.unavailable ? "opacity-50" : ""} ${node.destructive ? "text-destructive" : ""}`,
|
|
534
|
+
children: [
|
|
535
|
+
iconOf(node, engine),
|
|
536
|
+
/* @__PURE__ */ jsxs("span", { className: "flex min-w-0 flex-1 flex-col", children: [
|
|
537
|
+
/* @__PURE__ */ jsx2("span", { className: "truncate", children: node.label }),
|
|
538
|
+
node.description ? /* @__PURE__ */ jsx2("span", { className: "truncate text-xs text-muted-foreground", children: node.description }) : null
|
|
539
|
+
] }),
|
|
540
|
+
node.hint ? /* @__PURE__ */ jsx2("span", { className: "ml-auto text-xs text-muted-foreground", children: node.hint }) : null
|
|
541
|
+
]
|
|
542
|
+
}
|
|
543
|
+
);
|
|
544
|
+
if (!node.unavailable) return row;
|
|
545
|
+
return /* @__PURE__ */ jsxs(Tooltip, { children: [
|
|
546
|
+
/* @__PURE__ */ jsx2(TooltipTrigger, { asChild: true, children: row }),
|
|
547
|
+
/* @__PURE__ */ jsx2(TooltipContent, { children: node.unavailable.sentence })
|
|
548
|
+
] });
|
|
549
|
+
}
|
|
550
|
+
function LeafButton({
|
|
551
|
+
node,
|
|
552
|
+
engine,
|
|
553
|
+
showLabel
|
|
554
|
+
}) {
|
|
555
|
+
if (node.kind !== "item" && node.kind !== "checkbox" && node.kind !== "link") return null;
|
|
556
|
+
const unavailable = node.kind === "item" ? node.unavailable : void 0;
|
|
557
|
+
const pressed = node.kind === "item" ? node.pressed : node.kind === "checkbox" ? node.checked : void 0;
|
|
558
|
+
const glyph = iconOf(node, engine);
|
|
559
|
+
const button = /* @__PURE__ */ jsxs(
|
|
560
|
+
"button",
|
|
561
|
+
{
|
|
562
|
+
type: "button",
|
|
563
|
+
"data-alchemy-node": node.id,
|
|
564
|
+
"aria-label": node.label,
|
|
565
|
+
"aria-disabled": unavailable ? true : void 0,
|
|
566
|
+
"aria-pressed": pressed,
|
|
567
|
+
onMouseDown: (e) => {
|
|
568
|
+
if (node.kind === "item" && node.preserveSelection) e.preventDefault();
|
|
569
|
+
},
|
|
570
|
+
onClick: () => {
|
|
571
|
+
if (unavailable) return;
|
|
572
|
+
if (node.kind === "link") {
|
|
573
|
+
window.location.assign(node.href);
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
void engine.run(node.id);
|
|
577
|
+
},
|
|
578
|
+
className: `inline-flex h-7 min-w-7 items-center justify-center gap-1.5 rounded-md px-1.5 text-sm text-muted-foreground hover:bg-accent hover:text-foreground ${unavailable ? "cursor-default opacity-50 hover:bg-transparent" : ""} ${pressed ? "text-foreground" : ""}`,
|
|
579
|
+
children: [
|
|
580
|
+
glyph,
|
|
581
|
+
showLabel || !glyph ? /* @__PURE__ */ jsx2("span", { children: node.label }) : null
|
|
582
|
+
]
|
|
583
|
+
}
|
|
584
|
+
);
|
|
585
|
+
return /* @__PURE__ */ jsxs(Tooltip, { children: [
|
|
586
|
+
/* @__PURE__ */ jsx2(TooltipTrigger, { asChild: true, children: button }),
|
|
587
|
+
/* @__PURE__ */ jsx2(TooltipContent, { children: unavailable ? unavailable.sentence : node.label })
|
|
588
|
+
] });
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// src/react/bar.tsx
|
|
592
|
+
import { Fragment as Fragment3, jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
593
|
+
function isNarrow(width) {
|
|
594
|
+
if (typeof window === "undefined") return false;
|
|
595
|
+
return window.matchMedia?.(`(max-width: ${width - 1}px)`).matches ?? false;
|
|
596
|
+
}
|
|
597
|
+
var TRIGGER_CLASS = "inline-flex h-7 min-w-7 items-center justify-center gap-1 rounded-md px-1.5 text-muted-foreground hover:bg-accent hover:text-foreground";
|
|
598
|
+
function PrimarySubmenu({ node, engine }) {
|
|
599
|
+
return /* @__PURE__ */ jsxs2(
|
|
600
|
+
DropdownMenu,
|
|
601
|
+
{
|
|
602
|
+
onOpenChange: (open) => {
|
|
603
|
+
if (open && node.lazy && !node.loading) void engine.expand(node.id);
|
|
604
|
+
},
|
|
605
|
+
children: [
|
|
606
|
+
/* @__PURE__ */ jsx3(DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsx3("button", { type: "button", "aria-label": node.label, "data-alchemy-node": node.id, className: TRIGGER_CLASS, children: engine.icon(node.icon) ?? /* @__PURE__ */ jsx3("span", { className: "text-sm", children: node.label }) }) }),
|
|
607
|
+
/* @__PURE__ */ jsx3(DropdownMenuContent, { align: "start", className: "w-64 max-h-[var(--radix-dropdown-menu-content-available-height)] overflow-y-auto", children: node.loading ? /* @__PURE__ */ jsx3("div", { className: "px-2 py-1.5 text-xs text-muted-foreground", children: "Loading\u2026" }) : /* @__PURE__ */ jsx3(NodeList, { nodes: node.children, engine }) })
|
|
608
|
+
]
|
|
609
|
+
}
|
|
610
|
+
);
|
|
611
|
+
}
|
|
612
|
+
function ActionBar({
|
|
613
|
+
target,
|
|
614
|
+
mini = false,
|
|
615
|
+
className,
|
|
616
|
+
hideOverflow = false,
|
|
617
|
+
sheetBelow = 768,
|
|
618
|
+
loadSheet = () => import("@ai-matrx/alchemy/react/sheet"),
|
|
619
|
+
...options
|
|
620
|
+
}) {
|
|
621
|
+
const engine = useMenuEngine(target, options);
|
|
622
|
+
const [sheetOpen, setSheetOpen] = React4.useState(false);
|
|
623
|
+
const [Sheet, setSheet] = React4.useState(null);
|
|
624
|
+
if (!engine.model) return null;
|
|
625
|
+
const arranged = bar(engine.model, { mini });
|
|
626
|
+
if (arranged.primary.length === 0 && arranged.overflow.length === 0) return null;
|
|
627
|
+
return /* @__PURE__ */ jsx3(TooltipProvider, { children: /* @__PURE__ */ jsxs2(
|
|
628
|
+
"div",
|
|
629
|
+
{
|
|
630
|
+
role: "toolbar",
|
|
631
|
+
"aria-label": "Actions",
|
|
632
|
+
"data-alchemy-layout": "bar",
|
|
633
|
+
className: `flex items-center ${mini ? "gap-0" : "gap-0.5"} ${className ?? ""}`,
|
|
634
|
+
children: [
|
|
635
|
+
arranged.primary.map(
|
|
636
|
+
(node) => node.kind === "submenu" ? /* @__PURE__ */ jsx3(PrimarySubmenu, { node, engine }, node.id) : /* @__PURE__ */ jsx3(LeafButton, { node, engine, showLabel: false }, node.id)
|
|
637
|
+
),
|
|
638
|
+
hideOverflow || arranged.overflow.length === 0 ? null : isNarrow(sheetBelow) ? /* @__PURE__ */ jsxs2(Fragment3, { children: [
|
|
639
|
+
/* @__PURE__ */ jsx3(
|
|
640
|
+
"button",
|
|
641
|
+
{
|
|
642
|
+
type: "button",
|
|
643
|
+
"aria-label": "More actions",
|
|
644
|
+
"data-alchemy-trigger": "overflow",
|
|
645
|
+
className: TRIGGER_CLASS,
|
|
646
|
+
onClick: () => {
|
|
647
|
+
setSheetOpen(true);
|
|
648
|
+
if (!Sheet) void loadSheet().then((m) => setSheet(() => m.ActionSheet));
|
|
649
|
+
},
|
|
650
|
+
children: /* @__PURE__ */ jsx3("span", { "aria-hidden": true, className: "text-base leading-none", children: "\u22EF" })
|
|
651
|
+
}
|
|
652
|
+
),
|
|
653
|
+
Sheet ? /* @__PURE__ */ jsx3(Sheet, { ...options, target, open: sheetOpen, onOpenChange: setSheetOpen }) : null
|
|
654
|
+
] }) : /* @__PURE__ */ jsxs2(DropdownMenu, { children: [
|
|
655
|
+
/* @__PURE__ */ jsx3(DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsx3("button", { type: "button", "aria-label": "More actions", "data-alchemy-trigger": "overflow", className: TRIGGER_CLASS, children: /* @__PURE__ */ jsx3("span", { "aria-hidden": true, className: "text-base leading-none", children: "\u22EF" }) }) }),
|
|
656
|
+
/* @__PURE__ */ jsx3(DropdownMenuContent, { align: "end", className: "w-64 max-h-[var(--radix-dropdown-menu-content-available-height)] overflow-y-auto", children: /* @__PURE__ */ jsx3(SectionList, { sections: arranged.overflow, engine }) })
|
|
657
|
+
] })
|
|
658
|
+
]
|
|
659
|
+
}
|
|
660
|
+
) });
|
|
661
|
+
}
|
|
662
|
+
export {
|
|
663
|
+
ActionBar
|
|
664
|
+
};
|
|
665
|
+
//# sourceMappingURL=react-bar.js.map
|