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