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