@asyra/ui-context 0.2.0
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/.turbo/turbo-test$colon$ci.log +117 -0
- package/.turbo/turbo-test$colon$local.log +23 -0
- package/.turbo/turbo-test.log +0 -0
- package/README.md +1 -0
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +87 -0
- package/coverage/clover.xml +538 -0
- package/coverage/coverage-final.json +7443 -0
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +176 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +2 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +196 -0
- package/coverage/src/index.html +146 -0
- package/coverage/src/index.ts.html +97 -0
- package/coverage/src/stores/index.html +146 -0
- package/coverage/src/stores/render.ts.html +106 -0
- package/coverage/src/stores/scene-tree.ts.html +790 -0
- package/coverage/src/stores/selection.ts.html +220 -0
- package/coverage/src/stores/system-context/index.html +146 -0
- package/coverage/src/stores/system-context/index.ts.html +88 -0
- package/coverage/src/stores/system-context/primary-tool.ts.html +109 -0
- package/coverage/src/stores/system-context/system-context.ts.html +85 -0
- package/coverage/src/subscribes/index.html +161 -0
- package/coverage/src/subscribes/index.ts.html +124 -0
- package/coverage/src/subscribes/render.ts.html +151 -0
- package/coverage/src/subscribes/scene-tree.ts.html +373 -0
- package/coverage/src/subscribes/selection.ts.html +253 -0
- package/coverage/src/subscribes/system-context/index.html +131 -0
- package/coverage/src/subscribes/system-context/index.ts.html +100 -0
- package/coverage/src/subscribes/system-context/primary-tool.ts.html +151 -0
- package/coverage/src/types.ts.html +124 -0
- package/coverage/src/ui-context.ts.html +568 -0
- package/dist/__tests__/ui-context.test.d.ts +2 -0
- package/dist/__tests__/ui-context.test.d.ts.map +1 -0
- package/dist/__tests__/ui-context.test.js +170 -0
- package/dist/__tests__/ui-context.test.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/stores/__tests__/render.test.d.ts +2 -0
- package/dist/stores/__tests__/render.test.d.ts.map +1 -0
- package/dist/stores/__tests__/render.test.js +30 -0
- package/dist/stores/__tests__/render.test.js.map +1 -0
- package/dist/stores/__tests__/selection.test.d.ts +2 -0
- package/dist/stores/__tests__/selection.test.d.ts.map +1 -0
- package/dist/stores/__tests__/selection.test.js +128 -0
- package/dist/stores/__tests__/selection.test.js.map +1 -0
- package/dist/stores/render.d.ts +4 -0
- package/dist/stores/render.d.ts.map +1 -0
- package/dist/stores/render.js +7 -0
- package/dist/stores/render.js.map +1 -0
- package/dist/stores/scene-tree.d.ts +35 -0
- package/dist/stores/scene-tree.d.ts.map +1 -0
- package/dist/stores/scene-tree.js +158 -0
- package/dist/stores/scene-tree.js.map +1 -0
- package/dist/stores/selection.d.ts +8 -0
- package/dist/stores/selection.d.ts.map +1 -0
- package/dist/stores/selection.js +38 -0
- package/dist/stores/selection.js.map +1 -0
- package/dist/stores/system-context/__tests__/primary-tool.test.d.ts +2 -0
- package/dist/stores/system-context/__tests__/primary-tool.test.d.ts.map +1 -0
- package/dist/stores/system-context/__tests__/primary-tool.test.js +27 -0
- package/dist/stores/system-context/__tests__/primary-tool.test.js.map +1 -0
- package/dist/stores/system-context/index.d.ts +2 -0
- package/dist/stores/system-context/index.d.ts.map +1 -0
- package/dist/stores/system-context/index.js +2 -0
- package/dist/stores/system-context/index.js.map +1 -0
- package/dist/stores/system-context/primary-tool.d.ts +5 -0
- package/dist/stores/system-context/primary-tool.d.ts.map +1 -0
- package/dist/stores/system-context/primary-tool.js +7 -0
- package/dist/stores/system-context/primary-tool.js.map +1 -0
- package/dist/stores/system-context/system-context.d.ts +1 -0
- package/dist/stores/system-context/system-context.d.ts.map +1 -0
- package/dist/stores/system-context/system-context.js +2 -0
- package/dist/stores/system-context/system-context.js.map +1 -0
- package/dist/subscribes/index.d.ts +5 -0
- package/dist/subscribes/index.d.ts.map +1 -0
- package/dist/subscribes/index.js +12 -0
- package/dist/subscribes/index.js.map +1 -0
- package/dist/subscribes/render.d.ts +2 -0
- package/dist/subscribes/render.d.ts.map +1 -0
- package/dist/subscribes/render.js +15 -0
- package/dist/subscribes/render.js.map +1 -0
- package/dist/subscribes/scene-tree.d.ts +5 -0
- package/dist/subscribes/scene-tree.d.ts.map +1 -0
- package/dist/subscribes/scene-tree.js +71 -0
- package/dist/subscribes/scene-tree.js.map +1 -0
- package/dist/subscribes/selection.d.ts +5 -0
- package/dist/subscribes/selection.d.ts.map +1 -0
- package/dist/subscribes/selection.js +42 -0
- package/dist/subscribes/selection.js.map +1 -0
- package/dist/subscribes/system-context/index.d.ts +2 -0
- package/dist/subscribes/system-context/index.d.ts.map +1 -0
- package/dist/subscribes/system-context/index.js +5 -0
- package/dist/subscribes/system-context/index.js.map +1 -0
- package/dist/subscribes/system-context/primary-tool.d.ts +4 -0
- package/dist/subscribes/system-context/primary-tool.d.ts.map +1 -0
- package/dist/subscribes/system-context/primary-tool.js +15 -0
- package/dist/subscribes/system-context/primary-tool.js.map +1 -0
- package/dist/types.d.ts +11 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/ui-context.d.ts +28 -0
- package/dist/ui-context.d.ts.map +1 -0
- package/dist/ui-context.js +101 -0
- package/dist/ui-context.js.map +1 -0
- package/package.json +36 -0
- package/src/__tests__/ui-context.test.ts +220 -0
- package/src/index.ts +4 -0
- package/src/stores/__tests__/render.test.ts +38 -0
- package/src/stores/__tests__/selection.test.ts +190 -0
- package/src/stores/render.ts +7 -0
- package/src/stores/scene-tree.ts +235 -0
- package/src/stores/selection.ts +45 -0
- package/src/stores/system-context/__tests__/primary-tool.test.ts +37 -0
- package/src/stores/system-context/index.ts +1 -0
- package/src/stores/system-context/primary-tool.ts +8 -0
- package/src/stores/system-context/system-context.ts +0 -0
- package/src/subscribes/index.ts +13 -0
- package/src/subscribes/render.ts +22 -0
- package/src/subscribes/scene-tree.ts +96 -0
- package/src/subscribes/selection.ts +56 -0
- package/src/subscribes/system-context/index.ts +5 -0
- package/src/subscribes/system-context/primary-tool.ts +22 -0
- package/src/types.ts +13 -0
- package/src/ui-context.ts +161 -0
- package/tsconfig.json +17 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
2
|
+
import { UIContext } from '../ui-context'
|
|
3
|
+
import { BehaviorSubject } from 'rxjs'
|
|
4
|
+
import {
|
|
5
|
+
ComputedAttrs,
|
|
6
|
+
MIXED_STRING,
|
|
7
|
+
PrimaryToolType,
|
|
8
|
+
EntityTypes
|
|
9
|
+
} from '@asyra/utils'
|
|
10
|
+
|
|
11
|
+
describe('UIContext', () => {
|
|
12
|
+
let uiContext: UIContext
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
vi.clearAllMocks()
|
|
16
|
+
|
|
17
|
+
uiContext = new UIContext()
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
// Test constructor
|
|
21
|
+
it('should initialize all BehaviorSubjects with correct default values', () => {
|
|
22
|
+
expect(uiContext.zoom).toBeInstanceOf(BehaviorSubject)
|
|
23
|
+
expect(uiContext.zoom.getValue()).toBe(1)
|
|
24
|
+
expect(uiContext.flattenedElementIds).toBeInstanceOf(BehaviorSubject)
|
|
25
|
+
expect(uiContext.flattenedElementIds.getValue()).toEqual([])
|
|
26
|
+
expect(uiContext.elementSelection).toBeInstanceOf(BehaviorSubject)
|
|
27
|
+
expect(uiContext.elementSelection.getValue()).toEqual(new Set())
|
|
28
|
+
expect(uiContext.vertexSelection).toBeInstanceOf(BehaviorSubject)
|
|
29
|
+
expect(uiContext.vertexSelection.getValue()).toEqual(new Set())
|
|
30
|
+
expect(uiContext.x).toBeInstanceOf(BehaviorSubject)
|
|
31
|
+
expect(uiContext.x.getValue()).toBe(0)
|
|
32
|
+
expect(uiContext.y).toBeInstanceOf(BehaviorSubject)
|
|
33
|
+
expect(uiContext.y.getValue()).toBe(0)
|
|
34
|
+
expect(uiContext.width).toBeInstanceOf(BehaviorSubject)
|
|
35
|
+
expect(uiContext.width.getValue()).toBe(0)
|
|
36
|
+
expect(uiContext.height).toBeInstanceOf(BehaviorSubject)
|
|
37
|
+
expect(uiContext.height.getValue()).toBe(0)
|
|
38
|
+
expect(uiContext.rotation).toBeInstanceOf(BehaviorSubject)
|
|
39
|
+
expect(uiContext.rotation.getValue()).toBe(0)
|
|
40
|
+
expect(uiContext.primaryTool).toBeInstanceOf(BehaviorSubject)
|
|
41
|
+
expect(uiContext.primaryTool.getValue()).toBe(PrimaryToolType.SELECT)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
// Test updateElementSelection
|
|
45
|
+
it('should update elementSelection when selectedIds are different', () => {
|
|
46
|
+
const newSelection = new Set(['id1', 'id2'])
|
|
47
|
+
const nextSpy = vi.spyOn(uiContext.elementSelection, 'next')
|
|
48
|
+
|
|
49
|
+
uiContext.updateElementSelection(newSelection)
|
|
50
|
+
|
|
51
|
+
expect(nextSpy).toHaveBeenCalledWith(newSelection)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('should not update elementSelection when selectedIds are the same', () => {
|
|
55
|
+
const currentSelection = new Set(['id1'])
|
|
56
|
+
uiContext.elementSelection.next(currentSelection)
|
|
57
|
+
const nextSpy = vi.spyOn(uiContext.elementSelection, 'next')
|
|
58
|
+
|
|
59
|
+
uiContext.updateElementSelection(currentSelection)
|
|
60
|
+
|
|
61
|
+
expect(nextSpy).not.toHaveBeenCalled()
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
// Test updateVertexSelection
|
|
65
|
+
it('should update vertexSelection when selectedIds are different', () => {
|
|
66
|
+
const newSelection = new Set(['v1', 'v2'])
|
|
67
|
+
const nextSpy = vi.spyOn(uiContext.vertexSelection, 'next')
|
|
68
|
+
|
|
69
|
+
uiContext.updateVertexSelection(newSelection)
|
|
70
|
+
|
|
71
|
+
expect(nextSpy).toHaveBeenCalledWith(newSelection)
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('should not update vertexSelection when selectedIds are the same', () => {
|
|
75
|
+
const currentSelection = new Set(['v1'])
|
|
76
|
+
uiContext.vertexSelection.next(currentSelection)
|
|
77
|
+
const nextSpy = vi.spyOn(uiContext.vertexSelection, 'next')
|
|
78
|
+
|
|
79
|
+
uiContext.updateVertexSelection(currentSelection)
|
|
80
|
+
|
|
81
|
+
expect(nextSpy).not.toHaveBeenCalled()
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
// Test updateComputedProperty
|
|
85
|
+
it('should update computed property with consistent value', () => {
|
|
86
|
+
const nextSpy = vi.spyOn(uiContext.x, 'next')
|
|
87
|
+
|
|
88
|
+
uiContext.updateComputedProperty('x', [10, 10])
|
|
89
|
+
|
|
90
|
+
expect(nextSpy).toHaveBeenCalledWith(10)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('should update computed property with MIXED_STRING for mixed values', () => {
|
|
94
|
+
const nextSpy = vi.spyOn(uiContext.width, 'next')
|
|
95
|
+
|
|
96
|
+
uiContext.updateComputedProperty('width', [100, 200])
|
|
97
|
+
|
|
98
|
+
expect(nextSpy).toHaveBeenCalledWith(MIXED_STRING)
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('should not update computed property if value is the same', () => {
|
|
102
|
+
uiContext.y.next(50)
|
|
103
|
+
const nextSpy = vi.spyOn(uiContext.y, 'next')
|
|
104
|
+
|
|
105
|
+
uiContext.updateComputedProperty('y', [50, 50])
|
|
106
|
+
|
|
107
|
+
expect(nextSpy).not.toHaveBeenCalled()
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('should not update properties not in generalKeysToCompare', () => {
|
|
111
|
+
const nextSpy = vi.spyOn(uiContext.flattenedElementIds, 'next')
|
|
112
|
+
|
|
113
|
+
// Attempt to update a property not in generalKeysToCompare
|
|
114
|
+
uiContext.updateComputedProperty(
|
|
115
|
+
'flattenedElementIds' as unknown as keyof ComputedAttrs,
|
|
116
|
+
[
|
|
117
|
+
['id1'] as unknown as ComputedAttrs[keyof ComputedAttrs],
|
|
118
|
+
['id2'] as unknown as ComputedAttrs[keyof ComputedAttrs]
|
|
119
|
+
]
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
expect(nextSpy).not.toHaveBeenCalled()
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
// Test updateComputedProperties
|
|
126
|
+
it('should update all general properties with consistent values', () => {
|
|
127
|
+
const allElementData: ComputedAttrs[] = [
|
|
128
|
+
{
|
|
129
|
+
id: '1',
|
|
130
|
+
type: EntityTypes.RECTANGLE,
|
|
131
|
+
name: 'rect 1',
|
|
132
|
+
x: 10,
|
|
133
|
+
y: 20,
|
|
134
|
+
width: 100,
|
|
135
|
+
height: 50,
|
|
136
|
+
rotation: 45
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
id: '2',
|
|
140
|
+
type: EntityTypes.RECTANGLE,
|
|
141
|
+
name: 'rect 2',
|
|
142
|
+
x: 10,
|
|
143
|
+
y: 20,
|
|
144
|
+
width: 100,
|
|
145
|
+
height: 50,
|
|
146
|
+
rotation: 45
|
|
147
|
+
}
|
|
148
|
+
]
|
|
149
|
+
const xSpy = vi.spyOn(uiContext.x, 'next')
|
|
150
|
+
const ySpy = vi.spyOn(uiContext.y, 'next')
|
|
151
|
+
const widthSpy = vi.spyOn(uiContext.width, 'next')
|
|
152
|
+
const heightSpy = vi.spyOn(uiContext.height, 'next')
|
|
153
|
+
const rotationSpy = vi.spyOn(uiContext.rotation, 'next')
|
|
154
|
+
|
|
155
|
+
uiContext.updateComputedProperties(allElementData)
|
|
156
|
+
|
|
157
|
+
expect(xSpy).toHaveBeenCalledWith(10)
|
|
158
|
+
expect(ySpy).toHaveBeenCalledWith(20)
|
|
159
|
+
expect(widthSpy).toHaveBeenCalledWith(100)
|
|
160
|
+
expect(heightSpy).toHaveBeenCalledWith(50)
|
|
161
|
+
expect(rotationSpy).toHaveBeenCalledWith(45)
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
it('should update all general properties with MIXED_STRING for mixed values', () => {
|
|
165
|
+
const allElementData: ComputedAttrs[] = [
|
|
166
|
+
{
|
|
167
|
+
id: '1',
|
|
168
|
+
type: EntityTypes.RECTANGLE,
|
|
169
|
+
name: 'rect 1',
|
|
170
|
+
x: 10,
|
|
171
|
+
y: 20,
|
|
172
|
+
width: 100,
|
|
173
|
+
height: 50,
|
|
174
|
+
rotation: 0
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
id: '2',
|
|
178
|
+
type: EntityTypes.RECTANGLE,
|
|
179
|
+
name: 'rect 2',
|
|
180
|
+
x: 15,
|
|
181
|
+
y: 20,
|
|
182
|
+
width: 100,
|
|
183
|
+
height: 50,
|
|
184
|
+
rotation: 0
|
|
185
|
+
}
|
|
186
|
+
]
|
|
187
|
+
const xSpy = vi.spyOn(uiContext.x, 'next')
|
|
188
|
+
|
|
189
|
+
uiContext.updateComputedProperties(allElementData)
|
|
190
|
+
|
|
191
|
+
expect(xSpy).toHaveBeenCalledWith(MIXED_STRING)
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
// Test updateZoom
|
|
195
|
+
it('should update zoom value', () => {
|
|
196
|
+
const nextSpy = vi.spyOn(uiContext.zoom, 'next')
|
|
197
|
+
|
|
198
|
+
uiContext.updateZoom(2)
|
|
199
|
+
|
|
200
|
+
expect(nextSpy).toHaveBeenCalledWith(2)
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
// Test updatePrimaryTool
|
|
204
|
+
it('should update primaryTool when tool is different', () => {
|
|
205
|
+
const nextSpy = vi.spyOn(uiContext.primaryTool, 'next')
|
|
206
|
+
|
|
207
|
+
uiContext.updatePrimaryTool(PrimaryToolType.RECTANGLE)
|
|
208
|
+
|
|
209
|
+
expect(nextSpy).toHaveBeenCalledWith(PrimaryToolType.RECTANGLE)
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
it('should not update primaryTool when tool is the same', () => {
|
|
213
|
+
uiContext.primaryTool.next(PrimaryToolType.SELECT)
|
|
214
|
+
const nextSpy = vi.spyOn(uiContext.primaryTool, 'next')
|
|
215
|
+
|
|
216
|
+
uiContext.updatePrimaryTool(PrimaryToolType.SELECT)
|
|
217
|
+
|
|
218
|
+
expect(nextSpy).not.toHaveBeenCalled()
|
|
219
|
+
})
|
|
220
|
+
})
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
2
|
+
import RenderStore from '../render'
|
|
3
|
+
import * as UIContextModule from '../../ui-context'
|
|
4
|
+
|
|
5
|
+
// Mock ui-context
|
|
6
|
+
vi.mock('../../ui-context', () => ({
|
|
7
|
+
default: {
|
|
8
|
+
updateZoom: vi.fn()
|
|
9
|
+
}
|
|
10
|
+
}))
|
|
11
|
+
|
|
12
|
+
describe('RenderStore', () => {
|
|
13
|
+
let renderStore: RenderStore
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
vi.clearAllMocks()
|
|
17
|
+
vi.resetAllMocks()
|
|
18
|
+
renderStore = new RenderStore()
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('should update zoom in ui context', () => {
|
|
22
|
+
renderStore.updateZoom(2.5)
|
|
23
|
+
|
|
24
|
+
expect(UIContextModule.default.updateZoom).toHaveBeenCalledWith(2.5)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('should update zoom to 1', () => {
|
|
28
|
+
renderStore.updateZoom(1)
|
|
29
|
+
|
|
30
|
+
expect(UIContextModule.default.updateZoom).toHaveBeenCalledWith(1)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('should update zoom to 0.5', () => {
|
|
34
|
+
renderStore.updateZoom(0.5)
|
|
35
|
+
|
|
36
|
+
expect(UIContextModule.default.updateZoom).toHaveBeenCalledWith(0.5)
|
|
37
|
+
})
|
|
38
|
+
})
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest'
|
|
2
|
+
import SelectionStore from '../selection'
|
|
3
|
+
import * as SelectionModule from '@asyra/selection'
|
|
4
|
+
import * as SceneTreeModule from '@asyra/scene-tree'
|
|
5
|
+
import * as UIContextModule from '../../ui-context'
|
|
6
|
+
import {
|
|
7
|
+
SELECTION_TYPES,
|
|
8
|
+
ComputedAttrs,
|
|
9
|
+
EntityTypes,
|
|
10
|
+
ElementInstanceTypes
|
|
11
|
+
} from '@asyra/utils'
|
|
12
|
+
|
|
13
|
+
// Mock external dependencies
|
|
14
|
+
vi.mock('@asyra/selection', () => ({
|
|
15
|
+
default: {
|
|
16
|
+
get: vi.fn()
|
|
17
|
+
},
|
|
18
|
+
SelectionManager: vi.fn()
|
|
19
|
+
}))
|
|
20
|
+
|
|
21
|
+
vi.mock('@asyra/scene-tree', () => ({
|
|
22
|
+
default: {
|
|
23
|
+
getElementById: vi.fn()
|
|
24
|
+
}
|
|
25
|
+
}))
|
|
26
|
+
|
|
27
|
+
vi.mock('../../ui-context', () => ({
|
|
28
|
+
default: {
|
|
29
|
+
updateElementSelection: vi.fn(),
|
|
30
|
+
updateVertexSelection: vi.fn(),
|
|
31
|
+
updateComputedProperties: vi.fn()
|
|
32
|
+
}
|
|
33
|
+
}))
|
|
34
|
+
|
|
35
|
+
describe('SelectionStore', () => {
|
|
36
|
+
let selectionStore: SelectionStore
|
|
37
|
+
let mockSelectionManager: { get: Mock }
|
|
38
|
+
let mockSelection: { getSelectedIds: Mock }
|
|
39
|
+
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
vi.clearAllMocks()
|
|
42
|
+
vi.resetAllMocks()
|
|
43
|
+
|
|
44
|
+
mockSelection = {
|
|
45
|
+
getSelectedIds: vi.fn(() => new Set(['elem-1', 'elem-2']))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
mockSelectionManager = {
|
|
49
|
+
get: vi.fn(() => mockSelection)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
vi.mocked(SelectionModule.default.get).mockImplementation(
|
|
53
|
+
mockSelectionManager.get
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
selectionStore = new SelectionStore()
|
|
57
|
+
selectionStore.selectionManager =
|
|
58
|
+
mockSelectionManager as unknown as SelectionModule.SelectionManager
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
// Test updateSelection for ELEMENT type
|
|
62
|
+
it('should update element selection and computed properties when elements are selected', () => {
|
|
63
|
+
const mockElement1 = {
|
|
64
|
+
getAllComputedData: vi.fn(
|
|
65
|
+
() =>
|
|
66
|
+
({
|
|
67
|
+
id: 'elem-1',
|
|
68
|
+
type: EntityTypes.RECTANGLE,
|
|
69
|
+
name: 'Element 1',
|
|
70
|
+
x: 10,
|
|
71
|
+
y: 20,
|
|
72
|
+
width: 100,
|
|
73
|
+
height: 50,
|
|
74
|
+
rotation: 0
|
|
75
|
+
}) as ComputedAttrs
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const mockElement2 = {
|
|
80
|
+
getAllComputedData: vi.fn(
|
|
81
|
+
() =>
|
|
82
|
+
({
|
|
83
|
+
id: 'elem-2',
|
|
84
|
+
type: EntityTypes.RECTANGLE,
|
|
85
|
+
name: 'Element 2',
|
|
86
|
+
x: 10,
|
|
87
|
+
y: 20,
|
|
88
|
+
width: 100,
|
|
89
|
+
height: 50,
|
|
90
|
+
rotation: 0
|
|
91
|
+
}) as ComputedAttrs
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
vi.mocked(SceneTreeModule.default.getElementById)
|
|
96
|
+
.mockReturnValueOnce(mockElement1 as unknown as ElementInstanceTypes)
|
|
97
|
+
.mockReturnValueOnce(mockElement2 as unknown as ElementInstanceTypes)
|
|
98
|
+
|
|
99
|
+
selectionStore.updateSelection(SELECTION_TYPES.ELEMENT)
|
|
100
|
+
|
|
101
|
+
expect(mockSelectionManager.get).toHaveBeenCalledWith(
|
|
102
|
+
SELECTION_TYPES.ELEMENT
|
|
103
|
+
)
|
|
104
|
+
expect(UIContextModule.default.updateElementSelection).toHaveBeenCalledWith(
|
|
105
|
+
new Set(['elem-1', 'elem-2'])
|
|
106
|
+
)
|
|
107
|
+
expect(
|
|
108
|
+
UIContextModule.default.updateComputedProperties
|
|
109
|
+
).toHaveBeenCalledWith([
|
|
110
|
+
{
|
|
111
|
+
id: 'elem-1',
|
|
112
|
+
type: EntityTypes.RECTANGLE,
|
|
113
|
+
name: 'Element 1',
|
|
114
|
+
x: 10,
|
|
115
|
+
y: 20,
|
|
116
|
+
width: 100,
|
|
117
|
+
height: 50,
|
|
118
|
+
rotation: 0
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
id: 'elem-2',
|
|
122
|
+
type: EntityTypes.RECTANGLE,
|
|
123
|
+
name: 'Element 2',
|
|
124
|
+
x: 10,
|
|
125
|
+
y: 20,
|
|
126
|
+
width: 100,
|
|
127
|
+
height: 50,
|
|
128
|
+
rotation: 0
|
|
129
|
+
}
|
|
130
|
+
])
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it('should not update computed properties when no elements are selected', () => {
|
|
134
|
+
mockSelection.getSelectedIds = vi.fn(() => new Set())
|
|
135
|
+
|
|
136
|
+
selectionStore.updateSelection(SELECTION_TYPES.ELEMENT)
|
|
137
|
+
|
|
138
|
+
expect(UIContextModule.default.updateElementSelection).toHaveBeenCalledWith(
|
|
139
|
+
new Set()
|
|
140
|
+
)
|
|
141
|
+
expect(
|
|
142
|
+
UIContextModule.default.updateComputedProperties
|
|
143
|
+
).not.toHaveBeenCalled()
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
it('should handle missing elements gracefully', () => {
|
|
147
|
+
vi.mocked(SceneTreeModule.default.getElementById)
|
|
148
|
+
.mockReturnValueOnce(undefined as unknown as ElementInstanceTypes)
|
|
149
|
+
.mockReturnValueOnce(undefined as unknown as ElementInstanceTypes)
|
|
150
|
+
|
|
151
|
+
selectionStore.updateSelection(SELECTION_TYPES.ELEMENT)
|
|
152
|
+
|
|
153
|
+
expect(UIContextModule.default.updateElementSelection).toHaveBeenCalled()
|
|
154
|
+
expect(
|
|
155
|
+
UIContextModule.default.updateComputedProperties
|
|
156
|
+
).toHaveBeenCalledWith([])
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
// Test updateSelection for VERTEX type
|
|
160
|
+
it('should update vertex selection when vertices are selected', () => {
|
|
161
|
+
mockSelection.getSelectedIds = vi.fn(
|
|
162
|
+
() => new Set(['vertex-1', 'vertex-2'])
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
selectionStore.updateSelection(SELECTION_TYPES.VERTEX)
|
|
166
|
+
|
|
167
|
+
expect(mockSelectionManager.get).toHaveBeenCalledWith(
|
|
168
|
+
SELECTION_TYPES.VERTEX
|
|
169
|
+
)
|
|
170
|
+
expect(UIContextModule.default.updateVertexSelection).toHaveBeenCalledWith(
|
|
171
|
+
new Set(['vertex-1', 'vertex-2'])
|
|
172
|
+
)
|
|
173
|
+
expect(
|
|
174
|
+
UIContextModule.default.updateComputedProperties
|
|
175
|
+
).not.toHaveBeenCalled()
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
it('should return early when selection is null', () => {
|
|
179
|
+
mockSelectionManager.get = vi.fn(() => null)
|
|
180
|
+
|
|
181
|
+
selectionStore.updateSelection(SELECTION_TYPES.ELEMENT)
|
|
182
|
+
|
|
183
|
+
expect(
|
|
184
|
+
UIContextModule.default.updateElementSelection
|
|
185
|
+
).not.toHaveBeenCalled()
|
|
186
|
+
expect(
|
|
187
|
+
UIContextModule.default.updateComputedProperties
|
|
188
|
+
).not.toHaveBeenCalled()
|
|
189
|
+
})
|
|
190
|
+
})
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { BehaviorSubject } from 'rxjs'
|
|
2
|
+
import { EntityTypes } from '@asyra/utils'
|
|
3
|
+
import type {
|
|
4
|
+
ComputedAttrs,
|
|
5
|
+
DataTypes,
|
|
6
|
+
ElementRawData,
|
|
7
|
+
GroupRawData,
|
|
8
|
+
WorkspaceRawData
|
|
9
|
+
} from '@asyra/utils'
|
|
10
|
+
import type { SceneTree, Workspace } from '@asyra/scene-tree'
|
|
11
|
+
import uiContext from '../ui-context'
|
|
12
|
+
|
|
13
|
+
type UIWorkspaceData = Partial<
|
|
14
|
+
Pick<WorkspaceRawData, 'id' | 'name' | 'type' | 'children'> & ComputedAttrs
|
|
15
|
+
>
|
|
16
|
+
|
|
17
|
+
type UIAllElementData = (ElementRawData | GroupRawData) & ComputedAttrs
|
|
18
|
+
|
|
19
|
+
type UIElementData = Partial<UIAllElementData>
|
|
20
|
+
|
|
21
|
+
export default class SceneTreeStore {
|
|
22
|
+
private sceneTree: SceneTree
|
|
23
|
+
private dirty: boolean
|
|
24
|
+
private workspaceId: string
|
|
25
|
+
private workspace: BehaviorSubject<UIWorkspaceData>
|
|
26
|
+
private _elements: Map<
|
|
27
|
+
string,
|
|
28
|
+
BehaviorSubject<UIElementData> | BehaviorSubject<UIWorkspaceData>
|
|
29
|
+
>
|
|
30
|
+
private _deletedMap: Map<
|
|
31
|
+
string,
|
|
32
|
+
BehaviorSubject<UIElementData> | BehaviorSubject<UIWorkspaceData>
|
|
33
|
+
>
|
|
34
|
+
|
|
35
|
+
constructor(sceneTree: SceneTree) {
|
|
36
|
+
this.sceneTree = sceneTree
|
|
37
|
+
this.dirty = false
|
|
38
|
+
this.workspaceId = ''
|
|
39
|
+
this.workspace = new BehaviorSubject<UIWorkspaceData>({
|
|
40
|
+
id: this.workspaceId,
|
|
41
|
+
name: '',
|
|
42
|
+
children: [],
|
|
43
|
+
type: EntityTypes.WORKSPACE
|
|
44
|
+
})
|
|
45
|
+
this._elements = new Map()
|
|
46
|
+
this._deletedMap = new Map()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
reload() {
|
|
50
|
+
this.workspaceId = this.sceneTree.workspace
|
|
51
|
+
const currentWs = this.sceneTree.currentWorkspace
|
|
52
|
+
|
|
53
|
+
if (
|
|
54
|
+
this.workspaceId &&
|
|
55
|
+
currentWs &&
|
|
56
|
+
currentWs.get('id') === this.workspaceId
|
|
57
|
+
) {
|
|
58
|
+
const ws = currentWs
|
|
59
|
+
this.workspace = new BehaviorSubject<UIWorkspaceData>({
|
|
60
|
+
id: ws.get('id'),
|
|
61
|
+
name: ws.get('name'),
|
|
62
|
+
type: ws.get('type'),
|
|
63
|
+
children: [...((ws as Workspace).get('children') || [])]
|
|
64
|
+
})
|
|
65
|
+
this.addToMap(ws.get('id'), this.workspace)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
this.sceneTree.getAllElements().forEach((element, id) => {
|
|
69
|
+
if (element.get('type') !== EntityTypes.WORKSPACE) {
|
|
70
|
+
this.addToMap(id, new BehaviorSubject(element.save() as UIElementData))
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
this.updateFlattenedElementIds()
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
markDirty() {
|
|
78
|
+
this.dirty = true
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
clearDirty() {
|
|
82
|
+
this.dirty = false
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
fireChange() {
|
|
86
|
+
if (this.dirty) {
|
|
87
|
+
this.clearDirty()
|
|
88
|
+
this.updateFlattenedElementIds()
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
getElement(
|
|
93
|
+
elementId: string
|
|
94
|
+
):
|
|
95
|
+
| BehaviorSubject<UIElementData>
|
|
96
|
+
| BehaviorSubject<UIWorkspaceData>
|
|
97
|
+
| undefined {
|
|
98
|
+
return this._elements.get(elementId)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
addToMap(
|
|
102
|
+
elementId: string,
|
|
103
|
+
elementSubject:
|
|
104
|
+
| BehaviorSubject<UIElementData>
|
|
105
|
+
| BehaviorSubject<UIWorkspaceData>
|
|
106
|
+
) {
|
|
107
|
+
this.removeFromDeleteMap(elementId)
|
|
108
|
+
this._elements.set(elementId, elementSubject)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
removeFromMap(elementId: string) {
|
|
112
|
+
const elementSubject = this.getElement(elementId)
|
|
113
|
+
if (elementSubject) {
|
|
114
|
+
this.addToDeleteMap(elementId, elementSubject)
|
|
115
|
+
}
|
|
116
|
+
this._elements.delete(elementId)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
addToDeleteMap(
|
|
120
|
+
elementId: string,
|
|
121
|
+
elementSubject:
|
|
122
|
+
| BehaviorSubject<UIElementData>
|
|
123
|
+
| BehaviorSubject<UIWorkspaceData>
|
|
124
|
+
) {
|
|
125
|
+
this._deletedMap.set(elementId, elementSubject)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
getRestoreElementById(
|
|
129
|
+
elementId: string
|
|
130
|
+
):
|
|
131
|
+
| BehaviorSubject<UIElementData>
|
|
132
|
+
| BehaviorSubject<UIWorkspaceData>
|
|
133
|
+
| undefined {
|
|
134
|
+
return this._deletedMap.get(elementId)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
removeFromDeleteMap(elementId: string) {
|
|
138
|
+
this._deletedMap.delete(elementId)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
isGroup(element: UIElementData) {
|
|
142
|
+
return 'children' in element
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
getFlattenedElementIds() {
|
|
146
|
+
const ids: string[] = []
|
|
147
|
+
const workspace = this.workspace.getValue()
|
|
148
|
+
if (workspace?.children) {
|
|
149
|
+
workspace.children.forEach((childId: string) => {
|
|
150
|
+
this.collectChildrenIds(childId, ids)
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return ids
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
collectChildrenIds(elementId: string, ids: string[]): void {
|
|
158
|
+
const elementS = this.getElement(elementId)
|
|
159
|
+
if (!elementS) return
|
|
160
|
+
|
|
161
|
+
const element = elementS.getValue()
|
|
162
|
+
if (element.id) {
|
|
163
|
+
ids.push(element.id)
|
|
164
|
+
}
|
|
165
|
+
if (this.isGroup(element)) {
|
|
166
|
+
;(element as GroupRawData).children.forEach((childId: string) => {
|
|
167
|
+
const child = this.getElement(childId)?.getValue()
|
|
168
|
+
if (!child) return
|
|
169
|
+
|
|
170
|
+
this.collectChildrenIds(childId, ids)
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
updateFlattenedElementIds() {
|
|
176
|
+
uiContext.flattenedElementIds.next(this.getFlattenedElementIds())
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
addElement(data: Partial<ElementRawData | GroupRawData>) {
|
|
180
|
+
this.markDirty()
|
|
181
|
+
const elementSubject =
|
|
182
|
+
this.getRestoreElementById(data.id as string) || new BehaviorSubject(data)
|
|
183
|
+
this.addToMap(data.id as string, elementSubject)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
removeElement(
|
|
187
|
+
data: Partial<ElementRawData | GroupRawData>,
|
|
188
|
+
parentId: string
|
|
189
|
+
): void {
|
|
190
|
+
this.markDirty()
|
|
191
|
+
const parent = this.getElement(parentId)
|
|
192
|
+
const avaliableParent =
|
|
193
|
+
parent ?? (this.workspace as BehaviorSubject<UIWorkspaceData>)
|
|
194
|
+
if (avaliableParent && data.id) {
|
|
195
|
+
const parentData = avaliableParent.getValue() as GroupRawData
|
|
196
|
+
const idx = parentData.children.indexOf(data.id)
|
|
197
|
+
const newChildren = [...parentData.children]
|
|
198
|
+
|
|
199
|
+
newChildren.splice(idx, 1)
|
|
200
|
+
|
|
201
|
+
avaliableParent.next({
|
|
202
|
+
...parentData,
|
|
203
|
+
children: newChildren
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
this.removeFromMap(data.id)
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
updateElement(elementId: string, key: string, after: DataTypes) {
|
|
211
|
+
const element = this.getElement(elementId)
|
|
212
|
+
if (!element) return
|
|
213
|
+
|
|
214
|
+
this.markDirty()
|
|
215
|
+
const current = element.getValue()
|
|
216
|
+
if ('children' in current) {
|
|
217
|
+
;(element as BehaviorSubject<UIWorkspaceData | GroupRawData>).next({
|
|
218
|
+
...current,
|
|
219
|
+
[key]: after
|
|
220
|
+
})
|
|
221
|
+
} else {
|
|
222
|
+
;(element as BehaviorSubject<UIElementData>).next({
|
|
223
|
+
...current,
|
|
224
|
+
[key]: after
|
|
225
|
+
})
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
getElementGeneralData(elementId: string) {
|
|
230
|
+
const element = this.getElement(elementId)
|
|
231
|
+
if (!element) return
|
|
232
|
+
|
|
233
|
+
return element.getValue()
|
|
234
|
+
}
|
|
235
|
+
}
|