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

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