@dxos/react-ui-attention 0.8.4-main.dedc0f3 → 0.8.4-main.e00bdcdb52

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 (51) hide show
  1. package/dist/lib/browser/chunk-DAIZNFKL.mjs +668 -0
  2. package/dist/lib/browser/chunk-DAIZNFKL.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +84 -13
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +20 -5
  7. package/dist/lib/browser/testing/index.mjs.map +3 -3
  8. package/dist/lib/browser/types.mjs +1 -0
  9. package/dist/lib/browser/types.mjs.map +7 -0
  10. package/dist/lib/node-esm/chunk-HZTQDMTS.mjs +670 -0
  11. package/dist/lib/node-esm/chunk-HZTQDMTS.mjs.map +7 -0
  12. package/dist/lib/node-esm/index.mjs +84 -13
  13. package/dist/lib/node-esm/index.mjs.map +4 -4
  14. package/dist/lib/node-esm/meta.json +1 -1
  15. package/dist/lib/node-esm/testing/index.mjs +20 -5
  16. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  17. package/dist/lib/node-esm/types.mjs +2 -0
  18. package/dist/lib/node-esm/types.mjs.map +7 -0
  19. package/dist/types/src/attention.d.ts +56 -20
  20. package/dist/types/src/attention.d.ts.map +1 -1
  21. package/dist/types/src/components/AttentionGlyph.d.ts.map +1 -1
  22. package/dist/types/src/components/AttentionGlyph.stories.d.ts +0 -1
  23. package/dist/types/src/components/AttentionGlyph.stories.d.ts.map +1 -1
  24. package/dist/types/src/components/AttentionProvider.d.ts +13 -14
  25. package/dist/types/src/components/AttentionProvider.d.ts.map +1 -1
  26. package/dist/types/src/components/AttentionProvider.stories.d.ts +0 -1
  27. package/dist/types/src/components/AttentionProvider.stories.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/selection.d.ts +24 -4
  31. package/dist/types/src/selection.d.ts.map +1 -1
  32. package/dist/types/src/testing/decorators/withAttention.d.ts +5 -1
  33. package/dist/types/src/testing/decorators/withAttention.d.ts.map +1 -1
  34. package/dist/types/src/types.d.ts +9 -0
  35. package/dist/types/src/types.d.ts.map +1 -0
  36. package/dist/types/tsconfig.tsbuildinfo +1 -1
  37. package/package.json +33 -31
  38. package/src/attention.test.ts +188 -27
  39. package/src/attention.ts +175 -74
  40. package/src/components/AttentionGlyph.stories.tsx +3 -5
  41. package/src/components/AttentionGlyph.tsx +13 -6
  42. package/src/components/AttentionProvider.stories.tsx +3 -6
  43. package/src/components/AttentionProvider.tsx +79 -96
  44. package/src/components/SelectionProvider.tsx +63 -16
  45. package/src/selection.ts +113 -72
  46. package/src/testing/decorators/withAttention.ts +21 -4
  47. package/src/types.ts +13 -0
  48. package/dist/lib/browser/chunk-IEH7T3ZP.mjs +0 -674
  49. package/dist/lib/browser/chunk-IEH7T3ZP.mjs.map +0 -7
  50. package/dist/lib/node-esm/chunk-NFTXIHW4.mjs +0 -676
  51. package/dist/lib/node-esm/chunk-NFTXIHW4.mjs.map +0 -7
