@dxos/react-ui-attention 0.8.4-main.72ec0f3 → 0.8.4-main.7996785055

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.
Files changed (54) hide show
  1. package/dist/lib/browser/chunk-GMKHOVJ5.mjs +1 -0
  2. package/dist/lib/browser/chunk-GMKHOVJ5.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-TMCXF7JS.mjs +714 -0
  4. package/dist/lib/browser/chunk-TMCXF7JS.mjs.map +7 -0
  5. package/dist/lib/browser/index.mjs +79 -11
  6. package/dist/lib/browser/index.mjs.map +4 -4
  7. package/dist/lib/browser/meta.json +1 -1
  8. package/dist/lib/browser/testing/index.mjs +18 -4
  9. package/dist/lib/browser/testing/index.mjs.map +3 -3
  10. package/dist/lib/browser/types.mjs +2 -0
  11. package/dist/lib/browser/types.mjs.map +7 -0
  12. package/dist/lib/node-esm/chunk-IN7CM2ZH.mjs +716 -0
  13. package/dist/lib/node-esm/chunk-IN7CM2ZH.mjs.map +7 -0
  14. package/dist/lib/node-esm/chunk-UHVMYYIB.mjs +2 -0
  15. package/dist/lib/node-esm/chunk-UHVMYYIB.mjs.map +7 -0
  16. package/dist/lib/node-esm/index.mjs +79 -11
  17. package/dist/lib/node-esm/index.mjs.map +4 -4
  18. package/dist/lib/node-esm/meta.json +1 -1
  19. package/dist/lib/node-esm/testing/index.mjs +18 -4
  20. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  21. package/dist/lib/node-esm/types.mjs +3 -0
  22. package/dist/lib/node-esm/types.mjs.map +7 -0
  23. package/dist/types/src/attention.d.ts +44 -20
  24. package/dist/types/src/attention.d.ts.map +1 -1
  25. package/dist/types/src/components/AttentionGlyph.d.ts.map +1 -1
  26. package/dist/types/src/components/AttentionProvider.d.ts +12 -13
  27. package/dist/types/src/components/AttentionProvider.d.ts.map +1 -1
  28. package/dist/types/src/components/SelectionProvider.d.ts +5 -4
  29. package/dist/types/src/components/SelectionProvider.d.ts.map +1 -1
  30. package/dist/types/src/index.d.ts +1 -0
  31. package/dist/types/src/index.d.ts.map +1 -1
  32. package/dist/types/src/selection.d.ts +23 -3
  33. package/dist/types/src/selection.d.ts.map +1 -1
  34. package/dist/types/src/testing/decorators/withAttention.d.ts +5 -1
  35. package/dist/types/src/testing/decorators/withAttention.d.ts.map +1 -1
  36. package/dist/types/src/types.d.ts +9 -0
  37. package/dist/types/src/types.d.ts.map +1 -0
  38. package/dist/types/tsconfig.tsbuildinfo +1 -1
  39. package/package.json +36 -24
  40. package/src/attention.test.ts +161 -27
  41. package/src/attention.ts +154 -75
  42. package/src/components/AttentionGlyph.stories.tsx +3 -3
  43. package/src/components/AttentionGlyph.tsx +13 -6
  44. package/src/components/AttentionProvider.stories.tsx +2 -2
  45. package/src/components/AttentionProvider.tsx +69 -87
  46. package/src/components/SelectionProvider.tsx +62 -15
  47. package/src/index.ts +1 -0
  48. package/src/selection.ts +111 -71
  49. package/src/testing/decorators/withAttention.ts +20 -3
  50. package/src/types.ts +13 -0
  51. package/dist/lib/browser/chunk-MVIMDGR2.mjs +0 -662
  52. package/dist/lib/browser/chunk-MVIMDGR2.mjs.map +0 -7
  53. package/dist/lib/node-esm/chunk-NPW2V7JQ.mjs +0 -664
  54. package/dist/lib/node-esm/chunk-NPW2V7JQ.mjs.map +0 -7
