@dxos/react-ui-attention 0.8.4-main.dedc0f3 → 0.8.4-main.e00bdcdb52
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.
- package/dist/lib/browser/chunk-DAIZNFKL.mjs +668 -0
- package/dist/lib/browser/chunk-DAIZNFKL.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +84 -13
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +20 -5
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/browser/types.mjs +1 -0
- package/dist/lib/browser/types.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-HZTQDMTS.mjs +670 -0
- package/dist/lib/node-esm/chunk-HZTQDMTS.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +84 -13
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +20 -5
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/types.mjs +2 -0
- package/dist/lib/node-esm/types.mjs.map +7 -0
- package/dist/types/src/attention.d.ts +56 -20
- package/dist/types/src/attention.d.ts.map +1 -1
- package/dist/types/src/components/AttentionGlyph.d.ts.map +1 -1
- package/dist/types/src/components/AttentionGlyph.stories.d.ts +0 -1
- package/dist/types/src/components/AttentionGlyph.stories.d.ts.map +1 -1
- package/dist/types/src/components/AttentionProvider.d.ts +13 -14
- package/dist/types/src/components/AttentionProvider.d.ts.map +1 -1
- package/dist/types/src/components/AttentionProvider.stories.d.ts +0 -1
- package/dist/types/src/components/AttentionProvider.stories.d.ts.map +1 -1
- package/dist/types/src/components/SelectionProvider.d.ts +5 -4
- package/dist/types/src/components/SelectionProvider.d.ts.map +1 -1
- package/dist/types/src/selection.d.ts +24 -4
- package/dist/types/src/selection.d.ts.map +1 -1
- package/dist/types/src/testing/decorators/withAttention.d.ts +5 -1
- package/dist/types/src/testing/decorators/withAttention.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +9 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +33 -31
- package/src/attention.test.ts +188 -27
- package/src/attention.ts +175 -74
- package/src/components/AttentionGlyph.stories.tsx +3 -5
- package/src/components/AttentionGlyph.tsx +13 -6
- package/src/components/AttentionProvider.stories.tsx +3 -6
- package/src/components/AttentionProvider.tsx +79 -96
- package/src/components/SelectionProvider.tsx +63 -16
- package/src/selection.ts +113 -72
- package/src/testing/decorators/withAttention.ts +21 -4
- package/src/types.ts +13 -0
- package/dist/lib/browser/chunk-IEH7T3ZP.mjs +0 -674
- package/dist/lib/browser/chunk-IEH7T3ZP.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-NFTXIHW4.mjs +0 -676
- package/dist/lib/node-esm/chunk-NFTXIHW4.mjs.map +0 -7
package/src/selection.ts
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
5
|
+
import { Atom, type Registry } from '@effect-atom/atom-react';
|
|
6
|
+
import * as Match from 'effect/Match';
|
|
7
|
+
import * as Schema from 'effect/Schema';
|
|
7
8
|
|
|
8
9
|
import { invariant } from '@dxos/invariant';
|
|
9
|
-
import { type Live, live } from '@dxos/live-object';
|
|
10
10
|
|
|
11
11
|
// TODO(burdon): Reconcile with @dxos/graph.
|
|
12
12
|
|
|
@@ -69,117 +69,158 @@ export const getSelectionSet = (selectionManager: SelectionManager, contextId?:
|
|
|
69
69
|
* Each context maintains its own selection mode and state.
|
|
70
70
|
*/
|
|
71
71
|
export class SelectionManager {
|
|
72
|
-
private readonly _state
|
|
72
|
+
private readonly _state: Atom.Writable<{ selections: Record<string, Selection> }>;
|
|
73
|
+
|
|
74
|
+
constructor(
|
|
75
|
+
private readonly _registry: Registry.Registry,
|
|
76
|
+
initial: Record<string, Selection> = {},
|
|
77
|
+
) {
|
|
78
|
+
this._state = Atom.make<{ selections: Record<string, Selection> }>({
|
|
79
|
+
selections: { ...initial },
|
|
80
|
+
});
|
|
81
|
+
}
|
|
73
82
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
83
|
+
/**
|
|
84
|
+
* Get the state atom for reactive access in the graph system.
|
|
85
|
+
*/
|
|
86
|
+
get state(): Atom.Atom<{ selections: Record<string, Selection> }> {
|
|
87
|
+
return this._state;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Gets the current state.
|
|
92
|
+
*/
|
|
93
|
+
getState(): { selections: Record<string, Selection> } {
|
|
94
|
+
return this._registry.get(this._state);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Subscribe to changes in the selection state.
|
|
99
|
+
*/
|
|
100
|
+
subscribe(cb: (state: { selections: Record<string, Selection> }) => void): () => void {
|
|
101
|
+
this._registry.get(this._state);
|
|
102
|
+
return this._registry.subscribe(this._state, () => {
|
|
103
|
+
cb(this._registry.get(this._state));
|
|
104
|
+
});
|
|
80
105
|
}
|
|
81
106
|
|
|
82
107
|
getSelectionContexts(): string[] {
|
|
83
|
-
return Object.keys(this._state.selections);
|
|
108
|
+
return Object.keys(this._registry.get(this._state).selections);
|
|
84
109
|
}
|
|
85
110
|
|
|
111
|
+
getSelection(contextId: string): Selection | undefined;
|
|
112
|
+
getSelection<T extends SelectionMode>(contextId: string, mode: T): Selection;
|
|
86
113
|
getSelection<T extends SelectionMode | undefined>(
|
|
87
114
|
contextId: string,
|
|
88
115
|
mode: T = undefined as T,
|
|
89
|
-
):
|
|
90
|
-
const
|
|
116
|
+
): Selection | undefined {
|
|
117
|
+
const state = this._registry.get(this._state);
|
|
118
|
+
const selection = state.selections[contextId];
|
|
91
119
|
if (!mode || selection) {
|
|
92
120
|
return selection;
|
|
93
121
|
}
|
|
94
122
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
123
|
+
// Create new selection for the context.
|
|
124
|
+
const newSelection = defaultSelection(mode);
|
|
125
|
+
this._registry.update(this._state, (state) => ({
|
|
126
|
+
selections: { ...state.selections, [contextId]: newSelection },
|
|
127
|
+
}));
|
|
128
|
+
return newSelection;
|
|
99
129
|
}
|
|
100
130
|
|
|
101
131
|
// TODO(burdon): Disambiguate with getSelection?
|
|
102
132
|
getSelected<T extends SelectionMode>(contextId: string, mode: T = 'multi' as T): SelectionResult<T> {
|
|
103
133
|
const selection = this.getSelection(contextId, mode);
|
|
104
134
|
invariant(selection?.mode === mode, 'Selection mode mismatch');
|
|
135
|
+
// Cast required because TypeScript can't infer the relationship between T and the matched result.
|
|
105
136
|
return Match.type<Selection>().pipe(
|
|
106
137
|
Match.when({ mode: 'single' }, (s) => s.id),
|
|
107
138
|
Match.when({ mode: 'multi' }, (s) => s.ids),
|
|
108
139
|
Match.when({ mode: 'range' }, (s) => (s.from && s.to ? { from: s.from, to: s.to } : undefined)),
|
|
109
140
|
Match.when({ mode: 'multi-range' }, (s) => s.ranges),
|
|
110
141
|
Match.exhaustive,
|
|
111
|
-
)(selection) as
|
|
142
|
+
)(selection) as SelectionResult<T>;
|
|
112
143
|
}
|
|
113
144
|
|
|
114
145
|
updateSingle(contextId: string, id: string): void {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
146
|
+
const selection = this.getSelection(contextId, 'single');
|
|
147
|
+
invariant(selection?.mode === 'single', 'Selection mode is not single');
|
|
148
|
+
this._registry.update(this._state, (state) => ({
|
|
149
|
+
selections: {
|
|
150
|
+
...state.selections,
|
|
151
|
+
[contextId]: { ...selection, id },
|
|
152
|
+
},
|
|
153
|
+
}));
|
|
120
154
|
}
|
|
121
155
|
|
|
122
156
|
updateMulti(contextId: string, ids: string[]) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
157
|
+
const selection = this.getSelection(contextId, 'multi');
|
|
158
|
+
invariant(selection?.mode === 'multi', 'Selection mode is not multi');
|
|
159
|
+
this._registry.update(this._state, (state) => ({
|
|
160
|
+
selections: {
|
|
161
|
+
...state.selections,
|
|
162
|
+
[contextId]: { ...selection, ids: [...ids] },
|
|
163
|
+
},
|
|
164
|
+
}));
|
|
128
165
|
}
|
|
129
166
|
|
|
130
167
|
updateRange(contextId: string, from: string, to: string) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
168
|
+
const selection = this.getSelection(contextId, 'range');
|
|
169
|
+
invariant(selection?.mode === 'range', 'Selection mode is not range');
|
|
170
|
+
this._registry.update(this._state, (state) => ({
|
|
171
|
+
selections: {
|
|
172
|
+
...state.selections,
|
|
173
|
+
[contextId]: { ...selection, from, to },
|
|
174
|
+
},
|
|
175
|
+
}));
|
|
137
176
|
}
|
|
138
177
|
|
|
139
178
|
updateMultiRange(contextId: string, ranges: { from: string; to: string }[]) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
179
|
+
const selection = this.getSelection(contextId, 'multi-range');
|
|
180
|
+
invariant(selection?.mode === 'multi-range', 'Selection mode is not multi-range');
|
|
181
|
+
this._registry.update(this._state, (state) => ({
|
|
182
|
+
selections: {
|
|
183
|
+
...state.selections,
|
|
184
|
+
[contextId]: { ...selection, ranges: [...ranges] },
|
|
185
|
+
},
|
|
186
|
+
}));
|
|
145
187
|
}
|
|
146
188
|
|
|
147
189
|
clearSelection(contextId: string): void {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
Match.exhaustive,
|
|
168
|
-
)(selection);
|
|
169
|
-
});
|
|
190
|
+
const selection = this.getSelection(contextId);
|
|
191
|
+
if (!selection) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const clearedSelection = Match.type<Selection>().pipe(
|
|
196
|
+
Match.when({ mode: 'single' }, (s) => ({ ...s, id: undefined })),
|
|
197
|
+
Match.when({ mode: 'multi' }, (s) => ({ ...s, ids: [] })),
|
|
198
|
+
Match.when({ mode: 'range' }, (s) => ({ ...s, from: undefined, to: undefined })),
|
|
199
|
+
Match.when({ mode: 'multi-range' }, (s) => ({ ...s, ranges: [] })),
|
|
200
|
+
Match.exhaustive,
|
|
201
|
+
)(selection);
|
|
202
|
+
|
|
203
|
+
this._registry.update(this._state, (state) => ({
|
|
204
|
+
selections: {
|
|
205
|
+
...state.selections,
|
|
206
|
+
[contextId]: clearedSelection,
|
|
207
|
+
},
|
|
208
|
+
}));
|
|
170
209
|
}
|
|
171
210
|
|
|
172
211
|
toggleSelection(contextId: string, id: string): void {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
212
|
+
const selection = this.getSelection(contextId, 'multi');
|
|
213
|
+
invariant(selection?.mode === 'multi', 'Selection mode is not multi');
|
|
214
|
+
|
|
215
|
+
const newIds = selection.ids.includes(id)
|
|
216
|
+
? selection.ids.filter((existingId) => existingId !== id)
|
|
217
|
+
: [...selection.ids, id];
|
|
218
|
+
|
|
219
|
+
this._registry.update(this._state, (state) => ({
|
|
220
|
+
selections: {
|
|
221
|
+
...state.selections,
|
|
222
|
+
[contextId]: { ...selection, ids: newIds },
|
|
223
|
+
},
|
|
224
|
+
}));
|
|
184
225
|
}
|
|
185
226
|
}
|
|
@@ -2,11 +2,28 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import { Registry, RegistryContext } from '@effect-atom/atom-react';
|
|
5
6
|
import { type Decorator } from '@storybook/react';
|
|
6
|
-
import { createElement } from 'react';
|
|
7
|
+
import { createElement, useMemo } from 'react';
|
|
7
8
|
|
|
8
|
-
import {
|
|
9
|
+
import { AttentionManager } from '../../attention';
|
|
10
|
+
import { RootAttentionProvider, SelectionProvider } from '../../components';
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Storybook decorator that provides attention context.
|
|
14
|
+
* @param initialAttendedId Optional ID to set as initially attended.
|
|
15
|
+
*/
|
|
16
|
+
export const withAttention = (initialAttendedId?: string): Decorator => {
|
|
17
|
+
return (Story) => {
|
|
18
|
+
const registry = useMemo(() => Registry.make(), []);
|
|
19
|
+
const attention = useMemo(
|
|
20
|
+
() => (initialAttendedId ? new AttentionManager(registry, [initialAttendedId]) : undefined),
|
|
21
|
+
[registry],
|
|
22
|
+
);
|
|
23
|
+
return createElement(
|
|
24
|
+
RegistryContext.Provider,
|
|
25
|
+
{ value: registry },
|
|
26
|
+
createElement(RootAttentionProvider, { attention }, createElement(SelectionProvider, {}, createElement(Story))),
|
|
27
|
+
);
|
|
28
|
+
};
|
|
12
29
|
};
|