@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/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-ui-attention",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.e00bdcdb52",
|
|
4
4
|
"description": "Signifier components and providers for attention system.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"author": "DXOS.org",
|
|
9
|
-
"sideEffects":
|
|
13
|
+
"sideEffects": false,
|
|
10
14
|
"type": "module",
|
|
11
15
|
"exports": {
|
|
12
16
|
".": {
|
|
@@ -15,6 +19,12 @@
|
|
|
15
19
|
"browser": "./dist/lib/browser/index.mjs",
|
|
16
20
|
"node": "./dist/lib/node-esm/index.mjs"
|
|
17
21
|
},
|
|
22
|
+
"./types": {
|
|
23
|
+
"source": "./src/types.ts",
|
|
24
|
+
"types": "./dist/types/src/types.d.ts",
|
|
25
|
+
"browser": "./dist/lib/browser/types.mjs",
|
|
26
|
+
"node": "./dist/lib/node-esm/types.mjs"
|
|
27
|
+
},
|
|
18
28
|
"./testing": {
|
|
19
29
|
"source": "./src/testing/index.ts",
|
|
20
30
|
"types": "./dist/types/src/testing/index.d.ts",
|
|
@@ -23,20 +33,12 @@
|
|
|
23
33
|
}
|
|
24
34
|
},
|
|
25
35
|
"types": "dist/types/src/index.d.ts",
|
|
26
|
-
"typesVersions": {
|
|
27
|
-
"*": {
|
|
28
|
-
"testing": [
|
|
29
|
-
"dist/types/src/testing/index.d.ts"
|
|
30
|
-
]
|
|
31
|
-
}
|
|
32
|
-
},
|
|
33
36
|
"files": [
|
|
34
37
|
"dist",
|
|
35
38
|
"src"
|
|
36
39
|
],
|
|
37
40
|
"dependencies": {
|
|
38
|
-
"@
|
|
39
|
-
"@preact/signals-core": "^1.9.0",
|
|
41
|
+
"@effect-atom/atom-react": "^0.5.0",
|
|
40
42
|
"@radix-ui/primitive": "1.1.1",
|
|
41
43
|
"@radix-ui/react-compose-refs": "1.1.1",
|
|
42
44
|
"@radix-ui/react-context": "1.1.1",
|
|
@@ -44,30 +46,30 @@
|
|
|
44
46
|
"@radix-ui/react-primitive": "2.0.2",
|
|
45
47
|
"@radix-ui/react-slot": "1.1.2",
|
|
46
48
|
"@radix-ui/react-use-controllable-state": "1.1.0",
|
|
47
|
-
"effect": "3.
|
|
48
|
-
"@dxos/
|
|
49
|
-
"@dxos/
|
|
50
|
-
"@dxos/
|
|
51
|
-
"@dxos/
|
|
52
|
-
"@dxos/
|
|
49
|
+
"effect": "3.20.0",
|
|
50
|
+
"@dxos/invariant": "0.8.4-main.e00bdcdb52",
|
|
51
|
+
"@dxos/echo": "0.8.4-main.e00bdcdb52",
|
|
52
|
+
"@dxos/react-hooks": "0.8.4-main.e00bdcdb52",
|
|
53
|
+
"@dxos/util": "0.8.4-main.e00bdcdb52",
|
|
54
|
+
"@dxos/log": "0.8.4-main.e00bdcdb52"
|
|
53
55
|
},
|
|
54
56
|
"devDependencies": {
|
|
55
|
-
"@types/react": "~
|
|
56
|
-
"@types/react-dom": "~
|
|
57
|
-
"react": "~
|
|
58
|
-
"react-dom": "~
|
|
59
|
-
"vite": "
|
|
60
|
-
"@dxos/
|
|
61
|
-
"@dxos/
|
|
62
|
-
"@dxos/
|
|
63
|
-
"@dxos/storybook-utils": "0.8.4-main.
|
|
57
|
+
"@types/react": "~19.2.7",
|
|
58
|
+
"@types/react-dom": "~19.2.3",
|
|
59
|
+
"react": "~19.2.3",
|
|
60
|
+
"react-dom": "~19.2.3",
|
|
61
|
+
"vite": "^8.0.10",
|
|
62
|
+
"@dxos/random": "0.8.4-main.e00bdcdb52",
|
|
63
|
+
"@dxos/react-ui": "0.8.4-main.e00bdcdb52",
|
|
64
|
+
"@dxos/ui-theme": "0.8.4-main.e00bdcdb52",
|
|
65
|
+
"@dxos/storybook-utils": "0.8.4-main.e00bdcdb52"
|
|
64
66
|
},
|
|
65
67
|
"peerDependencies": {
|
|
66
|
-
"effect": "
|
|
67
|
-
"react": "~
|
|
68
|
-
"react-dom": "~
|
|
69
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
70
|
-
"@dxos/
|
|
68
|
+
"effect": "3.20.0",
|
|
69
|
+
"react": "~19.2.3",
|
|
70
|
+
"react-dom": "~19.2.3",
|
|
71
|
+
"@dxos/react-ui": "0.8.4-main.e00bdcdb52",
|
|
72
|
+
"@dxos/ui-theme": "0.8.4-main.e00bdcdb52"
|
|
71
73
|
},
|
|
72
74
|
"publishConfig": {
|
|
73
75
|
"access": "public"
|
package/src/attention.test.ts
CHANGED
|
@@ -2,46 +2,207 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { Registry } from '@effect-atom/atom-react';
|
|
6
|
+
import { describe, test } from 'vitest';
|
|
6
7
|
|
|
7
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
AttentionManager,
|
|
10
|
+
expandAttendableId,
|
|
11
|
+
getLinkedVariant,
|
|
12
|
+
getParentId,
|
|
13
|
+
getSegmentId,
|
|
14
|
+
isLinkedSegment,
|
|
15
|
+
linkedSegment,
|
|
16
|
+
} from './attention';
|
|
8
17
|
|
|
9
18
|
describe('AttentionManager', () => {
|
|
10
|
-
test('takes an initial
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
expect(attention.
|
|
15
|
-
expect(attention.get(
|
|
16
|
-
expect(attention.get(
|
|
17
|
-
expect(attention.get(
|
|
18
|
-
expect(attention.get(
|
|
19
|
+
test('takes an initial attended id', ({ expect }) => {
|
|
20
|
+
const registry = Registry.make();
|
|
21
|
+
const attention = new AttentionManager(registry, ['root/a/b']);
|
|
22
|
+
|
|
23
|
+
expect(attention.getCurrent()).to.deep.equal(['root/a/b']);
|
|
24
|
+
expect(attention.get('root')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
|
|
25
|
+
expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
|
|
26
|
+
expect(attention.get('root/a/b')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
|
|
27
|
+
expect(attention.get('unrelated')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
|
|
19
28
|
});
|
|
20
29
|
|
|
21
|
-
test('current returns last updated
|
|
22
|
-
const
|
|
23
|
-
|
|
30
|
+
test('current returns last updated ids', ({ expect }) => {
|
|
31
|
+
const registry = Registry.make();
|
|
32
|
+
const attention = new AttentionManager(registry);
|
|
33
|
+
expect(attention.getCurrent()).to.deep.equal([]);
|
|
24
34
|
|
|
25
|
-
attention.update(['a
|
|
26
|
-
expect(attention.
|
|
35
|
+
attention.update(['root/a/b/c']);
|
|
36
|
+
expect(attention.getCurrent()).to.deep.equal(['root/a/b/c']);
|
|
27
37
|
});
|
|
28
38
|
|
|
29
|
-
test('keys returns all
|
|
30
|
-
const
|
|
39
|
+
test('keys returns all tracked qualified ids', ({ expect }) => {
|
|
40
|
+
const registry = Registry.make();
|
|
41
|
+
const attention = new AttentionManager(registry);
|
|
31
42
|
expect(attention.keys()).to.deep.equal([]);
|
|
32
43
|
|
|
33
|
-
attention.update(['a']);
|
|
34
|
-
expect(attention.keys()).to.
|
|
44
|
+
attention.update(['root/a']);
|
|
45
|
+
expect(attention.keys()).to.include('root');
|
|
46
|
+
expect(attention.keys()).to.include('root/a');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('get returns attention state for a given qualified id', ({ expect }) => {
|
|
50
|
+
const registry = Registry.make();
|
|
51
|
+
const attention = new AttentionManager(registry);
|
|
52
|
+
expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
|
|
35
53
|
|
|
36
|
-
attention.update(['a'
|
|
37
|
-
expect(attention.
|
|
54
|
+
attention.update(['root/a']);
|
|
55
|
+
expect(attention.get('root/a')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
|
|
56
|
+
expect(attention.get('root')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
|
|
38
57
|
});
|
|
39
58
|
|
|
40
|
-
test('
|
|
41
|
-
const
|
|
42
|
-
|
|
59
|
+
test('clears previous attention on update', ({ expect }) => {
|
|
60
|
+
const registry = Registry.make();
|
|
61
|
+
const attention = new AttentionManager(registry);
|
|
62
|
+
|
|
63
|
+
attention.update(['root/a/b']);
|
|
64
|
+
expect(attention.get('root/a/b')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
|
|
65
|
+
expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
|
|
66
|
+
|
|
67
|
+
attention.update(['root/x/y']);
|
|
68
|
+
expect(attention.get('root/a/b')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
|
|
69
|
+
expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
|
|
70
|
+
expect(attention.get('root/x/y')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
|
|
71
|
+
expect(attention.get('root/x')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
|
|
72
|
+
expect(attention.get('root')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test('linked segment marks parent with isRelated and isAncestor', ({ expect }) => {
|
|
76
|
+
const registry = Registry.make();
|
|
77
|
+
const attention = new AttentionManager(registry);
|
|
78
|
+
|
|
79
|
+
attention.update(['root/space/obj/~settings']);
|
|
80
|
+
expect(attention.get('root/space/obj/~settings')).to.deep.equal({
|
|
81
|
+
hasAttention: true,
|
|
82
|
+
isAncestor: false,
|
|
83
|
+
isRelated: false,
|
|
84
|
+
});
|
|
85
|
+
expect(attention.get('root/space/obj')).to.deep.equal({
|
|
86
|
+
hasAttention: false,
|
|
87
|
+
isAncestor: true,
|
|
88
|
+
isRelated: true,
|
|
89
|
+
});
|
|
90
|
+
expect(attention.get('root/space')).to.deep.equal({
|
|
91
|
+
hasAttention: false,
|
|
92
|
+
isAncestor: true,
|
|
93
|
+
isRelated: false,
|
|
94
|
+
});
|
|
95
|
+
expect(attention.get('root')).to.deep.equal({
|
|
96
|
+
hasAttention: false,
|
|
97
|
+
isAncestor: true,
|
|
98
|
+
isRelated: false,
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('switching from linked segment to non-linked clears parent isRelated', ({ expect }) => {
|
|
103
|
+
const registry = Registry.make();
|
|
104
|
+
const attention = new AttentionManager(registry);
|
|
105
|
+
|
|
106
|
+
attention.update(['root/space/obj/~settings']);
|
|
107
|
+
expect(attention.get('root/space/obj')).to.deep.equal({
|
|
108
|
+
hasAttention: false,
|
|
109
|
+
isAncestor: true,
|
|
110
|
+
isRelated: true,
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
attention.update(['root/space/obj']);
|
|
114
|
+
expect(attention.get('root/space/obj')).to.deep.equal({
|
|
115
|
+
hasAttention: true,
|
|
116
|
+
isAncestor: false,
|
|
117
|
+
isRelated: false,
|
|
118
|
+
});
|
|
119
|
+
expect(attention.get('root/space/obj/~settings')).to.deep.equal({
|
|
120
|
+
hasAttention: false,
|
|
121
|
+
isAncestor: false,
|
|
122
|
+
isRelated: false,
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('keys sharing the same segment id are marked isRelated', ({ expect }) => {
|
|
127
|
+
const registry = Registry.make();
|
|
128
|
+
const attention = new AttentionManager(registry);
|
|
129
|
+
|
|
130
|
+
// Pre-register the alternate path so the manager knows about it.
|
|
131
|
+
attention.get('root/z/y/c');
|
|
132
|
+
|
|
133
|
+
attention.update(['root/a/b/c']);
|
|
134
|
+
expect(attention.get('root/a/b/c')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
|
|
135
|
+
expect(attention.get('root/z/y/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: true });
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test('related keys are cleared when attention moves to a different segment id', ({ expect }) => {
|
|
139
|
+
const registry = Registry.make();
|
|
140
|
+
const attention = new AttentionManager(registry);
|
|
141
|
+
|
|
142
|
+
attention.get('root/z/y/c');
|
|
143
|
+
attention.update(['root/a/b/c']);
|
|
144
|
+
expect(attention.get('root/z/y/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: true });
|
|
145
|
+
|
|
146
|
+
attention.update(['root/x/y/d']);
|
|
147
|
+
expect(attention.get('root/z/y/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
|
|
148
|
+
expect(attention.get('root/a/b/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
describe('expandAttendableId', () => {
|
|
153
|
+
test('produces progressive prefixes', ({ expect }) => {
|
|
154
|
+
expect(expandAttendableId('root/space/types/doc')).to.deep.equal([
|
|
155
|
+
'root',
|
|
156
|
+
'root/space',
|
|
157
|
+
'root/space/types',
|
|
158
|
+
'root/space/types/doc',
|
|
159
|
+
]);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test('single segment returns itself', ({ expect }) => {
|
|
163
|
+
expect(expandAttendableId('root')).to.deep.equal(['root']);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe('isLinkedSegment', () => {
|
|
168
|
+
test('detects linked prefix on last segment', ({ expect }) => {
|
|
169
|
+
expect(isLinkedSegment('root/space/obj/~settings')).toBe(true);
|
|
170
|
+
expect(isLinkedSegment('root/space/obj')).toBe(false);
|
|
171
|
+
expect(isLinkedSegment('~standalone')).toBe(true);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
describe('linkedSegment', () => {
|
|
176
|
+
test('builds a linked segment', ({ expect }) => {
|
|
177
|
+
expect(linkedSegment('settings')).toBe('~settings');
|
|
178
|
+
expect(linkedSegment('comments')).toBe('~comments');
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
describe('getLinkedVariant', () => {
|
|
183
|
+
test('extracts variant from linked last segment', ({ expect }) => {
|
|
184
|
+
expect(getLinkedVariant('root/space/obj/~settings')).toBe('settings');
|
|
185
|
+
expect(getLinkedVariant('root/a/~comments')).toBe('comments');
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test('returns segment as-is when not linked', ({ expect }) => {
|
|
189
|
+
expect(getLinkedVariant('root/space/obj')).toBe('obj');
|
|
190
|
+
expect(getLinkedVariant('root')).toBe('root');
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
describe('getParentId', () => {
|
|
195
|
+
test('returns parent qualified id', ({ expect }) => {
|
|
196
|
+
expect(getParentId('root/space/obj/~settings')).toBe('root/space/obj');
|
|
197
|
+
expect(getParentId('root/space')).toBe('root');
|
|
198
|
+
expect(getParentId('root')).toBeUndefined();
|
|
199
|
+
});
|
|
200
|
+
});
|
|
43
201
|
|
|
44
|
-
|
|
45
|
-
|
|
202
|
+
describe('getSegmentId', () => {
|
|
203
|
+
test('returns the last path segment', ({ expect }) => {
|
|
204
|
+
expect(getSegmentId('root/space/obj')).toBe('obj');
|
|
205
|
+
expect(getSegmentId('root')).toBe('root');
|
|
206
|
+
expect(getSegmentId('root/a/~settings')).toBe('~settings');
|
|
46
207
|
});
|
|
47
208
|
});
|
package/src/attention.ts
CHANGED
|
@@ -2,119 +2,162 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import { Schema } from 'effect';
|
|
5
|
+
import { Atom, type Registry } from '@effect-atom/atom-react';
|
|
7
6
|
|
|
8
|
-
import { type
|
|
9
|
-
import { ComplexMap } from '@dxos/util';
|
|
10
|
-
|
|
11
|
-
// NOTE: Chosen from RFC 1738’s `safe` characters: http://www.faqs.org/rfcs/rfc1738.html
|
|
12
|
-
export const ATTENDABLE_PATH_SEPARATOR = '~';
|
|
13
|
-
|
|
14
|
-
export const AttentionSchema = Schema.mutable(
|
|
15
|
-
Schema.Struct({
|
|
16
|
-
hasAttention: Schema.Boolean,
|
|
17
|
-
isAncestor: Schema.Boolean,
|
|
18
|
-
isRelated: Schema.Boolean,
|
|
19
|
-
}),
|
|
20
|
-
);
|
|
21
|
-
|
|
22
|
-
export type Attention = Schema.Schema.Type<typeof AttentionSchema>;
|
|
23
|
-
|
|
24
|
-
// TODO(wittjosiah): Use mosaic path utility?
|
|
25
|
-
const stringKey = (key: string[]) => key.join(',');
|
|
7
|
+
import { type Attention } from './types';
|
|
26
8
|
|
|
27
9
|
/**
|
|
28
10
|
* Manages attention state for an application.
|
|
11
|
+
* Attention keys are slash-qualified graph IDs; ancestry is derived from progressive prefixes.
|
|
29
12
|
*/
|
|
30
|
-
// TODO(wittjosiah): Write unit tests.
|
|
31
13
|
export class AttentionManager {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
14
|
+
private readonly _map = new Map<string, Atom.Writable<Attention>>();
|
|
15
|
+
private readonly _currentAtom: Atom.Writable<string[]>;
|
|
16
|
+
|
|
17
|
+
constructor(
|
|
18
|
+
private readonly _registry: Registry.Registry,
|
|
19
|
+
initial: string[] = [],
|
|
20
|
+
) {
|
|
21
|
+
this._currentAtom = Atom.make<string[]>([]).pipe(Atom.keepAlive);
|
|
38
22
|
if (initial.length > 0) {
|
|
39
23
|
this.update(initial);
|
|
40
24
|
}
|
|
41
25
|
}
|
|
42
26
|
|
|
43
27
|
/**
|
|
44
|
-
*
|
|
45
|
-
|
|
46
|
-
|
|
28
|
+
* Atom for the currently attended element IDs.
|
|
29
|
+
*/
|
|
30
|
+
get current(): Atom.Atom<string[]> {
|
|
31
|
+
return this._currentAtom;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Gets the currently attended element IDs.
|
|
36
|
+
*/
|
|
37
|
+
getCurrent(): readonly string[] {
|
|
38
|
+
return this._registry.get(this._currentAtom);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Subscribe to changes in the current attention IDs.
|
|
47
43
|
*/
|
|
48
|
-
|
|
49
|
-
|
|
44
|
+
subscribeCurrent(cb: (current: readonly string[]) => void): () => void {
|
|
45
|
+
this._registry.get(this._currentAtom);
|
|
46
|
+
return this._registry.subscribe(this._currentAtom, () => {
|
|
47
|
+
cb(this._registry.get(this._currentAtom));
|
|
48
|
+
});
|
|
50
49
|
}
|
|
51
50
|
|
|
52
51
|
/**
|
|
53
|
-
* All
|
|
52
|
+
* All tracked qualified IDs.
|
|
54
53
|
*/
|
|
55
|
-
keys(): string[]
|
|
54
|
+
keys(): string[] {
|
|
56
55
|
return Array.from(this._map.keys());
|
|
57
56
|
}
|
|
58
57
|
|
|
59
58
|
/**
|
|
60
|
-
* Get the attention state for a given
|
|
59
|
+
* Get the attention state for a given qualified ID.
|
|
61
60
|
*/
|
|
62
|
-
get(
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
61
|
+
get(id: string): Attention {
|
|
62
|
+
const atom = this._getAtom(id);
|
|
63
|
+
return this._registry.get(atom);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Subscribe to changes in the attention state for a given qualified ID.
|
|
68
|
+
*/
|
|
69
|
+
subscribe(id: string, cb: (attention: Attention) => void): () => void {
|
|
70
|
+
const atom = this._getAtom(id);
|
|
71
|
+
this._registry.get(atom);
|
|
72
|
+
return this._registry.subscribe(atom, () => {
|
|
73
|
+
cb(this._registry.get(atom));
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private _getAtom(id: string): Atom.Writable<Attention> {
|
|
78
|
+
const existing = this._map.get(id);
|
|
79
|
+
if (existing) {
|
|
80
|
+
return existing;
|
|
66
81
|
}
|
|
67
82
|
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
83
|
+
const newAtom = Atom.make<Attention>({ hasAttention: false, isAncestor: false, isRelated: false }).pipe(
|
|
84
|
+
Atom.keepAlive,
|
|
85
|
+
);
|
|
86
|
+
this._map.set(id, newAtom);
|
|
87
|
+
return newAtom;
|
|
71
88
|
}
|
|
72
89
|
|
|
73
90
|
/**
|
|
74
91
|
* Update the currently attended element.
|
|
92
|
+
* Takes the array of qualified IDs collected from the DOM; the first element is the primary attended item.
|
|
93
|
+
* Ancestry is derived from the progressive prefixes of the primary ID.
|
|
94
|
+
* Relatedness is derived from the segment ID: any tracked key whose last `/` segment matches the
|
|
95
|
+
* attended ID's segment ID is marked `isRelated`. Additionally, if the primary ID is a linked segment
|
|
96
|
+
* (starts with `~`), its immediate parent gets `isRelated` alongside `isAncestor`.
|
|
75
97
|
*
|
|
76
98
|
* @internal
|
|
77
99
|
*/
|
|
78
|
-
update(
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
100
|
+
update(nextIds: string[]): void {
|
|
101
|
+
const primaryId = nextIds[0];
|
|
102
|
+
if (!primaryId) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
82
105
|
|
|
83
|
-
|
|
84
|
-
|
|
106
|
+
const currentIds = this.getCurrent();
|
|
107
|
+
const prevPrimaryId = currentIds[0];
|
|
108
|
+
// Clear previous attention state: ancestors, primary, and related keys.
|
|
109
|
+
if (prevPrimaryId) {
|
|
110
|
+
const prevPrefixes = expandAttendableId(prevPrimaryId);
|
|
111
|
+
for (const prefix of prevPrefixes) {
|
|
112
|
+
this._set(prefix, {});
|
|
113
|
+
}
|
|
114
|
+
const prevSegmentId = getSegmentId(prevPrimaryId);
|
|
115
|
+
for (const key of this.keys()) {
|
|
116
|
+
if (getSegmentId(key) === prevSegmentId) {
|
|
117
|
+
this._set(key, {});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
85
121
|
|
|
86
|
-
|
|
87
|
-
const nextRelatedKeys = this.keys().filter(
|
|
88
|
-
(key) => key[0] === nextRelatedTo && stringKey(key) !== stringKey(nextKey),
|
|
89
|
-
);
|
|
90
|
-
const prevRelatedKeys = currentRelatedKeys.filter(
|
|
91
|
-
(curr) => !nextRelatedKeys.find((next) => stringKey(curr) === stringKey(next)),
|
|
92
|
-
);
|
|
122
|
+
this._registry.set(this._currentAtom, nextIds);
|
|
93
123
|
|
|
94
|
-
|
|
95
|
-
const
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
124
|
+
// Set ancestors and primary.
|
|
125
|
+
const prefixes = expandAttendableId(primaryId);
|
|
126
|
+
const prefixSet = new Set(prefixes);
|
|
127
|
+
const linkedParent = isLinkedSegment(primaryId) ? getParentId(primaryId) : undefined;
|
|
128
|
+
|
|
129
|
+
for (const prefix of prefixes) {
|
|
130
|
+
if (prefix === primaryId) {
|
|
131
|
+
this._set(prefix, { hasAttention: true });
|
|
132
|
+
} else if (prefix === linkedParent) {
|
|
133
|
+
this._set(prefix, { isAncestor: true, isRelated: true });
|
|
134
|
+
} else {
|
|
135
|
+
this._set(prefix, { isAncestor: true });
|
|
136
|
+
}
|
|
137
|
+
}
|
|
99
138
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
139
|
+
// Set related keys: any tracked key sharing the same segment ID.
|
|
140
|
+
const segmentId = getSegmentId(primaryId);
|
|
141
|
+
for (const key of this.keys()) {
|
|
142
|
+
if (!prefixSet.has(key) && getSegmentId(key) === segmentId) {
|
|
143
|
+
this._set(key, { isRelated: true });
|
|
144
|
+
}
|
|
145
|
+
}
|
|
106
146
|
}
|
|
107
147
|
|
|
108
|
-
private _set(
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
148
|
+
private _set(id: string, attention: Partial<Attention>): void {
|
|
149
|
+
const atom = this._getAtom(id);
|
|
150
|
+
this._registry.set(atom, {
|
|
151
|
+
hasAttention: attention.hasAttention ?? false,
|
|
152
|
+
isAncestor: attention.isAncestor ?? false,
|
|
153
|
+
isRelated: attention.isRelated ?? false,
|
|
154
|
+
});
|
|
113
155
|
}
|
|
114
156
|
}
|
|
115
157
|
|
|
116
158
|
/**
|
|
117
|
-
* Accumulates all attendable
|
|
159
|
+
* Accumulates all attendable IDs between the element provided and the root, inclusive.
|
|
160
|
+
* Each `data-attendable-id` value is treated as a single qualified ID (no splitting).
|
|
118
161
|
*/
|
|
119
162
|
export const getAttendables = (selector: string, cursor: Element, acc: string[] = []): string[] => {
|
|
120
163
|
// Find the closest element with `data-attendable-id`, if any; start from cursor and move up the DOM tree.
|
|
@@ -130,8 +173,7 @@ export const getAttendables = (selector: string, cursor: Element, acc: string[]
|
|
|
130
173
|
return getAttendables(selector, trigger, acc);
|
|
131
174
|
}
|
|
132
175
|
} else {
|
|
133
|
-
acc.push(
|
|
134
|
-
// TODO: Attempt tail recursion.
|
|
176
|
+
acc.push(attendableId);
|
|
135
177
|
return !closestAttendable.parentElement ? acc : getAttendables(selector, closestAttendable.parentElement, acc);
|
|
136
178
|
}
|
|
137
179
|
}
|
|
@@ -142,3 +184,62 @@ export const getAttendables = (selector: string, cursor: Element, acc: string[]
|
|
|
142
184
|
export type AttendableId = { attendableId?: string };
|
|
143
185
|
|
|
144
186
|
export type Related = { related?: boolean };
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Prefix for linked segments. A linked segment's attention state is shared with its parent:
|
|
190
|
+
* when a linked node has attention, the parent is marked as both ancestor and related.
|
|
191
|
+
*/
|
|
192
|
+
const LINKED_PREFIX = '~';
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Decompose a qualified graph ID into progressive prefixes for ancestry tracking.
|
|
196
|
+
* e.g. `root/a/b/c` yields `['root', 'root/a', 'root/a/b', 'root/a/b/c']`.
|
|
197
|
+
*/
|
|
198
|
+
export const expandAttendableId = (qualifiedId: string): string[] => {
|
|
199
|
+
const segments = qualifiedId.split('/');
|
|
200
|
+
return segments.map((_, index) => segments.slice(0, index + 1).join('/'));
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Whether the last segment of a qualified ID is a linked segment (starts with `~`).
|
|
205
|
+
* Linked segments share their attention state with the parent node.
|
|
206
|
+
*/
|
|
207
|
+
export const isLinkedSegment = (qualifiedId: string): boolean => {
|
|
208
|
+
const lastSegment = qualifiedId.split('/').pop() ?? '';
|
|
209
|
+
return lastSegment.startsWith(LINKED_PREFIX);
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Build a linked segment ID from a variant name (e.g., `'settings'` -> `'~settings'`).
|
|
214
|
+
* Nodes identified by linked segments share their attention state with the parent node.
|
|
215
|
+
*/
|
|
216
|
+
export const linkedSegment = (variant: string): string => `${LINKED_PREFIX}${variant}`;
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Extract the variant name from the last segment of a qualified ID, stripping the linked prefix.
|
|
220
|
+
* e.g. `'root/space/obj/~settings'` -> `'settings'`.
|
|
221
|
+
* If the segment is not linked (no `~` prefix), returns the segment as-is.
|
|
222
|
+
*/
|
|
223
|
+
export const getLinkedVariant = (qualifiedId: string): string => {
|
|
224
|
+
const lastSegment = qualifiedId.split('/').pop() ?? '';
|
|
225
|
+
return lastSegment.startsWith(LINKED_PREFIX) ? lastSegment.slice(LINKED_PREFIX.length) : lastSegment;
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Get the parent qualified ID (everything before the last `/` segment).
|
|
230
|
+
*/
|
|
231
|
+
export const getParentId = (qualifiedId: string | undefined): string | undefined => {
|
|
232
|
+
if (!qualifiedId) {
|
|
233
|
+
return undefined;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const lastSlash = qualifiedId.lastIndexOf('/');
|
|
237
|
+
return lastSlash > 0 ? qualifiedId.slice(0, lastSlash) : undefined;
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Extract the segment ID (last segment) of a qualified ID.
|
|
242
|
+
*/
|
|
243
|
+
export const getSegmentId = (qualifiedId: string): string => {
|
|
244
|
+
return qualifiedId.split('/').pop() ?? qualifiedId;
|
|
245
|
+
};
|