@asyra/scene-tree 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.
Files changed (117) hide show
  1. package/.turbo/turbo-test$colon$ci.log +134 -0
  2. package/.turbo/turbo-test$colon$local.log +46 -0
  3. package/.turbo/turbo-test.log +53 -0
  4. package/README.md +1 -0
  5. package/coverage/base.css +224 -0
  6. package/coverage/block-navigation.js +87 -0
  7. package/coverage/clover.xml +778 -0
  8. package/coverage/coverage-final.json +10478 -0
  9. package/coverage/favicon.png +0 -0
  10. package/coverage/index.html +131 -0
  11. package/coverage/prettify.css +1 -0
  12. package/coverage/prettify.js +2 -0
  13. package/coverage/sort-arrow-sprite.png +0 -0
  14. package/coverage/sorter.js +196 -0
  15. package/coverage/src/components/computed.ts.html +313 -0
  16. package/coverage/src/components/element-change-handler.ts.html +145 -0
  17. package/coverage/src/components/element.ts.html +586 -0
  18. package/coverage/src/components/frame.ts.html +169 -0
  19. package/coverage/src/components/group.ts.html +295 -0
  20. package/coverage/src/components/index.html +236 -0
  21. package/coverage/src/components/index.ts.html +103 -0
  22. package/coverage/src/components/props.ts.html +355 -0
  23. package/coverage/src/components/rectangle.ts.html +169 -0
  24. package/coverage/src/components/workspace.ts.html +433 -0
  25. package/coverage/src/index.html +161 -0
  26. package/coverage/src/index.ts.html +103 -0
  27. package/coverage/src/sceneTree.ts.html +853 -0
  28. package/coverage/src/subscribes.ts.html +295 -0
  29. package/coverage/src/utils.ts.html +328 -0
  30. package/dist/__tests__/sceneTree.test.d.ts +2 -0
  31. package/dist/__tests__/sceneTree.test.d.ts.map +1 -0
  32. package/dist/__tests__/sceneTree.test.js +261 -0
  33. package/dist/__tests__/sceneTree.test.js.map +1 -0
  34. package/dist/components/computed.d.ts +13 -0
  35. package/dist/components/computed.d.ts.map +1 -0
  36. package/dist/components/computed.js +52 -0
  37. package/dist/components/computed.js.map +1 -0
  38. package/dist/components/element-change-handler.d.ts +10 -0
  39. package/dist/components/element-change-handler.d.ts.map +1 -0
  40. package/dist/components/element-change-handler.js +14 -0
  41. package/dist/components/element-change-handler.js.map +1 -0
  42. package/dist/components/element.d.ts +22 -0
  43. package/dist/components/element.d.ts.map +1 -0
  44. package/dist/components/element.js +114 -0
  45. package/dist/components/element.js.map +1 -0
  46. package/dist/components/frame.d.ts +11 -0
  47. package/dist/components/frame.d.ts.map +1 -0
  48. package/dist/components/frame.js +21 -0
  49. package/dist/components/frame.js.map +1 -0
  50. package/dist/components/group.d.ts +14 -0
  51. package/dist/components/group.d.ts.map +1 -0
  52. package/dist/components/group.js +48 -0
  53. package/dist/components/group.js.map +1 -0
  54. package/dist/components/index.d.ts +6 -0
  55. package/dist/components/index.d.ts.map +1 -0
  56. package/dist/components/index.js +6 -0
  57. package/dist/components/index.js.map +1 -0
  58. package/dist/components/legacy-element.d.ts +22 -0
  59. package/dist/components/legacy-element.d.ts.map +1 -0
  60. package/dist/components/legacy-element.js +120 -0
  61. package/dist/components/legacy-element.js.map +1 -0
  62. package/dist/components/polygon.d.ts +11 -0
  63. package/dist/components/polygon.d.ts.map +1 -0
  64. package/dist/components/polygon.js +21 -0
  65. package/dist/components/polygon.js.map +1 -0
  66. package/dist/components/props.d.ts +16 -0
  67. package/dist/components/props.d.ts.map +1 -0
  68. package/dist/components/props.js +63 -0
  69. package/dist/components/props.js.map +1 -0
  70. package/dist/components/rectangle.d.ts +11 -0
  71. package/dist/components/rectangle.d.ts.map +1 -0
  72. package/dist/components/rectangle.js +21 -0
  73. package/dist/components/rectangle.js.map +1 -0
  74. package/dist/components/shape-entity.d.ts +16 -0
  75. package/dist/components/shape-entity.d.ts.map +1 -0
  76. package/dist/components/shape-entity.js +42 -0
  77. package/dist/components/shape-entity.js.map +1 -0
  78. package/dist/components/shape.d.ts +40 -0
  79. package/dist/components/shape.d.ts.map +1 -0
  80. package/dist/components/shape.js +105 -0
  81. package/dist/components/shape.js.map +1 -0
  82. package/dist/components/workspace.d.ts +14 -0
  83. package/dist/components/workspace.d.ts.map +1 -0
  84. package/dist/components/workspace.js +89 -0
  85. package/dist/components/workspace.js.map +1 -0
  86. package/dist/index.d.ts +3 -0
  87. package/dist/index.d.ts.map +1 -0
  88. package/dist/index.js +5 -0
  89. package/dist/index.js.map +1 -0
  90. package/dist/sceneTree.d.ts +34 -0
  91. package/dist/sceneTree.d.ts.map +1 -0
  92. package/dist/sceneTree.js +185 -0
  93. package/dist/sceneTree.js.map +1 -0
  94. package/dist/subscribes.d.ts +2 -0
  95. package/dist/subscribes.d.ts.map +1 -0
  96. package/dist/subscribes.js +38 -0
  97. package/dist/subscribes.js.map +1 -0
  98. package/dist/utils.d.ts +19 -0
  99. package/dist/utils.d.ts.map +1 -0
  100. package/dist/utils.js +61 -0
  101. package/dist/utils.js.map +1 -0
  102. package/package.json +30 -0
  103. package/src/__tests__/sceneTree.test.ts +339 -0
  104. package/src/components/computed.ts +76 -0
  105. package/src/components/element-change-handler.ts +20 -0
  106. package/src/components/element.ts +167 -0
  107. package/src/components/frame.ts +28 -0
  108. package/src/components/group.ts +70 -0
  109. package/src/components/index.ts +6 -0
  110. package/src/components/props.ts +90 -0
  111. package/src/components/rectangle.ts +28 -0
  112. package/src/components/workspace.ts +116 -0
  113. package/src/index.ts +6 -0
  114. package/src/sceneTree.ts +256 -0
  115. package/src/subscribes.ts +70 -0
  116. package/src/utils.ts +81 -0
  117. package/tsconfig.json +17 -0