@@ -1,674 +0,0 @@
1
- // src/attention.ts
2
- import { untracked } from "@preact/signals-core";
3
- import { Schema } from "effect";
4
- import { live } from "@dxos/live-object";
5
- import { ComplexMap } from "@dxos/util";
6
- function _define_property(obj, key, value) {
7
- if (key in obj) {
8
- Object.defineProperty(obj, key, {
9
- value,
10
- enumerable: true,
11
- configurable: true,
12
- writable: true
13
- });
14
- } else {
15
- obj[key] = value;
16
- }
17
- return obj;
18
- }
19
- var ATTENDABLE_PATH_SEPARATOR = "~";
20
- var AttentionSchema = Schema.mutable(Schema.Struct({
21
- hasAttention: Schema.Boolean,
22
- isAncestor: Schema.Boolean,
23
- isRelated: Schema.Boolean
24
- }));
25
- var stringKey = (key) => key.join(",");
26
- var AttentionManager = class {
27
- /**
28
- * Currently attended element.
29
- *
30
- * @reactive
31
- */
32
- get current() {
33
- return this._state.current;
34
- }
35
- /**
36
- * All attention paths.
37
- */
38
- keys() {
39
- return Array.from(this._map.keys());
40
- }
41
- /**
42
- * Get the attention state for a given path.
43
- */
44
- get(key) {
45
- const object = this._map.get(key);
46
- if (object) {
47
- return object;
48
- }
49
- const newObject = live(AttentionSchema, {
50
- hasAttention: false,
51
- isAncestor: false,
52
- isRelated: false
53
- });
54
- this._map.set(key, newObject);
55
- return newObject;
56
- }
57
- /**
58
- * Update the currently attended element.
59
- *
60
- * @internal
61
- */
62
- update(nextKey) {
63
- const currentKey = untracked(() => this.current);
64
- const currentRelatedTo = currentKey[0];
65
- const nextRelatedTo = nextKey[0];
66
- this.get([
67
- nextRelatedTo
68
- ]);
69
- const currentRelatedKeys = this.keys().filter((key) => key[0] === currentRelatedTo);
70
- const nextRelatedKeys = this.keys().filter((key) => key[0] === nextRelatedTo && stringKey(key) !== stringKey(nextKey));
71
- const prevRelatedKeys = currentRelatedKeys.filter((curr) => !nextRelatedKeys.find((next) => stringKey(curr) === stringKey(next)));
72
- const currentAncestorKeys = currentKey.slice(1).map((_, i) => currentKey.slice(i + 1, currentKey.length));
73
- const nextAncestorKeys = nextKey.slice(1).map((_, i) => nextKey.slice(i + 1, nextKey.length));
74
- const prevAncestorKeys = currentAncestorKeys.filter((curr) => !nextAncestorKeys.find((next) => stringKey(curr) === stringKey(next)));
75
- this._state.current = nextKey;
76
- prevRelatedKeys.forEach((key) => this._set(key, {}));
77
- prevAncestorKeys.forEach((key) => this._set(key, {}));
78
- nextRelatedKeys.forEach((key) => this._set(key, {
79
- isRelated: true
80
- }));
81
- nextAncestorKeys.forEach((key) => this._set(key, {
82
- isAncestor: true
83
- }));
84
- this._set(nextKey, {
85
- hasAttention: true
86
- });
87
- }
88
- _set(key, attention) {
89
- const object = this.get(key);
90
- object.hasAttention = attention.hasAttention ?? false;
91
- object.isAncestor = attention.isAncestor ?? false;
92
- object.isRelated = attention.isRelated ?? false;
93
- }
94
- constructor(initial = []) {
95
- _define_property(this, "_map", new ComplexMap(stringKey));
96
- _define_property(this, "_state", live({
97
- current: []
98
- }));
99
- if (initial.length > 0) {
100
- this.update(initial);
101
- }
102
- }
103
- };
104
- var getAttendables = (selector, cursor, acc = []) => {
105
- const closestAttendable = cursor.closest(selector);
106
- if (closestAttendable) {
107
- const attendableId = closestAttendable.getAttribute("data-attendable-id");
108
- if (!attendableId) {
109
- const trigger = document.querySelector(`[aria-controls="${closestAttendable.getAttribute("id")}"]`);
110
- if (!trigger) {
111
- return acc;
112
- } else {
113
- return getAttendables(selector, trigger, acc);
114
- }
115
- } else {
116
- acc.push(...attendableId.split(ATTENDABLE_PATH_SEPARATOR));
117
- return !closestAttendable.parentElement ? acc : getAttendables(selector, closestAttendable.parentElement, acc);
118
- }
119
- }
120
- return [
121
- ...new Set(acc)
122
- ];
123
- };
124
-
125
- // src/components/AttentionGlyph.tsx
126
- import { useSignals as _useSignals } from "@preact-signals/safe-react/tracking";
127
- import React, { forwardRef, useMemo } from "react";
128
- import { Icon } from "@dxos/react-ui";
129
- import { mx } from "@dxos/react-ui-theme";
130
- var attentionGlyphStyles = "inline-block rounded-sm is-3 bs-3 bg-transparent text-accentText transition-colors [[data-contains-attended=true]_&]:bg-attentionRelated [[data-attention=true]_&]:bg-accentSurface [[data-attention=true]_&]:text-accentSurfaceText [[aria-current][data-attention=true]_&]:bg-accentSurface [[aria-current][data-attention=true]_&]:text-accentSurfaceText";
131
- var presenceIconStyles = "is-3 bs-3 group-[[aria-current]_&:hover]/attentionGlyphButton:hidden group-[[aria-current]_&:focus]/attentionGlyphButton:hidden group-[[data-attention=true]_&:hover]/attentionGlyphButton:hidden group-[[data-attention=true]_&:focus]/attentionGlyphButton:hidden group-[[aria-current][data-attention=true]_&:hover]/attentionGlyphButton:hidden group-[[aria-current][data-attention=true]_&:focus]/attentionGlyphButton:hidden";
132
- var PresenceOne = () => {
133
- var _effect = _useSignals();
134
- try {
135
- return /* @__PURE__ */ React.createElement("svg", {
136
- width: "12",
137
- height: "12",
138
- viewBox: "0 0 12 12",
139
- className: presenceIconStyles,
140
- fill: "none",
141
- xmlns: "http://www.w3.org/2000/svg"
142
- }, /* @__PURE__ */ React.createElement("circle", {
143
- cx: "6",
144
- cy: "6",
145
- r: "2.5",
146
- fill: "currentColor"
147
- }));
148
- } finally {
149
- _effect.f();
150
- }
151
- };
152
- var PresenceMany = () => {
153
- var _effect = _useSignals();
154
- try {
155
- return /* @__PURE__ */ React.createElement("svg", {
156
- width: "12",
157
- height: "12",
158
- viewBox: "0 0 12 12",
159
- className: presenceIconStyles,
160
- fill: "none",
161
- xmlns: "http://www.w3.org/2000/svg"
162
- }, /* @__PURE__ */ React.createElement("path", {
163
- d: "M6.75 8.27311C7.38815 7.72296 7.79212 6.90866 7.79212 6C7.79212 5.09134 7.38815 4.27704 6.75 3.72689C7.06722 3.58122 7.42019 3.5 7.79212 3.5C9.17283 3.5 10.2921 4.61929 10.2921 6C10.2921 7.38071 9.17283 8.5 7.79212 8.5C7.42019 8.5 7.06723 8.41878 6.75 8.27311Z",
164
- fill: "currentColor"
165
- }), /* @__PURE__ */ React.createElement("circle", {
166
- cx: "4.25",
167
- cy: "6",
168
- r: "2.5",
169
- fill: "currentColor"
170
- }));
171
- } finally {
172
- _effect.f();
173
- }
174
- };
175
- var Syncing = () => {
176
- var _effect = _useSignals();
177
- try {
178
- const animationProps = useMemo(() => ({
179
- // Synchronize animations.
180
- animationDelay: `-${Date.now() % 2e3}ms`
181
- }), []);
182
- return /* @__PURE__ */ React.createElement("div", {
183
- role: "status",
184
- className: "flex items-center"
185
- }, /* @__PURE__ */ React.createElement(Icon, {
186
- icon: "ph--circle-notch--bold",
187
- size: 3,
188
- style: animationProps,
189
- classNames: "text-subdued animate-[spin_2s_linear_infinite]"
190
- }));
191
- } finally {
192
- _effect.f();
193
- }
194
- };
195
- var AttentionGlyph = /* @__PURE__ */ forwardRef(({ presence, attended, syncing, containsAttended, classNames, ...props }, forwardedRef) => {
196
- var _effect = _useSignals();
197
- try {
198
- const icon = syncing ? /* @__PURE__ */ React.createElement(Syncing, null) : presence === "many" ? /* @__PURE__ */ React.createElement(PresenceMany, null) : presence === "one" ? /* @__PURE__ */ React.createElement(PresenceOne, null) : null;
199
- return /* @__PURE__ */ React.createElement("div", {
200
- role: "none",
201
- className: "flex",
202
- "data-attention": attended,
203
- "data-contains-attended": containsAttended
204
- }, /* @__PURE__ */ React.createElement("span", {
205
- role: "none",
206
- ...props,
207
- className: mx(attentionGlyphStyles, classNames),
208
- ref: forwardedRef
209
- }, icon));
210
- } finally {
211
- _effect.f();
212
- }
213
- });
214
-
215
- // src/components/AttentionProvider.tsx
216
- import { useSignals as _useSignals2 } from "@preact-signals/safe-react/tracking";
217
- import { createContext } from "@radix-ui/react-context";
218
- import { Primitive } from "@radix-ui/react-primitive";
219
- import { Slot } from "@radix-ui/react-slot";
220
- import React2, { forwardRef as forwardRef2, useMemo as useMemo2 } from "react";
221
- import { useDefaultValue } from "@dxos/react-ui";
222
- import { mx as mx2 } from "@dxos/react-ui-theme";
223
- var ATTENTION_NAME = "Attention";
224
- var ATTENABLE_ATTRIBUTE = "data-attendable-id";
225
- var ATTENTION_SOURCE_ATTRIBUTE = "data-is-attention-source";
226
- var [AttentionContextProvider, useAttentionContext] = createContext(ATTENTION_NAME, {
227
- attention: new AttentionManager(),
228
- path: []
229
- });
230
- var UNKNOWN_ATTENDABLE = {
231
- hasAttention: false,
232
- isAncestor: false,
233
- isRelated: false
234
- };
235
- var useAttention = (attendableId) => {
236
- const { attention, path } = useAttentionContext(ATTENTION_NAME);
237
- if (!attendableId) {
238
- return UNKNOWN_ATTENDABLE;
239
- }
240
- const current = [
241
- ...attendableId.split(ATTENDABLE_PATH_SEPARATOR),
242
- ...path
243
- ];
244
- return attention.get(current);
245
- };
246
- var useAttended = () => {
247
- const { attention } = useAttentionContext(ATTENTION_NAME);
248
- return attention.current;
249
- };
250
- var useAttentionAttributes = (attendableId) => {
251
- const { hasAttention } = useAttention(attendableId);
252
- return useMemo2(() => {
253
- const attributes = {
254
- [ATTENABLE_ATTRIBUTE]: attendableId
255
- };
256
- if (hasAttention) {
257
- attributes[ATTENTION_SOURCE_ATTRIBUTE] = "true";
258
- }
259
- return attributes;
260
- }, [
261
- attendableId,
262
- hasAttention
263
- ]);
264
- };
265
- var useAttentionPath = () => {
266
- const { path } = useAttentionContext(ATTENTION_NAME);
267
- return path;
268
- };
269
- var RootAttentionProvider = ({ children, attention: propsAttention, onChange }) => {
270
- var _effect = _useSignals2();
271
- try {
272
- const attention = useDefaultValue(propsAttention, () => new AttentionManager());
273
- const handleFocus = (event) => {
274
- const selector = [
275
- "[data-attendable-id]",
276
- ...Array.from(document.querySelectorAll("[aria-controls]")).map((el) => `[id="${el.getAttribute("aria-controls")}"]`)
277
- ].join(",");
278
- const prev = attention.current;
279
- const next = getAttendables(selector, event.target);
280
- if (next.length > 0 && (prev.length !== next.length || !!prev.find((id, index) => next[index] !== id))) {
281
- attention.update(next);
282
- onChange?.(next);
283
- }
284
- };
285
- return /* @__PURE__ */ React2.createElement(AttentionContextProvider, {
286
- attention,
287
- path: []
288
- }, /* @__PURE__ */ React2.createElement("div", {
289
- role: "none",
290
- className: "contents",
291
- onFocusCapture: handleFocus
292
- }, children));
293
- } finally {
294
- _effect.f();
295
- }
296
- };
297
- var AttentionProvider = ({ id, children }) => {
298
- var _effect = _useSignals2();
299
- try {
300
- const { attention, path } = useAttentionContext(ATTENTION_NAME);
301
- const nextPath = useMemo2(() => [
302
- id,
303
- ...path
304
- ], [
305
- id,
306
- path
307
- ]);
308
- return /* @__PURE__ */ React2.createElement(AttentionContextProvider, {
309
- attention,
310
- path: nextPath
311
- }, children);
312
- } finally {
313
- _effect.f();
314
- }
315
- };
316
- var AttendableContainer = /* @__PURE__ */ forwardRef2(({ id, classNames, children, asChild, ...props }, forwardedRef) => {
317
- var _effect = _useSignals2();
318
- try {
319
- const attentionAttrs = useAttentionAttributes(id);
320
- const Root = asChild ? Slot : Primitive.div;
321
- return /* @__PURE__ */ React2.createElement(Root, {
322
- role: "none",
323
- ...attentionAttrs,
324
- ...props,
325
- className: mx2("attention-surface", props.tabIndex === 0 && "dx-focus-ring-inset-over-all", classNames),
326
- ref: forwardedRef
327
- }, children);
328
- } finally {
329
- _effect.f();
330
- }
331
- });
332
-
333
- // src/selection.ts
334
- import { untracked as untracked2 } from "@preact/signals-core";
335
- import { Match, Schema as Schema2 } from "effect";
336
- import { invariant } from "@dxos/invariant";
337
- import { live as live2 } from "@dxos/live-object";
338
- function _define_property2(obj, key, value) {
339
- if (key in obj) {
340
- Object.defineProperty(obj, key, {
341
- value,
342
- enumerable: true,
343
- configurable: true,
344
- writable: true
345
- });
346
- } else {
347
- obj[key] = value;
348
- }
349
- return obj;
350
- }
351
- var __dxlog_file = "/__w/dxos/dxos/packages/ui/react-ui-attention/src/selection.ts";
352
- var SelectionSchema = Schema2.Union(Schema2.Struct({
353
- mode: Schema2.Literal("single"),
354
- id: Schema2.optional(Schema2.String)
355
- }).pipe(Schema2.mutable), Schema2.Struct({
356
- mode: Schema2.Literal("multi"),
357
- ids: Schema2.Array(Schema2.String).pipe(Schema2.mutable)
358
- }).pipe(Schema2.mutable), Schema2.Struct({
359
- mode: Schema2.Literal("range"),
360
- from: Schema2.optional(Schema2.String),
361
- to: Schema2.optional(Schema2.String)
362
- }).pipe(Schema2.mutable), Schema2.Struct({
363
- mode: Schema2.Literal("multi-range"),
364
- ranges: Schema2.Array(Schema2.Struct({
365
- from: Schema2.String,
366
- to: Schema2.String
367
- })).pipe(Schema2.mutable)
368
- }).pipe(Schema2.mutable)).pipe(Schema2.mutable);
369
- var defaultSelection = Match.type().pipe(Match.withReturnType(), Match.when("single", () => ({
370
- mode: "single"
371
- })), Match.when("multi", () => ({
372
- mode: "multi",
373
- ids: []
374
- })), Match.when("range", () => ({
375
- mode: "range"
376
- })), Match.when("multi-range", () => ({
377
- mode: "multi-range",
378
- ranges: []
379
- })), Match.exhaustive);
380
- var getSelectionSet = (selectionManager, contextId) => {
381
- const ids = new Set(contextId ? [
382
- contextId
383
- ] : []);
384
- for (const context of selectionManager.getSelectionContexts()) {
385
- const selection = selectionManager.getSelection(context);
386
- if (selection?.mode === "multi") {
387
- for (const id of selection.ids) {
388
- ids.add(id);
389
- }
390
- }
391
- }
392
- return ids;
393
- };
394
- var SelectionManager = class {
395
- getSelectionContexts() {
396
- return Object.keys(this._state.selections);
397
- }
398
- getSelection(contextId, mode = void 0) {
399
- const selection = untracked2(() => this._state.selections[contextId]);
400
- if (!mode || selection) {
401
- return selection;
402
- }
403
- return untracked2(() => {
404
- this._state.selections[contextId] = defaultSelection(mode);
405
- return this._state.selections[contextId];
406
- });
407
- }
408
- // TODO(burdon): Disambiguate with getSelection?
409
- getSelected(contextId, mode = "multi") {
410
- const selection = this.getSelection(contextId, mode);
411
- invariant(selection?.mode === mode, "Selection mode mismatch", {
412
- F: __dxlog_file,
413
- L: 104,
414
- S: this,
415
- A: [
416
- "selection?.mode === mode",
417
- "'Selection mode mismatch'"
418
- ]
419
- });
420
- return Match.type().pipe(Match.when({
421
- mode: "single"
422
- }, (s) => s.id), Match.when({
423
- mode: "multi"
424
- }, (s) => s.ids), Match.when({
425
- mode: "range"
426
- }, (s) => s.from && s.to ? {
427
- from: s.from,
428
- to: s.to
429
- } : void 0), Match.when({
430
- mode: "multi-range"
431
- }, (s) => s.ranges), Match.exhaustive)(selection);
432
- }
433
- updateSingle(contextId, id) {
434
- untracked2(() => {
435
- const selection = this.getSelection(contextId, "single");
436
- invariant(selection?.mode === "single", "Selection mode is not single", {
437
- F: __dxlog_file,
438
- L: 117,
439
- S: this,
440
- A: [
441
- "selection?.mode === 'single'",
442
- "'Selection mode is not single'"
443
- ]
444
- });
445
- selection.id = id;
446
- });
447
- }
448
- updateMulti(contextId, ids) {
449
- untracked2(() => {
450
- const selection = this.getSelection(contextId, "multi");
451
- invariant(selection?.mode === "multi", "Selection mode is not multi", {
452
- F: __dxlog_file,
453
- L: 125,
454
- S: this,
455
- A: [
456
- "selection?.mode === 'multi'",
457
- "'Selection mode is not multi'"
458
- ]
459
- });
460
- selection.ids.splice(0, selection.ids.length, ...ids);
461
- });
462
- }
463
- updateRange(contextId, from, to) {
464
- untracked2(() => {
465
- const selection = this.getSelection(contextId, "range");
466
- invariant(selection?.mode === "range", "Selection mode is not range", {
467
- F: __dxlog_file,
468
- L: 133,
469
- S: this,
470
- A: [
471
- "selection?.mode === 'range'",
472
- "'Selection mode is not range'"
473
- ]
474
- });
475
- selection.from = from;
476
- selection.to = to;
477
- });
478
- }
479
- updateMultiRange(contextId, ranges) {
480
- untracked2(() => {
481
- const selection = this.getSelection(contextId, "multi-range");
482
- invariant(selection?.mode === "multi-range", "Selection mode is not multi-range", {
483
- F: __dxlog_file,
484
- L: 142,
485
- S: this,
486
- A: [
487
- "selection?.mode === 'multi-range'",
488
- "'Selection mode is not multi-range'"
489
- ]
490
- });
491
- selection.ranges.splice(0, selection.ranges.length, ...ranges);
492
- });
493
- }
494
- clearSelection(contextId) {
495
- untracked2(() => {
496
- const selection = this.getSelection(contextId);
497
- Match.type().pipe(Match.when(void 0, () => {
498
- }), Match.when({
499
- mode: "single"
500
- }, (s) => {
501
- s.id = void 0;
502
- }), Match.when({
503
- mode: "multi"
504
- }, (s) => {
505
- s.ids.splice(0, s.ids.length);
506
- }), Match.when({
507
- mode: "range"
508
- }, (s) => {
509
- s.from = void 0;
510
- s.to = void 0;
511
- }), Match.when({
512
- mode: "multi-range"
513
- }, (s) => {
514
- s.ranges.splice(0, s.ranges.length);
515
- }), Match.exhaustive)(selection);
516
- });
517
- }
518
- toggleSelection(contextId, id) {
519
- untracked2(() => {
520
- const selection = this.getSelection(contextId, "multi");
521
- invariant(selection?.mode === "multi", "Selection mode is not multi", {
522
- F: __dxlog_file,
523
- L: 175,
524
- S: this,
525
- A: [
526
- "selection?.mode === 'multi'",
527
- "'Selection mode is not multi'"
528
- ]
529
- });
530
- if (selection.ids.includes(id)) {
531
- const index = selection.ids.indexOf(id);
532
- selection.ids.splice(index, 1);
533
- } else {
534
- selection.ids.push(id);
535
- }
536
- });
537
- }
538
- constructor(initial = {}) {
539
- _define_property2(this, "_state", live2({
540
- selections: {}
541
- }));
542
- if (Object.keys(initial).length > 0) {
543
- untracked2(() => {
544
- this._state.selections = initial;
545
- });
546
- }
547
- }
548
- };
549
-
550
- // src/components/SelectionProvider.tsx
551
- import { useSignals as _useSignals3 } from "@preact-signals/safe-react/tracking";
552
- import { createContext as createContext2 } from "@radix-ui/react-context";
553
- import { Match as Match2 } from "effect";
554
- import React3, { useCallback, useMemo as useMemo3 } from "react";
555
- import { useDefaultValue as useDefaultValue2 } from "@dxos/react-ui";
556
- var SELECTION_NAME = "Selection";
557
- var [SelectionContextProvider, useSelectionContext] = createContext2(SELECTION_NAME, {
558
- selection: new SelectionManager()
559
- });
560
- var SelectionProvider = ({ children, selection: propsSelection }) => {
561
- var _effect = _useSignals3();
562
- try {
563
- const selection = useDefaultValue2(propsSelection, () => new SelectionManager());
564
- return /* @__PURE__ */ React3.createElement(SelectionContextProvider, {
565
- selection
566
- }, children);
567
- } finally {
568
- _effect.f();
569
- }
570
- };
571
- var useSelectionManager = () => {
572
- const { selection } = useSelectionContext(SELECTION_NAME);
573
- return selection;
574
- };
575
- var useSelected = (contextId, mode = "multi") => {
576
- const { selection } = useSelectionContext(SELECTION_NAME);
577
- if (contextId) {
578
- return selection.getSelected(contextId, mode);
579
- }
580
- return Match2.type().pipe(Match2.when({
581
- mode: "single"
582
- }, (s) => s.id), Match2.when({
583
- mode: "multi"
584
- }, (s) => s.ids), Match2.when({
585
- mode: "range"
586
- }, (s) => s.from && s.to ? {
587
- from: s.from,
588
- to: s.to
589
- } : void 0), Match2.when({
590
- mode: "multi-range"
591
- }, (s) => s.ranges), Match2.exhaustive)(defaultSelection(mode));
592
- };
593
- var useSelectionActions = (contextIds, mode = "multi") => {
594
- const stableContextIds = useMemo3(() => contextIds, [
595
- JSON.stringify(contextIds)
596
- ]);
597
- const { selection } = useSelectionContext(SELECTION_NAME);
598
- const singleSelect = useCallback((id) => {
599
- for (const contextId of stableContextIds) {
600
- selection.updateSingle(contextId, id);
601
- }
602
- }, [
603
- selection,
604
- stableContextIds
605
- ]);
606
- const multiSelect = useCallback((ids) => {
607
- for (const contextId of stableContextIds) {
608
- selection.updateMulti(contextId, ids);
609
- }
610
- }, [
611
- selection,
612
- stableContextIds
613
- ]);
614
- const rangeSelect = useCallback((from, to) => {
615
- for (const contextId of stableContextIds) {
616
- selection.updateRange(contextId, from, to);
617
- }
618
- }, [
619
- selection,
620
- stableContextIds
621
- ]);
622
- const toggle = useCallback((id) => {
623
- for (const contextId of stableContextIds) {
624
- selection.toggleSelection(contextId, id);
625
- }
626
- }, [
627
- selection,
628
- stableContextIds
629
- ]);
630
- const clear = useCallback(() => {
631
- for (const contextId of stableContextIds) {
632
- selection.clearSelection(contextId);
633
- }
634
- }, [
635
- selection,
636
- stableContextIds
637
- ]);
638
- return {
639
- singleSelect,
640
- multiSelect,
641
- rangeSelect,
642
- toggle,
643
- clear
644
- };
645
- };
646
-
647
- export {
648
- ATTENDABLE_PATH_SEPARATOR,
649
- AttentionSchema,
650
- AttentionManager,
651
- getAttendables,
652
- Syncing,
653
- AttentionGlyph,
654
- ATTENTION_NAME,
655
- ATTENABLE_ATTRIBUTE,
656
- ATTENTION_SOURCE_ATTRIBUTE,
657
- useAttentionContext,
658
- useAttention,
659
- useAttended,
660
- useAttentionAttributes,
661
- useAttentionPath,
662
- RootAttentionProvider,
663
- AttentionProvider,
664
- AttendableContainer,
665
- SelectionSchema,
666
- defaultSelection,
667
- getSelectionSet,
668
- SelectionManager,
669
- SelectionProvider,
670
- useSelectionManager,
671
- useSelected,
672
- useSelectionActions
673
- };
674
- //# sourceMappingURL=chunk-IEH7T3ZP.mjs.map