@dxos/react-ui-attention 0.8.4-main.c4373fc → 0.8.4-main.c85a9c8dae

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