@dxos/react-ui-attention 0.8.4-main.f9ba587 → 0.8.4-main.fcc0d83b33
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/dist/lib/browser/chunk-DAIZNFKL.mjs +668 -0
- package/dist/lib/browser/chunk-DAIZNFKL.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +86 -13
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +20 -7
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/browser/types.mjs +1 -0
- package/dist/lib/browser/types.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-HZTQDMTS.mjs +670 -0
- package/dist/lib/node-esm/chunk-HZTQDMTS.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +86 -13
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +20 -7
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/types.mjs +2 -0
- package/dist/lib/node-esm/types.mjs.map +7 -0
- package/dist/types/src/attention.d.ts +56 -20
- package/dist/types/src/attention.d.ts.map +1 -1
- package/dist/types/src/components/AttentionGlyph.d.ts.map +1 -1
- package/dist/types/src/components/AttentionGlyph.stories.d.ts +9 -20
- package/dist/types/src/components/AttentionGlyph.stories.d.ts.map +1 -1
- package/dist/types/src/components/AttentionProvider.d.ts +15 -16
- package/dist/types/src/components/AttentionProvider.d.ts.map +1 -1
- package/dist/types/src/components/AttentionProvider.stories.d.ts +6 -4
- package/dist/types/src/components/AttentionProvider.stories.d.ts.map +1 -1
- package/dist/types/src/components/SelectionProvider.d.ts +6 -5
- package/dist/types/src/components/SelectionProvider.d.ts.map +1 -1
- package/dist/types/src/selection.d.ts +49 -17
- package/dist/types/src/selection.d.ts.map +1 -1
- package/dist/types/src/testing/decorators/withAttention.d.ts +5 -1
- package/dist/types/src/testing/decorators/withAttention.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +9 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +35 -31
- package/src/attention.test.ts +188 -27
- package/src/attention.ts +175 -74
- package/src/components/AttentionGlyph.stories.tsx +17 -14
- package/src/components/AttentionGlyph.tsx +14 -7
- package/src/components/AttentionProvider.stories.tsx +12 -9
- package/src/components/AttentionProvider.tsx +80 -97
- package/src/components/SelectionProvider.tsx +66 -19
- package/src/selection.ts +135 -65
- package/src/testing/decorators/withAttention.ts +21 -4
- package/src/types.ts +13 -0
- package/dist/lib/browser/chunk-QPEFFJ4E.mjs +0 -616
- package/dist/lib/browser/chunk-QPEFFJ4E.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-F2JRKDYV.mjs +0 -618
- package/dist/lib/node-esm/chunk-F2JRKDYV.mjs.map +0 -7
|
@@ -0,0 +1,670 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// src/attention.ts
|
|
4
|
+
import { Atom } from "@effect-atom/atom-react";
|
|
5
|
+
var AttentionManager = class {
|
|
6
|
+
_registry;
|
|
7
|
+
_map = /* @__PURE__ */ new Map();
|
|
8
|
+
_currentAtom;
|
|
9
|
+
constructor(_registry, initial = []) {
|
|
10
|
+
this._registry = _registry;
|
|
11
|
+
this._currentAtom = Atom.make([]).pipe(Atom.keepAlive);
|
|
12
|
+
if (initial.length > 0) {
|
|
13
|
+
this.update(initial);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Atom for the currently attended element IDs.
|
|
18
|
+
*/
|
|
19
|
+
get current() {
|
|
20
|
+
return this._currentAtom;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Gets the currently attended element IDs.
|
|
24
|
+
*/
|
|
25
|
+
getCurrent() {
|
|
26
|
+
return this._registry.get(this._currentAtom);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Subscribe to changes in the current attention IDs.
|
|
30
|
+
*/
|
|
31
|
+
subscribeCurrent(cb) {
|
|
32
|
+
this._registry.get(this._currentAtom);
|
|
33
|
+
return this._registry.subscribe(this._currentAtom, () => {
|
|
34
|
+
cb(this._registry.get(this._currentAtom));
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* All tracked qualified IDs.
|
|
39
|
+
*/
|
|
40
|
+
keys() {
|
|
41
|
+
return Array.from(this._map.keys());
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Get the attention state for a given qualified ID.
|
|
45
|
+
*/
|
|
46
|
+
get(id) {
|
|
47
|
+
const atom = this._getAtom(id);
|
|
48
|
+
return this._registry.get(atom);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Subscribe to changes in the attention state for a given qualified ID.
|
|
52
|
+
*/
|
|
53
|
+
subscribe(id, cb) {
|
|
54
|
+
const atom = this._getAtom(id);
|
|
55
|
+
this._registry.get(atom);
|
|
56
|
+
return this._registry.subscribe(atom, () => {
|
|
57
|
+
cb(this._registry.get(atom));
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
_getAtom(id) {
|
|
61
|
+
const existing = this._map.get(id);
|
|
62
|
+
if (existing) {
|
|
63
|
+
return existing;
|
|
64
|
+
}
|
|
65
|
+
const newAtom = Atom.make({
|
|
66
|
+
hasAttention: false,
|
|
67
|
+
isAncestor: false,
|
|
68
|
+
isRelated: false
|
|
69
|
+
}).pipe(Atom.keepAlive);
|
|
70
|
+
this._map.set(id, newAtom);
|
|
71
|
+
return newAtom;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Update the currently attended element.
|
|
75
|
+
* Takes the array of qualified IDs collected from the DOM; the first element is the primary attended item.
|
|
76
|
+
* Ancestry is derived from the progressive prefixes of the primary ID.
|
|
77
|
+
* Relatedness is derived from the segment ID: any tracked key whose last `/` segment matches the
|
|
78
|
+
* attended ID's segment ID is marked `isRelated`. Additionally, if the primary ID is a linked segment
|
|
79
|
+
* (starts with `~`), its immediate parent gets `isRelated` alongside `isAncestor`.
|
|
80
|
+
*
|
|
81
|
+
* @internal
|
|
82
|
+
*/
|
|
83
|
+
update(nextIds) {
|
|
84
|
+
const primaryId = nextIds[0];
|
|
85
|
+
if (!primaryId) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const currentIds = this.getCurrent();
|
|
89
|
+
const prevPrimaryId = currentIds[0];
|
|
90
|
+
if (prevPrimaryId) {
|
|
91
|
+
const prevPrefixes = expandAttendableId(prevPrimaryId);
|
|
92
|
+
for (const prefix of prevPrefixes) {
|
|
93
|
+
this._set(prefix, {});
|
|
94
|
+
}
|
|
95
|
+
const prevSegmentId = getSegmentId(prevPrimaryId);
|
|
96
|
+
for (const key of this.keys()) {
|
|
97
|
+
if (getSegmentId(key) === prevSegmentId) {
|
|
98
|
+
this._set(key, {});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
this._registry.set(this._currentAtom, nextIds);
|
|
103
|
+
const prefixes = expandAttendableId(primaryId);
|
|
104
|
+
const prefixSet = new Set(prefixes);
|
|
105
|
+
const linkedParent = isLinkedSegment(primaryId) ? getParentId(primaryId) : void 0;
|
|
106
|
+
for (const prefix of prefixes) {
|
|
107
|
+
if (prefix === primaryId) {
|
|
108
|
+
this._set(prefix, {
|
|
109
|
+
hasAttention: true
|
|
110
|
+
});
|
|
111
|
+
} else if (prefix === linkedParent) {
|
|
112
|
+
this._set(prefix, {
|
|
113
|
+
isAncestor: true,
|
|
114
|
+
isRelated: true
|
|
115
|
+
});
|
|
116
|
+
} else {
|
|
117
|
+
this._set(prefix, {
|
|
118
|
+
isAncestor: true
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const segmentId = getSegmentId(primaryId);
|
|
123
|
+
for (const key of this.keys()) {
|
|
124
|
+
if (!prefixSet.has(key) && getSegmentId(key) === segmentId) {
|
|
125
|
+
this._set(key, {
|
|
126
|
+
isRelated: true
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
_set(id, attention) {
|
|
132
|
+
const atom = this._getAtom(id);
|
|
133
|
+
this._registry.set(atom, {
|
|
134
|
+
hasAttention: attention.hasAttention ?? false,
|
|
135
|
+
isAncestor: attention.isAncestor ?? false,
|
|
136
|
+
isRelated: attention.isRelated ?? false
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
var getAttendables = (selector, cursor, acc = []) => {
|
|
141
|
+
const closestAttendable = cursor.closest(selector);
|
|
142
|
+
if (closestAttendable) {
|
|
143
|
+
const attendableId = closestAttendable.getAttribute("data-attendable-id");
|
|
144
|
+
if (!attendableId) {
|
|
145
|
+
const trigger = document.querySelector(`[aria-controls="${closestAttendable.getAttribute("id")}"]`);
|
|
146
|
+
if (!trigger) {
|
|
147
|
+
return acc;
|
|
148
|
+
} else {
|
|
149
|
+
return getAttendables(selector, trigger, acc);
|
|
150
|
+
}
|
|
151
|
+
} else {
|
|
152
|
+
acc.push(attendableId);
|
|
153
|
+
return !closestAttendable.parentElement ? acc : getAttendables(selector, closestAttendable.parentElement, acc);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return [
|
|
157
|
+
...new Set(acc)
|
|
158
|
+
];
|
|
159
|
+
};
|
|
160
|
+
var LINKED_PREFIX = "~";
|
|
161
|
+
var expandAttendableId = (qualifiedId) => {
|
|
162
|
+
const segments = qualifiedId.split("/");
|
|
163
|
+
return segments.map((_, index) => segments.slice(0, index + 1).join("/"));
|
|
164
|
+
};
|
|
165
|
+
var isLinkedSegment = (qualifiedId) => {
|
|
166
|
+
const lastSegment = qualifiedId.split("/").pop() ?? "";
|
|
167
|
+
return lastSegment.startsWith(LINKED_PREFIX);
|
|
168
|
+
};
|
|
169
|
+
var linkedSegment = (variant) => `${LINKED_PREFIX}${variant}`;
|
|
170
|
+
var getLinkedVariant = (qualifiedId) => {
|
|
171
|
+
const lastSegment = qualifiedId.split("/").pop() ?? "";
|
|
172
|
+
return lastSegment.startsWith(LINKED_PREFIX) ? lastSegment.slice(LINKED_PREFIX.length) : lastSegment;
|
|
173
|
+
};
|
|
174
|
+
var getParentId = (qualifiedId) => {
|
|
175
|
+
if (!qualifiedId) {
|
|
176
|
+
return void 0;
|
|
177
|
+
}
|
|
178
|
+
const lastSlash = qualifiedId.lastIndexOf("/");
|
|
179
|
+
return lastSlash > 0 ? qualifiedId.slice(0, lastSlash) : void 0;
|
|
180
|
+
};
|
|
181
|
+
var getSegmentId = (qualifiedId) => {
|
|
182
|
+
return qualifiedId.split("/").pop() ?? qualifiedId;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// src/components/AttentionProvider.tsx
|
|
186
|
+
import { RegistryContext } from "@effect-atom/atom-react";
|
|
187
|
+
import { createContext } from "@radix-ui/react-context";
|
|
188
|
+
import { Primitive } from "@radix-ui/react-primitive";
|
|
189
|
+
import { Slot } from "@radix-ui/react-slot";
|
|
190
|
+
import React, { forwardRef, useContext, useEffect, useMemo, useState } from "react";
|
|
191
|
+
import { log } from "@dxos/log";
|
|
192
|
+
import { useDefaultValue } from "@dxos/react-hooks";
|
|
193
|
+
import { mx } from "@dxos/ui-theme";
|
|
194
|
+
var __dxlog_file = "/__w/dxos/dxos/packages/ui/react-ui-attention/src/components/AttentionProvider.tsx";
|
|
195
|
+
var ATTENTION_NAME = "Attention";
|
|
196
|
+
var ATTENDABLE_ATTRIBUTE = "data-attendable-id";
|
|
197
|
+
var ATTENTION_SOURCE_ATTRIBUTE = "data-w-attention-source";
|
|
198
|
+
var [AttentionContextProvider, useAttentionContext] = createContext(ATTENTION_NAME, {
|
|
199
|
+
attention: void 0
|
|
200
|
+
});
|
|
201
|
+
var UNKNOWN_ATTENDABLE = {
|
|
202
|
+
hasAttention: false,
|
|
203
|
+
isAncestor: false,
|
|
204
|
+
isRelated: false
|
|
205
|
+
};
|
|
206
|
+
var useAttention = (attendableId) => {
|
|
207
|
+
const { attention } = useAttentionContext(ATTENTION_NAME);
|
|
208
|
+
const [state, setState] = useState(UNKNOWN_ATTENDABLE);
|
|
209
|
+
useEffect(() => {
|
|
210
|
+
if (!attendableId || !attention) {
|
|
211
|
+
setState(UNKNOWN_ATTENDABLE);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
const currentState = attention.get(attendableId);
|
|
215
|
+
setState(currentState);
|
|
216
|
+
return attention.subscribe(attendableId, (newState) => {
|
|
217
|
+
setState(newState);
|
|
218
|
+
});
|
|
219
|
+
}, [
|
|
220
|
+
attention,
|
|
221
|
+
attendableId
|
|
222
|
+
]);
|
|
223
|
+
return state;
|
|
224
|
+
};
|
|
225
|
+
var useAttended = () => {
|
|
226
|
+
const { attention } = useAttentionContext(ATTENTION_NAME);
|
|
227
|
+
const [current, setCurrent] = useState([]);
|
|
228
|
+
useEffect(() => {
|
|
229
|
+
if (!attention) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
setCurrent(attention.getCurrent());
|
|
233
|
+
return attention.subscribeCurrent((newCurrent) => {
|
|
234
|
+
setCurrent(newCurrent);
|
|
235
|
+
});
|
|
236
|
+
}, [
|
|
237
|
+
attention
|
|
238
|
+
]);
|
|
239
|
+
return current;
|
|
240
|
+
};
|
|
241
|
+
var useAttentionAttributes = (attendableId) => {
|
|
242
|
+
const { hasAttention } = useAttention(attendableId);
|
|
243
|
+
return useMemo(() => {
|
|
244
|
+
const attributes = {
|
|
245
|
+
[ATTENDABLE_ATTRIBUTE]: attendableId
|
|
246
|
+
};
|
|
247
|
+
if (hasAttention) {
|
|
248
|
+
attributes[ATTENTION_SOURCE_ATTRIBUTE] = "true";
|
|
249
|
+
}
|
|
250
|
+
return attributes;
|
|
251
|
+
}, [
|
|
252
|
+
attendableId,
|
|
253
|
+
hasAttention
|
|
254
|
+
]);
|
|
255
|
+
};
|
|
256
|
+
var RootAttentionProvider = ({ children, attention: propsAttention, onChange }) => {
|
|
257
|
+
const registry = useContext(RegistryContext);
|
|
258
|
+
const attention = useDefaultValue(propsAttention, () => new AttentionManager(registry));
|
|
259
|
+
const handleFocus = (event) => {
|
|
260
|
+
log("focus", {
|
|
261
|
+
related: event.relatedTarget,
|
|
262
|
+
target: event.target
|
|
263
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 83, S: void 0 });
|
|
264
|
+
const selector = [
|
|
265
|
+
"[data-attendable-id]",
|
|
266
|
+
...Array.from(document.querySelectorAll("[aria-controls]")).map((el) => `[id="${el.getAttribute("aria-controls")}"]`)
|
|
267
|
+
].join(",");
|
|
268
|
+
const prev = attention.getCurrent();
|
|
269
|
+
const next = getAttendables(selector, event.target);
|
|
270
|
+
if (next.length > 0 && (prev.length !== next.length || !!prev.find((id, index) => next[index] !== id))) {
|
|
271
|
+
attention.update(next);
|
|
272
|
+
onChange?.(next);
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
return /* @__PURE__ */ React.createElement(AttentionContextProvider, {
|
|
276
|
+
attention
|
|
277
|
+
}, /* @__PURE__ */ React.createElement("div", {
|
|
278
|
+
role: "none",
|
|
279
|
+
className: "contents",
|
|
280
|
+
onFocusCapture: handleFocus
|
|
281
|
+
}, children));
|
|
282
|
+
};
|
|
283
|
+
var AttendableContainer = /* @__PURE__ */ forwardRef(({ id, classNames, children, asChild, ...props }, forwardedRef) => {
|
|
284
|
+
const attentionAttrs = useAttentionAttributes(id);
|
|
285
|
+
const Comp = asChild ? Slot : Primitive.div;
|
|
286
|
+
return /* @__PURE__ */ React.createElement(Comp, {
|
|
287
|
+
role: "none",
|
|
288
|
+
...props,
|
|
289
|
+
...attentionAttrs,
|
|
290
|
+
className: mx("dx-attention-surface", props.tabIndex === 0 && "dx-focus-ring-inset-over-all", classNames),
|
|
291
|
+
ref: forwardedRef
|
|
292
|
+
}, children);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
// src/selection.ts
|
|
296
|
+
import { Atom as Atom2 } from "@effect-atom/atom-react";
|
|
297
|
+
import * as Match from "effect/Match";
|
|
298
|
+
import * as Schema from "effect/Schema";
|
|
299
|
+
import { invariant } from "@dxos/invariant";
|
|
300
|
+
var __dxlog_file2 = "/__w/dxos/dxos/packages/ui/react-ui-attention/src/selection.ts";
|
|
301
|
+
var SelectionSchema = Schema.Union(Schema.Struct({
|
|
302
|
+
mode: Schema.Literal("single"),
|
|
303
|
+
id: Schema.optional(Schema.String)
|
|
304
|
+
}).pipe(Schema.mutable), Schema.Struct({
|
|
305
|
+
mode: Schema.Literal("multi"),
|
|
306
|
+
ids: Schema.Array(Schema.String).pipe(Schema.mutable)
|
|
307
|
+
}).pipe(Schema.mutable), Schema.Struct({
|
|
308
|
+
mode: Schema.Literal("range"),
|
|
309
|
+
from: Schema.optional(Schema.String),
|
|
310
|
+
to: Schema.optional(Schema.String)
|
|
311
|
+
}).pipe(Schema.mutable), Schema.Struct({
|
|
312
|
+
mode: Schema.Literal("multi-range"),
|
|
313
|
+
ranges: Schema.Array(Schema.Struct({
|
|
314
|
+
from: Schema.String,
|
|
315
|
+
to: Schema.String
|
|
316
|
+
})).pipe(Schema.mutable)
|
|
317
|
+
}).pipe(Schema.mutable)).pipe(Schema.mutable);
|
|
318
|
+
var defaultSelection = Match.type().pipe(Match.withReturnType(), Match.when("single", () => ({
|
|
319
|
+
mode: "single"
|
|
320
|
+
})), Match.when("multi", () => ({
|
|
321
|
+
mode: "multi",
|
|
322
|
+
ids: []
|
|
323
|
+
})), Match.when("range", () => ({
|
|
324
|
+
mode: "range"
|
|
325
|
+
})), Match.when("multi-range", () => ({
|
|
326
|
+
mode: "multi-range",
|
|
327
|
+
ranges: []
|
|
328
|
+
})), Match.exhaustive);
|
|
329
|
+
var getSelectionSet = (selectionManager, contextId) => {
|
|
330
|
+
const ids = new Set(contextId ? [
|
|
331
|
+
contextId
|
|
332
|
+
] : []);
|
|
333
|
+
for (const context of selectionManager.getSelectionContexts()) {
|
|
334
|
+
const selection = selectionManager.getSelection(context);
|
|
335
|
+
if (selection?.mode === "multi") {
|
|
336
|
+
for (const id of selection.ids) {
|
|
337
|
+
ids.add(id);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return ids;
|
|
342
|
+
};
|
|
343
|
+
var SelectionManager = class {
|
|
344
|
+
_registry;
|
|
345
|
+
_state;
|
|
346
|
+
constructor(_registry, initial = {}) {
|
|
347
|
+
this._registry = _registry;
|
|
348
|
+
this._state = Atom2.make({
|
|
349
|
+
selections: {
|
|
350
|
+
...initial
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Get the state atom for reactive access in the graph system.
|
|
356
|
+
*/
|
|
357
|
+
get state() {
|
|
358
|
+
return this._state;
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Gets the current state.
|
|
362
|
+
*/
|
|
363
|
+
getState() {
|
|
364
|
+
return this._registry.get(this._state);
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Subscribe to changes in the selection state.
|
|
368
|
+
*/
|
|
369
|
+
subscribe(cb) {
|
|
370
|
+
this._registry.get(this._state);
|
|
371
|
+
return this._registry.subscribe(this._state, () => {
|
|
372
|
+
cb(this._registry.get(this._state));
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
getSelectionContexts() {
|
|
376
|
+
return Object.keys(this._registry.get(this._state).selections);
|
|
377
|
+
}
|
|
378
|
+
getSelection(contextId, mode = void 0) {
|
|
379
|
+
const state = this._registry.get(this._state);
|
|
380
|
+
const selection = state.selections[contextId];
|
|
381
|
+
if (!mode || selection) {
|
|
382
|
+
return selection;
|
|
383
|
+
}
|
|
384
|
+
const newSelection = defaultSelection(mode);
|
|
385
|
+
this._registry.update(this._state, (state2) => ({
|
|
386
|
+
selections: {
|
|
387
|
+
...state2.selections,
|
|
388
|
+
[contextId]: newSelection
|
|
389
|
+
}
|
|
390
|
+
}));
|
|
391
|
+
return newSelection;
|
|
392
|
+
}
|
|
393
|
+
// TODO(burdon): Disambiguate with getSelection?
|
|
394
|
+
getSelected(contextId, mode = "multi") {
|
|
395
|
+
const selection = this.getSelection(contextId, mode);
|
|
396
|
+
invariant(selection?.mode === mode, "Selection mode mismatch", { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 105, S: this, A: ["selection?.mode === mode", "'Selection mode mismatch'"] });
|
|
397
|
+
return Match.type().pipe(Match.when({
|
|
398
|
+
mode: "single"
|
|
399
|
+
}, (s) => s.id), Match.when({
|
|
400
|
+
mode: "multi"
|
|
401
|
+
}, (s) => s.ids), Match.when({
|
|
402
|
+
mode: "range"
|
|
403
|
+
}, (s) => s.from && s.to ? {
|
|
404
|
+
from: s.from,
|
|
405
|
+
to: s.to
|
|
406
|
+
} : void 0), Match.when({
|
|
407
|
+
mode: "multi-range"
|
|
408
|
+
}, (s) => s.ranges), Match.exhaustive)(selection);
|
|
409
|
+
}
|
|
410
|
+
updateSingle(contextId, id) {
|
|
411
|
+
const selection = this.getSelection(contextId, "single");
|
|
412
|
+
invariant(selection?.mode === "single", "Selection mode is not single", { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 122, S: this, A: ["selection?.mode === 'single'", "'Selection mode is not single'"] });
|
|
413
|
+
this._registry.update(this._state, (state) => ({
|
|
414
|
+
selections: {
|
|
415
|
+
...state.selections,
|
|
416
|
+
[contextId]: {
|
|
417
|
+
...selection,
|
|
418
|
+
id
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}));
|
|
422
|
+
}
|
|
423
|
+
updateMulti(contextId, ids) {
|
|
424
|
+
const selection = this.getSelection(contextId, "multi");
|
|
425
|
+
invariant(selection?.mode === "multi", "Selection mode is not multi", { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 135, S: this, A: ["selection?.mode === 'multi'", "'Selection mode is not multi'"] });
|
|
426
|
+
this._registry.update(this._state, (state) => ({
|
|
427
|
+
selections: {
|
|
428
|
+
...state.selections,
|
|
429
|
+
[contextId]: {
|
|
430
|
+
...selection,
|
|
431
|
+
ids: [
|
|
432
|
+
...ids
|
|
433
|
+
]
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}));
|
|
437
|
+
}
|
|
438
|
+
updateRange(contextId, from, to) {
|
|
439
|
+
const selection = this.getSelection(contextId, "range");
|
|
440
|
+
invariant(selection?.mode === "range", "Selection mode is not range", { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 150, S: this, A: ["selection?.mode === 'range'", "'Selection mode is not range'"] });
|
|
441
|
+
this._registry.update(this._state, (state) => ({
|
|
442
|
+
selections: {
|
|
443
|
+
...state.selections,
|
|
444
|
+
[contextId]: {
|
|
445
|
+
...selection,
|
|
446
|
+
from,
|
|
447
|
+
to
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}));
|
|
451
|
+
}
|
|
452
|
+
updateMultiRange(contextId, ranges) {
|
|
453
|
+
const selection = this.getSelection(contextId, "multi-range");
|
|
454
|
+
invariant(selection?.mode === "multi-range", "Selection mode is not multi-range", { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 164, S: this, A: ["selection?.mode === 'multi-range'", "'Selection mode is not multi-range'"] });
|
|
455
|
+
this._registry.update(this._state, (state) => ({
|
|
456
|
+
selections: {
|
|
457
|
+
...state.selections,
|
|
458
|
+
[contextId]: {
|
|
459
|
+
...selection,
|
|
460
|
+
ranges: [
|
|
461
|
+
...ranges
|
|
462
|
+
]
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}));
|
|
466
|
+
}
|
|
467
|
+
clearSelection(contextId) {
|
|
468
|
+
const selection = this.getSelection(contextId);
|
|
469
|
+
if (!selection) {
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
const clearedSelection = Match.type().pipe(Match.when({
|
|
473
|
+
mode: "single"
|
|
474
|
+
}, (s) => ({
|
|
475
|
+
...s,
|
|
476
|
+
id: void 0
|
|
477
|
+
})), Match.when({
|
|
478
|
+
mode: "multi"
|
|
479
|
+
}, (s) => ({
|
|
480
|
+
...s,
|
|
481
|
+
ids: []
|
|
482
|
+
})), Match.when({
|
|
483
|
+
mode: "range"
|
|
484
|
+
}, (s) => ({
|
|
485
|
+
...s,
|
|
486
|
+
from: void 0,
|
|
487
|
+
to: void 0
|
|
488
|
+
})), Match.when({
|
|
489
|
+
mode: "multi-range"
|
|
490
|
+
}, (s) => ({
|
|
491
|
+
...s,
|
|
492
|
+
ranges: []
|
|
493
|
+
})), Match.exhaustive)(selection);
|
|
494
|
+
this._registry.update(this._state, (state) => ({
|
|
495
|
+
selections: {
|
|
496
|
+
...state.selections,
|
|
497
|
+
[contextId]: clearedSelection
|
|
498
|
+
}
|
|
499
|
+
}));
|
|
500
|
+
}
|
|
501
|
+
toggleSelection(contextId, id) {
|
|
502
|
+
const selection = this.getSelection(contextId, "multi");
|
|
503
|
+
invariant(selection?.mode === "multi", "Selection mode is not multi", { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 213, S: this, A: ["selection?.mode === 'multi'", "'Selection mode is not multi'"] });
|
|
504
|
+
const newIds = selection.ids.includes(id) ? selection.ids.filter((existingId) => existingId !== id) : [
|
|
505
|
+
...selection.ids,
|
|
506
|
+
id
|
|
507
|
+
];
|
|
508
|
+
this._registry.update(this._state, (state) => ({
|
|
509
|
+
selections: {
|
|
510
|
+
...state.selections,
|
|
511
|
+
[contextId]: {
|
|
512
|
+
...selection,
|
|
513
|
+
ids: newIds
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}));
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
// src/components/SelectionProvider.tsx
|
|
521
|
+
import { RegistryContext as RegistryContext2 } from "@effect-atom/atom-react";
|
|
522
|
+
import { createContext as createContext2 } from "@radix-ui/react-context";
|
|
523
|
+
import * as Match2 from "effect/Match";
|
|
524
|
+
import React2, { useCallback, useContext as useContext2, useEffect as useEffect2, useMemo as useMemo2, useState as useState2 } from "react";
|
|
525
|
+
import { useDefaultValue as useDefaultValue2 } from "@dxos/react-hooks";
|
|
526
|
+
var SELECTION_NAME = "Selection";
|
|
527
|
+
var [SelectionContextProvider, useSelectionContext] = createContext2(SELECTION_NAME, {
|
|
528
|
+
selection: void 0
|
|
529
|
+
});
|
|
530
|
+
var SelectionProvider = ({ children, selection: propsSelection }) => {
|
|
531
|
+
const registry = useContext2(RegistryContext2);
|
|
532
|
+
const selection = useDefaultValue2(propsSelection, () => new SelectionManager(registry));
|
|
533
|
+
return /* @__PURE__ */ React2.createElement(SelectionContextProvider, {
|
|
534
|
+
selection
|
|
535
|
+
}, children);
|
|
536
|
+
};
|
|
537
|
+
var useSelectionManager = () => {
|
|
538
|
+
const { selection } = useSelectionContext(SELECTION_NAME);
|
|
539
|
+
return selection;
|
|
540
|
+
};
|
|
541
|
+
var getDefaultResult = (mode) => {
|
|
542
|
+
return Match2.type().pipe(Match2.when({
|
|
543
|
+
mode: "single"
|
|
544
|
+
}, (selection) => selection.id), Match2.when({
|
|
545
|
+
mode: "multi"
|
|
546
|
+
}, (selection) => selection.ids), Match2.when({
|
|
547
|
+
mode: "range"
|
|
548
|
+
}, (selection) => selection.from && selection.to ? {
|
|
549
|
+
from: selection.from,
|
|
550
|
+
to: selection.to
|
|
551
|
+
} : void 0), Match2.when({
|
|
552
|
+
mode: "multi-range"
|
|
553
|
+
}, (selection) => selection.ranges), Match2.exhaustive)(defaultSelection(mode));
|
|
554
|
+
};
|
|
555
|
+
var useSelected = (contextId, mode = "multi") => {
|
|
556
|
+
const { selection } = useSelectionContext(SELECTION_NAME);
|
|
557
|
+
const [state, setState] = useState2(() => contextId && selection ? selection.getSelected(contextId, mode) : getDefaultResult(mode));
|
|
558
|
+
useEffect2(() => {
|
|
559
|
+
if (!contextId || !selection) {
|
|
560
|
+
setState(getDefaultResult(mode));
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
setState(selection.getSelected(contextId, mode));
|
|
564
|
+
return selection.subscribe(() => {
|
|
565
|
+
setState(selection.getSelected(contextId, mode));
|
|
566
|
+
});
|
|
567
|
+
}, [
|
|
568
|
+
selection,
|
|
569
|
+
contextId,
|
|
570
|
+
mode
|
|
571
|
+
]);
|
|
572
|
+
return state;
|
|
573
|
+
};
|
|
574
|
+
var useSelectionActions = (contextIds, mode = "multi") => {
|
|
575
|
+
const stableContextIds = useMemo2(() => contextIds, [
|
|
576
|
+
JSON.stringify(contextIds)
|
|
577
|
+
]);
|
|
578
|
+
const { selection } = useSelectionContext(SELECTION_NAME);
|
|
579
|
+
const singleSelect = useCallback((id) => {
|
|
580
|
+
if (!selection) {
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
for (const contextId of stableContextIds) {
|
|
584
|
+
selection.updateSingle(contextId, id);
|
|
585
|
+
}
|
|
586
|
+
}, [
|
|
587
|
+
selection,
|
|
588
|
+
stableContextIds
|
|
589
|
+
]);
|
|
590
|
+
const multiSelect = useCallback((ids) => {
|
|
591
|
+
if (!selection) {
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
for (const contextId of stableContextIds) {
|
|
595
|
+
selection.updateMulti(contextId, ids);
|
|
596
|
+
}
|
|
597
|
+
}, [
|
|
598
|
+
selection,
|
|
599
|
+
stableContextIds
|
|
600
|
+
]);
|
|
601
|
+
const rangeSelect = useCallback((from, to) => {
|
|
602
|
+
if (!selection) {
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
for (const contextId of stableContextIds) {
|
|
606
|
+
selection.updateRange(contextId, from, to);
|
|
607
|
+
}
|
|
608
|
+
}, [
|
|
609
|
+
selection,
|
|
610
|
+
stableContextIds
|
|
611
|
+
]);
|
|
612
|
+
const toggle = useCallback((id) => {
|
|
613
|
+
if (!selection) {
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
for (const contextId of stableContextIds) {
|
|
617
|
+
selection.toggleSelection(contextId, id);
|
|
618
|
+
}
|
|
619
|
+
}, [
|
|
620
|
+
selection,
|
|
621
|
+
stableContextIds
|
|
622
|
+
]);
|
|
623
|
+
const clear = useCallback(() => {
|
|
624
|
+
if (!selection) {
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
for (const contextId of stableContextIds) {
|
|
628
|
+
selection.clearSelection(contextId);
|
|
629
|
+
}
|
|
630
|
+
}, [
|
|
631
|
+
selection,
|
|
632
|
+
stableContextIds
|
|
633
|
+
]);
|
|
634
|
+
return {
|
|
635
|
+
singleSelect,
|
|
636
|
+
multiSelect,
|
|
637
|
+
rangeSelect,
|
|
638
|
+
toggle,
|
|
639
|
+
clear
|
|
640
|
+
};
|
|
641
|
+
};
|
|
642
|
+
|
|
643
|
+
export {
|
|
644
|
+
AttentionManager,
|
|
645
|
+
getAttendables,
|
|
646
|
+
expandAttendableId,
|
|
647
|
+
isLinkedSegment,
|
|
648
|
+
linkedSegment,
|
|
649
|
+
getLinkedVariant,
|
|
650
|
+
getParentId,
|
|
651
|
+
getSegmentId,
|
|
652
|
+
ATTENTION_NAME,
|
|
653
|
+
ATTENDABLE_ATTRIBUTE,
|
|
654
|
+
ATTENTION_SOURCE_ATTRIBUTE,
|
|
655
|
+
useAttentionContext,
|
|
656
|
+
useAttention,
|
|
657
|
+
useAttended,
|
|
658
|
+
useAttentionAttributes,
|
|
659
|
+
RootAttentionProvider,
|
|
660
|
+
AttendableContainer,
|
|
661
|
+
SelectionSchema,
|
|
662
|
+
defaultSelection,
|
|
663
|
+
getSelectionSet,
|
|
664
|
+
SelectionManager,
|
|
665
|
+
SelectionProvider,
|
|
666
|
+
useSelectionManager,
|
|
667
|
+
useSelected,
|
|
668
|
+
useSelectionActions
|
|
669
|
+
};
|
|
670
|
+
//# sourceMappingURL=chunk-HZTQDMTS.mjs.map
|