@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,676 +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 { Schema } from "effect";
6
- import { live } from "@dxos/live-object";
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 { Match, Schema as Schema2 } from "effect";
338
- import { invariant } from "@dxos/invariant";
339
- import { live as live2 } from "@dxos/live-object";
340
- function _define_property2(obj, key, value) {
341
- if (key in obj) {
342
- Object.defineProperty(obj, key, {
343
- value,
344
- enumerable: true,
345
- configurable: true,
346
- writable: true
347
- });
348
- } else {
349
- obj[key] = value;
350
- }
351
- return obj;
352
- }
353
- var __dxlog_file = "/__w/dxos/dxos/packages/ui/react-ui-attention/src/selection.ts";
354
- var SelectionSchema = Schema2.Union(Schema2.Struct({
355
- mode: Schema2.Literal("single"),
356
- id: Schema2.optional(Schema2.String)
357
- }).pipe(Schema2.mutable), Schema2.Struct({
358
- mode: Schema2.Literal("multi"),
359
- ids: Schema2.Array(Schema2.String).pipe(Schema2.mutable)
360
- }).pipe(Schema2.mutable), Schema2.Struct({
361
- mode: Schema2.Literal("range"),
362
- from: Schema2.optional(Schema2.String),
363
- to: Schema2.optional(Schema2.String)
364
- }).pipe(Schema2.mutable), Schema2.Struct({
365
- mode: Schema2.Literal("multi-range"),
366
- ranges: Schema2.Array(Schema2.Struct({
367
- from: Schema2.String,
368
- to: Schema2.String
369
- })).pipe(Schema2.mutable)
370
- }).pipe(Schema2.mutable)).pipe(Schema2.mutable);
371
- var defaultSelection = Match.type().pipe(Match.withReturnType(), Match.when("single", () => ({
372
- mode: "single"
373
- })), Match.when("multi", () => ({
374
- mode: "multi",
375
- ids: []
376
- })), Match.when("range", () => ({
377
- mode: "range"
378
- })), Match.when("multi-range", () => ({
379
- mode: "multi-range",
380
- ranges: []
381
- })), Match.exhaustive);
382
- var getSelectionSet = (selectionManager, contextId) => {
383
- const ids = new Set(contextId ? [
384
- contextId
385
- ] : []);
386
- for (const context of selectionManager.getSelectionContexts()) {
387
- const selection = selectionManager.getSelection(context);
388
- if (selection?.mode === "multi") {
389
- for (const id of selection.ids) {
390
- ids.add(id);
391
- }
392
- }
393
- }
394
- return ids;
395
- };
396
- var SelectionManager = class {
397
- getSelectionContexts() {
398
- return Object.keys(this._state.selections);
399
- }
400
- getSelection(contextId, mode = void 0) {
401
- const selection = untracked2(() => this._state.selections[contextId]);
402
- if (!mode || selection) {
403
- return selection;
404
- }
405
- return untracked2(() => {
406
- this._state.selections[contextId] = defaultSelection(mode);
407
- return this._state.selections[contextId];
408
- });
409
- }
410
- // TODO(burdon): Disambiguate with getSelection?
411
- getSelected(contextId, mode = "multi") {
412
- const selection = this.getSelection(contextId, mode);
413
- invariant(selection?.mode === mode, "Selection mode mismatch", {
414
- F: __dxlog_file,
415
- L: 104,
416
- S: this,
417
- A: [
418
- "selection?.mode === mode",
419
- "'Selection mode mismatch'"
420
- ]
421
- });
422
- return Match.type().pipe(Match.when({
423
- mode: "single"
424
- }, (s) => s.id), Match.when({
425
- mode: "multi"
426
- }, (s) => s.ids), Match.when({
427
- mode: "range"
428
- }, (s) => s.from && s.to ? {
429
- from: s.from,
430
- to: s.to
431
- } : void 0), Match.when({
432
- mode: "multi-range"
433
- }, (s) => s.ranges), Match.exhaustive)(selection);
434
- }
435
- updateSingle(contextId, id) {
436
- untracked2(() => {
437
- const selection = this.getSelection(contextId, "single");
438
- invariant(selection?.mode === "single", "Selection mode is not single", {
439
- F: __dxlog_file,
440
- L: 117,
441
- S: this,
442
- A: [
443
- "selection?.mode === 'single'",
444
- "'Selection mode is not single'"
445
- ]
446
- });
447
- selection.id = id;
448
- });
449
- }
450
- updateMulti(contextId, ids) {
451
- untracked2(() => {
452
- const selection = this.getSelection(contextId, "multi");
453
- invariant(selection?.mode === "multi", "Selection mode is not multi", {
454
- F: __dxlog_file,
455
- L: 125,
456
- S: this,
457
- A: [
458
- "selection?.mode === 'multi'",
459
- "'Selection mode is not multi'"
460
- ]
461
- });
462
- selection.ids.splice(0, selection.ids.length, ...ids);
463
- });
464
- }
465
- updateRange(contextId, from, to) {
466
- untracked2(() => {
467
- const selection = this.getSelection(contextId, "range");
468
- invariant(selection?.mode === "range", "Selection mode is not range", {
469
- F: __dxlog_file,
470
- L: 133,
471
- S: this,
472
- A: [
473
- "selection?.mode === 'range'",
474
- "'Selection mode is not range'"
475
- ]
476
- });
477
- selection.from = from;
478
- selection.to = to;
479
- });
480
- }
481
- updateMultiRange(contextId, ranges) {
482
- untracked2(() => {
483
- const selection = this.getSelection(contextId, "multi-range");
484
- invariant(selection?.mode === "multi-range", "Selection mode is not multi-range", {
485
- F: __dxlog_file,
486
- L: 142,
487
- S: this,
488
- A: [
489
- "selection?.mode === 'multi-range'",
490
- "'Selection mode is not multi-range'"
491
- ]
492
- });
493
- selection.ranges.splice(0, selection.ranges.length, ...ranges);
494
- });
495
- }
496
- clearSelection(contextId) {
497
- untracked2(() => {
498
- const selection = this.getSelection(contextId);
499
- Match.type().pipe(Match.when(void 0, () => {
500
- }), Match.when({
501
- mode: "single"
502
- }, (s) => {
503
- s.id = void 0;
504
- }), Match.when({
505
- mode: "multi"
506
- }, (s) => {
507
- s.ids.splice(0, s.ids.length);
508
- }), Match.when({
509
- mode: "range"
510
- }, (s) => {
511
- s.from = void 0;
512
- s.to = void 0;
513
- }), Match.when({
514
- mode: "multi-range"
515
- }, (s) => {
516
- s.ranges.splice(0, s.ranges.length);
517
- }), Match.exhaustive)(selection);
518
- });
519
- }
520
- toggleSelection(contextId, id) {
521
- untracked2(() => {
522
- const selection = this.getSelection(contextId, "multi");
523
- invariant(selection?.mode === "multi", "Selection mode is not multi", {
524
- F: __dxlog_file,
525
- L: 175,
526
- S: this,
527
- A: [
528
- "selection?.mode === 'multi'",
529
- "'Selection mode is not multi'"
530
- ]
531
- });
532
- if (selection.ids.includes(id)) {
533
- const index = selection.ids.indexOf(id);
534
- selection.ids.splice(index, 1);
535
- } else {
536
- selection.ids.push(id);
537
- }
538
- });
539
- }
540
- constructor(initial = {}) {
541
- _define_property2(this, "_state", live2({
542
- selections: {}
543
- }));
544
- if (Object.keys(initial).length > 0) {
545
- untracked2(() => {
546
- this._state.selections = initial;
547
- });
548
- }
549
- }
550
- };
551
-
552
- // src/components/SelectionProvider.tsx
553
- import { useSignals as _useSignals3 } from "@preact-signals/safe-react/tracking";
554
- import { createContext as createContext2 } from "@radix-ui/react-context";
555
- import { Match as Match2 } from "effect";
556
- import React3, { useCallback, useMemo as useMemo3 } from "react";
557
- import { useDefaultValue as useDefaultValue2 } from "@dxos/react-ui";
558
- var SELECTION_NAME = "Selection";
559
- var [SelectionContextProvider, useSelectionContext] = createContext2(SELECTION_NAME, {
560
- selection: new SelectionManager()
561
- });
562
- var SelectionProvider = ({ children, selection: propsSelection }) => {
563
- var _effect = _useSignals3();
564
- try {
565
- const selection = useDefaultValue2(propsSelection, () => new SelectionManager());
566
- return /* @__PURE__ */ React3.createElement(SelectionContextProvider, {
567
- selection
568
- }, children);
569
- } finally {
570
- _effect.f();
571
- }
572
- };
573
- var useSelectionManager = () => {
574
- const { selection } = useSelectionContext(SELECTION_NAME);
575
- return selection;
576
- };
577
- var useSelected = (contextId, mode = "multi") => {
578
- const { selection } = useSelectionContext(SELECTION_NAME);
579
- if (contextId) {
580
- return selection.getSelected(contextId, mode);
581
- }
582
- return Match2.type().pipe(Match2.when({
583
- mode: "single"
584
- }, (s) => s.id), Match2.when({
585
- mode: "multi"
586
- }, (s) => s.ids), Match2.when({
587
- mode: "range"
588
- }, (s) => s.from && s.to ? {
589
- from: s.from,
590
- to: s.to
591
- } : void 0), Match2.when({
592
- mode: "multi-range"
593
- }, (s) => s.ranges), Match2.exhaustive)(defaultSelection(mode));
594
- };
595
- var useSelectionActions = (contextIds, mode = "multi") => {
596
- const stableContextIds = useMemo3(() => contextIds, [
597
- JSON.stringify(contextIds)
598
- ]);
599
- const { selection } = useSelectionContext(SELECTION_NAME);
600
- const singleSelect = useCallback((id) => {
601
- for (const contextId of stableContextIds) {
602
- selection.updateSingle(contextId, id);
603
- }
604
- }, [
605
- selection,
606
- stableContextIds
607
- ]);
608
- const multiSelect = useCallback((ids) => {
609
- for (const contextId of stableContextIds) {
610
- selection.updateMulti(contextId, ids);
611
- }
612
- }, [
613
- selection,
614
- stableContextIds
615
- ]);
616
- const rangeSelect = useCallback((from, to) => {
617
- for (const contextId of stableContextIds) {
618
- selection.updateRange(contextId, from, to);
619
- }
620
- }, [
621
- selection,
622
- stableContextIds
623
- ]);
624
- const toggle = useCallback((id) => {
625
- for (const contextId of stableContextIds) {
626
- selection.toggleSelection(contextId, id);
627
- }
628
- }, [
629
- selection,
630
- stableContextIds
631
- ]);
632
- const clear = useCallback(() => {
633
- for (const contextId of stableContextIds) {
634
- selection.clearSelection(contextId);
635
- }
636
- }, [
637
- selection,
638
- stableContextIds
639
- ]);
640
- return {
641
- singleSelect,
642
- multiSelect,
643
- rangeSelect,
644
- toggle,
645
- clear
646
- };
647
- };
648
-
649
- export {
650
- ATTENDABLE_PATH_SEPARATOR,
651
- AttentionSchema,
652
- AttentionManager,
653
- getAttendables,
654
- Syncing,
655
- AttentionGlyph,
656
- ATTENTION_NAME,
657
- ATTENABLE_ATTRIBUTE,
658
- ATTENTION_SOURCE_ATTRIBUTE,
659
- useAttentionContext,
660
- useAttention,
661
- useAttended,
662
- useAttentionAttributes,
663
- useAttentionPath,
664
- RootAttentionProvider,
665
- AttentionProvider,
666
- AttendableContainer,
667
- SelectionSchema,
668
- defaultSelection,
669
- getSelectionSet,
670
- SelectionManager,
671
- SelectionProvider,
672
- useSelectionManager,
673
- useSelected,
674
- useSelectionActions
675
- };
676
- //# sourceMappingURL=chunk-NFTXIHW4.mjs.map