@dxos/react-ui-attention 0.8.4-main.7ace549 → 0.8.4-main.8360d9e660
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-PAZBZOO2.mjs +7 -0
- package/dist/lib/browser/chunk-PAZBZOO2.mjs.map +7 -0
- package/dist/lib/browser/chunk-Q5SLNQ4B.mjs +717 -0
- package/dist/lib/browser/chunk-Q5SLNQ4B.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +81 -8
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +19 -4
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/browser/types.mjs +7 -0
- package/dist/lib/browser/types.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-LSWTWT3W.mjs +718 -0
- package/dist/lib/node-esm/chunk-LSWTWT3W.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-PMGNPL4M.mjs +9 -0
- package/dist/lib/node-esm/chunk-PMGNPL4M.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +81 -8
- 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 +19 -4
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/types.mjs +8 -0
- package/dist/lib/node-esm/types.mjs.map +7 -0
- package/dist/types/src/attention.d.ts +44 -21
- 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/AttentionProvider.d.ts +12 -13
- package/dist/types/src/components/AttentionProvider.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/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/selection.d.ts +23 -3
- 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 +10 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +36 -24
- package/src/attention.test.ts +160 -26
- package/src/attention.ts +152 -74
- package/src/components/AttentionGlyph.stories.tsx +3 -3
- package/src/components/AttentionGlyph.tsx +13 -6
- package/src/components/AttentionProvider.stories.tsx +2 -2
- package/src/components/AttentionProvider.tsx +68 -86
- package/src/components/SelectionProvider.tsx +62 -15
- package/src/index.ts +1 -0
- package/src/selection.ts +111 -71
- package/src/testing/decorators/withAttention.ts +20 -3
- package/src/types.ts +16 -0
- package/dist/lib/browser/chunk-R3FWNCI3.mjs +0 -656
- package/dist/lib/browser/chunk-R3FWNCI3.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-IKC2JDMW.mjs +0 -658
- package/dist/lib/node-esm/chunk-IKC2JDMW.mjs.map +0 -7
|
@@ -16,7 +16,7 @@ const Attendable = ({ id }: { id: string }) => {
|
|
|
16
16
|
|
|
17
17
|
return (
|
|
18
18
|
<div {...attentionAttrs}>
|
|
19
|
-
<textarea className='attention-surface resize-none' placeholder={id} />
|
|
19
|
+
<textarea className='dx-attention-surface resize-none' placeholder={id} />
|
|
20
20
|
</div>
|
|
21
21
|
);
|
|
22
22
|
};
|
|
@@ -35,7 +35,7 @@ const Story = () => {
|
|
|
35
35
|
const meta = {
|
|
36
36
|
title: 'ui/react-ui-attention/AttentionProvider',
|
|
37
37
|
render: Story,
|
|
38
|
-
decorators: [withTheme, withAttention],
|
|
38
|
+
decorators: [withTheme(), withAttention()],
|
|
39
39
|
} satisfies Meta;
|
|
40
40
|
|
|
41
41
|
export default meta;
|
|
@@ -2,51 +2,88 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import { RegistryContext } from '@effect-atom/atom-react';
|
|
5
6
|
import { createContext } from '@radix-ui/react-context';
|
|
6
7
|
import { Primitive } from '@radix-ui/react-primitive';
|
|
7
8
|
import { Slot } from '@radix-ui/react-slot';
|
|
8
|
-
import React, {
|
|
9
|
+
import React, {
|
|
10
|
+
type ComponentPropsWithRef,
|
|
11
|
+
type FocusEvent,
|
|
12
|
+
type PropsWithChildren,
|
|
13
|
+
forwardRef,
|
|
14
|
+
useContext,
|
|
15
|
+
useEffect,
|
|
16
|
+
useMemo,
|
|
17
|
+
useState,
|
|
18
|
+
} from 'react';
|
|
9
19
|
|
|
10
20
|
import { log } from '@dxos/log';
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
21
|
+
import { useDefaultValue } from '@dxos/react-hooks';
|
|
22
|
+
import { type ThemedClassName } from '@dxos/react-ui';
|
|
23
|
+
import { mx } from '@dxos/ui-theme';
|
|
13
24
|
|
|
14
|
-
import {
|
|
25
|
+
import { AttentionManager, getAttendables } from '../attention';
|
|
26
|
+
import { type Attention } from '../types';
|
|
15
27
|
|
|
16
28
|
const ATTENTION_NAME = 'Attention';
|
|
17
29
|
const ATTENDABLE_ATTRIBUTE = 'data-attendable-id';
|
|
18
|
-
const ATTENTION_SOURCE_ATTRIBUTE = 'data-
|
|
30
|
+
const ATTENTION_SOURCE_ATTRIBUTE = 'data-w-attention-source';
|
|
19
31
|
|
|
20
32
|
type AttentionContextValue = {
|
|
21
33
|
attention: AttentionManager;
|
|
22
|
-
path: string[];
|
|
23
34
|
};
|
|
24
35
|
|
|
25
36
|
const [AttentionContextProvider, useAttentionContext] = createContext<AttentionContextValue>(ATTENTION_NAME, {
|
|
26
|
-
attention:
|
|
27
|
-
path: [],
|
|
37
|
+
attention: undefined as unknown as AttentionManager,
|
|
28
38
|
});
|
|
29
39
|
|
|
30
40
|
const UNKNOWN_ATTENDABLE = { hasAttention: false, isAncestor: false, isRelated: false } as Attention;
|
|
31
41
|
|
|
42
|
+
/**
|
|
43
|
+
* Subscribe to the attention state for a qualified graph ID.
|
|
44
|
+
*/
|
|
32
45
|
const useAttention = (attendableId?: string): Attention => {
|
|
33
|
-
const { attention
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
46
|
+
const { attention } = useAttentionContext(ATTENTION_NAME);
|
|
47
|
+
const [state, setState] = useState<Attention>(UNKNOWN_ATTENDABLE);
|
|
48
|
+
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (!attendableId || !attention) {
|
|
51
|
+
setState(UNKNOWN_ATTENDABLE);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const currentState = attention.get(attendableId);
|
|
56
|
+
setState(currentState);
|
|
57
|
+
|
|
58
|
+
return attention.subscribe(attendableId, (newState) => {
|
|
59
|
+
setState(newState);
|
|
60
|
+
});
|
|
61
|
+
}, [attention, attendableId]);
|
|
37
62
|
|
|
38
|
-
|
|
39
|
-
return attention.get(current);
|
|
63
|
+
return state;
|
|
40
64
|
};
|
|
41
65
|
|
|
42
66
|
const useAttended = () => {
|
|
43
67
|
const { attention } = useAttentionContext(ATTENTION_NAME);
|
|
44
|
-
|
|
68
|
+
const [current, setCurrent] = useState<readonly string[]>([]);
|
|
69
|
+
|
|
70
|
+
useEffect(() => {
|
|
71
|
+
if (!attention) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
setCurrent(attention.getCurrent());
|
|
76
|
+
|
|
77
|
+
return attention.subscribeCurrent((newCurrent) => {
|
|
78
|
+
setCurrent(newCurrent);
|
|
79
|
+
});
|
|
80
|
+
}, [attention]);
|
|
81
|
+
|
|
82
|
+
return current;
|
|
45
83
|
};
|
|
46
84
|
|
|
47
85
|
/**
|
|
48
86
|
* Computes HTML element attributes to apply so the attention system can detect changes.
|
|
49
|
-
* @param attendableId
|
|
50
87
|
*/
|
|
51
88
|
const useAttentionAttributes = (attendableId?: string) => {
|
|
52
89
|
const { hasAttention } = useAttention(attendableId);
|
|
@@ -60,11 +97,6 @@ const useAttentionAttributes = (attendableId?: string) => {
|
|
|
60
97
|
}, [attendableId, hasAttention]);
|
|
61
98
|
};
|
|
62
99
|
|
|
63
|
-
const useAttentionPath = () => {
|
|
64
|
-
const { path } = useAttentionContext(ATTENTION_NAME);
|
|
65
|
-
return path;
|
|
66
|
-
};
|
|
67
|
-
|
|
68
100
|
const RootAttentionProvider = ({
|
|
69
101
|
children,
|
|
70
102
|
attention: propsAttention,
|
|
@@ -75,7 +107,9 @@ const RootAttentionProvider = ({
|
|
|
75
107
|
onChange: (nextAttended: string[]) => void;
|
|
76
108
|
}>
|
|
77
109
|
>) => {
|
|
78
|
-
const
|
|
110
|
+
const registry = useContext(RegistryContext);
|
|
111
|
+
const attention = useDefaultValue(propsAttention, () => new AttentionManager(registry));
|
|
112
|
+
|
|
79
113
|
const handleFocus = (event: FocusEvent) => {
|
|
80
114
|
// NOTE(thure): Use the following to debug focus movement across the app:
|
|
81
115
|
log('focus', { related: event.relatedTarget, target: event.target });
|
|
@@ -86,7 +120,7 @@ const RootAttentionProvider = ({
|
|
|
86
120
|
(el) => `[id="${el.getAttribute('aria-controls')}"]`,
|
|
87
121
|
),
|
|
88
122
|
].join(',');
|
|
89
|
-
const prev = attention.
|
|
123
|
+
const prev = attention.getCurrent();
|
|
90
124
|
const next = getAttendables(selector, event.target);
|
|
91
125
|
// TODO(wittjosiah): Not allowing empty state means that the attended item is not strictly guaranteed to be in the DOM.
|
|
92
126
|
// Currently this depends on the deck in order to ensure that when the attended item is removed something else is attended.
|
|
@@ -97,102 +131,50 @@ const RootAttentionProvider = ({
|
|
|
97
131
|
}
|
|
98
132
|
};
|
|
99
133
|
|
|
100
|
-
// NOTE(thure): Use the following to debug the macOS package issue #8540:
|
|
101
|
-
|
|
102
|
-
// const [startEl, setStartEl] = useState<HTMLElement | null>(null);
|
|
103
|
-
// const [endEl, setEndEl] = useState<HTMLElement | null>(null);
|
|
104
|
-
//
|
|
105
|
-
// const handleEventDebug = useCallback((event: any) => {
|
|
106
|
-
// console.log(`[${event.type}]`, event.target, event.currentTarget, [event.clientX, event.clientY]);
|
|
107
|
-
// }, []);
|
|
108
|
-
//
|
|
109
|
-
// const handleStartDebug = useCallback(
|
|
110
|
-
// (event: any) => {
|
|
111
|
-
// setStartEl(event.target);
|
|
112
|
-
// handleEventDebug(event);
|
|
113
|
-
// },
|
|
114
|
-
// [handleEventDebug],
|
|
115
|
-
// );
|
|
116
|
-
//
|
|
117
|
-
// const handleEndDebug = useCallback(
|
|
118
|
-
// (event: any) => {
|
|
119
|
-
// setEndEl(event.target);
|
|
120
|
-
// handleEventDebug(event);
|
|
121
|
-
// },
|
|
122
|
-
// [handleEventDebug],
|
|
123
|
-
// );
|
|
124
|
-
//
|
|
125
|
-
// const handleClickDebug = useCallback(
|
|
126
|
-
// (event: any) => {
|
|
127
|
-
// console.log('[click compare]', startEl, endEl, startEl === endEl);
|
|
128
|
-
// handleEventDebug(event);
|
|
129
|
-
// },
|
|
130
|
-
// [startEl, endEl, handleEventDebug],
|
|
131
|
-
// );
|
|
132
|
-
|
|
133
134
|
return (
|
|
134
|
-
<AttentionContextProvider attention={attention}
|
|
135
|
-
<div
|
|
136
|
-
role='none'
|
|
137
|
-
className='contents'
|
|
138
|
-
onFocusCapture={handleFocus}
|
|
139
|
-
// onClick={handleClickDebug}
|
|
140
|
-
// onMouseDown={handleStartDebug}
|
|
141
|
-
// onMouseUp={handleEndDebug}
|
|
142
|
-
>
|
|
135
|
+
<AttentionContextProvider attention={attention}>
|
|
136
|
+
<div role='none' className='contents' onFocusCapture={handleFocus}>
|
|
143
137
|
{children}
|
|
144
138
|
</div>
|
|
145
139
|
</AttentionContextProvider>
|
|
146
140
|
);
|
|
147
141
|
};
|
|
148
142
|
|
|
149
|
-
const AttentionProvider = ({ id, children }: PropsWithChildren<{ id: string }>) => {
|
|
150
|
-
const { attention, path } = useAttentionContext(ATTENTION_NAME);
|
|
151
|
-
const nextPath = useMemo(() => [id, ...path], [id, path]);
|
|
152
|
-
|
|
153
|
-
return (
|
|
154
|
-
<AttentionContextProvider attention={attention} path={nextPath}>
|
|
155
|
-
{children}
|
|
156
|
-
</AttentionContextProvider>
|
|
157
|
-
);
|
|
158
|
-
};
|
|
159
|
-
|
|
160
143
|
export type AttendableContainerProps = ThemedClassName<
|
|
161
144
|
ComponentPropsWithRef<'div'> & { id: string; asChild?: boolean }
|
|
162
145
|
>;
|
|
163
146
|
|
|
164
147
|
/**
|
|
165
|
-
* Note that DeckPlugin and StackPlugin both handle attention on their own,
|
|
166
|
-
* is not necessary to also render an `AttendableContainer`.
|
|
167
|
-
*
|
|
148
|
+
* Note that DeckPlugin and StackPlugin both handle attention on their own,
|
|
149
|
+
* and when rendering content in those cases it is not necessary to also render an `AttendableContainer`.
|
|
150
|
+
* This component is primarily for Storybook stories and other testing scenarios,
|
|
151
|
+
* or the rare cases where an attendable entity is rendered outside of either of those plugins.
|
|
168
152
|
*/
|
|
169
153
|
const AttendableContainer = forwardRef<HTMLDivElement, AttendableContainerProps>(
|
|
170
154
|
({ id, classNames, children, asChild, ...props }, forwardedRef) => {
|
|
171
155
|
const attentionAttrs = useAttentionAttributes(id);
|
|
172
|
-
const
|
|
156
|
+
const Comp = asChild ? Slot : Primitive.div;
|
|
173
157
|
return (
|
|
174
|
-
<
|
|
158
|
+
<Comp
|
|
175
159
|
role='none'
|
|
176
|
-
{...attentionAttrs}
|
|
177
160
|
{...props}
|
|
178
|
-
|
|
161
|
+
{...attentionAttrs}
|
|
162
|
+
className={mx('dx-attention-surface', props.tabIndex === 0 && 'dx-focus-ring-inset-over-all', classNames)}
|
|
179
163
|
ref={forwardedRef}
|
|
180
164
|
>
|
|
181
165
|
{children}
|
|
182
|
-
</
|
|
166
|
+
</Comp>
|
|
183
167
|
);
|
|
184
168
|
},
|
|
185
169
|
);
|
|
186
170
|
|
|
187
171
|
export {
|
|
188
172
|
RootAttentionProvider,
|
|
189
|
-
AttentionProvider,
|
|
190
173
|
AttendableContainer,
|
|
191
174
|
useAttentionContext,
|
|
192
175
|
useAttention,
|
|
193
176
|
useAttended,
|
|
194
177
|
useAttentionAttributes,
|
|
195
|
-
useAttentionPath,
|
|
196
178
|
ATTENTION_NAME,
|
|
197
179
|
ATTENDABLE_ATTRIBUTE,
|
|
198
180
|
ATTENTION_SOURCE_ATTRIBUTE,
|
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import { RegistryContext } from '@effect-atom/atom-react';
|
|
5
6
|
import { createContext } from '@radix-ui/react-context';
|
|
6
7
|
import * as Match from 'effect/Match';
|
|
7
|
-
import React, { type PropsWithChildren, useCallback, useMemo } from 'react';
|
|
8
|
+
import React, { type PropsWithChildren, useCallback, useContext, useEffect, useMemo, useState } from 'react';
|
|
8
9
|
|
|
9
|
-
import { useDefaultValue } from '@dxos/react-
|
|
10
|
+
import { useDefaultValue } from '@dxos/react-hooks';
|
|
10
11
|
|
|
11
12
|
import {
|
|
12
13
|
type Selection,
|
|
@@ -23,7 +24,7 @@ type SelectionContextValue = {
|
|
|
23
24
|
};
|
|
24
25
|
|
|
25
26
|
const [SelectionContextProvider, useSelectionContext] = createContext<SelectionContextValue>(SELECTION_NAME, {
|
|
26
|
-
selection:
|
|
27
|
+
selection: undefined as unknown as SelectionManager,
|
|
27
28
|
});
|
|
28
29
|
|
|
29
30
|
/**
|
|
@@ -34,7 +35,8 @@ export const SelectionProvider = ({
|
|
|
34
35
|
children,
|
|
35
36
|
selection: propsSelection,
|
|
36
37
|
}: PropsWithChildren<{ selection?: SelectionManager }>) => {
|
|
37
|
-
const
|
|
38
|
+
const registry = useContext(RegistryContext);
|
|
39
|
+
const selection = useDefaultValue(propsSelection, () => new SelectionManager(registry));
|
|
38
40
|
return <SelectionContextProvider selection={selection}>{children}</SelectionContextProvider>;
|
|
39
41
|
};
|
|
40
42
|
|
|
@@ -46,6 +48,18 @@ export const useSelectionManager = () => {
|
|
|
46
48
|
return selection;
|
|
47
49
|
};
|
|
48
50
|
|
|
51
|
+
const getDefaultResult = <T extends SelectionMode>(mode: T): SelectionResult<T> => {
|
|
52
|
+
return Match.type<Selection>().pipe(
|
|
53
|
+
Match.when({ mode: 'single' }, (selection) => selection.id),
|
|
54
|
+
Match.when({ mode: 'multi' }, (selection) => selection.ids),
|
|
55
|
+
Match.when({ mode: 'range' }, (selection) =>
|
|
56
|
+
selection.from && selection.to ? { from: selection.from, to: selection.to } : undefined,
|
|
57
|
+
),
|
|
58
|
+
Match.when({ mode: 'multi-range' }, (selection) => selection.ranges),
|
|
59
|
+
Match.exhaustive,
|
|
60
|
+
)(defaultSelection(mode)) as SelectionResult<T>;
|
|
61
|
+
};
|
|
62
|
+
|
|
49
63
|
/**
|
|
50
64
|
* Get the selected objects for a given context.
|
|
51
65
|
*/
|
|
@@ -54,28 +68,49 @@ export const useSelected = <T extends SelectionMode>(
|
|
|
54
68
|
mode: T = 'multi' as T,
|
|
55
69
|
): SelectionResult<T> => {
|
|
56
70
|
const { selection } = useSelectionContext(SELECTION_NAME);
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
71
|
+
const [state, setState] = useState<SelectionResult<T>>(() =>
|
|
72
|
+
contextId && selection ? selection.getSelected(contextId, mode) : getDefaultResult(mode),
|
|
73
|
+
);
|
|
60
74
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
if (!contextId || !selection) {
|
|
77
|
+
setState(getDefaultResult(mode));
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Set initial state.
|
|
82
|
+
setState(selection.getSelected(contextId, mode));
|
|
83
|
+
|
|
84
|
+
// Subscribe to changes.
|
|
85
|
+
return selection.subscribe(() => {
|
|
86
|
+
setState(selection.getSelected(contextId, mode));
|
|
87
|
+
});
|
|
88
|
+
}, [selection, contextId, mode]);
|
|
89
|
+
|
|
90
|
+
return state;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export type UseSelectionActions = {
|
|
94
|
+
singleSelect: (id: string) => void;
|
|
95
|
+
multiSelect: (ids: string[]) => void;
|
|
96
|
+
rangeSelect: (from: string, to: string) => void;
|
|
97
|
+
toggle: (id: string) => void;
|
|
98
|
+
clear: () => void;
|
|
68
99
|
};
|
|
69
100
|
|
|
70
101
|
/**
|
|
71
102
|
* Provides functions to manage the selection state for multiple contexts.
|
|
72
103
|
*/
|
|
73
|
-
|
|
104
|
+
// TODO(burdon): Mode not used.
|
|
105
|
+
export const useSelectionActions = (contextIds: string[], mode: SelectionMode = 'multi'): UseSelectionActions => {
|
|
74
106
|
const stableContextIds = useMemo(() => contextIds, [JSON.stringify(contextIds)]); // TODO(burdon): Avoid stringify.
|
|
75
107
|
const { selection } = useSelectionContext(SELECTION_NAME);
|
|
76
108
|
|
|
77
109
|
const singleSelect = useCallback(
|
|
78
110
|
(id: string) => {
|
|
111
|
+
if (!selection) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
79
114
|
for (const contextId of stableContextIds) {
|
|
80
115
|
selection.updateSingle(contextId, id);
|
|
81
116
|
}
|
|
@@ -85,6 +120,9 @@ export const useSelectionActions = (contextIds: string[], mode: SelectionMode =
|
|
|
85
120
|
|
|
86
121
|
const multiSelect = useCallback(
|
|
87
122
|
(ids: string[]) => {
|
|
123
|
+
if (!selection) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
88
126
|
for (const contextId of stableContextIds) {
|
|
89
127
|
selection.updateMulti(contextId, ids);
|
|
90
128
|
}
|
|
@@ -94,6 +132,9 @@ export const useSelectionActions = (contextIds: string[], mode: SelectionMode =
|
|
|
94
132
|
|
|
95
133
|
const rangeSelect = useCallback(
|
|
96
134
|
(from: string, to: string) => {
|
|
135
|
+
if (!selection) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
97
138
|
for (const contextId of stableContextIds) {
|
|
98
139
|
selection.updateRange(contextId, from, to);
|
|
99
140
|
}
|
|
@@ -103,6 +144,9 @@ export const useSelectionActions = (contextIds: string[], mode: SelectionMode =
|
|
|
103
144
|
|
|
104
145
|
const toggle = useCallback(
|
|
105
146
|
(id: string) => {
|
|
147
|
+
if (!selection) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
106
150
|
for (const contextId of stableContextIds) {
|
|
107
151
|
selection.toggleSelection(contextId, id);
|
|
108
152
|
}
|
|
@@ -111,6 +155,9 @@ export const useSelectionActions = (contextIds: string[], mode: SelectionMode =
|
|
|
111
155
|
);
|
|
112
156
|
|
|
113
157
|
const clear = useCallback(() => {
|
|
158
|
+
if (!selection) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
114
161
|
for (const contextId of stableContextIds) {
|
|
115
162
|
selection.clearSelection(contextId);
|
|
116
163
|
}
|
package/src/index.ts
CHANGED
package/src/selection.ts
CHANGED
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { Atom, type Registry } from '@effect-atom/atom-react';
|
|
6
6
|
import * as Match from 'effect/Match';
|
|
7
7
|
import * as Schema from 'effect/Schema';
|
|
8
8
|
|
|
9
9
|
import { invariant } from '@dxos/invariant';
|
|
10
|
-
import { type Live, live } from '@dxos/live-object';
|
|
11
10
|
|
|
12
11
|
// TODO(burdon): Reconcile with @dxos/graph.
|
|
13
12
|
|
|
@@ -70,117 +69,158 @@ export const getSelectionSet = (selectionManager: SelectionManager, contextId?:
|
|
|
70
69
|
* Each context maintains its own selection mode and state.
|
|
71
70
|
*/
|
|
72
71
|
export class SelectionManager {
|
|
73
|
-
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
|
+
}
|
|
74
82
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
+
});
|
|
81
105
|
}
|
|
82
106
|
|
|
83
107
|
getSelectionContexts(): string[] {
|
|
84
|
-
return Object.keys(this._state.selections);
|
|
108
|
+
return Object.keys(this._registry.get(this._state).selections);
|
|
85
109
|
}
|
|
86
110
|
|
|
111
|
+
getSelection(contextId: string): Selection | undefined;
|
|
112
|
+
getSelection<T extends SelectionMode>(contextId: string, mode: T): Selection;
|
|
87
113
|
getSelection<T extends SelectionMode | undefined>(
|
|
88
114
|
contextId: string,
|
|
89
115
|
mode: T = undefined as T,
|
|
90
|
-
):
|
|
91
|
-
const
|
|
116
|
+
): Selection | undefined {
|
|
117
|
+
const state = this._registry.get(this._state);
|
|
118
|
+
const selection = state.selections[contextId];
|
|
92
119
|
if (!mode || selection) {
|
|
93
120
|
return selection;
|
|
94
121
|
}
|
|
95
122
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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;
|
|
100
129
|
}
|
|
101
130
|
|
|
102
131
|
// TODO(burdon): Disambiguate with getSelection?
|
|
103
132
|
getSelected<T extends SelectionMode>(contextId: string, mode: T = 'multi' as T): SelectionResult<T> {
|
|
104
133
|
const selection = this.getSelection(contextId, mode);
|
|
105
134
|
invariant(selection?.mode === mode, 'Selection mode mismatch');
|
|
135
|
+
// Cast required because TypeScript can't infer the relationship between T and the matched result.
|
|
106
136
|
return Match.type<Selection>().pipe(
|
|
107
137
|
Match.when({ mode: 'single' }, (s) => s.id),
|
|
108
138
|
Match.when({ mode: 'multi' }, (s) => s.ids),
|
|
109
139
|
Match.when({ mode: 'range' }, (s) => (s.from && s.to ? { from: s.from, to: s.to } : undefined)),
|
|
110
140
|
Match.when({ mode: 'multi-range' }, (s) => s.ranges),
|
|
111
141
|
Match.exhaustive,
|
|
112
|
-
)(selection) as
|
|
142
|
+
)(selection) as SelectionResult<T>;
|
|
113
143
|
}
|
|
114
144
|
|
|
115
145
|
updateSingle(contextId: string, id: string): void {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
+
}));
|
|
121
154
|
}
|
|
122
155
|
|
|
123
156
|
updateMulti(contextId: string, ids: string[]) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
+
}));
|
|
129
165
|
}
|
|
130
166
|
|
|
131
167
|
updateRange(contextId: string, from: string, to: string) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
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
|
+
}));
|
|
138
176
|
}
|
|
139
177
|
|
|
140
178
|
updateMultiRange(contextId: string, ranges: { from: string; to: string }[]) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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
|
+
}));
|
|
146
187
|
}
|
|
147
188
|
|
|
148
189
|
clearSelection(contextId: string): void {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
Match.exhaustive,
|
|
169
|
-
)(selection);
|
|
170
|
-
});
|
|
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
|
+
}));
|
|
171
209
|
}
|
|
172
210
|
|
|
173
211
|
toggleSelection(contextId: string, id: string): void {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
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
|
+
}));
|
|
185
225
|
}
|
|
186
226
|
}
|