@dxos/react-ui-attention 0.8.4-main.406dc2a → 0.8.4-main.40e3dcdf1b

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