@@ -0,0 +1,339 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest'
2
+ import {
3
+ ElementInstanceTypes,
4
+ EntityTypes,
5
+ OWNER,
6
+ SCENE_TREE_ACTIONS,
7
+ SceneTreeChange,
8
+ resetIdCounter
9
+ } from '@asyra/utils'
10
+ import { SceneTree } from '../sceneTree'
11
+ import * as utils from '../utils'
12
+ import { EventTypes } from '@asyra/reactive-events' // Import EventTypes
13
+ import Workspace from '../components/workspace' // Assuming Workspace is a class
14
+ import Rectangle from '../components/rectangle'
15
+
16
+ vi.mock('../utils', () => ({
17
+ createElement: vi.fn(),
18
+ createWorkspace: vi.fn(),
19
+ stripNonRawFields: vi.fn((data: Record<string, unknown>) => {
20
+ const stripped: Record<string, unknown> = {}
21
+ Object.keys(data).forEach((key) => {
22
+ if (!['id', 'type', 'name', 'props'].includes(key)) {
23
+ stripped[key] = data[key]
24
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
25
+ delete data[key]
26
+ }
27
+ })
28
+ return stripped
29
+ })
30
+ }))
31
+
32
+ describe('SceneTree', () => {
33
+ let sceneTree: SceneTree
34
+
35
+ beforeEach(() => {
36
+ // Reset mocks before each test
37
+ vi.clearAllMocks()
38
+
39
+ resetIdCounter()
40
+ sceneTree = new SceneTree()
41
+ })
42
+
43
+ // Test _init() and basic workspace creation
44
+ it('should initialize with a new workspace if none exists', () => {
45
+ // Mock to return a workspace with proper ID
46
+ vi.mocked(utils.createWorkspace).mockReturnValue({
47
+ get: vi.fn().mockReturnValue('ws-1'),
48
+ save: vi
49
+ .fn()
50
+ .mockReturnValue({ id: 'ws-1', type: EntityTypes.WORKSPACE }),
51
+ addNewElement: vi.fn(),
52
+ load: vi.fn()
53
+ } as unknown as Workspace)
54
+
55
+ sceneTree.init()
56
+
57
+ expect(sceneTree.workspace).toBe('ws-1')
58
+ expect(sceneTree.workspaceList).toEqual(['ws-1'])
59
+ expect(sceneTree.getAllElements().has('ws-1')).toBe(true)
60
+ })
61
+
62
+ it('should not create a new workspace if one already exists', () => {
63
+ // Create a new SceneTree instance for this test to avoid interference
64
+ const customSceneTree = new SceneTree()
65
+ customSceneTree.workspace = 'existing-workspace'
66
+ customSceneTree.workspaceList = ['existing-workspace']
67
+
68
+ customSceneTree.init()
69
+
70
+ expect(utils.createWorkspace).not.toHaveBeenCalled()
71
+ })
72
+
73
+ // Test element creation and management
74
+ it('should create a new element and add a change for it', () => {
75
+ // Mock to return an element with proper ID based on input
76
+ const elementData = { id: 'el-1', type: EntityTypes.RECTANGLE }
77
+ vi.mocked(utils.createElement).mockReturnValue({
78
+ get: vi.fn().mockReturnValue('el-1'),
79
+ save: vi.fn().mockReturnValue(elementData),
80
+ updateComputedData: vi.fn()
81
+ } as unknown as Rectangle)
82
+
83
+ const newElement = sceneTree.createElement(elementData)
84
+
85
+ expect(utils.createElement).toHaveBeenCalledWith(elementData)
86
+ expect(newElement?.get('id')).toBe('el-1')
87
+ expect(sceneTree.changes.length).toBe(1)
88
+ expect(sceneTree.changes[0]).toEqual({
89
+ eventName: EventTypes.ADD_ELEMENT,
90
+ data: elementData,
91
+ action: SCENE_TREE_ACTIONS.ADD_ELEMENT,
92
+ owner: OWNER.SCENE_TREE,
93
+ undoType: EventTypes.REMOVE_ELEMENT,
94
+ undoAction: EventTypes.REMOVE_ELEMENT // This should be REMOVE_ELEMENT for undo
95
+ })
96
+ })
97
+
98
+ it('should not create a workspace element using createElement', () => {
99
+ const elementData = { id: 'ws-2', type: EntityTypes.WORKSPACE }
100
+ const newElement = sceneTree.createElement(elementData)
101
+
102
+ expect(newElement).toBeNull()
103
+ expect(sceneTree.changes.length).toBe(0)
104
+ })
105
+
106
+ it('should add an element to the map', () => {
107
+ const element = {
108
+ get: vi.fn(() => 'el-add')
109
+ } as unknown as ElementInstanceTypes
110
+
111
+ sceneTree.addToMap(element)
112
+
113
+ expect(sceneTree.getAllElements().has('el-add')).toBe(true)
114
+ })
115
+
116
+ it('should remove an element from the map', () => {
117
+ const element = {
118
+ get: vi.fn(() => 'el-remove')
119
+ } as unknown as ElementInstanceTypes
120
+
121
+ sceneTree.addToMap(element)
122
+ expect(sceneTree.getAllElements().has('el-remove')).toBe(true)
123
+
124
+ sceneTree.removeFromMap(element)
125
+ expect(sceneTree.getAllElements().has('el-remove')).toBe(false)
126
+ })
127
+
128
+ it('should get an element by ID', () => {
129
+ const element = {
130
+ get: vi.fn(() => 'el-get')
131
+ } as unknown as ElementInstanceTypes
132
+
133
+ sceneTree.addToMap(element)
134
+
135
+ expect(sceneTree.getElementById('el-get')).toBe(element)
136
+ })
137
+
138
+ // Test change tracking
139
+ it('should add a change to the changes array', () => {
140
+ const change = {
141
+ eventName: EventTypes.ADD_ELEMENT
142
+ } as unknown as SceneTreeChange
143
+
144
+ sceneTree.addChange(change)
145
+
146
+ expect(sceneTree.changes).toEqual([change])
147
+ })
148
+
149
+ it('should clean all changes', () => {
150
+ sceneTree.addChange({} as unknown as SceneTreeChange)
151
+
152
+ sceneTree.cleanChanges()
153
+
154
+ expect(sceneTree.changes).toEqual([])
155
+ })
156
+
157
+ it('should add a change for adding an element', () => {
158
+ const elementData = { id: 'el-change', type: EntityTypes.RECTANGLE }
159
+ const element = {
160
+ save: vi.fn(() => elementData),
161
+ get: vi.fn(() => 'el-change')
162
+ } as unknown as ElementInstanceTypes
163
+
164
+ sceneTree.addChangeForAddElement(element)
165
+
166
+ expect(sceneTree.changes.length).toBe(1)
167
+ expect(sceneTree.changes[0].action).toBe(SCENE_TREE_ACTIONS.ADD_ELEMENT)
168
+ })
169
+
170
+ it('should add a change for removing an element', () => {
171
+ const elementData = { id: 'el-change-remove', type: EntityTypes.RECTANGLE }
172
+ const element = {
173
+ save: vi.fn(() => elementData),
174
+ get: vi.fn(() => 'el-change-remove')
175
+ } as unknown as ElementInstanceTypes
176
+
177
+ sceneTree.addChangeForRemoveElement(element)
178
+
179
+ expect(sceneTree.changes.length).toBe(1)
180
+ expect(sceneTree.changes[0].action).toBe(SCENE_TREE_ACTIONS.REMOVE_ELEMENT)
181
+ })
182
+
183
+ // Test delete map functionality
184
+ it('should add an element to the deleted map', () => {
185
+ const element = {
186
+ get: vi.fn(() => 'el-deleted')
187
+ } as unknown as ElementInstanceTypes
188
+
189
+ sceneTree.addToDeleteMap(element)
190
+
191
+ expect(sceneTree._deletedMap.has('el-deleted')).toBe(true)
192
+ })
193
+
194
+ it('should remove an element from the deleted map', () => {
195
+ const element = {
196
+ get: vi.fn(() => 'el-deleted-remove')
197
+ } as unknown as ElementInstanceTypes
198
+ sceneTree.addToDeleteMap(element)
199
+
200
+ sceneTree.removeFromDeleteMap('el-deleted-remove')
201
+
202
+ expect(sceneTree._deletedMap.has('el-deleted-remove')).toBe(false)
203
+ })
204
+
205
+ it('should restore an element from the deleted map and add a change', () => {
206
+ const elementData = { id: 'el-restore', type: EntityTypes.RECTANGLE }
207
+ const element = {
208
+ save: vi.fn(() => elementData),
209
+ get: vi.fn(() => 'el-restore')
210
+ } as unknown as ElementInstanceTypes
211
+ sceneTree.addToDeleteMap(element)
212
+
213
+ const restored = sceneTree.getRestoreElementById('el-restore')
214
+
215
+ expect(restored).toBe(element)
216
+ // expect(sceneTree._deletedMap.has('el-restore')).toBe(false)
217
+ expect(sceneTree.changes.length).toBe(1)
218
+ expect(sceneTree.changes[0].action).toBe(SCENE_TREE_ACTIONS.ADD_ELEMENT)
219
+ })
220
+
221
+ // Test addNewElement (delegated to workspace)
222
+ it('should call addNewElement on the current workspace', () => {
223
+ sceneTree.init() // Ensure workspace is initialized
224
+ const elementData = {
225
+ x: 100,
226
+ y: 100
227
+ }
228
+ const workspace = sceneTree.currentWorkspace as Workspace
229
+ vi.spyOn(workspace, 'addNewElement')
230
+
231
+ sceneTree.addNewElement(elementData, undefined, -1, false)
232
+
233
+ // The workspace.addNewElement should be called with an ElementInstanceTypes, not the raw data
234
+ expect(workspace.addNewElement).toHaveBeenCalled()
235
+ expect(workspace.addNewElement).toHaveBeenCalledWith(
236
+ expect.any(Object),
237
+ undefined,
238
+ -1
239
+ )
240
+ })
241
+
242
+ // Test updateComputedData
243
+ it('should call updateComputedData on the element', () => {
244
+ const element = {
245
+ get: vi.fn(() => 'el-computed'),
246
+ updateComputedData: vi.fn()
247
+ } as unknown as ElementInstanceTypes
248
+ sceneTree.addToMap(element)
249
+ sceneTree.updateComputedData('el-computed', 'x', 100)
250
+ expect(element.updateComputedData).toHaveBeenCalledWith('x', 100)
251
+ })
252
+
253
+ // Test load and save
254
+ it('should load data correctly', () => {
255
+ const mockElement1 = {
256
+ id: 'el-load-1',
257
+ type: EntityTypes.RECTANGLE,
258
+ save: vi.fn(() => ({ id: 'el-load-1', type: EntityTypes.RECTANGLE })),
259
+ get: vi.fn((key: string) => (key === 'id' ? 'el-load-1' : undefined))
260
+ } as unknown as ElementInstanceTypes
261
+ const mockWorkspaceLoad = {
262
+ id: 'ws-load',
263
+ type: EntityTypes.WORKSPACE,
264
+ save: vi.fn(() => ({ id: 'ws-load', type: EntityTypes.WORKSPACE })),
265
+ get: vi.fn((key: string) => (key === 'id' ? 'ws-load' : undefined))
266
+ } as unknown as ElementInstanceTypes
267
+
268
+ vi.mocked(utils.createElement).mockImplementation(
269
+ () => mockElement1 as unknown as Rectangle
270
+ )
271
+ vi.mocked(utils.createWorkspace).mockImplementation(
272
+ () => mockWorkspaceLoad as unknown as Workspace
273
+ )
274
+
275
+ const dataToLoad = {
276
+ workspace: 'ws-load',
277
+ workspaceList: ['ws-load'],
278
+ elements: {
279
+ 'ws-load': {
280
+ id: 'ws-load',
281
+ type: EntityTypes.WORKSPACE,
282
+ name: 'ws-load',
283
+ visible: true,
284
+ lock: false,
285
+ children: []
286
+ },
287
+ 'el-load-1': {
288
+ id: 'el-load-1',
289
+ type: EntityTypes.RECTANGLE,
290
+ name: 'el-load-1',
291
+ visible: true,
292
+ lock: false
293
+ }
294
+ }
295
+ }
296
+
297
+ sceneTree.load(dataToLoad)
298
+
299
+ expect(sceneTree.workspace).toBe('ws-load')
300
+ expect(sceneTree.workspaceList).toEqual(['ws-load'])
301
+ expect(sceneTree.getAllElements().has('ws-load')).toBe(true)
302
+ expect(sceneTree.getAllElements().has('el-load-1')).toBe(true)
303
+ expect(utils.createWorkspace).toHaveBeenCalledWith(
304
+ dataToLoad.elements['ws-load']
305
+ )
306
+ expect(utils.createElement).toHaveBeenCalledWith(
307
+ dataToLoad.elements['el-load-1']
308
+ )
309
+ })
310
+
311
+ it('should save data correctly', () => {
312
+ // Mock workspace creation with proper ID
313
+ vi.mocked(utils.createWorkspace).mockReturnValue({
314
+ get: vi.fn().mockReturnValue('ws-1'),
315
+ save: vi
316
+ .fn()
317
+ .mockReturnValue({ id: 'ws-1', type: EntityTypes.WORKSPACE }),
318
+ addNewElement: vi.fn(),
319
+ load: vi.fn()
320
+ } as unknown as Workspace)
321
+
322
+ sceneTree.init() // Ensure initial workspace
323
+ const elementData = { id: 'el-1', type: EntityTypes.RECTANGLE }
324
+ const element = {
325
+ save: vi.fn(() => elementData),
326
+ get: vi.fn(() => 'el-1')
327
+ } as unknown as ElementInstanceTypes
328
+ const workspace = sceneTree.currentWorkspace as Workspace
329
+ const workspaceSaveData = workspace.save()
330
+ sceneTree.addToMap(element)
331
+
332
+ const savedData = sceneTree.save()
333
+
334
+ expect(savedData.workspace).toBe('ws-1')
335
+ expect(savedData.workspaceList).toEqual(['ws-1'])
336
+ expect(savedData.elements['ws-1']).toEqual(workspaceSaveData)
337
+ expect(savedData.elements['el-1']).toEqual(elementData)
338
+ })
339
+ })
@@ -0,0 +1,76 @@
1
+ import propsManager from '@asyra/props-manager'
2
+ import {
3
+ ComputedAttrs,
4
+ IComputed,
5
+ IDTypes,
6
+ NameTypes,
7
+ PropsRawData,
8
+ Setter
9
+ } from '@asyra/utils'
10
+ import Props from './props'
11
+ import ElementChangeHandler from './element-change-handler'
12
+
13
+ const elementChangeHandler = new ElementChangeHandler()
14
+
15
+ const PROPS_MAP: Record<string, string[]> = {
16
+ position: ['x', 'y'],
17
+ dimension: ['width', 'height']
18
+ }
19
+
20
+ class Computed<T extends ComputedAttrs>
21
+ extends Setter<T>
22
+ implements IComputed<T>
23
+ {
24
+ _idType!: IDTypes
25
+ _nameType!: NameTypes
26
+
27
+ constructor(elementId: string, props: Props) {
28
+ super(elementChangeHandler.addChange)
29
+
30
+ this._init()
31
+ this.data.id = elementId
32
+ this.setup(props)
33
+ }
34
+
35
+ _init() {
36
+ this.data = {
37
+ id: '',
38
+ x: 0,
39
+ y: 0,
40
+ width: 0,
41
+ height: 0
42
+ } as T
43
+ }
44
+
45
+ setup(props: Props): void {
46
+ Object.keys(PROPS_MAP).forEach((propName) => {
47
+ const map = PROPS_MAP[propName]
48
+ const propId = props[propName as keyof PropsRawData] as string
49
+ const propComponent = propsManager.getComponentById(propId)
50
+ if (!propComponent) {
51
+ return
52
+ }
53
+
54
+ const values = propComponent.getValue()
55
+ map.forEach((propKey) => {
56
+ this.data[propKey as keyof T] = values[propKey] as T[keyof T]
57
+ })
58
+ })
59
+ }
60
+
61
+ set<K extends keyof T>(key: K, data: T[K]) {
62
+ super.set(key, data)
63
+ }
64
+
65
+ save() {
66
+ const data = {} as T
67
+ data.x = this.data.x
68
+ data.y = this.data.y
69
+ data.width = this.data.width
70
+ data.height = this.data.height
71
+
72
+ return data
73
+ }
74
+ }
75
+
76
+ export default Computed
@@ -0,0 +1,20 @@
1
+ import type { ChangeHandler, DataTypes } from '@asyra/utils'
2
+ import { OWNER, SCENE_TREE_ACTIONS } from '@asyra/utils'
3
+ import { EventTypes } from '@asyra/reactive-events'
4
+ import sceneTree from '../sceneTree'
5
+
6
+ export default class ElementChangeHandler implements ChangeHandler {
7
+ addChange(data: {
8
+ id: string
9
+ key: string
10
+ before: DataTypes
11
+ after: DataTypes
12
+ }): void {
13
+ sceneTree.addChange({
14
+ action: SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA,
15
+ owner: OWNER.SCENE_TREE,
16
+ eventName: EventTypes.UPDATE_COMPUTED_DATA,
17
+ ...data
18
+ })
19
+ }
20
+ }
@@ -0,0 +1,167 @@
1
+ import type {
2
+ ElementRawData,
3
+ ElementAttrs,
4
+ IElement,
5
+ PropsRawData,
6
+ ComputedAttrs,
7
+ PropertyComponentInstanceDataTypes
8
+ } from '@asyra/utils'
9
+ import {
10
+ Setter,
11
+ IDTypes,
12
+ NameTypes,
13
+ EntityTypes,
14
+ id,
15
+ loadId,
16
+ name,
17
+ loadName
18
+ } from '@asyra/utils'
19
+ import Props from './props'
20
+ import Computed from './computed'
21
+ import ElementChangeHandler from './element-change-handler'
22
+
23
+ const elementChangeHandler = new ElementChangeHandler()
24
+
25
+ type ElementDataType = Partial<ElementRawData>
26
+
27
+ const ElementProps: (keyof ElementAttrs)[] = ['id', 'name', 'visible', 'lock']
28
+
29
+ class Element<T extends ElementAttrs = ElementAttrs>
30
+ extends Setter<T>
31
+ implements IElement<T>
32
+ {
33
+ _idType!: IDTypes
34
+ _nameType!: NameTypes
35
+
36
+ props!: Props
37
+ computed!: Computed<ComputedAttrs>
38
+
39
+ constructor(data?: Partial<ElementRawData>) {
40
+ super(elementChangeHandler.addChange)
41
+ this._init()
42
+
43
+ if (data && Object.keys(data).length) {
44
+ this.load(data)
45
+ } else {
46
+ this.create()
47
+ }
48
+
49
+ this.setupProps(data?.props)
50
+ }
51
+
52
+ _init(): void {
53
+ this._idType ??= IDTypes.ELEMENT
54
+ this._nameType ??= NameTypes.ELEMENT
55
+ this.data = {
56
+ id: '',
57
+ type: EntityTypes.UNDEFINED,
58
+ name: '',
59
+ visible: false,
60
+ lock: true
61
+ } as T
62
+ }
63
+
64
+ create(): void {
65
+ this.data = {
66
+ id: id(this._idType),
67
+ type: EntityTypes.ELEMENT,
68
+ name: name(this._nameType),
69
+ visible: true,
70
+ lock: false
71
+ } as T
72
+ }
73
+
74
+ load(data: ElementDataType): void {
75
+ if (!data) {
76
+ return
77
+ }
78
+
79
+ if (data.type !== EntityTypes.WORKSPACE) {
80
+ ElementProps.forEach((propName) => {
81
+ switch (propName) {
82
+ case 'id': {
83
+ const id = data.id
84
+ if (id) {
85
+ this.data.id = id
86
+ loadId(id, this._idType)
87
+ }
88
+ break
89
+ }
90
+ case 'name': {
91
+ const name = data.name
92
+ if (name) {
93
+ this.data.name = name
94
+ loadName(name, this._nameType)
95
+ }
96
+ break
97
+ }
98
+ default: {
99
+ const key = propName as keyof ElementAttrs
100
+ const newValue = data[key] as T[keyof T]
101
+ if (newValue !== undefined) {
102
+ this.data[propName as keyof T] = newValue
103
+ }
104
+ }
105
+ }
106
+ })
107
+ }
108
+ }
109
+
110
+ save(): ElementRawData {
111
+ const data = {} as ElementRawData
112
+ data.id = this.get('id')
113
+ data.type = this.get('type')
114
+ data.name = this.get('name')
115
+ data.visible = this.get('visible')
116
+ data.lock = this.get('lock')
117
+
118
+ if (this.data.type !== EntityTypes.WORKSPACE) {
119
+ data.props = this.props.save()
120
+ }
121
+
122
+ return data
123
+ }
124
+
125
+ setupProps(propsData?: Partial<PropsRawData>) {
126
+ const elementId = this.get('id') as string
127
+ if (this.data.type !== EntityTypes.WORKSPACE) {
128
+ if (propsData) {
129
+ this.props = new Props(elementId, propsData)
130
+ } else {
131
+ this.props = new Props(elementId)
132
+ }
133
+
134
+ this.computed = new Computed(elementId, this.props)
135
+ }
136
+ }
137
+
138
+ updateComputedData<K extends keyof ComputedAttrs>(
139
+ key: K,
140
+ data: ComputedAttrs[K]
141
+ ) {
142
+ if (!(key in this.data)) {
143
+ this.computed.set(key, data)
144
+
145
+ // Convert data type from ComputedAttrs to PropertyComponentInstanceDataTypes
146
+ type KEY = keyof PropertyComponentInstanceDataTypes
147
+ this.props.updateData(
148
+ key as KEY,
149
+ data as PropertyComponentInstanceDataTypes[KEY]
150
+ )
151
+ }
152
+ }
153
+
154
+ getAllComputedData() {
155
+ if (this.get('type') !== EntityTypes.WORKSPACE) {
156
+ return this.computed.save()
157
+ }
158
+
159
+ return {}
160
+ }
161
+
162
+ cleanup() {
163
+ this.props.cleanup()
164
+ }
165
+ }
166
+
167
+ export default Element
@@ -0,0 +1,28 @@
1
+ import type { FrameRawData } from '@asyra/utils'
2
+ import { EntityTypes, NameTypes } from '@asyra/utils'
3
+ import Group from './group'
4
+
5
+ type FrameDataType = Partial<FrameRawData>
6
+
7
+ class Frame extends Group {
8
+ constructor(data?: Partial<FrameRawData>) {
9
+ super(data)
10
+ }
11
+
12
+ _init(): void {
13
+ this._nameType = NameTypes.FRAME
14
+ super._init()
15
+ this.data.type = EntityTypes.FRAME
16
+ }
17
+
18
+ create(): void {
19
+ super.create()
20
+ this.data.type = EntityTypes.FRAME
21
+ }
22
+
23
+ load(data: FrameDataType): void {
24
+ super.load(data)
25
+ }
26
+ }
27
+
28
+ export default Frame
@@ -0,0 +1,70 @@
1
+ import type {
2
+ GroupRawData,
3
+ GroupAttrs,
4
+ ElementInstanceTypes,
5
+ IGroupElement,
6
+ ElementRawData
7
+ } from '@asyra/utils'
8
+ import { EntityTypes, NameTypes } from '@asyra/utils'
9
+ import Element from './element'
10
+
11
+ type GroupDataType = Partial<GroupRawData>
12
+
13
+ class Group<T extends GroupAttrs = GroupAttrs>
14
+ extends Element<T>
15
+ implements IGroupElement<T>
16
+ {
17
+ constructor(data?: Partial<ElementRawData>) {
18
+ super(data)
19
+ }
20
+
21
+ _init(): void {
22
+ this._nameType ??= NameTypes.GROUP
23
+ super._init()
24
+ this.data.type = EntityTypes.GROUP
25
+ this.data.children = []
26
+ }
27
+
28
+ load(data: GroupDataType): void {
29
+ super.load(data)
30
+ this.data.children = (data.children as string[]) || []
31
+ }
32
+
33
+ create(): void {
34
+ super.create()
35
+ this.data.type = EntityTypes.GROUP
36
+ }
37
+
38
+ save(): GroupRawData {
39
+ const data = super.save() as GroupRawData
40
+ data.children = this.data.children
41
+ return data
42
+ }
43
+
44
+ addElement(element: ElementInstanceTypes, index = -1) {
45
+ if (!element) {
46
+ return
47
+ }
48
+
49
+ const children = [...this.get('children')]
50
+ const idx = index ?? children.length
51
+ children.splice(idx, 0, element.get('id'))
52
+ this.set('children', children)
53
+ }
54
+
55
+ removeElement(element: ElementInstanceTypes, index: number) {
56
+ if (!element) {
57
+ return
58
+ }
59
+
60
+ const children = [...this.get('children')]
61
+ if (children.indexOf(element.get('id')) !== index) {
62
+ return
63
+ }
64
+
65
+ children.splice(index, 1)
66
+ this.set('children', children)
67
+ }
68
+ }
69
+
70
+ export default Group
@@ -0,0 +1,6 @@
1
+ import Rectangle from './rectangle'
2
+ import Group from './group'
3
+ import Frame from './frame'
4
+ import Workspace from './workspace'
5
+
6
+ export { Rectangle, Group, Frame, Workspace }