@dxos/react-ui-attention 0.8.4-main.67995b8 → 0.8.4-main.6fa680abb7

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