reifyui 0.12.0 → 0.13.0
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.md +29 -0
- package/dist/chart.js +2 -0
- package/dist/chart.js.map +1 -1
- package/dist/chunk-EL2DHWOH.js +1710 -0
- package/dist/chunk-EL2DHWOH.js.map +1 -0
- package/dist/chunk-J633KV57.js +41 -0
- package/dist/chunk-J633KV57.js.map +1 -0
- package/dist/chunk-J7SECRIG.js +295 -0
- package/dist/chunk-J7SECRIG.js.map +1 -0
- package/dist/chunk-PIRPLH2Z.js +231 -0
- package/dist/chunk-PIRPLH2Z.js.map +1 -0
- package/dist/chunk-PZ5AY32C.js +10 -0
- package/dist/chunk-PZ5AY32C.js.map +1 -0
- package/dist/chunk-UKG4Z6QK.js +130 -0
- package/dist/chunk-UKG4Z6QK.js.map +1 -0
- package/dist/chunk-ZYWE47EO.js +19 -0
- package/dist/chunk-ZYWE47EO.js.map +1 -0
- package/dist/collab.cjs +169 -0
- package/dist/collab.cjs.map +1 -0
- package/dist/collab.d.ts +30 -0
- package/dist/collab.js +18 -0
- package/dist/collab.js.map +1 -0
- package/dist/graph.cjs +2244 -0
- package/dist/graph.cjs.map +1 -0
- package/dist/graph.d.ts +41 -0
- package/dist/graph.js +1870 -0
- package/dist/graph.js.map +1 -0
- package/dist/harness.js +1 -0
- package/dist/harness.js.map +1 -1
- package/dist/index.cjs +1699 -817
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +680 -1769
- package/dist/index.js.map +1 -1
- package/dist/sheet-doc.cjs +235 -0
- package/dist/sheet-doc.cjs.map +1 -0
- package/dist/sheet-doc.d.ts +16 -0
- package/dist/sheet-doc.js +20 -0
- package/dist/sheet-doc.js.map +1 -0
- package/dist/sheet.cjs +2322 -0
- package/dist/sheet.cjs.map +1 -0
- package/dist/sheet.d.ts +23 -0
- package/dist/sheet.js +237 -0
- package/dist/sheet.js.map +1 -0
- package/dist/slides.js +2 -0
- package/dist/slides.js.map +1 -1
- package/package.json +34 -2
- package/src/auth/client.js +4 -2
- package/src/collab/colors.js +18 -0
- package/src/collab/index.d.ts +30 -0
- package/src/collab/index.js +8 -0
- package/src/collab/keepAlive.js +34 -0
- package/src/collab/useLiveDoc.js +76 -0
- package/src/components/CascadeMenu.jsx +9 -3
- package/src/components/Composer.jsx +37 -3
- package/src/components/resizable.jsx +82 -12
- package/src/graph/ConnectPane.jsx +634 -0
- package/src/graph/ConnectToolDetail.jsx +565 -0
- package/src/graph/LogsPane.jsx +328 -0
- package/src/graph/Skeleton.jsx +13 -0
- package/src/graph/draft.js +108 -0
- package/src/graph/format.js +37 -0
- package/src/graph/index.d.ts +41 -0
- package/src/graph/index.js +14 -0
- package/src/graph/useGraphCollab.js +299 -0
- package/src/graph/vgraph.js +215 -0
- package/src/index.d.ts +3 -0
- package/src/index.js +4 -1
- package/src/mention/MentionInput.jsx +420 -0
- package/src/mention/mentionModel.js +177 -0
- package/src/sheet/RunStrip.jsx +57 -0
- package/src/sheet/SheetGrid.jsx +448 -423
- package/src/sheet/doc.d.ts +16 -0
- package/src/sheet/index.d.ts +23 -0
- package/src/sheet/index.js +10 -0
- package/src/sheet/sheetDoc.js +203 -0
- package/src/sheet/useSheetCollab.js +102 -0
- package/src/sheet/useSheetRun.js +80 -0
- package/styles/chat.css +2 -0
- package/styles/graph.css +395 -0
- package/styles/mention.css +79 -0
- package/styles/sheet.css +156 -112
package/dist/sheet.cjs
ADDED
|
@@ -0,0 +1,2322 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/sheet/index.js
|
|
30
|
+
var sheet_exports = {};
|
|
31
|
+
__export(sheet_exports, {
|
|
32
|
+
RunStrip: () => RunStrip,
|
|
33
|
+
SheetGrid: () => SheetGrid,
|
|
34
|
+
isRunActive: () => isRunActive,
|
|
35
|
+
runProgress: () => runProgress,
|
|
36
|
+
sheetDoc: () => sheetDoc_exports,
|
|
37
|
+
sheetOps: () => sheetOps,
|
|
38
|
+
sheetToAoA: () => sheetToAoA,
|
|
39
|
+
sheetToDelimited: () => sheetToDelimited,
|
|
40
|
+
useSheetCollab: () => useSheetCollab,
|
|
41
|
+
useSheetRun: () => useSheetRun,
|
|
42
|
+
withQueuedCells: () => withQueuedCells
|
|
43
|
+
});
|
|
44
|
+
module.exports = __toCommonJS(sheet_exports);
|
|
45
|
+
|
|
46
|
+
// src/sheet/SheetGrid.jsx
|
|
47
|
+
var import_react4 = require("react");
|
|
48
|
+
|
|
49
|
+
// src/mention/MentionInput.jsx
|
|
50
|
+
var import_react3 = __toESM(require("react"), 1);
|
|
51
|
+
|
|
52
|
+
// src/components/Popover.jsx
|
|
53
|
+
var import_react2 = __toESM(require("react"), 1);
|
|
54
|
+
var import_react_dom = require("react-dom");
|
|
55
|
+
|
|
56
|
+
// src/components/overlay.js
|
|
57
|
+
var import_react = require("react");
|
|
58
|
+
var stack = [];
|
|
59
|
+
var listening = false;
|
|
60
|
+
function onKey(e) {
|
|
61
|
+
if (e.key !== "Escape") return;
|
|
62
|
+
const top = stack[stack.length - 1];
|
|
63
|
+
if (!top) return;
|
|
64
|
+
e.preventDefault();
|
|
65
|
+
top.onEscape?.();
|
|
66
|
+
}
|
|
67
|
+
function listen() {
|
|
68
|
+
if (listening || typeof document === "undefined") return;
|
|
69
|
+
document.addEventListener("keydown", onKey);
|
|
70
|
+
listening = true;
|
|
71
|
+
}
|
|
72
|
+
function unlisten() {
|
|
73
|
+
if (!listening || stack.length) return;
|
|
74
|
+
document.removeEventListener("keydown", onKey);
|
|
75
|
+
listening = false;
|
|
76
|
+
}
|
|
77
|
+
function pushOverlay(entry) {
|
|
78
|
+
stack.push(entry);
|
|
79
|
+
listen();
|
|
80
|
+
return () => {
|
|
81
|
+
const i = stack.indexOf(entry);
|
|
82
|
+
if (i >= 0) stack.splice(i, 1);
|
|
83
|
+
unlisten();
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function useOverlayEscape(active, onEscape) {
|
|
87
|
+
(0, import_react.useEffect)(() => {
|
|
88
|
+
if (!active) return void 0;
|
|
89
|
+
return pushOverlay({ onEscape });
|
|
90
|
+
}, [active, onEscape]);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/components/Popover.jsx
|
|
94
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
95
|
+
var GAP = 6;
|
|
96
|
+
var MARGIN = 8;
|
|
97
|
+
function Popover(props) {
|
|
98
|
+
const {
|
|
99
|
+
open,
|
|
100
|
+
anchorRef,
|
|
101
|
+
onClose,
|
|
102
|
+
width = 280,
|
|
103
|
+
minHeight = 140,
|
|
104
|
+
placement = "auto",
|
|
105
|
+
children,
|
|
106
|
+
className,
|
|
107
|
+
label
|
|
108
|
+
} = props;
|
|
109
|
+
const popRef = (0, import_react2.useRef)(null);
|
|
110
|
+
const [pos, setPos] = (0, import_react2.useState)(null);
|
|
111
|
+
(0, import_react2.useEffect)(() => {
|
|
112
|
+
if (!open) {
|
|
113
|
+
setPos(null);
|
|
114
|
+
return void 0;
|
|
115
|
+
}
|
|
116
|
+
const place = () => {
|
|
117
|
+
const el = anchorRef?.current;
|
|
118
|
+
if (!el) return;
|
|
119
|
+
const r = el.getBoundingClientRect();
|
|
120
|
+
const vw = window.innerWidth;
|
|
121
|
+
const vh = window.innerHeight;
|
|
122
|
+
const w = Math.min(width, vw - MARGIN * 2);
|
|
123
|
+
const below = vh - r.bottom - GAP - MARGIN;
|
|
124
|
+
const above = r.top - GAP - MARGIN;
|
|
125
|
+
const up = placement === "above" || placement === "auto" && below < minHeight && above > below;
|
|
126
|
+
const maxHeight = Math.max(minHeight, Math.floor(up ? above : below));
|
|
127
|
+
const left = Math.max(MARGIN, Math.min(r.left, vw - w - MARGIN));
|
|
128
|
+
setPos(up ? { left, bottom: Math.round(vh - r.top + GAP), width: w, maxHeight } : { left, top: Math.round(r.bottom + GAP), width: w, maxHeight });
|
|
129
|
+
};
|
|
130
|
+
place();
|
|
131
|
+
window.addEventListener("resize", place);
|
|
132
|
+
window.addEventListener("scroll", place, true);
|
|
133
|
+
return () => {
|
|
134
|
+
window.removeEventListener("resize", place);
|
|
135
|
+
window.removeEventListener("scroll", place, true);
|
|
136
|
+
};
|
|
137
|
+
}, [open, anchorRef, width, minHeight, placement]);
|
|
138
|
+
(0, import_react2.useEffect)(() => {
|
|
139
|
+
if (!open) return void 0;
|
|
140
|
+
const onDown = (e) => {
|
|
141
|
+
if (popRef.current?.contains(e.target)) return;
|
|
142
|
+
if (anchorRef?.current?.contains(e.target)) return;
|
|
143
|
+
onClose?.();
|
|
144
|
+
};
|
|
145
|
+
document.addEventListener("pointerdown", onDown);
|
|
146
|
+
return () => document.removeEventListener("pointerdown", onDown);
|
|
147
|
+
}, [open, anchorRef, onClose]);
|
|
148
|
+
const escape = (0, import_react2.useCallback)(() => onClose?.(), [onClose]);
|
|
149
|
+
useOverlayEscape(open, escape);
|
|
150
|
+
if (!open || !pos || typeof document === "undefined") return null;
|
|
151
|
+
return (0, import_react_dom.createPortal)(
|
|
152
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
153
|
+
"div",
|
|
154
|
+
{
|
|
155
|
+
ref: popRef,
|
|
156
|
+
className: ["uic-pop", className || ""].filter(Boolean).join(" "),
|
|
157
|
+
style: pos,
|
|
158
|
+
role: "dialog",
|
|
159
|
+
"aria-label": label,
|
|
160
|
+
children
|
|
161
|
+
}
|
|
162
|
+
),
|
|
163
|
+
document.body
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// src/mention/mentionModel.js
|
|
168
|
+
var CURLY = Object.freeze({ open: "{{", close: "}}" });
|
|
169
|
+
var AT = Object.freeze({ open: "@", close: "" });
|
|
170
|
+
var CHAT_LINK = Object.freeze({
|
|
171
|
+
write: (item) => `[@${item.name}](#m-${item.id})`,
|
|
172
|
+
pattern: /\[@(?<name>[^\]\n]+)\]\(#m-(?<id>[^)\s]+)\)/g
|
|
173
|
+
});
|
|
174
|
+
function makeResolver(items) {
|
|
175
|
+
const byName = /* @__PURE__ */ new Map();
|
|
176
|
+
const byId = /* @__PURE__ */ new Map();
|
|
177
|
+
for (const it of items || []) {
|
|
178
|
+
if (!it) continue;
|
|
179
|
+
if (it.name) byName.set(it.name, it);
|
|
180
|
+
if (it.id != null) byId.set(String(it.id), it);
|
|
181
|
+
}
|
|
182
|
+
const names = [...byName.keys()].sort((a, b) => b.length - a.length);
|
|
183
|
+
return { get: (name) => byName.get(name) || null, byId: (id) => byId.get(String(id)) || null, names };
|
|
184
|
+
}
|
|
185
|
+
function parsePattern(text, format, resolver) {
|
|
186
|
+
const out = [];
|
|
187
|
+
const re = new RegExp(format.pattern.source, format.pattern.flags.includes("g") ? format.pattern.flags : format.pattern.flags + "g");
|
|
188
|
+
let last = 0;
|
|
189
|
+
let m;
|
|
190
|
+
while (m = re.exec(text)) {
|
|
191
|
+
if (m.index > last) out.push({ type: "text", text: text.slice(last, m.index) });
|
|
192
|
+
const id = m.groups && m.groups.id != null ? m.groups.id : null;
|
|
193
|
+
const name = m.groups && m.groups.name || "";
|
|
194
|
+
const item = id != null && resolver && resolver.byId(id) || resolver && resolver.get(name) || { id: id ?? name, name, kind: null };
|
|
195
|
+
out.push({ type: "mention", id: item.id, name: item.name || name, kind: item.kind || null, item });
|
|
196
|
+
last = m.index + m[0].length;
|
|
197
|
+
}
|
|
198
|
+
if (last < text.length) out.push({ type: "text", text: text.slice(last) });
|
|
199
|
+
return out;
|
|
200
|
+
}
|
|
201
|
+
function parseValue(value, format, resolver) {
|
|
202
|
+
const text = value == null ? "" : String(value);
|
|
203
|
+
if (format && format.pattern) return parsePattern(text, format, resolver);
|
|
204
|
+
const { open, close } = format || CURLY;
|
|
205
|
+
const out = [];
|
|
206
|
+
let buf = "";
|
|
207
|
+
let i = 0;
|
|
208
|
+
const flush = () => {
|
|
209
|
+
if (buf) {
|
|
210
|
+
out.push({ type: "text", text: buf });
|
|
211
|
+
buf = "";
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
while (i < text.length) {
|
|
215
|
+
if (resolver && text.startsWith(open, i)) {
|
|
216
|
+
const boundary = close ? true : i === 0 || /[\s([{"'>]/.test(text[i - 1]);
|
|
217
|
+
let hit = null;
|
|
218
|
+
if (boundary) {
|
|
219
|
+
for (const name of resolver.names) {
|
|
220
|
+
const start = i + open.length;
|
|
221
|
+
if (text.startsWith(name, start)) {
|
|
222
|
+
const end = start + name.length;
|
|
223
|
+
if (!close || text.startsWith(close, end)) {
|
|
224
|
+
if (close || end === text.length || /[\s.,;:!?)\]}"']/.test(text[end])) {
|
|
225
|
+
hit = { name, end: end + close.length };
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
if (hit) {
|
|
233
|
+
flush();
|
|
234
|
+
const item = resolver.get(hit.name);
|
|
235
|
+
out.push({ type: "mention", id: item.id, name: item.name, kind: item.kind || null, item });
|
|
236
|
+
i = hit.end;
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
buf += text[i];
|
|
241
|
+
i += 1;
|
|
242
|
+
}
|
|
243
|
+
flush();
|
|
244
|
+
return out;
|
|
245
|
+
}
|
|
246
|
+
function serializeSegments(segments, format) {
|
|
247
|
+
const f = format || CURLY;
|
|
248
|
+
if (f.write) return (segments || []).map((s) => s.type === "mention" ? f.write(s.item || s) : s.text).join("");
|
|
249
|
+
const { open, close } = f;
|
|
250
|
+
return (segments || []).map((s) => s.type === "mention" ? `${open}${s.name}${close}` : s.text).join("");
|
|
251
|
+
}
|
|
252
|
+
function mentionsOf(segments) {
|
|
253
|
+
const seen = /* @__PURE__ */ new Set();
|
|
254
|
+
const out = [];
|
|
255
|
+
for (const s of segments || []) {
|
|
256
|
+
if (s.type !== "mention" || seen.has(s.id)) continue;
|
|
257
|
+
seen.add(s.id);
|
|
258
|
+
out.push({ id: s.id, name: s.name, kind: s.kind || null });
|
|
259
|
+
}
|
|
260
|
+
return out;
|
|
261
|
+
}
|
|
262
|
+
function norm(s) {
|
|
263
|
+
return String(s || "").toLowerCase();
|
|
264
|
+
}
|
|
265
|
+
function subsequence(hay, needle) {
|
|
266
|
+
let j = 0;
|
|
267
|
+
for (let i = 0; i < hay.length && j < needle.length; i++) if (hay[i] === needle[j]) j++;
|
|
268
|
+
return j === needle.length;
|
|
269
|
+
}
|
|
270
|
+
function scoreItem(item, query) {
|
|
271
|
+
const q = norm(query).trim();
|
|
272
|
+
if (!q) return 1;
|
|
273
|
+
const name = norm(item.name);
|
|
274
|
+
if (name === q) return 120;
|
|
275
|
+
if (name.startsWith(q)) return 100;
|
|
276
|
+
if (name.split(/[\s_-]+/).some((w) => w.startsWith(q))) return 80;
|
|
277
|
+
if (name.includes(q)) return 60;
|
|
278
|
+
const initials = name.split(/[\s_-]+/).map((w) => w[0]).join("");
|
|
279
|
+
if (initials.startsWith(q)) return 50;
|
|
280
|
+
if (subsequence(name, q)) return 30;
|
|
281
|
+
const desc = norm(item.description);
|
|
282
|
+
if (desc && desc.includes(q)) return 20;
|
|
283
|
+
const kws = (item.keywords || []).map(norm);
|
|
284
|
+
if (kws.some((k) => k.includes(q))) return 15;
|
|
285
|
+
return 0;
|
|
286
|
+
}
|
|
287
|
+
function searchItems(items, query) {
|
|
288
|
+
const scored = (items || []).map((it, i) => ({ it, i, s: scoreItem(it, query) })).filter((x) => x.s > 0);
|
|
289
|
+
scored.sort((a, b) => b.s - a.s || a.i - b.i);
|
|
290
|
+
return scored.map((x) => x.it);
|
|
291
|
+
}
|
|
292
|
+
function groupItems(items, query) {
|
|
293
|
+
const groups = /* @__PURE__ */ new Map();
|
|
294
|
+
for (const it of searchItems(items, query)) {
|
|
295
|
+
const key = it.section || "";
|
|
296
|
+
if (!groups.has(key)) groups.set(key, []);
|
|
297
|
+
groups.get(key).push(it);
|
|
298
|
+
}
|
|
299
|
+
const order = [""];
|
|
300
|
+
for (const it of items || []) {
|
|
301
|
+
const k = it.section || "";
|
|
302
|
+
if (!order.includes(k)) order.push(k);
|
|
303
|
+
}
|
|
304
|
+
return order.filter((k) => groups.has(k)).map((k) => ({ section: k, items: groups.get(k) }));
|
|
305
|
+
}
|
|
306
|
+
function activeTrigger(textBeforeCaret, trigger = "@", maxLen = 48) {
|
|
307
|
+
const at = textBeforeCaret.lastIndexOf(trigger);
|
|
308
|
+
if (at < 0) return null;
|
|
309
|
+
if (at > 0 && !/[\s([{"'>]/.test(textBeforeCaret[at - 1])) return null;
|
|
310
|
+
const query = textBeforeCaret.slice(at + trigger.length);
|
|
311
|
+
if (query.length > maxLen || /\n/.test(query)) return null;
|
|
312
|
+
return { start: at, query };
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// src/mention/MentionInput.jsx
|
|
316
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
317
|
+
var MENTION_ATTR = "data-mention-id";
|
|
318
|
+
function monogram(name) {
|
|
319
|
+
const parts = String(name || "").trim().split(/\s+/).filter(Boolean);
|
|
320
|
+
return ((parts[0] || "")[0] || "") + ((parts[1] || "")[0] || "") || "?";
|
|
321
|
+
}
|
|
322
|
+
function buildPillNode(item) {
|
|
323
|
+
const el = document.createElement("span");
|
|
324
|
+
el.className = "uic-mention";
|
|
325
|
+
el.setAttribute(MENTION_ATTR, item.id);
|
|
326
|
+
el.setAttribute("data-name", item.name);
|
|
327
|
+
if (item.kind) el.setAttribute("data-kind", item.kind);
|
|
328
|
+
el.setAttribute("contenteditable", "false");
|
|
329
|
+
if (item.description) el.title = item.description;
|
|
330
|
+
if (item.color) el.style.setProperty("--uic-mention-color", item.color);
|
|
331
|
+
const ic = document.createElement("span");
|
|
332
|
+
ic.className = "uic-mention-ic";
|
|
333
|
+
if (item.avatar) {
|
|
334
|
+
const img = document.createElement("img");
|
|
335
|
+
img.src = item.avatar;
|
|
336
|
+
img.alt = "";
|
|
337
|
+
ic.appendChild(img);
|
|
338
|
+
} else {
|
|
339
|
+
ic.textContent = typeof item.icon === "string" && item.icon ? item.icon : monogram(item.name);
|
|
340
|
+
if (!(typeof item.icon === "string" && item.icon)) ic.classList.add("is-mono");
|
|
341
|
+
}
|
|
342
|
+
const nm = document.createElement("span");
|
|
343
|
+
nm.className = "uic-mention-name";
|
|
344
|
+
nm.textContent = item.name;
|
|
345
|
+
el.appendChild(ic);
|
|
346
|
+
el.appendChild(nm);
|
|
347
|
+
return el;
|
|
348
|
+
}
|
|
349
|
+
function readSegments(root, resolver) {
|
|
350
|
+
const out = [];
|
|
351
|
+
let buf = "";
|
|
352
|
+
const flush = () => {
|
|
353
|
+
if (buf) {
|
|
354
|
+
out.push({ type: "text", text: buf });
|
|
355
|
+
buf = "";
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
const walk = (node, isLastAtRoot) => {
|
|
359
|
+
const kids = Array.from(node.childNodes);
|
|
360
|
+
kids.forEach((k, i) => {
|
|
361
|
+
const last = isLastAtRoot && i === kids.length - 1;
|
|
362
|
+
if (k.nodeType === 3) {
|
|
363
|
+
buf += k.data.replace(/\u00a0/g, " ");
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
if (k.nodeType !== 1) return;
|
|
367
|
+
const tag = k.tagName;
|
|
368
|
+
if (tag === "BR") {
|
|
369
|
+
if (!last) buf += "\n";
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
if (k.hasAttribute(MENTION_ATTR)) {
|
|
373
|
+
flush();
|
|
374
|
+
const id = k.getAttribute(MENTION_ATTR);
|
|
375
|
+
const name = k.getAttribute("data-name") || "";
|
|
376
|
+
const item = resolver.get(name) || { id, name, kind: k.getAttribute("data-kind") || null };
|
|
377
|
+
out.push({ type: "mention", id: item.id, name: item.name, kind: item.kind || null, item });
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
if (tag === "DIV" || tag === "P") {
|
|
381
|
+
if (out.length || buf) buf += "\n";
|
|
382
|
+
walk(k, last);
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
walk(k, last);
|
|
386
|
+
});
|
|
387
|
+
};
|
|
388
|
+
walk(root, true);
|
|
389
|
+
flush();
|
|
390
|
+
return out;
|
|
391
|
+
}
|
|
392
|
+
function writeSegments(root, segments) {
|
|
393
|
+
root.textContent = "";
|
|
394
|
+
for (const s of segments) {
|
|
395
|
+
if (s.type === "mention") {
|
|
396
|
+
root.appendChild(buildPillNode(s.item || s));
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
const lines = String(s.text).split("\n");
|
|
400
|
+
lines.forEach((line, i) => {
|
|
401
|
+
if (i > 0) root.appendChild(document.createElement("br"));
|
|
402
|
+
if (line) root.appendChild(document.createTextNode(line));
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
root.appendChild(document.createElement("br"));
|
|
406
|
+
}
|
|
407
|
+
function placeCaretAfter(node) {
|
|
408
|
+
const sel = window.getSelection();
|
|
409
|
+
if (!sel) return;
|
|
410
|
+
const r = document.createRange();
|
|
411
|
+
r.setStartAfter(node);
|
|
412
|
+
r.collapse(true);
|
|
413
|
+
sel.removeAllRanges();
|
|
414
|
+
sel.addRange(r);
|
|
415
|
+
}
|
|
416
|
+
function selectionIn(root) {
|
|
417
|
+
const sel = typeof window !== "undefined" ? window.getSelection() : null;
|
|
418
|
+
if (!sel || !sel.rangeCount) return null;
|
|
419
|
+
const r = sel.getRangeAt(0);
|
|
420
|
+
if (!root.contains(r.startContainer)) return null;
|
|
421
|
+
return { sel, range: r };
|
|
422
|
+
}
|
|
423
|
+
var MentionInput = (0, import_react3.forwardRef)(function MentionInput2(props, ref) {
|
|
424
|
+
const {
|
|
425
|
+
value = "",
|
|
426
|
+
onChange,
|
|
427
|
+
items = [],
|
|
428
|
+
format = CURLY,
|
|
429
|
+
trigger = "@",
|
|
430
|
+
placeholder,
|
|
431
|
+
disabled = false,
|
|
432
|
+
onSubmit,
|
|
433
|
+
submitOnEnter = false,
|
|
434
|
+
autoFocus = false,
|
|
435
|
+
className = "",
|
|
436
|
+
label,
|
|
437
|
+
menuWidth = 360,
|
|
438
|
+
menuTitle,
|
|
439
|
+
emptyText = "No match",
|
|
440
|
+
renderIcon,
|
|
441
|
+
onOpenChange,
|
|
442
|
+
onFocus,
|
|
443
|
+
onBlur,
|
|
444
|
+
onKeyDown,
|
|
445
|
+
id
|
|
446
|
+
} = props;
|
|
447
|
+
const editorRef = (0, import_react3.useRef)(null);
|
|
448
|
+
const resolver = (0, import_react3.useMemo)(() => makeResolver(items), [items]);
|
|
449
|
+
const [menu, setMenu] = (0, import_react3.useState)(null);
|
|
450
|
+
const [active, setActive] = (0, import_react3.useState)(0);
|
|
451
|
+
const [empty, setEmpty] = (0, import_react3.useState)(!value);
|
|
452
|
+
const listId = (0, import_react3.useMemo)(() => `uic-mention-${Math.random().toString(36).slice(2, 8)}`, []);
|
|
453
|
+
const emit = (0, import_react3.useCallback)(() => {
|
|
454
|
+
const root = editorRef.current;
|
|
455
|
+
if (!root) return;
|
|
456
|
+
const segs = readSegments(root, resolver);
|
|
457
|
+
const next = serializeSegments(segs, format);
|
|
458
|
+
setEmpty(!next);
|
|
459
|
+
if (onChange) onChange(next, mentionsOf(segs));
|
|
460
|
+
}, [resolver, format, onChange]);
|
|
461
|
+
(0, import_react3.useLayoutEffect)(() => {
|
|
462
|
+
const root = editorRef.current;
|
|
463
|
+
if (!root) return;
|
|
464
|
+
const current = serializeSegments(readSegments(root, resolver), format);
|
|
465
|
+
if (current === (value || "")) return;
|
|
466
|
+
writeSegments(root, parseValue(value || "", format, resolver));
|
|
467
|
+
setEmpty(!value);
|
|
468
|
+
}, [value, resolver, format]);
|
|
469
|
+
(0, import_react3.useEffect)(() => {
|
|
470
|
+
if (autoFocus && editorRef.current) editorRef.current.focus();
|
|
471
|
+
}, [autoFocus]);
|
|
472
|
+
const closeMenu = (0, import_react3.useCallback)(() => setMenu(null), []);
|
|
473
|
+
(0, import_react3.useEffect)(() => {
|
|
474
|
+
if (onOpenChange) onOpenChange(!!menu);
|
|
475
|
+
}, [menu, onOpenChange]);
|
|
476
|
+
const groups = (0, import_react3.useMemo)(() => menu ? groupItems(items, menu.query) : [], [items, menu]);
|
|
477
|
+
const flat = (0, import_react3.useMemo)(() => groups.flatMap((g) => g.items), [groups]);
|
|
478
|
+
(0, import_react3.useEffect)(() => {
|
|
479
|
+
setActive(0);
|
|
480
|
+
}, [menu && menu.query, groups.length]);
|
|
481
|
+
const syncMenu = (0, import_react3.useCallback)(() => {
|
|
482
|
+
const root = editorRef.current;
|
|
483
|
+
const s = root && selectionIn(root);
|
|
484
|
+
if (!s || !s.range.collapsed) {
|
|
485
|
+
setMenu(null);
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
const node = s.range.startContainer;
|
|
489
|
+
if (node.nodeType !== 3) {
|
|
490
|
+
setMenu(null);
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
const hit = activeTrigger(node.data.slice(0, s.range.startOffset), trigger);
|
|
494
|
+
if (!hit) {
|
|
495
|
+
setMenu(null);
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
const r = document.createRange();
|
|
499
|
+
r.setStart(node, hit.start);
|
|
500
|
+
r.setEnd(node, Math.min(node.data.length, hit.start + 1));
|
|
501
|
+
const rect = r.getBoundingClientRect();
|
|
502
|
+
setMenu({ node, start: hit.start, query: hit.query, rect: rect.width || rect.height ? rect : root.getBoundingClientRect() });
|
|
503
|
+
}, [trigger]);
|
|
504
|
+
const anchorRef = (0, import_react3.useMemo)(() => ({
|
|
505
|
+
current: {
|
|
506
|
+
getBoundingClientRect: () => menu ? menu.rect : new DOMRect(),
|
|
507
|
+
contains: () => false
|
|
508
|
+
}
|
|
509
|
+
}), [menu]);
|
|
510
|
+
const insertPill = (0, import_react3.useCallback)((item, replace) => {
|
|
511
|
+
const root = editorRef.current;
|
|
512
|
+
if (!root || !item || item.disabled) return;
|
|
513
|
+
const pill = buildPillNode(item);
|
|
514
|
+
const space = document.createTextNode("\xA0");
|
|
515
|
+
if (replace && replace.node && replace.node.parentNode === root) {
|
|
516
|
+
const { node, start, query } = replace;
|
|
517
|
+
node.deleteData(start, trigger.length + query.length);
|
|
518
|
+
const after = node.splitText(start);
|
|
519
|
+
root.insertBefore(pill, after);
|
|
520
|
+
root.insertBefore(space, after);
|
|
521
|
+
} else {
|
|
522
|
+
const s = selectionIn(root);
|
|
523
|
+
if (s) {
|
|
524
|
+
s.range.deleteContents();
|
|
525
|
+
s.range.insertNode(space);
|
|
526
|
+
s.range.insertNode(pill);
|
|
527
|
+
} else {
|
|
528
|
+
root.appendChild(pill);
|
|
529
|
+
root.appendChild(space);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
placeCaretAfter(space);
|
|
533
|
+
setMenu(null);
|
|
534
|
+
emit();
|
|
535
|
+
}, [emit, trigger]);
|
|
536
|
+
const pick = (0, import_react3.useCallback)((item) => insertPill(item, menu), [insertPill, menu]);
|
|
537
|
+
const insertText = (0, import_react3.useCallback)((text) => {
|
|
538
|
+
const root = editorRef.current;
|
|
539
|
+
if (!root) return;
|
|
540
|
+
root.focus();
|
|
541
|
+
let done = false;
|
|
542
|
+
try {
|
|
543
|
+
done = document.execCommand("insertText", false, text);
|
|
544
|
+
} catch {
|
|
545
|
+
done = false;
|
|
546
|
+
}
|
|
547
|
+
if (!done) {
|
|
548
|
+
const s = selectionIn(root);
|
|
549
|
+
const tn = document.createTextNode(text);
|
|
550
|
+
if (s) {
|
|
551
|
+
s.range.deleteContents();
|
|
552
|
+
s.range.insertNode(tn);
|
|
553
|
+
placeCaretAfter(tn);
|
|
554
|
+
} else root.appendChild(tn);
|
|
555
|
+
}
|
|
556
|
+
emit();
|
|
557
|
+
syncMenu();
|
|
558
|
+
}, [emit, syncMenu]);
|
|
559
|
+
const wrapSelection = (0, import_react3.useCallback)((before, after = "") => {
|
|
560
|
+
const root = editorRef.current;
|
|
561
|
+
if (!root) return;
|
|
562
|
+
root.focus();
|
|
563
|
+
const s = selectionIn(root);
|
|
564
|
+
const selected = s ? s.range.toString() : "";
|
|
565
|
+
insertText(before + selected + after);
|
|
566
|
+
if (!selected && after) {
|
|
567
|
+
const sel = window.getSelection();
|
|
568
|
+
for (let i = 0; i < after.length; i++) sel && sel.modify && sel.modify("move", "backward", "character");
|
|
569
|
+
}
|
|
570
|
+
}, [insertText]);
|
|
571
|
+
const prefixLine = (0, import_react3.useCallback)((prefix) => {
|
|
572
|
+
const root = editorRef.current;
|
|
573
|
+
if (!root) return;
|
|
574
|
+
root.focus();
|
|
575
|
+
const s = selectionIn(root);
|
|
576
|
+
const node = s && s.range.startContainer;
|
|
577
|
+
const before = node && node.nodeType === 3 ? node.data.slice(0, s.range.startOffset) : "";
|
|
578
|
+
const atLineStart = !before || /\n$/.test(before) || node && node.nodeType !== 3;
|
|
579
|
+
insertText((atLineStart ? "" : "\n") + prefix);
|
|
580
|
+
}, [insertText]);
|
|
581
|
+
(0, import_react3.useImperativeHandle)(ref, () => ({
|
|
582
|
+
focus: () => editorRef.current && editorRef.current.focus(),
|
|
583
|
+
clear: () => {
|
|
584
|
+
const root = editorRef.current;
|
|
585
|
+
if (root) {
|
|
586
|
+
writeSegments(root, []);
|
|
587
|
+
emit();
|
|
588
|
+
}
|
|
589
|
+
},
|
|
590
|
+
insertMention: (item) => insertPill(item, null),
|
|
591
|
+
insertText,
|
|
592
|
+
wrapSelection,
|
|
593
|
+
prefixLine,
|
|
594
|
+
get el() {
|
|
595
|
+
return editorRef.current;
|
|
596
|
+
}
|
|
597
|
+
}), [emit, insertPill, insertText, wrapSelection, prefixLine]);
|
|
598
|
+
const handleInput = () => {
|
|
599
|
+
emit();
|
|
600
|
+
syncMenu();
|
|
601
|
+
};
|
|
602
|
+
const handleKeyDown = (e) => {
|
|
603
|
+
if (onKeyDown) onKeyDown(e);
|
|
604
|
+
if (e.defaultPrevented) return;
|
|
605
|
+
if (menu) {
|
|
606
|
+
if (e.key === "ArrowDown") {
|
|
607
|
+
e.preventDefault();
|
|
608
|
+
setActive((a) => flat.length ? (a + 1) % flat.length : 0);
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
if (e.key === "ArrowUp") {
|
|
612
|
+
e.preventDefault();
|
|
613
|
+
setActive((a) => flat.length ? (a - 1 + flat.length) % flat.length : 0);
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
if ((e.key === "Enter" || e.key === "Tab") && flat.length) {
|
|
617
|
+
e.preventDefault();
|
|
618
|
+
pick(flat[active]);
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
if (e.key === "Escape") {
|
|
622
|
+
e.preventDefault();
|
|
623
|
+
setMenu(null);
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
if (e.key === "Enter") {
|
|
628
|
+
if (submitOnEnter && !e.shiftKey) {
|
|
629
|
+
e.preventDefault();
|
|
630
|
+
if (onSubmit) onSubmit();
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
e.preventDefault();
|
|
634
|
+
insertText("\n");
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
const handlePaste = (e) => {
|
|
638
|
+
e.preventDefault();
|
|
639
|
+
const text = e.clipboardData && e.clipboardData.getData("text/plain") || "";
|
|
640
|
+
if (!text) return;
|
|
641
|
+
const segs = parseValue(text, format, resolver);
|
|
642
|
+
if (segs.every((s2) => s2.type === "text")) {
|
|
643
|
+
insertText(text);
|
|
644
|
+
return;
|
|
645
|
+
}
|
|
646
|
+
const root = editorRef.current;
|
|
647
|
+
const s = selectionIn(root);
|
|
648
|
+
if (!s) {
|
|
649
|
+
insertText(text);
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
s.range.deleteContents();
|
|
653
|
+
const frag = document.createDocumentFragment();
|
|
654
|
+
let lastNode = null;
|
|
655
|
+
for (const seg of segs) {
|
|
656
|
+
if (seg.type === "mention") {
|
|
657
|
+
lastNode = buildPillNode(seg.item || seg);
|
|
658
|
+
frag.appendChild(lastNode);
|
|
659
|
+
continue;
|
|
660
|
+
}
|
|
661
|
+
seg.text.split("\n").forEach((line, i) => {
|
|
662
|
+
if (i > 0) {
|
|
663
|
+
lastNode = document.createElement("br");
|
|
664
|
+
frag.appendChild(lastNode);
|
|
665
|
+
}
|
|
666
|
+
if (line) {
|
|
667
|
+
lastNode = document.createTextNode(line);
|
|
668
|
+
frag.appendChild(lastNode);
|
|
669
|
+
}
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
s.range.insertNode(frag);
|
|
673
|
+
if (lastNode) placeCaretAfter(lastNode);
|
|
674
|
+
emit();
|
|
675
|
+
syncMenu();
|
|
676
|
+
};
|
|
677
|
+
const handleBlur = (e) => {
|
|
678
|
+
if (onBlur) onBlur(e);
|
|
679
|
+
};
|
|
680
|
+
const handleFocus = (e) => {
|
|
681
|
+
if (onFocus) onFocus(e);
|
|
682
|
+
};
|
|
683
|
+
(0, import_react3.useEffect)(() => {
|
|
684
|
+
if (!menu) return void 0;
|
|
685
|
+
const onSel = () => syncMenu();
|
|
686
|
+
document.addEventListener("selectionchange", onSel);
|
|
687
|
+
return () => document.removeEventListener("selectionchange", onSel);
|
|
688
|
+
}, [menu, syncMenu]);
|
|
689
|
+
(0, import_react3.useEffect)(() => {
|
|
690
|
+
if (!menu) return;
|
|
691
|
+
const el = document.getElementById(`${listId}-${active}`);
|
|
692
|
+
const list = el && el.closest(".uic-mention-menu");
|
|
693
|
+
if (!el || !list) return;
|
|
694
|
+
const top = el.offsetTop - list.offsetTop;
|
|
695
|
+
const bottom = top + el.offsetHeight;
|
|
696
|
+
if (top < list.scrollTop) list.scrollTop = top;
|
|
697
|
+
else if (bottom > list.scrollTop + list.clientHeight) list.scrollTop = bottom - list.clientHeight;
|
|
698
|
+
}, [active, menu, listId]);
|
|
699
|
+
const iconOf = (item) => {
|
|
700
|
+
if (renderIcon) {
|
|
701
|
+
const n = renderIcon(item);
|
|
702
|
+
if (n) return n;
|
|
703
|
+
}
|
|
704
|
+
if (item.avatar) return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("img", { src: item.avatar, alt: "" });
|
|
705
|
+
if (typeof item.icon === "string" && item.icon) return item.icon;
|
|
706
|
+
if (item.icon) return item.icon;
|
|
707
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "is-mono", children: monogram(item.name) });
|
|
708
|
+
};
|
|
709
|
+
let index = -1;
|
|
710
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: ["uic-mention-field", disabled ? "is-disabled" : "", className].filter(Boolean).join(" "), children: [
|
|
711
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
712
|
+
"div",
|
|
713
|
+
{
|
|
714
|
+
ref: editorRef,
|
|
715
|
+
id,
|
|
716
|
+
className: "uic-mention-editor" + (empty ? " is-empty" : ""),
|
|
717
|
+
contentEditable: !disabled,
|
|
718
|
+
suppressContentEditableWarning: true,
|
|
719
|
+
role: "textbox",
|
|
720
|
+
"aria-multiline": "true",
|
|
721
|
+
"aria-label": label,
|
|
722
|
+
"aria-autocomplete": "list",
|
|
723
|
+
"aria-expanded": !!menu,
|
|
724
|
+
"aria-controls": menu ? listId : void 0,
|
|
725
|
+
"aria-activedescendant": menu && flat.length ? `${listId}-${active}` : void 0,
|
|
726
|
+
"data-placeholder": placeholder || "",
|
|
727
|
+
spellCheck: "true",
|
|
728
|
+
onInput: handleInput,
|
|
729
|
+
onKeyDown: handleKeyDown,
|
|
730
|
+
onPaste: handlePaste,
|
|
731
|
+
onFocus: handleFocus,
|
|
732
|
+
onBlur: handleBlur
|
|
733
|
+
}
|
|
734
|
+
),
|
|
735
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Popover, { open: !!menu, anchorRef, onClose: closeMenu, width: menuWidth, minHeight: 120, className: "uic-mention-pop", label: menuTitle || "Mentions", children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "uic-mention-menu", role: "listbox", id: listId, onMouseDown: (e) => e.preventDefault(), children: [
|
|
736
|
+
menuTitle ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "uic-mention-head", children: menuTitle }) : null,
|
|
737
|
+
groups.map((g) => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "uic-mention-group", children: [
|
|
738
|
+
g.section ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "uic-mention-sec", children: g.section }) : null,
|
|
739
|
+
g.items.map((item) => {
|
|
740
|
+
index += 1;
|
|
741
|
+
const i = index;
|
|
742
|
+
const dis = !!item.disabled;
|
|
743
|
+
const reason = typeof item.disabled === "string" ? item.disabled : null;
|
|
744
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
745
|
+
"button",
|
|
746
|
+
{
|
|
747
|
+
type: "button",
|
|
748
|
+
id: `${listId}-${i}`,
|
|
749
|
+
role: "option",
|
|
750
|
+
"aria-selected": i === active,
|
|
751
|
+
"aria-disabled": dis || void 0,
|
|
752
|
+
className: "uic-mention-item" + (i === active ? " is-active" : "") + (dis ? " is-disabled" : ""),
|
|
753
|
+
style: item.color ? { "--uic-mention-color": item.color } : void 0,
|
|
754
|
+
onMouseEnter: () => setActive(i),
|
|
755
|
+
onClick: () => {
|
|
756
|
+
if (!dis) pick(item);
|
|
757
|
+
},
|
|
758
|
+
children: [
|
|
759
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "uic-mention-ic", children: iconOf(item) }),
|
|
760
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("span", { className: "uic-mention-text", children: [
|
|
761
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "uic-mention-name", children: item.name }),
|
|
762
|
+
item.description ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "uic-mention-desc", children: item.description }) : null
|
|
763
|
+
] }),
|
|
764
|
+
reason || item.tag ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "uic-mention-tag", children: reason || item.tag }) : null
|
|
765
|
+
]
|
|
766
|
+
},
|
|
767
|
+
item.id
|
|
768
|
+
);
|
|
769
|
+
})
|
|
770
|
+
] }, g.section || "_")),
|
|
771
|
+
!flat.length ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "uic-mention-empty", children: emptyText }) : null
|
|
772
|
+
] }) })
|
|
773
|
+
] });
|
|
774
|
+
});
|
|
775
|
+
|
|
776
|
+
// src/sheet/SheetGrid.jsx
|
|
777
|
+
var import_react_dom2 = require("react-dom");
|
|
778
|
+
|
|
779
|
+
// src/collab/colors.js
|
|
780
|
+
var PEER_PALETTE = ["#1e40af", "#c2410c", "#15803d", "#6d28d9", "#be123c", "#0e7490", "#a16207", "#4338ca"];
|
|
781
|
+
function peerColor(key) {
|
|
782
|
+
let h = 0;
|
|
783
|
+
for (const ch of String(key || "")) h = h * 31 + ch.charCodeAt(0) >>> 0;
|
|
784
|
+
return PEER_PALETTE[h % PEER_PALETTE.length];
|
|
785
|
+
}
|
|
786
|
+
function withAlpha(hex, alpha) {
|
|
787
|
+
const m = /^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(String(hex || ""));
|
|
788
|
+
if (!m) return hex;
|
|
789
|
+
return `rgba(${parseInt(m[1], 16)}, ${parseInt(m[2], 16)}, ${parseInt(m[3], 16)}, ${alpha})`;
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
// src/sheet/SheetGrid.jsx
|
|
793
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
794
|
+
var cellKey = (rowId, colId) => `${rowId}:${colId}`;
|
|
795
|
+
var uid = (p) => `${p}_${Math.random().toString(36).slice(2, 10)}`;
|
|
796
|
+
var TYPE_LABEL = {
|
|
797
|
+
text: "Text",
|
|
798
|
+
number: "Number",
|
|
799
|
+
select: "Select",
|
|
800
|
+
tags: "Tags",
|
|
801
|
+
checkbox: "Checkbox",
|
|
802
|
+
date: "Date",
|
|
803
|
+
url: "Link",
|
|
804
|
+
resource: "Resource",
|
|
805
|
+
compute: "Computed"
|
|
806
|
+
};
|
|
807
|
+
var TYPE_GLYPH = { text: "Aa", number: "#", select: "\u25BE", tags: "#", checkbox: "\u2713", date: "\u25A6", url: "\u2197", resource: "\u25EB", compute: "\u2726" };
|
|
808
|
+
var MANUAL_TYPES = ["text", "number", "select", "tags", "checkbox", "date", "url", "resource"];
|
|
809
|
+
var COMPUTE_KINDS = [
|
|
810
|
+
{ kind: "prompt", label: "AI prompt" },
|
|
811
|
+
{ kind: "harness", label: "Agent (harness)" },
|
|
812
|
+
{ kind: "workflow", label: "Workflow" },
|
|
813
|
+
{ kind: "image", label: "Generate image" }
|
|
814
|
+
];
|
|
815
|
+
var RESOURCE_KINDS = ["image", "sheet", "slides", "workflow", "graph"];
|
|
816
|
+
var RESOURCE_ICON = { image: "\u{1F5BC}", sheet: "\u25A6", slides: "\u25A4", workflow: "\u2699", graph: "\u25C9" };
|
|
817
|
+
function PopPortal({ anchor, width = 250, estHeight = 300, className, children, onClose }) {
|
|
818
|
+
const boxRef = (0, import_react4.useRef)(null);
|
|
819
|
+
(0, import_react4.useEffect)(() => {
|
|
820
|
+
if (!onClose) return void 0;
|
|
821
|
+
const close = (e) => {
|
|
822
|
+
if (e?.type === "scroll" && boxRef.current && e.target instanceof Node && boxRef.current.contains(e.target)) return;
|
|
823
|
+
onClose();
|
|
824
|
+
};
|
|
825
|
+
window.addEventListener("scroll", close, true);
|
|
826
|
+
window.addEventListener("resize", close);
|
|
827
|
+
return () => {
|
|
828
|
+
window.removeEventListener("scroll", close, true);
|
|
829
|
+
window.removeEventListener("resize", close);
|
|
830
|
+
};
|
|
831
|
+
}, [onClose]);
|
|
832
|
+
if (!anchor) return null;
|
|
833
|
+
const left = Math.max(8, Math.min(anchor.right - width, window.innerWidth - width - 8));
|
|
834
|
+
let top = anchor.bottom + 6;
|
|
835
|
+
if (top + estHeight > window.innerHeight - 8) top = Math.max(8, anchor.top - estHeight - 6);
|
|
836
|
+
return (0, import_react_dom2.createPortal)(
|
|
837
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { ref: boxRef, className, style: { position: "fixed", top, left, width, zIndex: 1e3 }, children }),
|
|
838
|
+
document.body
|
|
839
|
+
);
|
|
840
|
+
}
|
|
841
|
+
var PILL_PALETTE = [
|
|
842
|
+
{ name: "green", color: "#D1FAE5" },
|
|
843
|
+
{ name: "red", color: "#FEE2E2" },
|
|
844
|
+
{ name: "amber", color: "#FEF3C7" },
|
|
845
|
+
{ name: "blue", color: "#DBEAFE" },
|
|
846
|
+
{ name: "purple", color: "#EDE9FE" },
|
|
847
|
+
{ name: "pink", color: "#FCE7F3" },
|
|
848
|
+
{ name: "teal", color: "#CCFBF1" },
|
|
849
|
+
{ name: "gray", color: "#E5E7EB" }
|
|
850
|
+
];
|
|
851
|
+
var PILL_FALLBACKS = ["#E0E7FF", "#FCE7F3", "#D1FAE5", "#FEF3C7", "#E0F2FE", "#F3E8FF", "#FFE4E6"];
|
|
852
|
+
function normOptions(options) {
|
|
853
|
+
return (options || []).map((o, i) => {
|
|
854
|
+
if (typeof o === "string") return { label: o, color: PILL_FALLBACKS[i % PILL_FALLBACKS.length] };
|
|
855
|
+
return { label: o.label ?? String(o.value ?? ""), color: o.color || PILL_FALLBACKS[i % PILL_FALLBACKS.length] };
|
|
856
|
+
});
|
|
857
|
+
}
|
|
858
|
+
var sameValue = (a, b) => String(a).trim().toLowerCase() === String(b).trim().toLowerCase();
|
|
859
|
+
function optionFor(value, options) {
|
|
860
|
+
if (value === void 0 || value === null || value === "") return null;
|
|
861
|
+
return normOptions(options).find((o) => sameValue(o.label, value)) || null;
|
|
862
|
+
}
|
|
863
|
+
function pillColor(label, options) {
|
|
864
|
+
const hit = optionFor(label, options);
|
|
865
|
+
if (hit) return hit.color;
|
|
866
|
+
let h = 0;
|
|
867
|
+
for (const ch of String(label)) h = h * 31 + ch.charCodeAt(0) >>> 0;
|
|
868
|
+
return PILL_FALLBACKS[h % PILL_FALLBACKS.length];
|
|
869
|
+
}
|
|
870
|
+
function RefImage({ refVal, fetchBlobUrl }) {
|
|
871
|
+
const [src, setSrc] = (0, import_react4.useState)(/^https?:\/\//i.test(refVal.url || "") ? refVal.url : null);
|
|
872
|
+
(0, import_react4.useEffect)(() => {
|
|
873
|
+
let dead = false;
|
|
874
|
+
let obj = null;
|
|
875
|
+
if (!src && refVal.url && fetchBlobUrl) {
|
|
876
|
+
fetchBlobUrl(refVal.url).then((u) => {
|
|
877
|
+
if (!dead) {
|
|
878
|
+
obj = u;
|
|
879
|
+
setSrc(u);
|
|
880
|
+
}
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
return () => {
|
|
884
|
+
dead = true;
|
|
885
|
+
if (obj) URL.revokeObjectURL(obj);
|
|
886
|
+
};
|
|
887
|
+
}, [refVal.url]);
|
|
888
|
+
if (!src) return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("span", { className: "shg-refchip", children: [
|
|
889
|
+
RESOURCE_ICON.image,
|
|
890
|
+
" ",
|
|
891
|
+
refVal.name || "image"
|
|
892
|
+
] });
|
|
893
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("img", { className: "shg-refimg", src, alt: refVal.name || "", title: refVal.name || "" });
|
|
894
|
+
}
|
|
895
|
+
function ResourceChip({ refVal, onOpen }) {
|
|
896
|
+
const icon = RESOURCE_ICON[refVal.kind] || "\u25C7";
|
|
897
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
898
|
+
"button",
|
|
899
|
+
{
|
|
900
|
+
className: "shg-refchip shg-refchip-btn",
|
|
901
|
+
title: `Open ${refVal.kind}`,
|
|
902
|
+
onClick: (e) => {
|
|
903
|
+
e.stopPropagation();
|
|
904
|
+
onOpen && onOpen(refVal);
|
|
905
|
+
},
|
|
906
|
+
children: [
|
|
907
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "shg-refchip-ic", children: icon }),
|
|
908
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "shg-refchip-name", children: refVal.name || refVal.id || refVal.kind })
|
|
909
|
+
]
|
|
910
|
+
}
|
|
911
|
+
);
|
|
912
|
+
}
|
|
913
|
+
function StatusDot({ status }) {
|
|
914
|
+
if (!status || status === "empty" || status === "done") return null;
|
|
915
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: `shg-dot shg-dot-${status}`, title: status });
|
|
916
|
+
}
|
|
917
|
+
function CellValue({ cell, col, fetchBlobUrl, onOpenResource }) {
|
|
918
|
+
const v = cell?.value;
|
|
919
|
+
if (v === void 0 || v === null || v === "") return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "shg-val-txt" });
|
|
920
|
+
if (v && typeof v === "object" && !Array.isArray(v) && v.kind) {
|
|
921
|
+
if (v.kind === "image") return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(RefImage, { refVal: v, fetchBlobUrl });
|
|
922
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ResourceChip, { refVal: v, onOpen: onOpenResource });
|
|
923
|
+
}
|
|
924
|
+
const styled = col.type !== "tags" && optionFor(v, col.options);
|
|
925
|
+
if (styled) return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "shg-pill", style: { background: styled.color }, children: styled.label });
|
|
926
|
+
switch (col.type) {
|
|
927
|
+
case "select":
|
|
928
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "shg-pill", style: { background: pillColor(String(v), col.options) }, children: String(v) });
|
|
929
|
+
case "tags": {
|
|
930
|
+
const tags = Array.isArray(v) ? v : String(v).split(",").map((s) => s.trim()).filter(Boolean);
|
|
931
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "shg-pills", children: tags.map((t) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "shg-pill", style: { background: pillColor(t, col.options) }, children: t }, t)) });
|
|
932
|
+
}
|
|
933
|
+
case "checkbox":
|
|
934
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "shg-check" + (v ? " on" : ""), children: v ? "\u2713" : "" });
|
|
935
|
+
case "url": {
|
|
936
|
+
const href = /^https?:\/\//i.test(String(v)) ? String(v) : `https://${v}`;
|
|
937
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("a", { className: "shg-link", href, target: "_blank", rel: "noreferrer", onClick: (e) => e.stopPropagation(), children: String(v) });
|
|
938
|
+
}
|
|
939
|
+
case "number":
|
|
940
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "shg-val-txt shg-num", children: String(v) });
|
|
941
|
+
default:
|
|
942
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "shg-val-txt", children: typeof v === "object" ? JSON.stringify(v) : String(v) });
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
function SheetGrid({ sheet, ops, onRunCell, onRunColumn, readOnly = false, fetchBlobUrl, onOpenResource, peers, onPresence }) {
|
|
946
|
+
const columns = sheet?.columns || [];
|
|
947
|
+
const rows = sheet?.rows || [];
|
|
948
|
+
const cells = sheet?.cells || {};
|
|
949
|
+
const tabId = sheet?.id;
|
|
950
|
+
const canEdit = !readOnly && !!ops && !!tabId;
|
|
951
|
+
const [editing, setEditing] = (0, import_react4.useState)(null);
|
|
952
|
+
const [draft, setDraft] = (0, import_react4.useState)("");
|
|
953
|
+
const [menuCol, setMenuCol] = (0, import_react4.useState)(null);
|
|
954
|
+
const [sizeDraft, setSizeDraft] = (0, import_react4.useState)({});
|
|
955
|
+
const [dnd, setDnd] = (0, import_react4.useState)(null);
|
|
956
|
+
const [dropAt, setDropAt] = (0, import_react4.useState)(null);
|
|
957
|
+
const inputRef = (0, import_react4.useRef)(null);
|
|
958
|
+
const dragRef = (0, import_react4.useRef)(null);
|
|
959
|
+
const [sel, setSel] = (0, import_react4.useState)(null);
|
|
960
|
+
const dragSel = (0, import_react4.useRef)(false);
|
|
961
|
+
const rootRef = (0, import_react4.useRef)(null);
|
|
962
|
+
const presenceRef = (0, import_react4.useRef)(onPresence);
|
|
963
|
+
presenceRef.current = onPresence;
|
|
964
|
+
const range = sel ? {
|
|
965
|
+
r0: Math.min(sel.a.r, sel.f.r),
|
|
966
|
+
r1: Math.max(sel.a.r, sel.f.r),
|
|
967
|
+
c0: Math.min(sel.a.c, sel.f.c),
|
|
968
|
+
c1: Math.max(sel.a.c, sel.f.c)
|
|
969
|
+
} : null;
|
|
970
|
+
const inSel = (ri, ci) => !!range && ri >= range.r0 && ri <= range.r1 && ci >= range.c0 && ci <= range.c1;
|
|
971
|
+
const isFocus = (ri, ci) => !!sel && sel.f.r === ri && sel.f.c === ci;
|
|
972
|
+
const clampSel = (p) => ({ r: Math.max(0, Math.min(p.r, rows.length - 1)), c: Math.max(0, Math.min(p.c, columns.length - 1)) });
|
|
973
|
+
const moveFocus = (dr, dc, extend) => setSel((cur) => {
|
|
974
|
+
if (!cur || !rows.length || !columns.length) return cur;
|
|
975
|
+
const f = clampSel({ r: cur.f.r + dr, c: cur.f.c + dc });
|
|
976
|
+
return { a: extend ? cur.a : f, f };
|
|
977
|
+
});
|
|
978
|
+
(0, import_react4.useEffect)(() => {
|
|
979
|
+
const up = () => {
|
|
980
|
+
dragSel.current = false;
|
|
981
|
+
};
|
|
982
|
+
window.addEventListener("mouseup", up);
|
|
983
|
+
return () => window.removeEventListener("mouseup", up);
|
|
984
|
+
}, []);
|
|
985
|
+
(0, import_react4.useEffect)(() => {
|
|
986
|
+
const fn = presenceRef.current;
|
|
987
|
+
if (!fn) return;
|
|
988
|
+
const ids = (p) => [rows[p.r]?.id, columns[p.c]?.id];
|
|
989
|
+
fn({
|
|
990
|
+
sel: sel && rows[sel.f.r] && columns[sel.f.c] ? { a: ids(sel.a), f: ids(sel.f) } : null,
|
|
991
|
+
typing: editing && typeof draft === "string" ? { key: cellKey(editing.rowId, editing.colId), text: draft } : null
|
|
992
|
+
});
|
|
993
|
+
}, [sel, editing, draft, rows, columns]);
|
|
994
|
+
const peerLayer = (0, import_react4.useMemo)(() => {
|
|
995
|
+
const focus = {}, tint = {}, typing = {};
|
|
996
|
+
const rIdx = new Map(rows.map((r, i) => [r.id, i]));
|
|
997
|
+
const cIdx = new Map(columns.map((c, i) => [c.id, i]));
|
|
998
|
+
for (const p of peers || []) {
|
|
999
|
+
if (p.typing && p.typing.key) typing[p.typing.key] = { text: p.typing.text, color: p.color, name: p.name };
|
|
1000
|
+
if (!p.sel || !p.sel.a || !p.sel.f) continue;
|
|
1001
|
+
const a = { r: rIdx.get(p.sel.a[0]), c: cIdx.get(p.sel.a[1]) }, f = { r: rIdx.get(p.sel.f[0]), c: cIdx.get(p.sel.f[1]) };
|
|
1002
|
+
if ([a.r, a.c, f.r, f.c].some((v) => v === void 0)) continue;
|
|
1003
|
+
focus[cellKey(p.sel.f[0], p.sel.f[1])] = p;
|
|
1004
|
+
for (let r = Math.min(a.r, f.r); r <= Math.max(a.r, f.r); r++) {
|
|
1005
|
+
for (let c = Math.min(a.c, f.c); c <= Math.max(a.c, f.c); c++) {
|
|
1006
|
+
const k = cellKey(rows[r].id, columns[c].id);
|
|
1007
|
+
if (!tint[k]) tint[k] = p.color;
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
return { focus, tint, typing };
|
|
1012
|
+
}, [peers, rows, columns]);
|
|
1013
|
+
const startResize = (e, kind, id, start) => {
|
|
1014
|
+
if (readOnly) return;
|
|
1015
|
+
e.preventDefault();
|
|
1016
|
+
e.stopPropagation();
|
|
1017
|
+
const origin = kind === "col" ? e.clientX : e.clientY;
|
|
1018
|
+
dragRef.current = { kind, id, start, origin, last: start };
|
|
1019
|
+
const onMove = (ev) => {
|
|
1020
|
+
const d = dragRef.current;
|
|
1021
|
+
if (!d) return;
|
|
1022
|
+
const delta = (d.kind === "col" ? ev.clientX : ev.clientY) - d.origin;
|
|
1023
|
+
const next = Math.max(d.kind === "col" ? 90 : 26, d.start + delta);
|
|
1024
|
+
d.last = next;
|
|
1025
|
+
setSizeDraft((sd) => ({ ...sd, [d.kind]: { ...sd[d.kind] || {}, [d.id]: next } }));
|
|
1026
|
+
};
|
|
1027
|
+
const onUp = () => {
|
|
1028
|
+
const d = dragRef.current;
|
|
1029
|
+
dragRef.current = null;
|
|
1030
|
+
window.removeEventListener("mousemove", onMove);
|
|
1031
|
+
window.removeEventListener("mouseup", onUp);
|
|
1032
|
+
document.body.style.cursor = "";
|
|
1033
|
+
if (!d) return;
|
|
1034
|
+
setSizeDraft({});
|
|
1035
|
+
if (!canEdit) return;
|
|
1036
|
+
if (d.kind === "col") {
|
|
1037
|
+
const c = columns.find((x) => x.id === d.id);
|
|
1038
|
+
if (c) ops.setColumn(tabId, { ...c, width: Math.round(d.last) });
|
|
1039
|
+
} else {
|
|
1040
|
+
const r = rows.find((x) => x.id === d.id);
|
|
1041
|
+
if (r) ops.setRow(tabId, { ...r, height: Math.round(d.last) });
|
|
1042
|
+
}
|
|
1043
|
+
};
|
|
1044
|
+
window.addEventListener("mousemove", onMove);
|
|
1045
|
+
window.addEventListener("mouseup", onUp);
|
|
1046
|
+
document.body.style.cursor = kind === "col" ? "col-resize" : "row-resize";
|
|
1047
|
+
};
|
|
1048
|
+
const colWidth = (col) => sizeDraft.col && sizeDraft.col[col.id] || col.width || void 0;
|
|
1049
|
+
const reorder = (kind, fromId, toId, after) => {
|
|
1050
|
+
if (!canEdit || fromId === toId) return;
|
|
1051
|
+
const arr2 = (kind === "col" ? columns : rows).map((x) => x.id);
|
|
1052
|
+
const i = arr2.indexOf(fromId);
|
|
1053
|
+
if (i < 0) return;
|
|
1054
|
+
arr2.splice(i, 1);
|
|
1055
|
+
let j = arr2.indexOf(toId);
|
|
1056
|
+
if (j < 0) return;
|
|
1057
|
+
if (after) j += 1;
|
|
1058
|
+
if (kind === "col") ops.moveColumn(tabId, fromId, j);
|
|
1059
|
+
else ops.moveRow(tabId, fromId, j);
|
|
1060
|
+
};
|
|
1061
|
+
const dndProps = (kind, id) => readOnly ? {} : {
|
|
1062
|
+
draggable: true,
|
|
1063
|
+
onDragStart: (e) => {
|
|
1064
|
+
if (e.target.closest && e.target.closest(".shg-resize-col, .shg-resize-row, input, button")) {
|
|
1065
|
+
e.preventDefault();
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
e.dataTransfer.effectAllowed = "move";
|
|
1069
|
+
e.dataTransfer.setData("text/plain", `${kind}:${id}`);
|
|
1070
|
+
setDnd({ kind, id });
|
|
1071
|
+
},
|
|
1072
|
+
onDragOver: (e) => {
|
|
1073
|
+
if (!dnd || dnd.kind !== kind || dnd.id === id) return;
|
|
1074
|
+
e.preventDefault();
|
|
1075
|
+
const r = e.currentTarget.getBoundingClientRect();
|
|
1076
|
+
const after = kind === "col" ? e.clientX > r.x + r.width / 2 : e.clientY > r.y + r.height / 2;
|
|
1077
|
+
setDropAt((d) => d && d.id === id && d.after === after ? d : { kind, id, after });
|
|
1078
|
+
},
|
|
1079
|
+
onDrop: (e) => {
|
|
1080
|
+
e.preventDefault();
|
|
1081
|
+
if (dnd && dnd.kind === kind && dnd.id !== id) {
|
|
1082
|
+
const r = e.currentTarget.getBoundingClientRect();
|
|
1083
|
+
const after = kind === "col" ? e.clientX > r.x + r.width / 2 : e.clientY > r.y + r.height / 2;
|
|
1084
|
+
reorder(kind, dnd.id, id, after);
|
|
1085
|
+
}
|
|
1086
|
+
setDnd(null);
|
|
1087
|
+
setDropAt(null);
|
|
1088
|
+
},
|
|
1089
|
+
onDragEnd: () => {
|
|
1090
|
+
setDnd(null);
|
|
1091
|
+
setDropAt(null);
|
|
1092
|
+
}
|
|
1093
|
+
};
|
|
1094
|
+
const dndClass = (kind, id) => {
|
|
1095
|
+
let cls = "";
|
|
1096
|
+
if (dnd && dnd.kind === kind && dnd.id === id) cls += " shg-dragging";
|
|
1097
|
+
if (dropAt && dropAt.kind === kind && dropAt.id === id) {
|
|
1098
|
+
cls += dropAt.after ? kind === "col" ? " shg-drop-after" : " shg-drop-below" : kind === "col" ? " shg-drop-before" : " shg-drop-above";
|
|
1099
|
+
}
|
|
1100
|
+
return cls;
|
|
1101
|
+
};
|
|
1102
|
+
const numW = Math.max(40, 24 + String(rows.length || 1).length * 9);
|
|
1103
|
+
const numStyle = { width: numW, minWidth: numW, maxWidth: numW };
|
|
1104
|
+
const rowHeight = (row) => sizeDraft.row && sizeDraft.row[row.id] || row.height || void 0;
|
|
1105
|
+
(0, import_react4.useEffect)(() => {
|
|
1106
|
+
if (editing && inputRef.current) inputRef.current.focus();
|
|
1107
|
+
}, [editing]);
|
|
1108
|
+
const setCell = (rowId, colId, value) => {
|
|
1109
|
+
if (!canEdit) return;
|
|
1110
|
+
ops.setCell(tabId, cellKey(rowId, colId), { value, status: "done", updated_at: Math.floor(Date.now() / 1e3) });
|
|
1111
|
+
};
|
|
1112
|
+
const openEdit = (rowId, col, anchorEl, initial) => {
|
|
1113
|
+
if (!canEdit) return;
|
|
1114
|
+
if (col.type === "compute") return;
|
|
1115
|
+
if (col.type === "checkbox") {
|
|
1116
|
+
const cur = cells[cellKey(rowId, col.id)];
|
|
1117
|
+
setCell(rowId, col.id, !(cur && cur.value));
|
|
1118
|
+
return;
|
|
1119
|
+
}
|
|
1120
|
+
const c = cells[cellKey(rowId, col.id)];
|
|
1121
|
+
const v = c?.value;
|
|
1122
|
+
if (col.type === "tags") setDraft(initial ?? (Array.isArray(v) ? v.join(", ") : v || ""));
|
|
1123
|
+
else if (col.type === "resource") setDraft(v && typeof v === "object" ? v : { kind: "sheet", name: "", id: "", url: "" });
|
|
1124
|
+
else setDraft(initial ?? (v == null ? "" : String(v)));
|
|
1125
|
+
setEditing({ rowId, colId: col.id, type: col.type, rect: anchorEl?.getBoundingClientRect?.() || null });
|
|
1126
|
+
};
|
|
1127
|
+
const commitEdit = () => {
|
|
1128
|
+
if (!editing) return;
|
|
1129
|
+
const { rowId, colId, type } = editing;
|
|
1130
|
+
let value = draft;
|
|
1131
|
+
if (type === "number") value = draft === "" ? null : Number(draft);
|
|
1132
|
+
else if (type === "tags") value = draft ? draft.split(",").map((s) => s.trim()).filter(Boolean) : [];
|
|
1133
|
+
setCell(rowId, colId, value);
|
|
1134
|
+
setEditing(null);
|
|
1135
|
+
};
|
|
1136
|
+
const addColumn = () => canEdit && ops.setColumn(tabId, { id: uid("col"), name: `Column ${columns.length + 1}`, type: "text" });
|
|
1137
|
+
const addRow = () => canEdit && ops.setRow(tabId, { id: uid("row") });
|
|
1138
|
+
const deleteColumn = (colId) => canEdit && ops.deleteColumn(tabId, colId);
|
|
1139
|
+
const deleteRow = (rowId) => canEdit && ops.deleteRow(tabId, rowId);
|
|
1140
|
+
const renameColumn = (colId, name) => {
|
|
1141
|
+
const c = columns.find((x) => x.id === colId);
|
|
1142
|
+
if (canEdit && c) ops.setColumn(tabId, { ...c, name });
|
|
1143
|
+
};
|
|
1144
|
+
const configureColumn = (colId, patch) => {
|
|
1145
|
+
const c = columns.find((x) => x.id === colId);
|
|
1146
|
+
if (!canEdit || !c) return;
|
|
1147
|
+
const next = { ...c, ...patch };
|
|
1148
|
+
if (patch.type && patch.type !== "compute") delete next.compute;
|
|
1149
|
+
ops.setColumn(tabId, next);
|
|
1150
|
+
};
|
|
1151
|
+
const cellText = (c) => {
|
|
1152
|
+
const v = c?.value;
|
|
1153
|
+
if (v === void 0 || v === null) return "";
|
|
1154
|
+
if (Array.isArray(v)) return v.join(", ");
|
|
1155
|
+
if (typeof v === "object") return v.name || v.url || v.id || "";
|
|
1156
|
+
return String(v);
|
|
1157
|
+
};
|
|
1158
|
+
const copySelection = (e) => {
|
|
1159
|
+
if (!range || editing) return;
|
|
1160
|
+
const lines = [];
|
|
1161
|
+
for (let r = range.r0; r <= range.r1; r++) {
|
|
1162
|
+
const parts = [];
|
|
1163
|
+
for (let c = range.c0; c <= range.c1; c++) parts.push(cellText(cells[cellKey(rows[r].id, columns[c].id)]));
|
|
1164
|
+
lines.push(parts.join(" "));
|
|
1165
|
+
}
|
|
1166
|
+
e.clipboardData.setData("text/plain", lines.join("\n"));
|
|
1167
|
+
e.preventDefault();
|
|
1168
|
+
};
|
|
1169
|
+
const pasteAt = (e) => {
|
|
1170
|
+
if (!sel || editing || !canEdit) return;
|
|
1171
|
+
const text = e.clipboardData.getData("text/plain");
|
|
1172
|
+
if (!text) return;
|
|
1173
|
+
e.preventDefault();
|
|
1174
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
1175
|
+
const patch = {};
|
|
1176
|
+
const lines = text.replace(/\r/g, "").split("\n");
|
|
1177
|
+
if (lines.length > 1 && lines[lines.length - 1] === "") lines.pop();
|
|
1178
|
+
lines.forEach((line, dr) => line.split(" ").forEach((raw, dc) => {
|
|
1179
|
+
const row = rows[sel.f.r + dr], col = columns[sel.f.c + dc];
|
|
1180
|
+
if (!row || !col || col.type === "compute") return;
|
|
1181
|
+
let value = raw;
|
|
1182
|
+
if (col.type === "number") value = raw === "" ? null : Number(raw);
|
|
1183
|
+
else if (col.type === "checkbox") value = /^(true|yes|1|✓)$/i.test(raw.trim());
|
|
1184
|
+
else if (col.type === "tags") value = raw ? raw.split(",").map((x) => x.trim()).filter(Boolean) : [];
|
|
1185
|
+
patch[cellKey(row.id, col.id)] = value === null || value === "" ? null : { value, status: "done", updated_at: now };
|
|
1186
|
+
}));
|
|
1187
|
+
if (Object.keys(patch).length) ops.setCells(tabId, patch);
|
|
1188
|
+
setSel({ a: sel.f, f: clampSel({ r: sel.f.r + lines.length - 1, c: sel.f.c + Math.max(...lines.map((l) => l.split(" ").length)) - 1 }) });
|
|
1189
|
+
};
|
|
1190
|
+
const clearSelection = () => {
|
|
1191
|
+
if (!range || !canEdit) return;
|
|
1192
|
+
const patch = {};
|
|
1193
|
+
for (let r = range.r0; r <= range.r1; r++) {
|
|
1194
|
+
for (let c = range.c0; c <= range.c1; c++) {
|
|
1195
|
+
if (columns[c].type === "compute") continue;
|
|
1196
|
+
const k = cellKey(rows[r].id, columns[c].id);
|
|
1197
|
+
if (cells[k]) patch[k] = null;
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
if (Object.keys(patch).length) ops.setCells(tabId, patch);
|
|
1201
|
+
};
|
|
1202
|
+
const onKeyDown = (e) => {
|
|
1203
|
+
if (editing || menuCol) return;
|
|
1204
|
+
if (!sel) return;
|
|
1205
|
+
const meta = e.metaKey || e.ctrlKey;
|
|
1206
|
+
if (meta && (e.key === "c" || e.key === "v" || e.key === "x")) return;
|
|
1207
|
+
const focusCell = () => ({ row: rows[sel.f.r], col: columns[sel.f.c] });
|
|
1208
|
+
switch (e.key) {
|
|
1209
|
+
case "ArrowUp":
|
|
1210
|
+
e.preventDefault();
|
|
1211
|
+
moveFocus(-1, 0, e.shiftKey);
|
|
1212
|
+
return;
|
|
1213
|
+
case "ArrowDown":
|
|
1214
|
+
e.preventDefault();
|
|
1215
|
+
moveFocus(1, 0, e.shiftKey);
|
|
1216
|
+
return;
|
|
1217
|
+
case "ArrowLeft":
|
|
1218
|
+
e.preventDefault();
|
|
1219
|
+
moveFocus(0, -1, e.shiftKey);
|
|
1220
|
+
return;
|
|
1221
|
+
case "ArrowRight":
|
|
1222
|
+
e.preventDefault();
|
|
1223
|
+
moveFocus(0, 1, e.shiftKey);
|
|
1224
|
+
return;
|
|
1225
|
+
case "Tab":
|
|
1226
|
+
e.preventDefault();
|
|
1227
|
+
moveFocus(0, e.shiftKey ? -1 : 1, false);
|
|
1228
|
+
return;
|
|
1229
|
+
case "Escape":
|
|
1230
|
+
setSel(null);
|
|
1231
|
+
return;
|
|
1232
|
+
case "Enter":
|
|
1233
|
+
case "F2": {
|
|
1234
|
+
e.preventDefault();
|
|
1235
|
+
const { row, col } = focusCell();
|
|
1236
|
+
if (row && col) openEdit(row.id, col, rootRef.current?.querySelector(`[data-cell="${cellKey(row.id, col.id)}"]`));
|
|
1237
|
+
return;
|
|
1238
|
+
}
|
|
1239
|
+
case "Delete":
|
|
1240
|
+
case "Backspace":
|
|
1241
|
+
e.preventDefault();
|
|
1242
|
+
clearSelection();
|
|
1243
|
+
return;
|
|
1244
|
+
default:
|
|
1245
|
+
}
|
|
1246
|
+
if (!meta && !e.altKey && e.key.length === 1) {
|
|
1247
|
+
const { row, col } = focusCell();
|
|
1248
|
+
if (row && col && col.type !== "compute" && col.type !== "checkbox") {
|
|
1249
|
+
e.preventDefault();
|
|
1250
|
+
openEdit(row.id, col, rootRef.current?.querySelector(`[data-cell="${cellKey(row.id, col.id)}"]`), e.key);
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
};
|
|
1254
|
+
const renderEditor = (col) => {
|
|
1255
|
+
if (col.type === "resource") {
|
|
1256
|
+
const d = typeof draft === "object" && draft ? draft : { kind: "sheet" };
|
|
1257
|
+
const set = (k, v) => setDraft({ ...d, [k]: v });
|
|
1258
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
1259
|
+
PopPortal,
|
|
1260
|
+
{
|
|
1261
|
+
anchor: editing.rect,
|
|
1262
|
+
width: 240,
|
|
1263
|
+
estHeight: 230,
|
|
1264
|
+
className: "shg-select-pop shg-ref-pop",
|
|
1265
|
+
onClose: () => setEditing(null),
|
|
1266
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { onClick: (e) => e.stopPropagation(), children: [
|
|
1267
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("select", { className: "shg-menu-input", value: d.kind || "sheet", onChange: (e) => set("kind", e.target.value), children: RESOURCE_KINDS.map((k) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("option", { value: k, children: k }, k)) }),
|
|
1268
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("input", { className: "shg-menu-input", placeholder: "Name", value: d.name || "", onChange: (e) => set("name", e.target.value) }),
|
|
1269
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("input", { className: "shg-menu-input", placeholder: "Resource id (e.g. sheet.\u2026)", value: d.id || "", onChange: (e) => set("id", e.target.value) }),
|
|
1270
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("input", { className: "shg-menu-input", placeholder: "or URL", value: d.url || "", onChange: (e) => set("url", e.target.value) }),
|
|
1271
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "shg-menu-actions", children: [
|
|
1272
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { className: "shg-pop-clear", onClick: () => {
|
|
1273
|
+
setCell(editing.rowId, editing.colId, null);
|
|
1274
|
+
setEditing(null);
|
|
1275
|
+
}, children: "Clear" }),
|
|
1276
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { className: "shg-menu-apply", onClick: () => {
|
|
1277
|
+
setCell(editing.rowId, editing.colId, d);
|
|
1278
|
+
setEditing(null);
|
|
1279
|
+
}, children: "Set" })
|
|
1280
|
+
] })
|
|
1281
|
+
] })
|
|
1282
|
+
}
|
|
1283
|
+
);
|
|
1284
|
+
}
|
|
1285
|
+
if (col.type === "select") {
|
|
1286
|
+
const opts = normOptions(col.options);
|
|
1287
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
1288
|
+
PopPortal,
|
|
1289
|
+
{
|
|
1290
|
+
anchor: editing.rect,
|
|
1291
|
+
width: 190,
|
|
1292
|
+
estHeight: 40 + opts.length * 30,
|
|
1293
|
+
className: "shg-select-pop",
|
|
1294
|
+
onClose: () => setEditing(null),
|
|
1295
|
+
children: [
|
|
1296
|
+
opts.length === 0 && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: "shg-pop-note", children: "No options yet \u2014 add them in the column menu." }),
|
|
1297
|
+
opts.map((o) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
1298
|
+
"button",
|
|
1299
|
+
{
|
|
1300
|
+
className: "shg-pill shg-pill-btn",
|
|
1301
|
+
style: { background: o.color },
|
|
1302
|
+
onClick: (e) => {
|
|
1303
|
+
e.stopPropagation();
|
|
1304
|
+
setCell(editing.rowId, editing.colId, o.label);
|
|
1305
|
+
setEditing(null);
|
|
1306
|
+
},
|
|
1307
|
+
children: o.label
|
|
1308
|
+
},
|
|
1309
|
+
o.label
|
|
1310
|
+
)),
|
|
1311
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
1312
|
+
"button",
|
|
1313
|
+
{
|
|
1314
|
+
className: "shg-pop-clear",
|
|
1315
|
+
onClick: (e) => {
|
|
1316
|
+
e.stopPropagation();
|
|
1317
|
+
setCell(editing.rowId, editing.colId, null);
|
|
1318
|
+
setEditing(null);
|
|
1319
|
+
},
|
|
1320
|
+
children: "Clear"
|
|
1321
|
+
}
|
|
1322
|
+
)
|
|
1323
|
+
]
|
|
1324
|
+
}
|
|
1325
|
+
);
|
|
1326
|
+
}
|
|
1327
|
+
const inputType = col.type === "date" ? "date" : col.type === "number" ? "number" : "text";
|
|
1328
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
1329
|
+
"input",
|
|
1330
|
+
{
|
|
1331
|
+
ref: inputRef,
|
|
1332
|
+
className: "shg-input",
|
|
1333
|
+
type: inputType,
|
|
1334
|
+
value: draft,
|
|
1335
|
+
placeholder: col.type === "tags" ? "tag, tag, \u2026" : "",
|
|
1336
|
+
onChange: (e) => setDraft(e.target.value),
|
|
1337
|
+
onBlur: commitEdit,
|
|
1338
|
+
onKeyDown: (e) => {
|
|
1339
|
+
if (e.key === "Enter") {
|
|
1340
|
+
e.preventDefault();
|
|
1341
|
+
commitEdit();
|
|
1342
|
+
moveFocus(1, 0, false);
|
|
1343
|
+
rootRef.current?.focus();
|
|
1344
|
+
} else if (e.key === "Tab") {
|
|
1345
|
+
e.preventDefault();
|
|
1346
|
+
commitEdit();
|
|
1347
|
+
moveFocus(0, e.shiftKey ? -1 : 1, false);
|
|
1348
|
+
rootRef.current?.focus();
|
|
1349
|
+
} else if (e.key === "Escape") {
|
|
1350
|
+
setEditing(null);
|
|
1351
|
+
rootRef.current?.focus();
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
);
|
|
1356
|
+
};
|
|
1357
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: "shg", ref: rootRef, tabIndex: 0, onKeyDown, onCopy: copySelection, onPaste: pasteAt, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: "shg-scroll", children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("table", { className: "shg-table", children: [
|
|
1358
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("thead", { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("tr", { children: [
|
|
1359
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("th", { className: "shg-corner", style: numStyle }),
|
|
1360
|
+
columns.map((col) => /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
1361
|
+
"th",
|
|
1362
|
+
{
|
|
1363
|
+
className: "shg-col" + (col.type === "compute" ? " shg-col-compute" : "") + dndClass("col", col.id),
|
|
1364
|
+
style: colWidth(col) ? { width: colWidth(col), minWidth: colWidth(col), maxWidth: colWidth(col) } : void 0,
|
|
1365
|
+
...dndProps("col", col.id),
|
|
1366
|
+
children: [
|
|
1367
|
+
!readOnly && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "shg-resize-col", onMouseDown: (e) => startResize(e, "col", col.id, colWidth(col) || e.currentTarget.closest("th").offsetWidth) }),
|
|
1368
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: "shg-col-h", children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
1369
|
+
ColumnHeader,
|
|
1370
|
+
{
|
|
1371
|
+
col,
|
|
1372
|
+
columns,
|
|
1373
|
+
readOnly,
|
|
1374
|
+
menuOpen: menuCol?.id === col.id,
|
|
1375
|
+
menuAnchor: menuCol?.id === col.id ? menuCol.rect : null,
|
|
1376
|
+
onMenu: (e) => setMenuCol(menuCol?.id === col.id ? null : { id: col.id, rect: e.currentTarget.getBoundingClientRect() }),
|
|
1377
|
+
onCloseMenu: () => setMenuCol(null),
|
|
1378
|
+
onRename: renameColumn,
|
|
1379
|
+
onConfigure: configureColumn,
|
|
1380
|
+
onDelete: () => {
|
|
1381
|
+
setMenuCol(null);
|
|
1382
|
+
deleteColumn(col.id);
|
|
1383
|
+
},
|
|
1384
|
+
onRun: col.type === "compute" && onRunColumn ? () => onRunColumn(col.id) : null,
|
|
1385
|
+
width: colWidth(col)
|
|
1386
|
+
}
|
|
1387
|
+
) })
|
|
1388
|
+
]
|
|
1389
|
+
},
|
|
1390
|
+
col.id
|
|
1391
|
+
)),
|
|
1392
|
+
!readOnly && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("th", { className: "shg-add-col", children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { className: "shg-add", onClick: addColumn, title: "Add column", children: "+" }) })
|
|
1393
|
+
] }) }),
|
|
1394
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("tbody", { children: [
|
|
1395
|
+
rows.map((row, ri) => /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
1396
|
+
"tr",
|
|
1397
|
+
{
|
|
1398
|
+
style: {
|
|
1399
|
+
...rowHeight(row) ? { height: rowHeight(row) } : null,
|
|
1400
|
+
"--shg-clamp": Math.max(1, Math.floor(((rowHeight(row) || 34) - 10) / 18))
|
|
1401
|
+
},
|
|
1402
|
+
children: [
|
|
1403
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
1404
|
+
"td",
|
|
1405
|
+
{
|
|
1406
|
+
className: "shg-rownum" + dndClass("row", row.id),
|
|
1407
|
+
style: numStyle,
|
|
1408
|
+
title: "Drag to reorder",
|
|
1409
|
+
...dndProps("row", row.id),
|
|
1410
|
+
children: [
|
|
1411
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { children: ri + 1 }),
|
|
1412
|
+
!readOnly && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { className: "shg-rowdel", onClick: () => deleteRow(row.id), title: "Delete row", children: "\xD7" }),
|
|
1413
|
+
!readOnly && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "shg-resize-row", onMouseDown: (e) => startResize(e, "row", row.id, rowHeight(row) || e.currentTarget.closest("tr").offsetHeight) })
|
|
1414
|
+
]
|
|
1415
|
+
}
|
|
1416
|
+
),
|
|
1417
|
+
columns.map((col, ci) => {
|
|
1418
|
+
const key = cellKey(row.id, col.id);
|
|
1419
|
+
const c = cells[key] || {};
|
|
1420
|
+
const isEditing = editing && editing.rowId === row.id && editing.colId === col.id;
|
|
1421
|
+
const peer = peerLayer.focus[key];
|
|
1422
|
+
const ghost = !isEditing && peerLayer.typing[key];
|
|
1423
|
+
const selected = inSel(ri, ci), focused = isFocus(ri, ci);
|
|
1424
|
+
const shadows = [];
|
|
1425
|
+
if (focused) shadows.push("inset 0 0 0 2px var(--brand, #0E7490)");
|
|
1426
|
+
if (peer) shadows.push(`inset 0 0 0 2px ${peer.color}`);
|
|
1427
|
+
const tint = !selected && peerLayer.tint[key];
|
|
1428
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
1429
|
+
"td",
|
|
1430
|
+
{
|
|
1431
|
+
"data-cell": key,
|
|
1432
|
+
className: "shg-cell" + (col.type === "compute" ? " shg-cell-compute" : "") + (c.status && col.type === "compute" ? ` shg-cell-${c.status}` : "") + (selected ? " shg-sel" : "") + (focused ? " shg-focus" : "") + (peer ? " shg-cell-peer" : ""),
|
|
1433
|
+
style: {
|
|
1434
|
+
...shadows.length ? { boxShadow: shadows.join(", ") } : null,
|
|
1435
|
+
...tint ? { background: withAlpha(tint, 0.1) } : null
|
|
1436
|
+
},
|
|
1437
|
+
onMouseDown: (e) => {
|
|
1438
|
+
if (e.button !== 0 || isEditing) return;
|
|
1439
|
+
if (editing) commitEdit();
|
|
1440
|
+
const p = { r: ri, c: ci };
|
|
1441
|
+
setSel((cur) => e.shiftKey && cur ? { a: cur.a, f: p } : { a: p, f: p });
|
|
1442
|
+
dragSel.current = true;
|
|
1443
|
+
e.preventDefault();
|
|
1444
|
+
rootRef.current?.focus();
|
|
1445
|
+
},
|
|
1446
|
+
onMouseEnter: () => {
|
|
1447
|
+
if (dragSel.current) setSel((cur) => cur ? { a: cur.a, f: { r: ri, c: ci } } : cur);
|
|
1448
|
+
},
|
|
1449
|
+
onClick: (e) => {
|
|
1450
|
+
if (col.type === "checkbox" && !isEditing) openEdit(row.id, col, e.currentTarget);
|
|
1451
|
+
},
|
|
1452
|
+
onDoubleClick: (e) => {
|
|
1453
|
+
if (!isEditing) openEdit(row.id, col, e.currentTarget);
|
|
1454
|
+
},
|
|
1455
|
+
title: c.error || "",
|
|
1456
|
+
children: [
|
|
1457
|
+
peer && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "shg-peer-flag", style: { background: peer.color }, children: peer.name }),
|
|
1458
|
+
isEditing ? renderEditor(col) : /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
1459
|
+
"div",
|
|
1460
|
+
{
|
|
1461
|
+
className: "shg-val",
|
|
1462
|
+
style: { maxWidth: colWidth(col) ? colWidth(col) - 20 : 400 },
|
|
1463
|
+
children: [
|
|
1464
|
+
col.type === "compute" && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(StatusDot, { status: c.status }),
|
|
1465
|
+
ghost ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "shg-ghost", style: { color: ghost.color }, title: `${ghost.name} is typing`, children: ghost.text }) : /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(CellValue, { cell: c, col, fetchBlobUrl, onOpenResource }),
|
|
1466
|
+
col.type === "compute" && !readOnly && onRunCell && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
1467
|
+
"button",
|
|
1468
|
+
{
|
|
1469
|
+
className: "shg-runcell",
|
|
1470
|
+
title: "Run this cell",
|
|
1471
|
+
onClick: (e) => {
|
|
1472
|
+
e.stopPropagation();
|
|
1473
|
+
onRunCell(row.id, col.id);
|
|
1474
|
+
},
|
|
1475
|
+
children: "\u25B6"
|
|
1476
|
+
}
|
|
1477
|
+
)
|
|
1478
|
+
]
|
|
1479
|
+
}
|
|
1480
|
+
)
|
|
1481
|
+
]
|
|
1482
|
+
},
|
|
1483
|
+
col.id
|
|
1484
|
+
);
|
|
1485
|
+
}),
|
|
1486
|
+
!readOnly && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("td", { className: "shg-pad" })
|
|
1487
|
+
]
|
|
1488
|
+
},
|
|
1489
|
+
row.id
|
|
1490
|
+
)),
|
|
1491
|
+
!readOnly && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("tr", { className: "shg-ghost-row", onClick: addRow, children: [
|
|
1492
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("td", { className: "shg-rownum shg-ghost-num", style: numStyle, children: "+" }),
|
|
1493
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("td", { className: "shg-ghost-cell", colSpan: columns.length + 1, children: "New row" })
|
|
1494
|
+
] })
|
|
1495
|
+
] })
|
|
1496
|
+
] }) }) });
|
|
1497
|
+
}
|
|
1498
|
+
function ColumnHeader({ col, columns, readOnly, menuOpen, menuAnchor, onMenu, onCloseMenu, onRename, onConfigure, onDelete, onRun, width }) {
|
|
1499
|
+
const [editing, setEditing] = (0, import_react4.useState)(false);
|
|
1500
|
+
const [name, setName] = (0, import_react4.useState)(col.name);
|
|
1501
|
+
(0, import_react4.useEffect)(() => setName(col.name), [col.name]);
|
|
1502
|
+
const w = Number(width) || 0;
|
|
1503
|
+
const fit = w && w < 160 ? " tight" : w && w < 200 ? " narrow" : "";
|
|
1504
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "shg-colhdr" + fit, children: [
|
|
1505
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "shg-colhdr-main", children: [
|
|
1506
|
+
editing && !readOnly ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
1507
|
+
"input",
|
|
1508
|
+
{
|
|
1509
|
+
className: "shg-colname-input",
|
|
1510
|
+
value: name,
|
|
1511
|
+
autoFocus: true,
|
|
1512
|
+
onChange: (e) => setName(e.target.value),
|
|
1513
|
+
onBlur: () => {
|
|
1514
|
+
setEditing(false);
|
|
1515
|
+
if (name.trim() && name !== col.name) onRename(col.id, name.trim());
|
|
1516
|
+
},
|
|
1517
|
+
onKeyDown: (e) => {
|
|
1518
|
+
if (e.key === "Enter") e.target.blur();
|
|
1519
|
+
}
|
|
1520
|
+
}
|
|
1521
|
+
) : /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "shg-colname", onDoubleClick: () => !readOnly && setEditing(true), title: col.name, children: col.name }),
|
|
1522
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("span", { className: "shg-coltype" + (col.type === "compute" ? " compute" : ""), children: [
|
|
1523
|
+
TYPE_LABEL[col.type] || col.type,
|
|
1524
|
+
col.type === "compute" && col.compute?.kind ? ` \xB7 ${col.compute.kind}` : ""
|
|
1525
|
+
] })
|
|
1526
|
+
] }),
|
|
1527
|
+
!readOnly && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "shg-colhdr-tools", children: [
|
|
1528
|
+
onRun && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("button", { className: "shg-colrun", onClick: onRun, title: "Run this column", "aria-label": "Run this column", children: [
|
|
1529
|
+
"\u25B6",
|
|
1530
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "shg-colrun-label", children: " Run" })
|
|
1531
|
+
] }),
|
|
1532
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { className: "shg-colmenu-btn", onClick: (e) => onMenu(e), title: "Column settings", children: "\u22EF" })
|
|
1533
|
+
] }),
|
|
1534
|
+
menuOpen && !readOnly && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
1535
|
+
ColumnMenu,
|
|
1536
|
+
{
|
|
1537
|
+
col,
|
|
1538
|
+
columns,
|
|
1539
|
+
anchor: menuAnchor,
|
|
1540
|
+
onApply: (patch) => {
|
|
1541
|
+
onConfigure(col.id, patch);
|
|
1542
|
+
onCloseMenu();
|
|
1543
|
+
},
|
|
1544
|
+
onDelete,
|
|
1545
|
+
onClose: onCloseMenu
|
|
1546
|
+
}
|
|
1547
|
+
)
|
|
1548
|
+
] });
|
|
1549
|
+
}
|
|
1550
|
+
function ValuePills({ values, onChange }) {
|
|
1551
|
+
const [text, setText] = (0, import_react4.useState)("");
|
|
1552
|
+
const [picking, setPicking] = (0, import_react4.useState)(null);
|
|
1553
|
+
const add = () => {
|
|
1554
|
+
const label = text.trim();
|
|
1555
|
+
if (!label) return;
|
|
1556
|
+
if (!values.some((v) => sameValue(v.label, label))) onChange([...values, { label }]);
|
|
1557
|
+
setText("");
|
|
1558
|
+
};
|
|
1559
|
+
const remove = (i) => {
|
|
1560
|
+
onChange(values.filter((_, j) => j !== i));
|
|
1561
|
+
setPicking(null);
|
|
1562
|
+
};
|
|
1563
|
+
const paint = (i, color) => {
|
|
1564
|
+
onChange(values.map((v, j) => j === i ? { ...v, color } : v));
|
|
1565
|
+
setPicking(null);
|
|
1566
|
+
};
|
|
1567
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "shg-values", onClick: (e) => {
|
|
1568
|
+
if (e.target === e.currentTarget) e.currentTarget.querySelector("input")?.focus();
|
|
1569
|
+
}, children: [
|
|
1570
|
+
values.map((v, i) => /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("span", { className: "shg-value", children: [
|
|
1571
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
1572
|
+
"button",
|
|
1573
|
+
{
|
|
1574
|
+
type: "button",
|
|
1575
|
+
className: "shg-pill shg-pill-btn",
|
|
1576
|
+
style: { background: v.color || PILL_FALLBACKS[i % PILL_FALLBACKS.length] },
|
|
1577
|
+
title: "Pick a color",
|
|
1578
|
+
"aria-expanded": picking === i,
|
|
1579
|
+
onClick: (e) => {
|
|
1580
|
+
e.stopPropagation();
|
|
1581
|
+
setPicking(picking === i ? null : i);
|
|
1582
|
+
},
|
|
1583
|
+
children: [
|
|
1584
|
+
v.label,
|
|
1585
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
1586
|
+
"span",
|
|
1587
|
+
{
|
|
1588
|
+
className: "shg-value-x",
|
|
1589
|
+
role: "button",
|
|
1590
|
+
"aria-label": `Remove ${v.label}`,
|
|
1591
|
+
onClick: (e) => {
|
|
1592
|
+
e.stopPropagation();
|
|
1593
|
+
remove(i);
|
|
1594
|
+
},
|
|
1595
|
+
children: "\xD7"
|
|
1596
|
+
}
|
|
1597
|
+
)
|
|
1598
|
+
]
|
|
1599
|
+
}
|
|
1600
|
+
),
|
|
1601
|
+
picking === i && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: "shg-swatches", role: "listbox", "aria-label": `Color for ${v.label}`, children: PILL_PALETTE.map((p) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
1602
|
+
"button",
|
|
1603
|
+
{
|
|
1604
|
+
type: "button",
|
|
1605
|
+
role: "option",
|
|
1606
|
+
"aria-selected": v.color === p.color,
|
|
1607
|
+
className: "shg-swatch" + (v.color === p.color ? " on" : ""),
|
|
1608
|
+
style: { background: p.color },
|
|
1609
|
+
title: p.name,
|
|
1610
|
+
onClick: (e) => {
|
|
1611
|
+
e.stopPropagation();
|
|
1612
|
+
paint(i, p.color);
|
|
1613
|
+
}
|
|
1614
|
+
},
|
|
1615
|
+
p.name
|
|
1616
|
+
)) })
|
|
1617
|
+
] }, v.label)),
|
|
1618
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
1619
|
+
"input",
|
|
1620
|
+
{
|
|
1621
|
+
className: "shg-values-input",
|
|
1622
|
+
value: text,
|
|
1623
|
+
placeholder: values.length ? "" : "Type a value, press Enter",
|
|
1624
|
+
onChange: (e) => setText(e.target.value),
|
|
1625
|
+
onKeyDown: (e) => {
|
|
1626
|
+
if (e.key === "Enter" || e.key === ",") {
|
|
1627
|
+
e.preventDefault();
|
|
1628
|
+
add();
|
|
1629
|
+
} else if (e.key === "Backspace" && !text && values.length) remove(values.length - 1);
|
|
1630
|
+
},
|
|
1631
|
+
onBlur: add
|
|
1632
|
+
}
|
|
1633
|
+
)
|
|
1634
|
+
] });
|
|
1635
|
+
}
|
|
1636
|
+
function ColumnMenu({ col, columns, anchor, onApply, onDelete, onClose }) {
|
|
1637
|
+
const [type, setType] = (0, import_react4.useState)(col.type || "text");
|
|
1638
|
+
const [values, setValues] = (0, import_react4.useState)(() => (col.options || []).map((o) => typeof o === "string" ? { label: o } : { label: o.label ?? String(o.value ?? ""), ...o.color ? { color: o.color } : {} }));
|
|
1639
|
+
const comp = col.compute || {};
|
|
1640
|
+
const [kind, setKind] = (0, import_react4.useState)(comp.kind || "prompt");
|
|
1641
|
+
const [prompt, setPrompt] = (0, import_react4.useState)(comp.prompt || "");
|
|
1642
|
+
const [harnessId, setHarnessId] = (0, import_react4.useState)(comp.harness_id || "");
|
|
1643
|
+
const [wfSlug, setWfSlug] = (0, import_react4.useState)(comp.workflow_slug || "");
|
|
1644
|
+
const [outKey, setOutKey] = (0, import_react4.useState)(comp.output_key || "");
|
|
1645
|
+
const [deps, setDeps] = (0, import_react4.useState)(new Set(comp.deps || []));
|
|
1646
|
+
const ref = (0, import_react4.useRef)(null);
|
|
1647
|
+
(0, import_react4.useEffect)(() => {
|
|
1648
|
+
const onDown = (e) => {
|
|
1649
|
+
if (ref.current && !ref.current.contains(e.target) && !(e.target.closest && e.target.closest(".uic-pop"))) onClose();
|
|
1650
|
+
};
|
|
1651
|
+
window.addEventListener("mousedown", onDown);
|
|
1652
|
+
return () => window.removeEventListener("mousedown", onDown);
|
|
1653
|
+
}, [onClose]);
|
|
1654
|
+
const promptItems = (0, import_react4.useMemo)(() => {
|
|
1655
|
+
const at = columns.findIndex((c) => c.id === col.id);
|
|
1656
|
+
return columns.filter((c) => c.id !== col.id).map((c) => {
|
|
1657
|
+
const idx = columns.findIndex((x) => x.id === c.id);
|
|
1658
|
+
const right = at >= 0 && idx > at;
|
|
1659
|
+
return {
|
|
1660
|
+
id: c.id,
|
|
1661
|
+
name: c.name,
|
|
1662
|
+
kind: c.type,
|
|
1663
|
+
section: right ? "Fills after this column" : "Columns",
|
|
1664
|
+
icon: TYPE_GLYPH[c.type] || "Aa",
|
|
1665
|
+
description: TYPE_LABEL[c.type] || c.type,
|
|
1666
|
+
color: c.type === "compute" ? "#7C3AED" : void 0,
|
|
1667
|
+
disabled: right ? "to the right" : false
|
|
1668
|
+
};
|
|
1669
|
+
});
|
|
1670
|
+
}, [columns, col.id]);
|
|
1671
|
+
const apply2 = () => {
|
|
1672
|
+
const patch = { type };
|
|
1673
|
+
patch.options = values.map((v) => v.color ? { label: v.label, color: v.color } : v.label);
|
|
1674
|
+
if (type === "compute") {
|
|
1675
|
+
const compute = { kind, deps: [...deps] };
|
|
1676
|
+
if (kind === "prompt") compute.prompt = prompt;
|
|
1677
|
+
if (kind === "harness") {
|
|
1678
|
+
compute.harness_id = harnessId;
|
|
1679
|
+
compute.prompt = prompt;
|
|
1680
|
+
}
|
|
1681
|
+
if (kind === "workflow") {
|
|
1682
|
+
compute.workflow_slug = wfSlug;
|
|
1683
|
+
compute.output_key = outKey;
|
|
1684
|
+
}
|
|
1685
|
+
patch.compute = compute;
|
|
1686
|
+
}
|
|
1687
|
+
onApply(patch);
|
|
1688
|
+
};
|
|
1689
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(PopPortal, { anchor, width: 250, estHeight: 340, className: "shg-menu", onClose, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { ref, onClick: (e) => e.stopPropagation(), children: [
|
|
1690
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("label", { className: "shg-menu-lbl", children: [
|
|
1691
|
+
"Type",
|
|
1692
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("select", { className: "shg-menu-input", value: type, onChange: (e) => setType(e.target.value), children: [
|
|
1693
|
+
MANUAL_TYPES.map((t) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("option", { value: t, children: TYPE_LABEL[t] }, t)),
|
|
1694
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("option", { value: "compute", children: "Computed" })
|
|
1695
|
+
] })
|
|
1696
|
+
] }),
|
|
1697
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "shg-menu-lbl", children: [
|
|
1698
|
+
type === "select" || type === "tags" ? "Options" : "Values",
|
|
1699
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ValuePills, { values, onChange: setValues })
|
|
1700
|
+
] }),
|
|
1701
|
+
type === "compute" && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_jsx_runtime3.Fragment, { children: [
|
|
1702
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("label", { className: "shg-menu-lbl", children: [
|
|
1703
|
+
"Runs",
|
|
1704
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("select", { className: "shg-menu-input", value: kind, onChange: (e) => setKind(e.target.value), children: COMPUTE_KINDS.map((k) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("option", { value: k.kind, children: k.label }, k.kind)) })
|
|
1705
|
+
] }),
|
|
1706
|
+
(kind === "prompt" || kind === "harness") && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "shg-menu-lbl", children: [
|
|
1707
|
+
"Prompt",
|
|
1708
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: "shg-menu-input shg-menu-prompt", children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
1709
|
+
MentionInput,
|
|
1710
|
+
{
|
|
1711
|
+
value: prompt,
|
|
1712
|
+
onChange: (v, ms) => {
|
|
1713
|
+
setPrompt(v);
|
|
1714
|
+
setDeps(new Set(ms.map((m) => m.id)));
|
|
1715
|
+
},
|
|
1716
|
+
items: promptItems,
|
|
1717
|
+
placeholder: "Summarize @Company in one line",
|
|
1718
|
+
menuTitle: "Columns this cell can read",
|
|
1719
|
+
emptyText: "No column by that name",
|
|
1720
|
+
label: "Prompt"
|
|
1721
|
+
}
|
|
1722
|
+
) }),
|
|
1723
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: "shg-menu-hint", children: "Type @ to add a column from this row. Columns to the left fill first." })
|
|
1724
|
+
] }),
|
|
1725
|
+
kind === "harness" && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("label", { className: "shg-menu-lbl", children: [
|
|
1726
|
+
"Agent id",
|
|
1727
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("input", { className: "shg-menu-input", value: harnessId, onChange: (e) => setHarnessId(e.target.value), placeholder: "chrn_\u2026" })
|
|
1728
|
+
] }),
|
|
1729
|
+
kind === "workflow" && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_jsx_runtime3.Fragment, { children: [
|
|
1730
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("label", { className: "shg-menu-lbl", children: [
|
|
1731
|
+
"Workflow id",
|
|
1732
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("input", { className: "shg-menu-input", value: wfSlug, onChange: (e) => setWfSlug(e.target.value), placeholder: "my-workflow" })
|
|
1733
|
+
] }),
|
|
1734
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("label", { className: "shg-menu-lbl", children: [
|
|
1735
|
+
"Output key (optional)",
|
|
1736
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("input", { className: "shg-menu-input", value: outKey, onChange: (e) => setOutKey(e.target.value), placeholder: "summary" })
|
|
1737
|
+
] })
|
|
1738
|
+
] })
|
|
1739
|
+
] }),
|
|
1740
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: "shg-menu-actions", children: [
|
|
1741
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { className: "shg-menu-del", onClick: onDelete, children: "Delete column" }),
|
|
1742
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)("button", { className: "shg-menu-apply", onClick: apply2, children: "Apply" })
|
|
1743
|
+
] })
|
|
1744
|
+
] }) });
|
|
1745
|
+
}
|
|
1746
|
+
function sheetToDelimited(sheet, sep = ",") {
|
|
1747
|
+
const cols = sheet?.columns || [];
|
|
1748
|
+
const rows = sheet?.rows || [];
|
|
1749
|
+
const cells = sheet?.cells || {};
|
|
1750
|
+
const esc = (v) => {
|
|
1751
|
+
const s = v == null ? "" : Array.isArray(v) ? v.join(", ") : typeof v === "object" ? JSON.stringify(v) : String(v);
|
|
1752
|
+
if (sep === "," && /[",\n]/.test(s)) return `"${s.replace(/"/g, '""')}"`;
|
|
1753
|
+
return s;
|
|
1754
|
+
};
|
|
1755
|
+
const header = cols.map((c) => esc(c.name)).join(sep);
|
|
1756
|
+
const body = rows.map((r) => cols.map((c) => {
|
|
1757
|
+
const cell = cells[cellKey(r.id, c.id)];
|
|
1758
|
+
return esc(cell ? cell.value : "");
|
|
1759
|
+
}).join(sep));
|
|
1760
|
+
return [header, ...body].join("\n");
|
|
1761
|
+
}
|
|
1762
|
+
function sheetToAoA(sheet) {
|
|
1763
|
+
const cols = sheet?.columns || [];
|
|
1764
|
+
const rows = sheet?.rows || [];
|
|
1765
|
+
const cells = sheet?.cells || {};
|
|
1766
|
+
const val = (v) => v == null ? "" : Array.isArray(v) ? v.join(", ") : typeof v === "object" ? JSON.stringify(v) : v;
|
|
1767
|
+
return [
|
|
1768
|
+
cols.map((c) => c.name),
|
|
1769
|
+
...rows.map((r) => cols.map((c) => val(cells[cellKey(r.id, c.id)]?.value)))
|
|
1770
|
+
];
|
|
1771
|
+
}
|
|
1772
|
+
|
|
1773
|
+
// src/sheet/useSheetCollab.js
|
|
1774
|
+
var import_react6 = require("react");
|
|
1775
|
+
|
|
1776
|
+
// src/sheet/sheetDoc.js
|
|
1777
|
+
var sheetDoc_exports = {};
|
|
1778
|
+
__export(sheetDoc_exports, {
|
|
1779
|
+
SHEET_PREFIX: () => SHEET_PREFIX,
|
|
1780
|
+
apply: () => apply,
|
|
1781
|
+
isSheetDoc: () => isSheetDoc,
|
|
1782
|
+
orderOf: () => orderOf,
|
|
1783
|
+
seed: () => seed,
|
|
1784
|
+
sheetIdOf: () => sheetIdOf,
|
|
1785
|
+
toJSON: () => toJSON
|
|
1786
|
+
});
|
|
1787
|
+
var SHEET_PREFIX = "cg-sheet:";
|
|
1788
|
+
var isSheetDoc = (name) => typeof name === "string" && name.startsWith(SHEET_PREFIX);
|
|
1789
|
+
var sheetIdOf = (name) => name.slice(SHEET_PREFIX.length);
|
|
1790
|
+
var map = (doc, name) => doc.getMap(name);
|
|
1791
|
+
var arr = (doc, name) => doc.getArray(name);
|
|
1792
|
+
function orderOf(order, m) {
|
|
1793
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1794
|
+
const out = [];
|
|
1795
|
+
for (const id of order.toArray()) {
|
|
1796
|
+
if (m.has(id) && !seen.has(id)) {
|
|
1797
|
+
seen.add(id);
|
|
1798
|
+
out.push(id);
|
|
1799
|
+
}
|
|
1800
|
+
}
|
|
1801
|
+
for (const id of m.keys()) if (!seen.has(id)) {
|
|
1802
|
+
seen.add(id);
|
|
1803
|
+
out.push(id);
|
|
1804
|
+
}
|
|
1805
|
+
return out;
|
|
1806
|
+
}
|
|
1807
|
+
function clearMap(m) {
|
|
1808
|
+
for (const k of [...m.keys()]) m.delete(k);
|
|
1809
|
+
}
|
|
1810
|
+
function clearArray(a) {
|
|
1811
|
+
if (a.length) a.delete(0, a.length);
|
|
1812
|
+
}
|
|
1813
|
+
function removeFromOrder(order, id) {
|
|
1814
|
+
const items = order.toArray();
|
|
1815
|
+
for (let i = items.length - 1; i >= 0; i--) if (items[i] === id) order.delete(i, 1);
|
|
1816
|
+
}
|
|
1817
|
+
function placeInOrder(order, id, at) {
|
|
1818
|
+
removeFromOrder(order, id);
|
|
1819
|
+
const n = order.length;
|
|
1820
|
+
const i = Number.isInteger(at) ? Math.max(0, Math.min(at, n)) : n;
|
|
1821
|
+
order.insert(i, [id]);
|
|
1822
|
+
}
|
|
1823
|
+
function tabIds(doc) {
|
|
1824
|
+
return orderOf(arr(doc, "taborder"), map(doc, "tabs"));
|
|
1825
|
+
}
|
|
1826
|
+
function clearTab(doc, tabId) {
|
|
1827
|
+
clearMap(map(doc, `cols:${tabId}`));
|
|
1828
|
+
clearArray(arr(doc, `colorder:${tabId}`));
|
|
1829
|
+
clearMap(map(doc, `rows:${tabId}`));
|
|
1830
|
+
clearArray(arr(doc, `roworder:${tabId}`));
|
|
1831
|
+
clearMap(map(doc, `cells:${tabId}`));
|
|
1832
|
+
}
|
|
1833
|
+
function fillTab(doc, tab) {
|
|
1834
|
+
const cols = map(doc, `cols:${tab.id}`), colorder = arr(doc, `colorder:${tab.id}`);
|
|
1835
|
+
const rows = map(doc, `rows:${tab.id}`), roworder = arr(doc, `roworder:${tab.id}`);
|
|
1836
|
+
const cells = map(doc, `cells:${tab.id}`);
|
|
1837
|
+
for (const c of tab.columns || []) {
|
|
1838
|
+
cols.set(c.id, c);
|
|
1839
|
+
colorder.push([c.id]);
|
|
1840
|
+
}
|
|
1841
|
+
for (const r of tab.rows || []) {
|
|
1842
|
+
rows.set(r.id, r);
|
|
1843
|
+
roworder.push([r.id]);
|
|
1844
|
+
}
|
|
1845
|
+
for (const [k, v] of Object.entries(tab.cells || {})) if (v !== null && v !== void 0) cells.set(k, v);
|
|
1846
|
+
}
|
|
1847
|
+
function seed(doc, sheet, origin = "seed") {
|
|
1848
|
+
doc.transact(() => {
|
|
1849
|
+
for (const id of tabIds(doc)) clearTab(doc, id);
|
|
1850
|
+
clearMap(map(doc, "tabs"));
|
|
1851
|
+
clearArray(arr(doc, "taborder"));
|
|
1852
|
+
const meta = map(doc, "meta");
|
|
1853
|
+
meta.set("title", String((sheet.meta || {}).title || ""));
|
|
1854
|
+
for (const tab of sheet.tabs || []) {
|
|
1855
|
+
map(doc, "tabs").set(tab.id, { id: tab.id, name: tab.name });
|
|
1856
|
+
arr(doc, "taborder").push([tab.id]);
|
|
1857
|
+
fillTab(doc, tab);
|
|
1858
|
+
}
|
|
1859
|
+
}, origin);
|
|
1860
|
+
}
|
|
1861
|
+
function toJSON(doc) {
|
|
1862
|
+
const meta = map(doc, "meta");
|
|
1863
|
+
const tabs = tabIds(doc).map((id) => {
|
|
1864
|
+
const t = map(doc, "tabs").get(id) || { id, name: "Sheet" };
|
|
1865
|
+
const cols = map(doc, `cols:${id}`), rows = map(doc, `rows:${id}`), cells = map(doc, `cells:${id}`);
|
|
1866
|
+
const columns = orderOf(arr(doc, `colorder:${id}`), cols).map((cid) => cols.get(cid));
|
|
1867
|
+
const rowList = orderOf(arr(doc, `roworder:${id}`), rows).map((rid) => rows.get(rid));
|
|
1868
|
+
const present = /* @__PURE__ */ new Set([...columns.map((c) => c.id)]);
|
|
1869
|
+
const rowSet = new Set(rowList.map((r) => r.id));
|
|
1870
|
+
const out = {};
|
|
1871
|
+
for (const [k, v] of cells.entries()) {
|
|
1872
|
+
const i = k.indexOf(":");
|
|
1873
|
+
if (i > 0 && rowSet.has(k.slice(0, i)) && present.has(k.slice(i + 1))) out[k] = v;
|
|
1874
|
+
}
|
|
1875
|
+
return { id, name: t.name, columns, rows: rowList, cells: out };
|
|
1876
|
+
});
|
|
1877
|
+
return { meta: { title: meta.get("title") || "" }, tabs };
|
|
1878
|
+
}
|
|
1879
|
+
function apply(doc, ops, origin = "service") {
|
|
1880
|
+
if (!Array.isArray(ops)) throw new Error("ops must be a list");
|
|
1881
|
+
for (const op of ops) validateOp(op);
|
|
1882
|
+
doc.transact(() => {
|
|
1883
|
+
for (const op of ops) applyOne(doc, op);
|
|
1884
|
+
}, origin);
|
|
1885
|
+
}
|
|
1886
|
+
var OPS = /* @__PURE__ */ new Set([
|
|
1887
|
+
"set_cell",
|
|
1888
|
+
"set_cells",
|
|
1889
|
+
"set_column",
|
|
1890
|
+
"delete_column",
|
|
1891
|
+
"move_column",
|
|
1892
|
+
"set_row",
|
|
1893
|
+
"delete_row",
|
|
1894
|
+
"move_row",
|
|
1895
|
+
"set_tab",
|
|
1896
|
+
"delete_tab",
|
|
1897
|
+
"move_tab",
|
|
1898
|
+
"set_meta",
|
|
1899
|
+
"replace"
|
|
1900
|
+
]);
|
|
1901
|
+
function validateOp(op) {
|
|
1902
|
+
if (!op || typeof op !== "object" || !OPS.has(op.op)) throw new Error(`unknown op ${op && op.op}`);
|
|
1903
|
+
const needTab = !["set_tab", "delete_tab", "move_tab", "set_meta", "replace"].includes(op.op);
|
|
1904
|
+
if (needTab && typeof op.tab !== "string") throw new Error(`${op.op}: tab required`);
|
|
1905
|
+
if (op.op === "set_cell" && typeof op.key !== "string") throw new Error("set_cell: key required");
|
|
1906
|
+
if (op.op === "set_cells" && (!op.cells || typeof op.cells !== "object")) throw new Error("set_cells: cells required");
|
|
1907
|
+
if (op.op === "set_column" && !(op.column && typeof op.column.id === "string")) throw new Error("set_column: column.id required");
|
|
1908
|
+
if (op.op === "set_row" && !(op.row && typeof op.row.id === "string")) throw new Error("set_row: row.id required");
|
|
1909
|
+
if (op.op === "set_tab" && !(op.tab && typeof op.tab.id === "string")) throw new Error("set_tab: tab.id required");
|
|
1910
|
+
if ((op.op === "delete_column" || op.op === "move_column" || op.op === "delete_row" || op.op === "move_row" || op.op === "delete_tab" || op.op === "move_tab") && typeof op.id !== "string") throw new Error(`${op.op}: id required`);
|
|
1911
|
+
if (op.op === "set_meta" && typeof op.key !== "string") throw new Error("set_meta: key required");
|
|
1912
|
+
if (op.op === "replace" && !(op.sheet && typeof op.sheet === "object")) throw new Error("replace: sheet required");
|
|
1913
|
+
}
|
|
1914
|
+
function applyOne(doc, op) {
|
|
1915
|
+
switch (op.op) {
|
|
1916
|
+
case "set_cell": {
|
|
1917
|
+
const cells = map(doc, `cells:${op.tab}`);
|
|
1918
|
+
if (op.cell === null || op.cell === void 0) cells.delete(op.key);
|
|
1919
|
+
else cells.set(op.key, op.cell);
|
|
1920
|
+
return;
|
|
1921
|
+
}
|
|
1922
|
+
case "set_cells": {
|
|
1923
|
+
const cells = map(doc, `cells:${op.tab}`);
|
|
1924
|
+
for (const [k, v] of Object.entries(op.cells)) {
|
|
1925
|
+
if (v === null || v === void 0) cells.delete(k);
|
|
1926
|
+
else cells.set(k, v);
|
|
1927
|
+
}
|
|
1928
|
+
return;
|
|
1929
|
+
}
|
|
1930
|
+
case "set_column": {
|
|
1931
|
+
map(doc, `cols:${op.tab}`).set(op.column.id, op.column);
|
|
1932
|
+
const order = arr(doc, `colorder:${op.tab}`);
|
|
1933
|
+
if (Number.isInteger(op.at) || !order.toArray().includes(op.column.id)) placeInOrder(order, op.column.id, op.at);
|
|
1934
|
+
return;
|
|
1935
|
+
}
|
|
1936
|
+
case "delete_column": {
|
|
1937
|
+
map(doc, `cols:${op.tab}`).delete(op.id);
|
|
1938
|
+
removeFromOrder(arr(doc, `colorder:${op.tab}`), op.id);
|
|
1939
|
+
const cells = map(doc, `cells:${op.tab}`);
|
|
1940
|
+
for (const k of [...cells.keys()]) if (k.endsWith(`:${op.id}`)) cells.delete(k);
|
|
1941
|
+
return;
|
|
1942
|
+
}
|
|
1943
|
+
case "move_column":
|
|
1944
|
+
placeInOrder(arr(doc, `colorder:${op.tab}`), op.id, op.at);
|
|
1945
|
+
return;
|
|
1946
|
+
case "set_row": {
|
|
1947
|
+
map(doc, `rows:${op.tab}`).set(op.row.id, op.row);
|
|
1948
|
+
const order = arr(doc, `roworder:${op.tab}`);
|
|
1949
|
+
if (Number.isInteger(op.at) || !order.toArray().includes(op.row.id)) placeInOrder(order, op.row.id, op.at);
|
|
1950
|
+
return;
|
|
1951
|
+
}
|
|
1952
|
+
case "delete_row": {
|
|
1953
|
+
map(doc, `rows:${op.tab}`).delete(op.id);
|
|
1954
|
+
removeFromOrder(arr(doc, `roworder:${op.tab}`), op.id);
|
|
1955
|
+
const cells = map(doc, `cells:${op.tab}`);
|
|
1956
|
+
for (const k of [...cells.keys()]) if (k.startsWith(`${op.id}:`)) cells.delete(k);
|
|
1957
|
+
return;
|
|
1958
|
+
}
|
|
1959
|
+
case "move_row":
|
|
1960
|
+
placeInOrder(arr(doc, `roworder:${op.tab}`), op.id, op.at);
|
|
1961
|
+
return;
|
|
1962
|
+
case "set_tab": {
|
|
1963
|
+
const t = op.tab;
|
|
1964
|
+
map(doc, "tabs").set(t.id, { id: t.id, name: String(t.name || "Sheet") });
|
|
1965
|
+
const order = arr(doc, "taborder");
|
|
1966
|
+
if (Number.isInteger(op.at) || !order.toArray().includes(t.id)) placeInOrder(order, t.id, op.at);
|
|
1967
|
+
return;
|
|
1968
|
+
}
|
|
1969
|
+
case "delete_tab": {
|
|
1970
|
+
map(doc, "tabs").delete(op.id);
|
|
1971
|
+
removeFromOrder(arr(doc, "taborder"), op.id);
|
|
1972
|
+
clearTab(doc, op.id);
|
|
1973
|
+
return;
|
|
1974
|
+
}
|
|
1975
|
+
case "move_tab":
|
|
1976
|
+
placeInOrder(arr(doc, "taborder"), op.id, op.at);
|
|
1977
|
+
return;
|
|
1978
|
+
case "set_meta": {
|
|
1979
|
+
const meta = map(doc, "meta");
|
|
1980
|
+
if (op.value === null || op.value === void 0) meta.delete(op.key);
|
|
1981
|
+
else meta.set(op.key, op.value);
|
|
1982
|
+
return;
|
|
1983
|
+
}
|
|
1984
|
+
case "replace":
|
|
1985
|
+
seed(doc, op.sheet, "service");
|
|
1986
|
+
return;
|
|
1987
|
+
default:
|
|
1988
|
+
throw new Error(`unknown op ${op.op}`);
|
|
1989
|
+
}
|
|
1990
|
+
}
|
|
1991
|
+
|
|
1992
|
+
// src/collab/useLiveDoc.js
|
|
1993
|
+
var import_react5 = require("react");
|
|
1994
|
+
var import_provider = require("@hocuspocus/provider");
|
|
1995
|
+
|
|
1996
|
+
// src/collab/keepAlive.js
|
|
1997
|
+
var TICK_MS = 1e4;
|
|
1998
|
+
function keepPresenceAlive(awareness, intervalMs = TICK_MS) {
|
|
1999
|
+
if (!awareness || typeof Worker === "undefined" || typeof Blob === "undefined") return () => {
|
|
2000
|
+
};
|
|
2001
|
+
let url = "";
|
|
2002
|
+
let worker = null;
|
|
2003
|
+
try {
|
|
2004
|
+
url = URL.createObjectURL(new Blob(
|
|
2005
|
+
[`setInterval(() => postMessage(0), ${Math.max(1e3, intervalMs)});`],
|
|
2006
|
+
{ type: "text/javascript" }
|
|
2007
|
+
));
|
|
2008
|
+
worker = new Worker(url);
|
|
2009
|
+
} catch {
|
|
2010
|
+
if (url) URL.revokeObjectURL(url);
|
|
2011
|
+
return () => {
|
|
2012
|
+
};
|
|
2013
|
+
}
|
|
2014
|
+
worker.onmessage = () => {
|
|
2015
|
+
const state = awareness.getLocalState();
|
|
2016
|
+
if (state !== null && state !== void 0) awareness.setLocalState(state);
|
|
2017
|
+
};
|
|
2018
|
+
return () => {
|
|
2019
|
+
worker.terminate();
|
|
2020
|
+
URL.revokeObjectURL(url);
|
|
2021
|
+
};
|
|
2022
|
+
}
|
|
2023
|
+
|
|
2024
|
+
// src/collab/useLiveDoc.js
|
|
2025
|
+
function useLiveDoc(resourceId, { fetchTicket, field = "presence" } = {}) {
|
|
2026
|
+
const [doc, setDoc] = (0, import_react5.useState)(null);
|
|
2027
|
+
const [provider, setProvider] = (0, import_react5.useState)(null);
|
|
2028
|
+
const [ready, setReady] = (0, import_react5.useState)(false);
|
|
2029
|
+
const [states, setStates] = (0, import_react5.useState)([]);
|
|
2030
|
+
const [selfClientId, setSelfClientId] = (0, import_react5.useState)(null);
|
|
2031
|
+
const providerRef = (0, import_react5.useRef)(null);
|
|
2032
|
+
const lastRef = (0, import_react5.useRef)(null);
|
|
2033
|
+
(0, import_react5.useEffect)(() => {
|
|
2034
|
+
if (!resourceId || typeof fetchTicket !== "function") return void 0;
|
|
2035
|
+
let dead = false;
|
|
2036
|
+
let current = null;
|
|
2037
|
+
let retry = null;
|
|
2038
|
+
let stopKeepAlive = () => {
|
|
2039
|
+
};
|
|
2040
|
+
async function connect() {
|
|
2041
|
+
let ticket = null;
|
|
2042
|
+
try {
|
|
2043
|
+
ticket = await fetchTicket(resourceId);
|
|
2044
|
+
} catch {
|
|
2045
|
+
ticket = null;
|
|
2046
|
+
}
|
|
2047
|
+
if (dead) return;
|
|
2048
|
+
if (!ticket) {
|
|
2049
|
+
retry = window.setTimeout(connect, 6e4);
|
|
2050
|
+
return;
|
|
2051
|
+
}
|
|
2052
|
+
current = new import_provider.HocuspocusProvider({
|
|
2053
|
+
url: ticket.url,
|
|
2054
|
+
name: ticket.doc,
|
|
2055
|
+
parameters: { cg_token: ticket.token },
|
|
2056
|
+
onAuthenticationFailed: () => {
|
|
2057
|
+
stopKeepAlive();
|
|
2058
|
+
try {
|
|
2059
|
+
current.destroy();
|
|
2060
|
+
} catch {
|
|
2061
|
+
}
|
|
2062
|
+
if (!dead) retry = window.setTimeout(connect, 5e3);
|
|
2063
|
+
}
|
|
2064
|
+
});
|
|
2065
|
+
stopKeepAlive = keepPresenceAlive(current.awareness);
|
|
2066
|
+
providerRef.current = current;
|
|
2067
|
+
setProvider(current);
|
|
2068
|
+
if (lastRef.current) {
|
|
2069
|
+
try {
|
|
2070
|
+
current.setAwarenessField(field, lastRef.current);
|
|
2071
|
+
} catch {
|
|
2072
|
+
}
|
|
2073
|
+
}
|
|
2074
|
+
current.on("awarenessUpdate", ({ states: next }) => {
|
|
2075
|
+
if (!dead) setStates(next || []);
|
|
2076
|
+
});
|
|
2077
|
+
current.on("synced", () => {
|
|
2078
|
+
if (dead) return;
|
|
2079
|
+
setSelfClientId(current.awareness ? current.awareness.clientID : null);
|
|
2080
|
+
setDoc(current.document);
|
|
2081
|
+
setReady(true);
|
|
2082
|
+
});
|
|
2083
|
+
current.on("status", ({ status }) => {
|
|
2084
|
+
if (!dead && status === "disconnected") setReady(false);
|
|
2085
|
+
});
|
|
2086
|
+
}
|
|
2087
|
+
connect();
|
|
2088
|
+
return () => {
|
|
2089
|
+
dead = true;
|
|
2090
|
+
window.clearTimeout(retry);
|
|
2091
|
+
stopKeepAlive();
|
|
2092
|
+
if (current) {
|
|
2093
|
+
try {
|
|
2094
|
+
current.destroy();
|
|
2095
|
+
} catch {
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2098
|
+
providerRef.current = null;
|
|
2099
|
+
setProvider(null);
|
|
2100
|
+
setDoc(null);
|
|
2101
|
+
setReady(false);
|
|
2102
|
+
setStates([]);
|
|
2103
|
+
setSelfClientId(null);
|
|
2104
|
+
};
|
|
2105
|
+
}, [resourceId]);
|
|
2106
|
+
const setAwareness = (0, import_react5.useCallback)((value) => {
|
|
2107
|
+
lastRef.current = value;
|
|
2108
|
+
const p = providerRef.current;
|
|
2109
|
+
if (p) {
|
|
2110
|
+
try {
|
|
2111
|
+
p.setAwarenessField(field, value);
|
|
2112
|
+
} catch {
|
|
2113
|
+
}
|
|
2114
|
+
}
|
|
2115
|
+
}, [field]);
|
|
2116
|
+
return { doc, provider, ready, states, selfClientId, setAwareness };
|
|
2117
|
+
}
|
|
2118
|
+
|
|
2119
|
+
// src/sheet/useSheetCollab.js
|
|
2120
|
+
var COPILOT_STALE_MS = 15 * 60 * 1e3;
|
|
2121
|
+
function sheetOps(doc) {
|
|
2122
|
+
const one = (op) => apply(doc, [op], "local");
|
|
2123
|
+
return {
|
|
2124
|
+
setCell: (tab, key, cell) => one({ op: "set_cell", tab, key, cell }),
|
|
2125
|
+
setCells: (tab, cells) => one({ op: "set_cells", tab, cells }),
|
|
2126
|
+
setColumn: (tab, column, at) => one({ op: "set_column", tab, column, ...Number.isInteger(at) ? { at } : {} }),
|
|
2127
|
+
deleteColumn: (tab, id) => one({ op: "delete_column", tab, id }),
|
|
2128
|
+
moveColumn: (tab, id, at) => one({ op: "move_column", tab, id, at }),
|
|
2129
|
+
setRow: (tab, row, at) => one({ op: "set_row", tab, row, ...Number.isInteger(at) ? { at } : {} }),
|
|
2130
|
+
deleteRow: (tab, id) => one({ op: "delete_row", tab, id }),
|
|
2131
|
+
moveRow: (tab, id, at) => one({ op: "move_row", tab, id, at }),
|
|
2132
|
+
setTab: (tab, at) => one({ op: "set_tab", tab, ...Number.isInteger(at) ? { at } : {} }),
|
|
2133
|
+
deleteTab: (id) => one({ op: "delete_tab", id }),
|
|
2134
|
+
moveTab: (id, at) => one({ op: "move_tab", id, at }),
|
|
2135
|
+
setTitle: (title) => one({ op: "set_meta", key: "title", value: title })
|
|
2136
|
+
};
|
|
2137
|
+
}
|
|
2138
|
+
function useSheetCollab(resourceId, { fetchTicket, me } = {}) {
|
|
2139
|
+
const live = useLiveDoc(resourceId, { fetchTicket, field: "presence" });
|
|
2140
|
+
const [sheet, setSheet] = (0, import_react6.useState)(null);
|
|
2141
|
+
const [liveRun, setLiveRun] = (0, import_react6.useState)(null);
|
|
2142
|
+
const [copilotBuilding, setCopilotBuilding] = (0, import_react6.useState)(false);
|
|
2143
|
+
const who = me || {};
|
|
2144
|
+
(0, import_react6.useEffect)(() => {
|
|
2145
|
+
const ydoc = live.doc;
|
|
2146
|
+
if (!ydoc) {
|
|
2147
|
+
setSheet(null);
|
|
2148
|
+
setLiveRun(null);
|
|
2149
|
+
setCopilotBuilding(false);
|
|
2150
|
+
return void 0;
|
|
2151
|
+
}
|
|
2152
|
+
const meta = ydoc.getMap("meta");
|
|
2153
|
+
let timer = 0;
|
|
2154
|
+
let dead = false;
|
|
2155
|
+
const snapshot = () => {
|
|
2156
|
+
timer = 0;
|
|
2157
|
+
if (dead) return;
|
|
2158
|
+
setSheet(toJSON(ydoc));
|
|
2159
|
+
setLiveRun(meta.get("run") || null);
|
|
2160
|
+
const cop = meta.get("copilot");
|
|
2161
|
+
setCopilotBuilding(!!(cop && cop.building && (!cop.at || Date.now() - cop.at < COPILOT_STALE_MS)));
|
|
2162
|
+
};
|
|
2163
|
+
const onUpdate = () => {
|
|
2164
|
+
if (!timer) timer = window.setTimeout(snapshot, 0);
|
|
2165
|
+
};
|
|
2166
|
+
ydoc.on("update", onUpdate);
|
|
2167
|
+
snapshot();
|
|
2168
|
+
return () => {
|
|
2169
|
+
dead = true;
|
|
2170
|
+
ydoc.off("update", onUpdate);
|
|
2171
|
+
window.clearTimeout(timer);
|
|
2172
|
+
};
|
|
2173
|
+
}, [live.doc]);
|
|
2174
|
+
(0, import_react6.useEffect)(() => {
|
|
2175
|
+
live.setAwareness({ id: who.id || "", name: who.name || who.email || "Someone", tab: null, sel: null, typing: null, at: Date.now() });
|
|
2176
|
+
}, [resourceId, live.setAwareness]);
|
|
2177
|
+
const peers = (0, import_react6.useMemo)(() => {
|
|
2178
|
+
const out = [];
|
|
2179
|
+
for (const st of live.states) {
|
|
2180
|
+
const p = st.presence;
|
|
2181
|
+
if (!p || !p.id || st.clientId === live.selfClientId) continue;
|
|
2182
|
+
out.push({ ...p, clientId: st.clientId, color: peerColor(p.id) });
|
|
2183
|
+
}
|
|
2184
|
+
return out;
|
|
2185
|
+
}, [live.states, live.selfClientId]);
|
|
2186
|
+
const ops = (0, import_react6.useMemo)(() => live.doc ? sheetOps(live.doc) : null, [live.doc]);
|
|
2187
|
+
const setPresence = (0, import_react6.useCallback)((tab, state) => {
|
|
2188
|
+
live.setAwareness({
|
|
2189
|
+
id: who.id || "",
|
|
2190
|
+
name: who.name || who.email || "Someone",
|
|
2191
|
+
tab: tab || null,
|
|
2192
|
+
sel: state && state.sel || null,
|
|
2193
|
+
typing: state && state.typing || null,
|
|
2194
|
+
at: Date.now()
|
|
2195
|
+
});
|
|
2196
|
+
}, [live.setAwareness, who.id, who.name, who.email]);
|
|
2197
|
+
return { ready: live.ready, sheet, ops, liveRun, copilotBuilding, peers, setPresence };
|
|
2198
|
+
}
|
|
2199
|
+
|
|
2200
|
+
// src/sheet/useSheetRun.js
|
|
2201
|
+
var import_react7 = require("react");
|
|
2202
|
+
var ACTIVE = /* @__PURE__ */ new Set(["running", "cancelling"]);
|
|
2203
|
+
var isRunActive = (run) => !!run && ACTIVE.has(run.status);
|
|
2204
|
+
function useSheetRun(resourceId, api, liveRun) {
|
|
2205
|
+
const [run, setRun] = (0, import_react7.useState)(null);
|
|
2206
|
+
const [error, setError] = (0, import_react7.useState)("");
|
|
2207
|
+
(0, import_react7.useEffect)(() => {
|
|
2208
|
+
setRun(null);
|
|
2209
|
+
setError("");
|
|
2210
|
+
}, [resourceId]);
|
|
2211
|
+
(0, import_react7.useEffect)(() => {
|
|
2212
|
+
if (!liveRun) return;
|
|
2213
|
+
setRun((cur) => {
|
|
2214
|
+
if (isRunActive(liveRun) || cur && cur.run_id === liveRun.run_id) return liveRun;
|
|
2215
|
+
return cur;
|
|
2216
|
+
});
|
|
2217
|
+
}, [liveRun]);
|
|
2218
|
+
const start = (0, import_react7.useCallback)(async (body) => {
|
|
2219
|
+
setError("");
|
|
2220
|
+
try {
|
|
2221
|
+
const r = await api.startRun(resourceId, body);
|
|
2222
|
+
setRun(r);
|
|
2223
|
+
return r;
|
|
2224
|
+
} catch (e) {
|
|
2225
|
+
setError(String(e?.message || e));
|
|
2226
|
+
return null;
|
|
2227
|
+
}
|
|
2228
|
+
}, [resourceId, api]);
|
|
2229
|
+
const cancel = (0, import_react7.useCallback)(async () => {
|
|
2230
|
+
if (!isRunActive(run)) return;
|
|
2231
|
+
try {
|
|
2232
|
+
setRun(await api.cancelRun(resourceId, run.run_id));
|
|
2233
|
+
} catch (e) {
|
|
2234
|
+
setError(String(e?.message || e));
|
|
2235
|
+
}
|
|
2236
|
+
}, [resourceId, api, run]);
|
|
2237
|
+
const dismiss = (0, import_react7.useCallback)(() => {
|
|
2238
|
+
setRun(null);
|
|
2239
|
+
setError("");
|
|
2240
|
+
}, []);
|
|
2241
|
+
return { run, error, start, cancel, dismiss };
|
|
2242
|
+
}
|
|
2243
|
+
function withQueuedCells(tab, run) {
|
|
2244
|
+
if (!tab || !isRunActive(run) || run.tab_id !== tab.id) return tab;
|
|
2245
|
+
const rows = run.rows || (tab.rows || []).map((r) => r.id);
|
|
2246
|
+
const cells = { ...tab.cells || {} };
|
|
2247
|
+
let touched = false;
|
|
2248
|
+
for (const rid of rows) {
|
|
2249
|
+
for (const col of run.columns || []) {
|
|
2250
|
+
const key = `${rid}:${col.id}`;
|
|
2251
|
+
const cur = cells[key];
|
|
2252
|
+
if (cur && cur.run_id === run.run_id) continue;
|
|
2253
|
+
cells[key] = { ...cur || {}, status: "queued" };
|
|
2254
|
+
touched = true;
|
|
2255
|
+
}
|
|
2256
|
+
}
|
|
2257
|
+
return touched ? { ...tab, cells } : tab;
|
|
2258
|
+
}
|
|
2259
|
+
function runProgress(tab, run) {
|
|
2260
|
+
if (!run) return 0;
|
|
2261
|
+
let n = 0;
|
|
2262
|
+
for (const cell of Object.values(tab?.cells || {})) {
|
|
2263
|
+
if (cell && cell.run_id === run.run_id && cell.status !== "running") n += 1;
|
|
2264
|
+
}
|
|
2265
|
+
return n;
|
|
2266
|
+
}
|
|
2267
|
+
|
|
2268
|
+
// src/sheet/RunStrip.jsx
|
|
2269
|
+
var import_react8 = __toESM(require("react"), 1);
|
|
2270
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
2271
|
+
function outcome(run) {
|
|
2272
|
+
const c = run.counts;
|
|
2273
|
+
if (!c) return "";
|
|
2274
|
+
const parts = [`${c.done || 0} done`];
|
|
2275
|
+
if (c.failed) parts.push(`${c.failed} failed`);
|
|
2276
|
+
if (c.skipped) parts.push(`${c.skipped} skipped`);
|
|
2277
|
+
return parts.join(", ");
|
|
2278
|
+
}
|
|
2279
|
+
function RunStrip({ run, error, filled = 0, onCancel, onDismiss }) {
|
|
2280
|
+
if (!run && !error) return null;
|
|
2281
|
+
let text = error;
|
|
2282
|
+
let tone = "failed";
|
|
2283
|
+
if (run) {
|
|
2284
|
+
const names = (run.columns || []).map((c) => c.name).join(", ");
|
|
2285
|
+
const total = run.cell_count || 0;
|
|
2286
|
+
tone = run.status;
|
|
2287
|
+
if (run.status === "running") {
|
|
2288
|
+
text = `Filling ${names}: ${filled} of ${total} cells, ${run.concurrency} rows at a time`;
|
|
2289
|
+
} else if (run.status === "cancelling") {
|
|
2290
|
+
text = `Stopping after the cells in flight: ${filled} of ${total} cells`;
|
|
2291
|
+
} else if (run.status === "done") {
|
|
2292
|
+
const o = outcome(run);
|
|
2293
|
+
text = o ? `Filled ${names}: ${o}` : `Filled ${names}`;
|
|
2294
|
+
} else if (run.status === "cancelled") {
|
|
2295
|
+
const c = run.counts;
|
|
2296
|
+
const o = outcome(run);
|
|
2297
|
+
const notRun = c ? Math.max(0, total - ((c.done || 0) + (c.failed || 0) + (c.skipped || 0))) : null;
|
|
2298
|
+
text = o ? `Stopped: ${o}, ${notRun} not run` : "Stopped";
|
|
2299
|
+
} else {
|
|
2300
|
+
const o = outcome(run);
|
|
2301
|
+
text = run.error ? `The run stopped early: ${run.error}` : "The run could not start";
|
|
2302
|
+
if (o) text += ` (${o})`;
|
|
2303
|
+
tone = "failed";
|
|
2304
|
+
}
|
|
2305
|
+
}
|
|
2306
|
+
const active = isRunActive(run);
|
|
2307
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: `shg-run shg-run-${tone}`, role: "status", "aria-live": "polite", children: [
|
|
2308
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { className: "shg-run-dot", "aria-hidden": "true" }),
|
|
2309
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { className: "shg-run-text", title: text, children: text }),
|
|
2310
|
+
active ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
2311
|
+
"button",
|
|
2312
|
+
{
|
|
2313
|
+
type: "button",
|
|
2314
|
+
className: "shg-run-stop",
|
|
2315
|
+
onClick: onCancel,
|
|
2316
|
+
disabled: run.status === "cancelling",
|
|
2317
|
+
children: "Stop"
|
|
2318
|
+
}
|
|
2319
|
+
) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("button", { type: "button", className: "shg-run-x", onClick: onDismiss, "aria-label": "Dismiss", children: "\xD7" })
|
|
2320
|
+
] });
|
|
2321
|
+
}
|
|
2322
|
+
//# sourceMappingURL=sheet.cjs.map
|