@dxos/react-ui-attention 0.8.4-main.ae835ea → 0.8.4-main.bc674ce

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-HJRZWOEX.mjs +705 -0
  2. package/dist/lib/browser/chunk-HJRZWOEX.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-PAZBZOO2.mjs +7 -0
  4. package/dist/lib/browser/chunk-PAZBZOO2.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 +10 -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-7TQWDE4M.mjs +706 -0
  13. package/dist/lib/node-esm/chunk-7TQWDE4M.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 +10 -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.map +1 -1
  34. package/dist/types/src/types.d.ts +10 -0
  35. package/dist/types/src/types.d.ts.map +1 -0
  36. package/dist/types/tsconfig.tsbuildinfo +1 -1
  37. package/package.json +35 -23
  38. package/src/attention.test.ts +12 -7
  39. package/src/attention.ts +64 -42
  40. package/src/components/AttentionGlyph.stories.tsx +1 -1
  41. package/src/components/AttentionGlyph.tsx +1 -1
  42. package/src/components/AttentionProvider.tsx +69 -62
  43. package/src/components/SelectionProvider.tsx +60 -15
  44. package/src/index.ts +1 -0
  45. package/src/selection.ts +111 -71
  46. package/src/testing/decorators/withAttention.ts +9 -3
  47. package/src/types.ts +16 -0
  48. package/dist/lib/browser/chunk-BVWUINLI.mjs +0 -651
  49. package/dist/lib/browser/chunk-BVWUINLI.mjs.map +0 -7
  50. package/dist/lib/node-esm/chunk-J34PD35G.mjs +0 -653
  51. package/dist/lib/node-esm/chunk-J34PD35G.mjs.map +0 -7
