@dxos/react-ui-attention 0.8.4-main.b97322e → 0.8.4-main.bcb3aa67d6

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