@@ -0,0 +1,716 @@
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 has a
79
+ * separator-prefixed last segment (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 separatedParent = isSeparatorPrefixed(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 === separatedParent) {
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 ATTENDABLE_PATH_SEPARATOR = "~";
161
+ var expandAttendableId = (qualifiedId) => {
162
+ const segments = qualifiedId.split("/");
163
+ return segments.map((_, index) => segments.slice(0, index + 1).join("/"));
164
+ };
165
+ var isSeparatorPrefixed = (qualifiedId) => {
166
+ const lastSegment = qualifiedId.split("/").pop() ?? "";
167
+ return lastSegment.startsWith(ATTENDABLE_PATH_SEPARATOR);
168
+ };
169
+ var getParentId = (qualifiedId) => {
170
+ if (!qualifiedId) {
171
+ return void 0;
172
+ }
173
+ const lastSlash = qualifiedId.lastIndexOf("/");
174
+ return lastSlash > 0 ? qualifiedId.slice(0, lastSlash) : void 0;
175
+ };
176
+ var getSegmentId = (qualifiedId) => {
177
+ return qualifiedId.split("/").pop() ?? qualifiedId;
178
+ };
179
+
180
+ // src/components/AttentionProvider.tsx
181
+ import { RegistryContext } from "@effect-atom/atom-react";
182
+ import { createContext } from "@radix-ui/react-context";
183
+ import { Primitive } from "@radix-ui/react-primitive";
184
+ import { Slot } from "@radix-ui/react-slot";
185
+ import React, { forwardRef, useContext, useEffect, useMemo, useState } from "react";
186
+ import { log } from "@dxos/log";
187
+ import { useDefaultValue } from "@dxos/react-hooks";
188
+ import { mx } from "@dxos/ui-theme";
189
+ var __dxlog_file = "/__w/dxos/dxos/packages/ui/react-ui-attention/src/components/AttentionProvider.tsx";
190
+ var ATTENTION_NAME = "Attention";
191
+ var ATTENDABLE_ATTRIBUTE = "data-attendable-id";
192
+ var ATTENTION_SOURCE_ATTRIBUTE = "data-w-attention-source";
193
+ var [AttentionContextProvider, useAttentionContext] = createContext(ATTENTION_NAME, {
194
+ attention: void 0
195
+ });
196
+ var UNKNOWN_ATTENDABLE = {
197
+ hasAttention: false,
198
+ isAncestor: false,
199
+ isRelated: false
200
+ };
201
+ var useAttention = (attendableId) => {
202
+ const { attention } = useAttentionContext(ATTENTION_NAME);
203
+ const [state, setState] = useState(UNKNOWN_ATTENDABLE);
204
+ useEffect(() => {
205
+ if (!attendableId || !attention) {
206
+ setState(UNKNOWN_ATTENDABLE);
207
+ return;
208
+ }
209
+ const currentState = attention.get(attendableId);
210
+ setState(currentState);
211
+ return attention.subscribe(attendableId, (newState) => {
212
+ setState(newState);
213
+ });
214
+ }, [
215
+ attention,
216
+ attendableId
217
+ ]);
218
+ return state;
219
+ };
220
+ var useAttended = () => {
221
+ const { attention } = useAttentionContext(ATTENTION_NAME);
222
+ const [current, setCurrent] = useState([]);
223
+ useEffect(() => {
224
+ if (!attention) {
225
+ return;
226
+ }
227
+ setCurrent(attention.getCurrent());
228
+ return attention.subscribeCurrent((newCurrent) => {
229
+ setCurrent(newCurrent);
230
+ });
231
+ }, [
232
+ attention
233
+ ]);
234
+ return current;
235
+ };
236
+ var useAttentionAttributes = (attendableId) => {
237
+ const { hasAttention } = useAttention(attendableId);
238
+ return useMemo(() => {
239
+ const attributes = {
240
+ [ATTENDABLE_ATTRIBUTE]: attendableId
241
+ };
242
+ if (hasAttention) {
243
+ attributes[ATTENTION_SOURCE_ATTRIBUTE] = "true";
244
+ }
245
+ return attributes;
246
+ }, [
247
+ attendableId,
248
+ hasAttention
249
+ ]);
250
+ };
251
+ var RootAttentionProvider = ({ children, attention: propsAttention, onChange }) => {
252
+ const registry = useContext(RegistryContext);
253
+ const attention = useDefaultValue(propsAttention, () => new AttentionManager(registry));
254
+ const handleFocus = (event) => {
255
+ log("focus", {
256
+ related: event.relatedTarget,
257
+ target: event.target
258
+ }, {
259
+ F: __dxlog_file,
260
+ L: 115,
261
+ S: void 0,
262
+ C: (f, a) => f(...a)
263
+ });
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", {
397
+ F: __dxlog_file2,
398
+ L: 134,
399
+ S: this,
400
+ A: [
401
+ "selection?.mode === mode",
402
+ "'Selection mode mismatch'"
403
+ ]
404
+ });
405
+ return Match.type().pipe(Match.when({
406
+ mode: "single"
407
+ }, (s) => s.id), Match.when({
408
+ mode: "multi"
409
+ }, (s) => s.ids), Match.when({
410
+ mode: "range"
411
+ }, (s) => s.from && s.to ? {
412
+ from: s.from,
413
+ to: s.to
414
+ } : void 0), Match.when({
415
+ mode: "multi-range"
416
+ }, (s) => s.ranges), Match.exhaustive)(selection);
417
+ }
418
+ updateSingle(contextId, id) {
419
+ const selection = this.getSelection(contextId, "single");
420
+ invariant(selection?.mode === "single", "Selection mode is not single", {
421
+ F: __dxlog_file2,
422
+ L: 147,
423
+ S: this,
424
+ A: [
425
+ "selection?.mode === 'single'",
426
+ "'Selection mode is not single'"
427
+ ]
428
+ });
429
+ this._registry.update(this._state, (state) => ({
430
+ selections: {
431
+ ...state.selections,
432
+ [contextId]: {
433
+ ...selection,
434
+ id
435
+ }
436
+ }
437
+ }));
438
+ }
439
+ updateMulti(contextId, ids) {
440
+ const selection = this.getSelection(contextId, "multi");
441
+ invariant(selection?.mode === "multi", "Selection mode is not multi", {
442
+ F: __dxlog_file2,
443
+ L: 158,
444
+ S: this,
445
+ A: [
446
+ "selection?.mode === 'multi'",
447
+ "'Selection mode is not multi'"
448
+ ]
449
+ });
450
+ this._registry.update(this._state, (state) => ({
451
+ selections: {
452
+ ...state.selections,
453
+ [contextId]: {
454
+ ...selection,
455
+ ids: [
456
+ ...ids
457
+ ]
458
+ }
459
+ }
460
+ }));
461
+ }
462
+ updateRange(contextId, from, to) {
463
+ const selection = this.getSelection(contextId, "range");
464
+ invariant(selection?.mode === "range", "Selection mode is not range", {
465
+ F: __dxlog_file2,
466
+ L: 169,
467
+ S: this,
468
+ A: [
469
+ "selection?.mode === 'range'",
470
+ "'Selection mode is not range'"
471
+ ]
472
+ });
473
+ this._registry.update(this._state, (state) => ({
474
+ selections: {
475
+ ...state.selections,
476
+ [contextId]: {
477
+ ...selection,
478
+ from,
479
+ to
480
+ }
481
+ }
482
+ }));
483
+ }
484
+ updateMultiRange(contextId, ranges) {
485
+ const selection = this.getSelection(contextId, "multi-range");
486
+ invariant(selection?.mode === "multi-range", "Selection mode is not multi-range", {
487
+ F: __dxlog_file2,
488
+ L: 180,
489
+ S: this,
490
+ A: [
491
+ "selection?.mode === 'multi-range'",
492
+ "'Selection mode is not multi-range'"
493
+ ]
494
+ });
495
+ this._registry.update(this._state, (state) => ({
496
+ selections: {
497
+ ...state.selections,
498
+ [contextId]: {
499
+ ...selection,
500
+ ranges: [
501
+ ...ranges
502
+ ]
503
+ }
504
+ }
505
+ }));
506
+ }
507
+ clearSelection(contextId) {
508
+ const selection = this.getSelection(contextId);
509
+ if (!selection) {
510
+ return;
511
+ }
512
+ const clearedSelection = Match.type().pipe(Match.when({
513
+ mode: "single"
514
+ }, (s) => ({
515
+ ...s,
516
+ id: void 0
517
+ })), Match.when({
518
+ mode: "multi"
519
+ }, (s) => ({
520
+ ...s,
521
+ ids: []
522
+ })), Match.when({
523
+ mode: "range"
524
+ }, (s) => ({
525
+ ...s,
526
+ from: void 0,
527
+ to: void 0
528
+ })), Match.when({
529
+ mode: "multi-range"
530
+ }, (s) => ({
531
+ ...s,
532
+ ranges: []
533
+ })), Match.exhaustive)(selection);
534
+ this._registry.update(this._state, (state) => ({
535
+ selections: {
536
+ ...state.selections,
537
+ [contextId]: clearedSelection
538
+ }
539
+ }));
540
+ }
541
+ toggleSelection(contextId, id) {
542
+ const selection = this.getSelection(contextId, "multi");
543
+ invariant(selection?.mode === "multi", "Selection mode is not multi", {
544
+ F: __dxlog_file2,
545
+ L: 213,
546
+ S: this,
547
+ A: [
548
+ "selection?.mode === 'multi'",
549
+ "'Selection mode is not multi'"
550
+ ]
551
+ });
552
+ const newIds = selection.ids.includes(id) ? selection.ids.filter((existingId) => existingId !== id) : [
553
+ ...selection.ids,
554
+ id
555
+ ];
556
+ this._registry.update(this._state, (state) => ({
557
+ selections: {
558
+ ...state.selections,
559
+ [contextId]: {
560
+ ...selection,
561
+ ids: newIds
562
+ }
563
+ }
564
+ }));
565
+ }
566
+ };
567
+
568
+ // src/components/SelectionProvider.tsx
569
+ import { RegistryContext as RegistryContext2 } from "@effect-atom/atom-react";
570
+ import { createContext as createContext2 } from "@radix-ui/react-context";
571
+ import * as Match2 from "effect/Match";
572
+ import React2, { useCallback, useContext as useContext2, useEffect as useEffect2, useMemo as useMemo2, useState as useState2 } from "react";
573
+ import { useDefaultValue as useDefaultValue2 } from "@dxos/react-hooks";
574
+ var SELECTION_NAME = "Selection";
575
+ var [SelectionContextProvider, useSelectionContext] = createContext2(SELECTION_NAME, {
576
+ selection: void 0
577
+ });
578
+ var SelectionProvider = ({ children, selection: propsSelection }) => {
579
+ const registry = useContext2(RegistryContext2);
580
+ const selection = useDefaultValue2(propsSelection, () => new SelectionManager(registry));
581
+ return /* @__PURE__ */ React2.createElement(SelectionContextProvider, {
582
+ selection
583
+ }, children);
584
+ };
585
+ var useSelectionManager = () => {
586
+ const { selection } = useSelectionContext(SELECTION_NAME);
587
+ return selection;
588
+ };
589
+ var getDefaultResult = (mode) => {
590
+ return Match2.type().pipe(Match2.when({
591
+ mode: "single"
592
+ }, (selection) => selection.id), Match2.when({
593
+ mode: "multi"
594
+ }, (selection) => selection.ids), Match2.when({
595
+ mode: "range"
596
+ }, (selection) => selection.from && selection.to ? {
597
+ from: selection.from,
598
+ to: selection.to
599
+ } : void 0), Match2.when({
600
+ mode: "multi-range"
601
+ }, (selection) => selection.ranges), Match2.exhaustive)(defaultSelection(mode));
602
+ };
603
+ var useSelected = (contextId, mode = "multi") => {
604
+ const { selection } = useSelectionContext(SELECTION_NAME);
605
+ const [state, setState] = useState2(() => contextId && selection ? selection.getSelected(contextId, mode) : getDefaultResult(mode));
606
+ useEffect2(() => {
607
+ if (!contextId || !selection) {
608
+ setState(getDefaultResult(mode));
609
+ return;
610
+ }
611
+ setState(selection.getSelected(contextId, mode));
612
+ return selection.subscribe(() => {
613
+ setState(selection.getSelected(contextId, mode));
614
+ });
615
+ }, [
616
+ selection,
617
+ contextId,
618
+ mode
619
+ ]);
620
+ return state;
621
+ };
622
+ var useSelectionActions = (contextIds, mode = "multi") => {
623
+ const stableContextIds = useMemo2(() => contextIds, [
624
+ JSON.stringify(contextIds)
625
+ ]);
626
+ const { selection } = useSelectionContext(SELECTION_NAME);
627
+ const singleSelect = useCallback((id) => {
628
+ if (!selection) {
629
+ return;
630
+ }
631
+ for (const contextId of stableContextIds) {
632
+ selection.updateSingle(contextId, id);
633
+ }
634
+ }, [
635
+ selection,
636
+ stableContextIds
637
+ ]);
638
+ const multiSelect = useCallback((ids) => {
639
+ if (!selection) {
640
+ return;
641
+ }
642
+ for (const contextId of stableContextIds) {
643
+ selection.updateMulti(contextId, ids);
644
+ }
645
+ }, [
646
+ selection,
647
+ stableContextIds
648
+ ]);
649
+ const rangeSelect = useCallback((from, to) => {
650
+ if (!selection) {
651
+ return;
652
+ }
653
+ for (const contextId of stableContextIds) {
654
+ selection.updateRange(contextId, from, to);
655
+ }
656
+ }, [
657
+ selection,
658
+ stableContextIds
659
+ ]);
660
+ const toggle = useCallback((id) => {
661
+ if (!selection) {
662
+ return;
663
+ }
664
+ for (const contextId of stableContextIds) {
665
+ selection.toggleSelection(contextId, id);
666
+ }
667
+ }, [
668
+ selection,
669
+ stableContextIds
670
+ ]);
671
+ const clear = useCallback(() => {
672
+ if (!selection) {
673
+ return;
674
+ }
675
+ for (const contextId of stableContextIds) {
676
+ selection.clearSelection(contextId);
677
+ }
678
+ }, [
679
+ selection,
680
+ stableContextIds
681
+ ]);
682
+ return {
683
+ singleSelect,
684
+ multiSelect,
685
+ rangeSelect,
686
+ toggle,
687
+ clear
688
+ };
689
+ };
690
+
691
+ export {
692
+ AttentionManager,
693
+ getAttendables,
694
+ expandAttendableId,
695
+ isSeparatorPrefixed,
696
+ getParentId,
697
+ getSegmentId,
698
+ ATTENTION_NAME,
699
+ ATTENDABLE_ATTRIBUTE,
700
+ ATTENTION_SOURCE_ATTRIBUTE,
701
+ useAttentionContext,
702
+ useAttention,
703
+ useAttended,
704
+ useAttentionAttributes,
705
+ RootAttentionProvider,
706
+ AttendableContainer,
707
+ SelectionSchema,
708
+ defaultSelection,
709
+ getSelectionSet,
710
+ SelectionManager,
711
+ SelectionProvider,
712
+ useSelectionManager,
713
+ useSelected,
714
+ useSelectionActions
715
+ };
716
+ //# sourceMappingURL=chunk-IN7CM2ZH.mjs.map