@dxos/react-ui-attention 0.8.2-main.fbd8ed0 → 0.8.2-staging.4d6ad0f

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.
@@ -0,0 +1,592 @@
1
+ // packages/ui/react-ui-attention/src/attention.ts
2
+ import { untracked } from "@preact/signals-core";
3
+ import { Schema } from "effect";
4
+ import { live } from "@dxos/live-object";
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
+ constructor(initial = []) {
15
+ // Each attention path is associated with an attention object.
16
+ // The lookup is not a reactive object to ensure that attention for each path is subscribable independently.
17
+ this._map = new ComplexMap(stringKey);
18
+ this._state = live({
19
+ current: []
20
+ });
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
+ // packages/ui/react-ui-attention/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
+ // packages/ui/react-ui-attention/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, { useMemo as useMemo2, forwardRef as forwardRef2 } from "react";
210
+ import { useDefaultValue } from "@dxos/react-ui";
211
+ import { mx as mx2 } from "@dxos/react-ui-theme";
212
+ var ATTENTION_NAME = "Attention";
213
+ var ATTENABLE_ATTRIBUTE = "data-attendable-id";
214
+ var ATTENTION_SOURCE_ATTRIBUTE = "data-is-attention-source";
215
+ var [AttentionContextProvider, useAttentionContext] = createContext(ATTENTION_NAME, {
216
+ attention: new AttentionManager(),
217
+ path: []
218
+ });
219
+ var UNKNOWN_ATTENDABLE = {
220
+ hasAttention: false,
221
+ isAncestor: false,
222
+ isRelated: false
223
+ };
224
+ var useAttention = (attendableId) => {
225
+ const { attention, path } = useAttentionContext(ATTENTION_NAME);
226
+ if (!attendableId) {
227
+ return UNKNOWN_ATTENDABLE;
228
+ }
229
+ const current = [
230
+ ...attendableId.split(ATTENDABLE_PATH_SEPARATOR),
231
+ ...path
232
+ ];
233
+ return attention.get(current);
234
+ };
235
+ var useAttended = () => {
236
+ const { attention } = useAttentionContext(ATTENTION_NAME);
237
+ return attention.current;
238
+ };
239
+ var useAttendableAttributes = (attendableId) => {
240
+ const { hasAttention } = useAttention(attendableId);
241
+ return useMemo2(() => {
242
+ const attributes = {
243
+ [ATTENABLE_ATTRIBUTE]: attendableId
244
+ };
245
+ if (hasAttention) {
246
+ attributes[ATTENTION_SOURCE_ATTRIBUTE] = "true";
247
+ }
248
+ return attributes;
249
+ }, [
250
+ attendableId,
251
+ hasAttention
252
+ ]);
253
+ };
254
+ var useAttentionPath = () => {
255
+ const { path } = useAttentionContext(ATTENTION_NAME);
256
+ return path;
257
+ };
258
+ var RootAttentionProvider = ({ children, attention: propsAttention, onChange }) => {
259
+ var _effect = _useSignals2();
260
+ try {
261
+ const attention = useDefaultValue(propsAttention, () => new AttentionManager());
262
+ const handleFocus = (event) => {
263
+ const selector = [
264
+ "[data-attendable-id]",
265
+ ...Array.from(document.querySelectorAll("[aria-controls]")).map((el) => `[id="${el.getAttribute("aria-controls")}"]`)
266
+ ].join(",");
267
+ const prev = attention.current;
268
+ const next = getAttendables(selector, event.target);
269
+ if (next.length > 0 && (prev.length !== next.length || !!prev.find((id, index) => next[index] !== id))) {
270
+ attention.update(next);
271
+ onChange?.(next);
272
+ }
273
+ };
274
+ return /* @__PURE__ */ React2.createElement(AttentionContextProvider, {
275
+ attention,
276
+ path: []
277
+ }, /* @__PURE__ */ React2.createElement("div", {
278
+ role: "none",
279
+ className: "contents",
280
+ onFocusCapture: handleFocus
281
+ }, children));
282
+ } finally {
283
+ _effect.f();
284
+ }
285
+ };
286
+ var AttentionProvider = ({ id, children }) => {
287
+ var _effect = _useSignals2();
288
+ try {
289
+ const { attention, path } = useAttentionContext(ATTENTION_NAME);
290
+ const nextPath = useMemo2(() => [
291
+ id,
292
+ ...path
293
+ ], [
294
+ id,
295
+ path
296
+ ]);
297
+ return /* @__PURE__ */ React2.createElement(AttentionContextProvider, {
298
+ attention,
299
+ path: nextPath
300
+ }, children);
301
+ } finally {
302
+ _effect.f();
303
+ }
304
+ };
305
+ var AttendableContainer = /* @__PURE__ */ forwardRef2(({ id, classNames, children, asChild, ...props }, forwardedRef) => {
306
+ var _effect = _useSignals2();
307
+ try {
308
+ const attendableAttrs = useAttendableAttributes(id);
309
+ const Root = asChild ? Slot : Primitive.div;
310
+ return /* @__PURE__ */ React2.createElement(Root, {
311
+ role: "none",
312
+ ...attendableAttrs,
313
+ ...props,
314
+ className: mx2("attention-surface", props.tabIndex === 0 && "dx-focus-ring-inset-over-all", classNames),
315
+ ref: forwardedRef
316
+ }, children);
317
+ } finally {
318
+ _effect.f();
319
+ }
320
+ });
321
+
322
+ // packages/ui/react-ui-attention/src/selection.ts
323
+ import { untracked as untracked2 } from "@preact/signals-core";
324
+ import { Match } from "effect";
325
+ import { invariant } from "@dxos/invariant";
326
+ import { live as live2 } from "@dxos/live-object";
327
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/ui/react-ui-attention/src/selection.ts";
328
+ var defaultSelection = Match.type().pipe(Match.when("single", () => ({
329
+ mode: "single"
330
+ })), Match.when("multi", () => ({
331
+ mode: "multi",
332
+ ids: []
333
+ })), Match.when("range", () => ({
334
+ mode: "range"
335
+ })), Match.when("multi-range", () => ({
336
+ mode: "multi-range",
337
+ ranges: []
338
+ })), Match.exhaustive);
339
+ var SelectionManager = class {
340
+ constructor(initial = {}) {
341
+ this._state = live2({
342
+ selections: {}
343
+ });
344
+ if (Object.keys(initial).length > 0) {
345
+ untracked2(() => {
346
+ this._state.selections = initial;
347
+ });
348
+ }
349
+ }
350
+ getSelection(contextId, mode = void 0) {
351
+ const selection = untracked2(() => this._state.selections[contextId]);
352
+ if (!mode || selection) {
353
+ return selection;
354
+ }
355
+ return untracked2(() => {
356
+ this._state.selections[contextId] = defaultSelection(mode);
357
+ return this._state.selections[contextId];
358
+ });
359
+ }
360
+ getSelected(contextId, mode = "multi") {
361
+ const selection = this.getSelection(contextId, mode);
362
+ invariant(selection?.mode === mode, "Selection mode mismatch", {
363
+ F: __dxlog_file,
364
+ L: 70,
365
+ S: this,
366
+ A: [
367
+ "selection?.mode === mode",
368
+ "'Selection mode mismatch'"
369
+ ]
370
+ });
371
+ return Match.type().pipe(Match.when({
372
+ mode: "single"
373
+ }, (s) => s.id), Match.when({
374
+ mode: "multi"
375
+ }, (s) => s.ids), Match.when({
376
+ mode: "range"
377
+ }, (s) => s.from && s.to ? {
378
+ from: s.from,
379
+ to: s.to
380
+ } : void 0), Match.when({
381
+ mode: "multi-range"
382
+ }, (s) => s.ranges), Match.exhaustive)(selection);
383
+ }
384
+ updateSingle(contextId, id) {
385
+ untracked2(() => {
386
+ const selection = this.getSelection(contextId, "single");
387
+ invariant(selection?.mode === "single", "Selection mode is not single", {
388
+ F: __dxlog_file,
389
+ L: 83,
390
+ S: this,
391
+ A: [
392
+ "selection?.mode === 'single'",
393
+ "'Selection mode is not single'"
394
+ ]
395
+ });
396
+ selection.id = id;
397
+ });
398
+ }
399
+ updateMulti(contextId, ids) {
400
+ untracked2(() => {
401
+ const selection = this.getSelection(contextId, "multi");
402
+ invariant(selection?.mode === "multi", "Selection mode is not multi", {
403
+ F: __dxlog_file,
404
+ L: 91,
405
+ S: this,
406
+ A: [
407
+ "selection?.mode === 'multi'",
408
+ "'Selection mode is not multi'"
409
+ ]
410
+ });
411
+ selection.ids = ids;
412
+ });
413
+ }
414
+ updateRange(contextId, from, to) {
415
+ untracked2(() => {
416
+ const selection = this.getSelection(contextId, "range");
417
+ invariant(selection?.mode === "range", "Selection mode is not range", {
418
+ F: __dxlog_file,
419
+ L: 99,
420
+ S: this,
421
+ A: [
422
+ "selection?.mode === 'range'",
423
+ "'Selection mode is not range'"
424
+ ]
425
+ });
426
+ selection.from = from;
427
+ selection.to = to;
428
+ });
429
+ }
430
+ updateMultiRange(contextId, ranges) {
431
+ untracked2(() => {
432
+ const selection = this.getSelection(contextId, "multi-range");
433
+ invariant(selection?.mode === "multi-range", "Selection mode is not multi-range", {
434
+ F: __dxlog_file,
435
+ L: 108,
436
+ S: this,
437
+ A: [
438
+ "selection?.mode === 'multi-range'",
439
+ "'Selection mode is not multi-range'"
440
+ ]
441
+ });
442
+ selection.ranges = ranges;
443
+ });
444
+ }
445
+ clearSelection(contextId) {
446
+ untracked2(() => {
447
+ const selection = this.getSelection(contextId);
448
+ if (selection) {
449
+ this._state.selections[contextId] = defaultSelection(selection.mode);
450
+ }
451
+ });
452
+ }
453
+ toggleSelection(contextId, id) {
454
+ untracked2(() => {
455
+ const selection = this.getSelection(contextId, "multi");
456
+ invariant(selection?.mode === "multi", "Selection mode is not multi", {
457
+ F: __dxlog_file,
458
+ L: 125,
459
+ S: this,
460
+ A: [
461
+ "selection?.mode === 'multi'",
462
+ "'Selection mode is not multi'"
463
+ ]
464
+ });
465
+ if (selection.ids.includes(id)) {
466
+ const index = selection.ids.indexOf(id);
467
+ selection.ids.splice(index, 1);
468
+ } else {
469
+ selection.ids.push(id);
470
+ }
471
+ });
472
+ }
473
+ };
474
+
475
+ // packages/ui/react-ui-attention/src/components/SelectionProvider.tsx
476
+ import { useSignals as _useSignals3 } from "@preact-signals/safe-react/tracking";
477
+ import { createContext as createContext2 } from "@radix-ui/react-context";
478
+ import { Match as Match2 } from "effect";
479
+ import React3, { useCallback, useMemo as useMemo3 } from "react";
480
+ import { useDefaultValue as useDefaultValue2 } from "@dxos/react-ui";
481
+ var SELECTION_NAME = "Selection";
482
+ var [SelectionContextProvider, useSelectionContext] = createContext2(SELECTION_NAME, {
483
+ selection: new SelectionManager()
484
+ });
485
+ var SelectionProvider = ({ children, selection: propsSelection }) => {
486
+ var _effect = _useSignals3();
487
+ try {
488
+ const selection = useDefaultValue2(propsSelection, () => new SelectionManager());
489
+ return /* @__PURE__ */ React3.createElement(SelectionContextProvider, {
490
+ selection
491
+ }, children);
492
+ } finally {
493
+ _effect.f();
494
+ }
495
+ };
496
+ var useSelected = (contextId, mode = "multi") => {
497
+ const { selection } = useSelectionContext(SELECTION_NAME);
498
+ if (contextId) {
499
+ return selection.getSelected(contextId, mode);
500
+ }
501
+ return Match2.type().pipe(Match2.when({
502
+ mode: "single"
503
+ }, (s) => s.id), Match2.when({
504
+ mode: "multi"
505
+ }, (s) => s.ids), Match2.when({
506
+ mode: "range"
507
+ }, (s) => s.from && s.to ? {
508
+ from: s.from,
509
+ to: s.to
510
+ } : void 0), Match2.when({
511
+ mode: "multi-range"
512
+ }, (s) => s.ranges), Match2.exhaustive)(defaultSelection(mode));
513
+ };
514
+ var useSelectionActions = (contextIds, mode = "multi") => {
515
+ const stableContextIds = useMemo3(() => contextIds, [
516
+ JSON.stringify(contextIds)
517
+ ]);
518
+ const { selection } = useSelectionContext(SELECTION_NAME);
519
+ const singleSelect = useCallback((id) => {
520
+ for (const contextId of stableContextIds) {
521
+ selection.updateSingle(contextId, id);
522
+ }
523
+ }, [
524
+ selection,
525
+ stableContextIds
526
+ ]);
527
+ const multiSelect = useCallback((ids) => {
528
+ for (const contextId of stableContextIds) {
529
+ selection.updateMulti(contextId, ids);
530
+ }
531
+ }, [
532
+ selection,
533
+ stableContextIds
534
+ ]);
535
+ const rangeSelect = useCallback((from, to) => {
536
+ for (const contextId of stableContextIds) {
537
+ selection.updateRange(contextId, from, to);
538
+ }
539
+ }, [
540
+ selection,
541
+ stableContextIds
542
+ ]);
543
+ const toggle = useCallback((id) => {
544
+ for (const contextId of stableContextIds) {
545
+ selection.toggleSelection(contextId, id);
546
+ }
547
+ }, [
548
+ selection,
549
+ stableContextIds
550
+ ]);
551
+ const clear = useCallback(() => {
552
+ for (const contextId of stableContextIds) {
553
+ selection.clearSelection(contextId);
554
+ }
555
+ }, [
556
+ selection,
557
+ stableContextIds
558
+ ]);
559
+ return {
560
+ singleSelect,
561
+ multiSelect,
562
+ rangeSelect,
563
+ toggle,
564
+ clear
565
+ };
566
+ };
567
+
568
+ export {
569
+ ATTENDABLE_PATH_SEPARATOR,
570
+ AttentionSchema,
571
+ AttentionManager,
572
+ getAttendables,
573
+ Syncing,
574
+ AttentionGlyph,
575
+ ATTENTION_NAME,
576
+ ATTENABLE_ATTRIBUTE,
577
+ ATTENTION_SOURCE_ATTRIBUTE,
578
+ useAttentionContext,
579
+ useAttention,
580
+ useAttended,
581
+ useAttendableAttributes,
582
+ useAttentionPath,
583
+ RootAttentionProvider,
584
+ AttentionProvider,
585
+ AttendableContainer,
586
+ defaultSelection,
587
+ SelectionManager,
588
+ SelectionProvider,
589
+ useSelected,
590
+ useSelectionActions
591
+ };
592
+ //# sourceMappingURL=chunk-4I5OVUE3.mjs.map