@dxos/react-ui-attention 0.8.4-main.e8ec1fe → 0.8.4-main.ef1bc66f44

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