@@ -1,653 +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 { useDefaultValue } from "@dxos/react-ui";
213
- import { mx as mx2 } from "@dxos/react-ui-theme";
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
- const selector = [
266
- "[data-attendable-id]",
267
- ...Array.from(document.querySelectorAll("[aria-controls]")).map((el) => `[id="${el.getAttribute("aria-controls")}"]`)
268
- ].join(",");
269
- const prev = attention.current;
270
- const next = getAttendables(selector, event.target);
271
- if (next.length > 0 && (prev.length !== next.length || !!prev.find((id, index) => next[index] !== id))) {
272
- attention.update(next);
273
- onChange?.(next);
274
- }
275
- };
276
- return /* @__PURE__ */ React2.createElement(AttentionContextProvider, {
277
- attention,
278
- path: []
279
- }, /* @__PURE__ */ React2.createElement("div", {
280
- role: "none",
281
- className: "contents",
282
- onFocusCapture: handleFocus
283
- }, children));
284
- } finally {
285
- _effect.f();
286
- }
287
- };
288
- var AttentionProvider = ({ id, children }) => {
289
- var _effect = _useSignals2();
290
- try {
291
- const { attention, path } = useAttentionContext(ATTENTION_NAME);
292
- const nextPath = useMemo2(() => [
293
- id,
294
- ...path
295
- ], [
296
- id,
297
- path
298
- ]);
299
- return /* @__PURE__ */ React2.createElement(AttentionContextProvider, {
300
- attention,
301
- path: nextPath
302
- }, children);
303
- } finally {
304
- _effect.f();
305
- }
306
- };
307
- var AttendableContainer = /* @__PURE__ */ forwardRef2(({ id, classNames, children, asChild, ...props }, forwardedRef) => {
308
- var _effect = _useSignals2();
309
- try {
310
- const attentionAttrs = useAttentionAttributes(id);
311
- const Root = asChild ? Slot : Primitive.div;
312
- return /* @__PURE__ */ React2.createElement(Root, {
313
- role: "none",
314
- ...attentionAttrs,
315
- ...props,
316
- className: mx2("attention-surface", props.tabIndex === 0 && "dx-focus-ring-inset-over-all", classNames),
317
- ref: forwardedRef
318
- }, children);
319
- } finally {
320
- _effect.f();
321
- }
322
- });
323
-
324
- // src/selection.ts
325
- import { untracked as untracked2 } from "@preact/signals-core";
326
- import * as Match from "effect/Match";
327
- import * as Schema2 from "effect/Schema";
328
- import { invariant } from "@dxos/invariant";
329
- import { live as live2 } from "@dxos/live-object";
330
- var __dxlog_file = "/__w/dxos/dxos/packages/ui/react-ui-attention/src/selection.ts";
331
- var SelectionSchema = Schema2.Union(Schema2.Struct({
332
- mode: Schema2.Literal("single"),
333
- id: Schema2.optional(Schema2.String)
334
- }).pipe(Schema2.mutable), Schema2.Struct({
335
- mode: Schema2.Literal("multi"),
336
- ids: Schema2.Array(Schema2.String).pipe(Schema2.mutable)
337
- }).pipe(Schema2.mutable), Schema2.Struct({
338
- mode: Schema2.Literal("range"),
339
- from: Schema2.optional(Schema2.String),
340
- to: Schema2.optional(Schema2.String)
341
- }).pipe(Schema2.mutable), Schema2.Struct({
342
- mode: Schema2.Literal("multi-range"),
343
- ranges: Schema2.Array(Schema2.Struct({
344
- from: Schema2.String,
345
- to: Schema2.String
346
- })).pipe(Schema2.mutable)
347
- }).pipe(Schema2.mutable)).pipe(Schema2.mutable);
348
- var defaultSelection = Match.type().pipe(Match.withReturnType(), Match.when("single", () => ({
349
- mode: "single"
350
- })), Match.when("multi", () => ({
351
- mode: "multi",
352
- ids: []
353
- })), Match.when("range", () => ({
354
- mode: "range"
355
- })), Match.when("multi-range", () => ({
356
- mode: "multi-range",
357
- ranges: []
358
- })), Match.exhaustive);
359
- var getSelectionSet = (selectionManager, contextId) => {
360
- const ids = new Set(contextId ? [
361
- contextId
362
- ] : []);
363
- for (const context of selectionManager.getSelectionContexts()) {
364
- const selection = selectionManager.getSelection(context);
365
- if (selection?.mode === "multi") {
366
- for (const id of selection.ids) {
367
- ids.add(id);
368
- }
369
- }
370
- }
371
- return ids;
372
- };
373
- var SelectionManager = class {
374
- _state = live2({
375
- selections: {}
376
- });
377
- constructor(initial = {}) {
378
- if (Object.keys(initial).length > 0) {
379
- untracked2(() => {
380
- this._state.selections = initial;
381
- });
382
- }
383
- }
384
- getSelectionContexts() {
385
- return Object.keys(this._state.selections);
386
- }
387
- getSelection(contextId, mode = void 0) {
388
- const selection = untracked2(() => this._state.selections[contextId]);
389
- if (!mode || selection) {
390
- return selection;
391
- }
392
- return untracked2(() => {
393
- this._state.selections[contextId] = defaultSelection(mode);
394
- return this._state.selections[contextId];
395
- });
396
- }
397
- // TODO(burdon): Disambiguate with getSelection?
398
- getSelected(contextId, mode = "multi") {
399
- const selection = this.getSelection(contextId, mode);
400
- invariant(selection?.mode === mode, "Selection mode mismatch", {
401
- F: __dxlog_file,
402
- L: 105,
403
- S: this,
404
- A: [
405
- "selection?.mode === mode",
406
- "'Selection mode mismatch'"
407
- ]
408
- });
409
- return Match.type().pipe(Match.when({
410
- mode: "single"
411
- }, (s) => s.id), Match.when({
412
- mode: "multi"
413
- }, (s) => s.ids), Match.when({
414
- mode: "range"
415
- }, (s) => s.from && s.to ? {
416
- from: s.from,
417
- to: s.to
418
- } : void 0), Match.when({
419
- mode: "multi-range"
420
- }, (s) => s.ranges), Match.exhaustive)(selection);
421
- }
422
- updateSingle(contextId, id) {
423
- untracked2(() => {
424
- const selection = this.getSelection(contextId, "single");
425
- invariant(selection?.mode === "single", "Selection mode is not single", {
426
- F: __dxlog_file,
427
- L: 118,
428
- S: this,
429
- A: [
430
- "selection?.mode === 'single'",
431
- "'Selection mode is not single'"
432
- ]
433
- });
434
- selection.id = id;
435
- });
436
- }
437
- updateMulti(contextId, ids) {
438
- untracked2(() => {
439
- const selection = this.getSelection(contextId, "multi");
440
- invariant(selection?.mode === "multi", "Selection mode is not multi", {
441
- F: __dxlog_file,
442
- L: 126,
443
- S: this,
444
- A: [
445
- "selection?.mode === 'multi'",
446
- "'Selection mode is not multi'"
447
- ]
448
- });
449
- selection.ids.splice(0, selection.ids.length, ...ids);
450
- });
451
- }
452
- updateRange(contextId, from, to) {
453
- untracked2(() => {
454
- const selection = this.getSelection(contextId, "range");
455
- invariant(selection?.mode === "range", "Selection mode is not range", {
456
- F: __dxlog_file,
457
- L: 134,
458
- S: this,
459
- A: [
460
- "selection?.mode === 'range'",
461
- "'Selection mode is not range'"
462
- ]
463
- });
464
- selection.from = from;
465
- selection.to = to;
466
- });
467
- }
468
- updateMultiRange(contextId, ranges) {
469
- untracked2(() => {
470
- const selection = this.getSelection(contextId, "multi-range");
471
- invariant(selection?.mode === "multi-range", "Selection mode is not multi-range", {
472
- F: __dxlog_file,
473
- L: 143,
474
- S: this,
475
- A: [
476
- "selection?.mode === 'multi-range'",
477
- "'Selection mode is not multi-range'"
478
- ]
479
- });
480
- selection.ranges.splice(0, selection.ranges.length, ...ranges);
481
- });
482
- }
483
- clearSelection(contextId) {
484
- untracked2(() => {
485
- const selection = this.getSelection(contextId);
486
- Match.type().pipe(Match.when(void 0, () => {
487
- }), Match.when({
488
- mode: "single"
489
- }, (s) => {
490
- s.id = void 0;
491
- }), Match.when({
492
- mode: "multi"
493
- }, (s) => {
494
- s.ids.splice(0, s.ids.length);
495
- }), Match.when({
496
- mode: "range"
497
- }, (s) => {
498
- s.from = void 0;
499
- s.to = void 0;
500
- }), Match.when({
501
- mode: "multi-range"
502
- }, (s) => {
503
- s.ranges.splice(0, s.ranges.length);
504
- }), Match.exhaustive)(selection);
505
- });
506
- }
507
- toggleSelection(contextId, id) {
508
- untracked2(() => {
509
- const selection = this.getSelection(contextId, "multi");
510
- invariant(selection?.mode === "multi", "Selection mode is not multi", {
511
- F: __dxlog_file,
512
- L: 176,
513
- S: this,
514
- A: [
515
- "selection?.mode === 'multi'",
516
- "'Selection mode is not multi'"
517
- ]
518
- });
519
- if (selection.ids.includes(id)) {
520
- const index = selection.ids.indexOf(id);
521
- selection.ids.splice(index, 1);
522
- } else {
523
- selection.ids.push(id);
524
- }
525
- });
526
- }
527
- };
528
-
529
- // src/components/SelectionProvider.tsx
530
- import { useSignals as _useSignals3 } from "@preact-signals/safe-react/tracking";
531
- import { createContext as createContext2 } from "@radix-ui/react-context";
532
- import * as Match2 from "effect/Match";
533
- import React3, { useCallback, useMemo as useMemo3 } from "react";
534
- import { useDefaultValue as useDefaultValue2 } from "@dxos/react-ui";
535
- var SELECTION_NAME = "Selection";
536
- var [SelectionContextProvider, useSelectionContext] = createContext2(SELECTION_NAME, {
537
- selection: new SelectionManager()
538
- });
539
- var SelectionProvider = ({ children, selection: propsSelection }) => {
540
- var _effect = _useSignals3();
541
- try {
542
- const selection = useDefaultValue2(propsSelection, () => new SelectionManager());
543
- return /* @__PURE__ */ React3.createElement(SelectionContextProvider, {
544
- selection
545
- }, children);
546
- } finally {
547
- _effect.f();
548
- }
549
- };
550
- var useSelectionManager = () => {
551
- const { selection } = useSelectionContext(SELECTION_NAME);
552
- return selection;
553
- };
554
- var useSelected = (contextId, mode = "multi") => {
555
- const { selection } = useSelectionContext(SELECTION_NAME);
556
- if (contextId) {
557
- return selection.getSelected(contextId, mode);
558
- }
559
- return Match2.type().pipe(Match2.when({
560
- mode: "single"
561
- }, (s) => s.id), Match2.when({
562
- mode: "multi"
563
- }, (s) => s.ids), Match2.when({
564
- mode: "range"
565
- }, (s) => s.from && s.to ? {
566
- from: s.from,
567
- to: s.to
568
- } : void 0), Match2.when({
569
- mode: "multi-range"
570
- }, (s) => s.ranges), Match2.exhaustive)(defaultSelection(mode));
571
- };
572
- var useSelectionActions = (contextIds, mode = "multi") => {
573
- const stableContextIds = useMemo3(() => contextIds, [
574
- JSON.stringify(contextIds)
575
- ]);
576
- const { selection } = useSelectionContext(SELECTION_NAME);
577
- const singleSelect = useCallback((id) => {
578
- for (const contextId of stableContextIds) {
579
- selection.updateSingle(contextId, id);
580
- }
581
- }, [
582
- selection,
583
- stableContextIds
584
- ]);
585
- const multiSelect = useCallback((ids) => {
586
- for (const contextId of stableContextIds) {
587
- selection.updateMulti(contextId, ids);
588
- }
589
- }, [
590
- selection,
591
- stableContextIds
592
- ]);
593
- const rangeSelect = useCallback((from, to) => {
594
- for (const contextId of stableContextIds) {
595
- selection.updateRange(contextId, from, to);
596
- }
597
- }, [
598
- selection,
599
- stableContextIds
600
- ]);
601
- const toggle = useCallback((id) => {
602
- for (const contextId of stableContextIds) {
603
- selection.toggleSelection(contextId, id);
604
- }
605
- }, [
606
- selection,
607
- stableContextIds
608
- ]);
609
- const clear = useCallback(() => {
610
- for (const contextId of stableContextIds) {
611
- selection.clearSelection(contextId);
612
- }
613
- }, [
614
- selection,
615
- stableContextIds
616
- ]);
617
- return {
618
- singleSelect,
619
- multiSelect,
620
- rangeSelect,
621
- toggle,
622
- clear
623
- };
624
- };
625
-
626
- export {
627
- ATTENDABLE_PATH_SEPARATOR,
628
- AttentionSchema,
629
- AttentionManager,
630
- getAttendables,
631
- Syncing,
632
- AttentionGlyph,
633
- ATTENTION_NAME,
634
- ATTENDABLE_ATTRIBUTE,
635
- ATTENTION_SOURCE_ATTRIBUTE,
636
- useAttentionContext,
637
- useAttention,
638
- useAttended,
639
- useAttentionAttributes,
640
- useAttentionPath,
641
- RootAttentionProvider,
642
- AttentionProvider,
643
- AttendableContainer,
644
- SelectionSchema,
645
- defaultSelection,
646
- getSelectionSet,
647
- SelectionManager,
648
- SelectionProvider,
649
- useSelectionManager,
650
- useSelected,
651
- useSelectionActions
652
- };
653
- //# sourceMappingURL=chunk-J34PD35G.mjs.map