@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.
- package/.turbo/turbo-test$colon$ci.log +134 -0
- package/.turbo/turbo-test$colon$local.log +46 -0
- package/.turbo/turbo-test.log +53 -0
- package/README.md +1 -0
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +87 -0
- package/coverage/clover.xml +778 -0
- package/coverage/coverage-final.json +10478 -0
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +131 -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/components/computed.ts.html +313 -0
- package/coverage/src/components/element-change-handler.ts.html +145 -0
- package/coverage/src/components/element.ts.html +586 -0
- package/coverage/src/components/frame.ts.html +169 -0
- package/coverage/src/components/group.ts.html +295 -0
- package/coverage/src/components/index.html +236 -0
- package/coverage/src/components/index.ts.html +103 -0
- package/coverage/src/components/props.ts.html +355 -0
- package/coverage/src/components/rectangle.ts.html +169 -0
- package/coverage/src/components/workspace.ts.html +433 -0
- package/coverage/src/index.html +161 -0
- package/coverage/src/index.ts.html +103 -0
- package/coverage/src/sceneTree.ts.html +853 -0
- package/coverage/src/subscribes.ts.html +295 -0
- package/coverage/src/utils.ts.html +328 -0
- package/dist/__tests__/sceneTree.test.d.ts +2 -0
- package/dist/__tests__/sceneTree.test.d.ts.map +1 -0
- package/dist/__tests__/sceneTree.test.js +261 -0
- package/dist/__tests__/sceneTree.test.js.map +1 -0
- package/dist/components/computed.d.ts +13 -0
- package/dist/components/computed.d.ts.map +1 -0
- package/dist/components/computed.js +52 -0
- package/dist/components/computed.js.map +1 -0
- package/dist/components/element-change-handler.d.ts +10 -0
- package/dist/components/element-change-handler.d.ts.map +1 -0
- package/dist/components/element-change-handler.js +14 -0
- package/dist/components/element-change-handler.js.map +1 -0
- package/dist/components/element.d.ts +22 -0
- package/dist/components/element.d.ts.map +1 -0
- package/dist/components/element.js +114 -0
- package/dist/components/element.js.map +1 -0
- package/dist/components/frame.d.ts +11 -0
- package/dist/components/frame.d.ts.map +1 -0
- package/dist/components/frame.js +21 -0
- package/dist/components/frame.js.map +1 -0
- package/dist/components/group.d.ts +14 -0
- package/dist/components/group.d.ts.map +1 -0
- package/dist/components/group.js +48 -0
- package/dist/components/group.js.map +1 -0
- package/dist/components/index.d.ts +6 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/index.js +6 -0
- package/dist/components/index.js.map +1 -0
- package/dist/components/legacy-element.d.ts +22 -0
- package/dist/components/legacy-element.d.ts.map +1 -0
- package/dist/components/legacy-element.js +120 -0
- package/dist/components/legacy-element.js.map +1 -0
- package/dist/components/polygon.d.ts +11 -0
- package/dist/components/polygon.d.ts.map +1 -0
- package/dist/components/polygon.js +21 -0
- package/dist/components/polygon.js.map +1 -0
- package/dist/components/props.d.ts +16 -0
- package/dist/components/props.d.ts.map +1 -0
- package/dist/components/props.js +63 -0
- package/dist/components/props.js.map +1 -0
- package/dist/components/rectangle.d.ts +11 -0
- package/dist/components/rectangle.d.ts.map +1 -0
- package/dist/components/rectangle.js +21 -0
- package/dist/components/rectangle.js.map +1 -0
- package/dist/components/shape-entity.d.ts +16 -0
- package/dist/components/shape-entity.d.ts.map +1 -0
- package/dist/components/shape-entity.js +42 -0
- package/dist/components/shape-entity.js.map +1 -0
- package/dist/components/shape.d.ts +40 -0
- package/dist/components/shape.d.ts.map +1 -0
- package/dist/components/shape.js +105 -0
- package/dist/components/shape.js.map +1 -0
- package/dist/components/workspace.d.ts +14 -0
- package/dist/components/workspace.d.ts.map +1 -0
- package/dist/components/workspace.js +89 -0
- package/dist/components/workspace.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/sceneTree.d.ts +34 -0
- package/dist/sceneTree.d.ts.map +1 -0
- package/dist/sceneTree.js +185 -0
- package/dist/sceneTree.js.map +1 -0
- package/dist/subscribes.d.ts +2 -0
- package/dist/subscribes.d.ts.map +1 -0
- package/dist/subscribes.js +38 -0
- package/dist/subscribes.js.map +1 -0
- package/dist/utils.d.ts +19 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +61 -0
- package/dist/utils.js.map +1 -0
- package/package.json +30 -0
- package/src/__tests__/sceneTree.test.ts +339 -0
- package/src/components/computed.ts +76 -0
- package/src/components/element-change-handler.ts +20 -0
- package/src/components/element.ts +167 -0
- package/src/components/frame.ts +28 -0
- package/src/components/group.ts +70 -0
- package/src/components/index.ts +6 -0
- package/src/components/props.ts +90 -0
- package/src/components/rectangle.ts +28 -0
- package/src/components/workspace.ts +116 -0
- package/src/index.ts +6 -0
- package/src/sceneTree.ts +256 -0
- package/src/subscribes.ts +70 -0
- package/src/utils.ts +81 -0
- package/tsconfig.json +17 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { PropertyTypes } from '@asyra/utils'
|
|
2
|
+
import type {
|
|
3
|
+
PropertyComponentInstanceDataTypes,
|
|
4
|
+
PropsRawData
|
|
5
|
+
} from '@asyra/utils'
|
|
6
|
+
import { removeProperty } from '@asyra/reactive-events'
|
|
7
|
+
import propsManager from '@asyra/props-manager'
|
|
8
|
+
|
|
9
|
+
type PropsDataType = Partial<PropsRawData>
|
|
10
|
+
|
|
11
|
+
const PROP_NAMES: PropertyTypes[] = [
|
|
12
|
+
PropertyTypes.POSITION,
|
|
13
|
+
PropertyTypes.DIMENSION
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
type AliasKeys = 'x' | 'y' | 'width' | 'height'
|
|
17
|
+
|
|
18
|
+
const PROP_ALIAS: Record<AliasKeys, PropertyTypes> = {
|
|
19
|
+
x: PropertyTypes.POSITION,
|
|
20
|
+
y: PropertyTypes.POSITION,
|
|
21
|
+
width: PropertyTypes.DIMENSION,
|
|
22
|
+
height: PropertyTypes.DIMENSION
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
class Props {
|
|
26
|
+
elementId: string
|
|
27
|
+
position?: PropsRawData[PropertyTypes.POSITION]
|
|
28
|
+
dimension?: PropsRawData[PropertyTypes.DIMENSION]
|
|
29
|
+
|
|
30
|
+
constructor(elementId: string, data?: PropsDataType) {
|
|
31
|
+
this.elementId = elementId
|
|
32
|
+
|
|
33
|
+
if (data) {
|
|
34
|
+
this.load(data)
|
|
35
|
+
} else {
|
|
36
|
+
this.init()
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
init() {
|
|
41
|
+
const propertyComponents = PROP_NAMES.map((propName) =>
|
|
42
|
+
propsManager.createProperty({ type: propName })
|
|
43
|
+
)
|
|
44
|
+
const propIdsMap = propsManager.addProperty(propertyComponents)
|
|
45
|
+
propsManager.commitChanges()
|
|
46
|
+
if (!propIdsMap) {
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
PROP_NAMES.forEach((propName) => {
|
|
51
|
+
this[propName] = propIdsMap[propName]
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
load(data: PropsDataType = {}): void {
|
|
56
|
+
PROP_NAMES.forEach((propName) => {
|
|
57
|
+
this[propName] = data[propName]
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
save(): PropsRawData {
|
|
62
|
+
return PROP_NAMES.reduce((acc, propName) => {
|
|
63
|
+
const key = propName as keyof PropsRawData
|
|
64
|
+
acc[key] = this[key] as string
|
|
65
|
+
return acc
|
|
66
|
+
}, {} as PropsRawData)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
updateData<K extends keyof PropertyComponentInstanceDataTypes>(
|
|
70
|
+
key: K,
|
|
71
|
+
data: PropertyComponentInstanceDataTypes[K]
|
|
72
|
+
) {
|
|
73
|
+
const propName = (PROP_ALIAS[key] || key) as PropertyTypes
|
|
74
|
+
const propComponentId = this[propName]
|
|
75
|
+
if (!propComponentId) {
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
propsManager.updatePropsData(propComponentId, key, data)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
cleanup() {
|
|
83
|
+
const removedPropertyIds = PROP_NAMES.map((propName) => ({
|
|
84
|
+
id: this[propName]
|
|
85
|
+
}))
|
|
86
|
+
removeProperty(removedPropertyIds)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export default Props
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { RectangleRawData } from '@asyra/utils'
|
|
2
|
+
import { EntityTypes, NameTypes } from '@asyra/utils'
|
|
3
|
+
import Element from './element'
|
|
4
|
+
|
|
5
|
+
type RectangleDataType = Partial<RectangleRawData>
|
|
6
|
+
|
|
7
|
+
class Rectangle extends Element {
|
|
8
|
+
constructor(data?: Partial<RectangleRawData>) {
|
|
9
|
+
super(data)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
_init(): void {
|
|
13
|
+
this._nameType = NameTypes.RECTANGLE
|
|
14
|
+
super._init()
|
|
15
|
+
this.data.type = EntityTypes.RECTANGLE
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
create(): void {
|
|
19
|
+
super.create()
|
|
20
|
+
this.data.type = EntityTypes.RECTANGLE
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
load(data: RectangleDataType): void {
|
|
24
|
+
super.load(data)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export default Rectangle
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
WorkspaceRawData,
|
|
3
|
+
ElementInstanceTypes,
|
|
4
|
+
GroupInstanceTypes,
|
|
5
|
+
IElement
|
|
6
|
+
} from '@asyra/utils'
|
|
7
|
+
import { isGroupEntity, IDTypes, NameTypes, EntityTypes } from '@asyra/utils'
|
|
8
|
+
import Group from './group'
|
|
9
|
+
import sceneTree from '../sceneTree'
|
|
10
|
+
|
|
11
|
+
type WorkspaceDataType = Partial<WorkspaceRawData>
|
|
12
|
+
|
|
13
|
+
class Workspace extends Group {
|
|
14
|
+
constructor() {
|
|
15
|
+
super()
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
_init(): void {
|
|
19
|
+
this._idType = IDTypes.WORKSPACE
|
|
20
|
+
this._nameType = NameTypes.WORKSPACE
|
|
21
|
+
super._init()
|
|
22
|
+
this.data.type = EntityTypes.WORKSPACE
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
create(): void {
|
|
26
|
+
super.create()
|
|
27
|
+
this.data.type = EntityTypes.WORKSPACE
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
load(data: WorkspaceDataType): void {
|
|
31
|
+
super.load(data)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
get firstFrame(): ElementInstanceTypes | null {
|
|
35
|
+
let result = null
|
|
36
|
+
|
|
37
|
+
const children = this.get('children')
|
|
38
|
+
for (const childId of children) {
|
|
39
|
+
const child = sceneTree.getElementById(childId)
|
|
40
|
+
if (
|
|
41
|
+
child &&
|
|
42
|
+
isGroupEntity(child.get('type')) &&
|
|
43
|
+
(child as GroupInstanceTypes).get('children')
|
|
44
|
+
) {
|
|
45
|
+
result = child
|
|
46
|
+
break
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return result
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
addNewElement(
|
|
54
|
+
element: ElementInstanceTypes,
|
|
55
|
+
parent?: GroupInstanceTypes,
|
|
56
|
+
index = -1
|
|
57
|
+
) {
|
|
58
|
+
if (!element) {
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let avaliableParent = parent
|
|
63
|
+
if (!avaliableParent) {
|
|
64
|
+
const firstFrame = this.firstFrame
|
|
65
|
+
if (firstFrame) {
|
|
66
|
+
avaliableParent = this.firstFrame as GroupInstanceTypes
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (avaliableParent && avaliableParent.get('children')) {
|
|
71
|
+
// Add new element to Group type instance
|
|
72
|
+
avaliableParent.addElement(element, index)
|
|
73
|
+
} else {
|
|
74
|
+
// Add new element to Workspace
|
|
75
|
+
const originalChildrenList = [...this.get('children')]
|
|
76
|
+
const idx = index > -1 ? index : this.get('children').length
|
|
77
|
+
originalChildrenList.splice(idx, 0, element.get('id'))
|
|
78
|
+
this.set('children', originalChildrenList)
|
|
79
|
+
}
|
|
80
|
+
sceneTree.addToMap(element)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
removeElement(element: IElement, index: number, parent?: GroupInstanceTypes) {
|
|
84
|
+
if (!element) {
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
let avaliableParent = parent
|
|
89
|
+
if (!avaliableParent) {
|
|
90
|
+
const firstFrame = this.firstFrame
|
|
91
|
+
if (firstFrame) {
|
|
92
|
+
avaliableParent = this.firstFrame as GroupInstanceTypes
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const elementId = element.get('id')
|
|
97
|
+
if (avaliableParent && avaliableParent.get('children')) {
|
|
98
|
+
// Remove element from Group type instance
|
|
99
|
+
const idx = index ?? avaliableParent?.get('children').indexOf(elementId)
|
|
100
|
+
avaliableParent.removeElement(element, idx)
|
|
101
|
+
} else {
|
|
102
|
+
// Add new element to Workspace
|
|
103
|
+
const originalChildrenList = [...this.get('children')]
|
|
104
|
+
const idx = index ?? this.get('children').indexOf(elementId)
|
|
105
|
+
originalChildrenList.splice(idx, 1)
|
|
106
|
+
this.set('children', originalChildrenList)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
element.cleanup()
|
|
110
|
+
|
|
111
|
+
// Remove element from Workspace
|
|
112
|
+
sceneTree.removeFromMap(element)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export default Workspace
|
package/src/index.ts
ADDED
package/src/sceneTree.ts
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ComputedAttrs,
|
|
3
|
+
SceneTreeRawData,
|
|
4
|
+
WorkspaceRawData,
|
|
5
|
+
ElementRawData,
|
|
6
|
+
ElementInstanceTypes,
|
|
7
|
+
GroupInstanceTypes,
|
|
8
|
+
SceneTreeChange,
|
|
9
|
+
EVENT_OPTIONS,
|
|
10
|
+
CreateElementData
|
|
11
|
+
} from '@asyra/utils'
|
|
12
|
+
import { EntityTypes, OWNER, SCENE_TREE_ACTIONS } from '@asyra/utils'
|
|
13
|
+
import { EventTypes, updateTransaction } from '@asyra/reactive-events'
|
|
14
|
+
import propsManager from '@asyra/props-manager'
|
|
15
|
+
import { createElement, createWorkspace, stripNonRawFields } from './utils'
|
|
16
|
+
import type Workspace from './components/workspace'
|
|
17
|
+
|
|
18
|
+
type SceneTreeDataType = SceneTreeRawData
|
|
19
|
+
|
|
20
|
+
class SceneTree {
|
|
21
|
+
_elements: Map<string, ElementInstanceTypes> = new Map()
|
|
22
|
+
_deletedMap: Map<string, ElementInstanceTypes> = new Map()
|
|
23
|
+
workspace: string = ''
|
|
24
|
+
workspaceList: string[] = []
|
|
25
|
+
changes: SceneTreeChange[] = []
|
|
26
|
+
|
|
27
|
+
_init(): void {
|
|
28
|
+
if (!this.workspace && !this.workspaceList.length) {
|
|
29
|
+
const initWorkspace = createWorkspace() as ElementInstanceTypes
|
|
30
|
+
if (initWorkspace) {
|
|
31
|
+
this.addToMap(initWorkspace)
|
|
32
|
+
this.workspaceList = [initWorkspace.get('id')]
|
|
33
|
+
this.workspace = this.workspaceList[0]
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
init() {
|
|
39
|
+
this._init()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
load(data: SceneTreeDataType) {
|
|
43
|
+
if (!data) return
|
|
44
|
+
|
|
45
|
+
if (data.elements) {
|
|
46
|
+
for (const elementId in data.elements) {
|
|
47
|
+
const elementData = data.elements[elementId]
|
|
48
|
+
let element
|
|
49
|
+
if (elementData.type === EntityTypes.WORKSPACE) {
|
|
50
|
+
element = createWorkspace(elementData as WorkspaceRawData)
|
|
51
|
+
} else {
|
|
52
|
+
element = createElement(elementData)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
this.addToMap(element as ElementInstanceTypes)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (data.workspace) {
|
|
60
|
+
this.workspace = data.workspace
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (data.workspaceList) {
|
|
64
|
+
this.workspaceList = data.workspaceList
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
save() {
|
|
69
|
+
const data: SceneTreeRawData = {
|
|
70
|
+
workspace: this.workspace,
|
|
71
|
+
workspaceList: this.workspaceList,
|
|
72
|
+
elements: {}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
this._elements.forEach((element, id) => {
|
|
76
|
+
data.elements[id] = element.save()
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
return data
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
addChange(change: SceneTreeChange) {
|
|
83
|
+
this.changes.push(change)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
cleanChanges() {
|
|
87
|
+
this.changes = []
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
getAllElements() {
|
|
91
|
+
return this._elements
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
getElementById(elementId: string): ElementInstanceTypes | undefined {
|
|
95
|
+
return this._elements.get(elementId)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
addToMap(element: ElementInstanceTypes) {
|
|
99
|
+
const elId = element.get('id')
|
|
100
|
+
if (!element || !elId) {
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
this.removeFromDeleteMap(elId)
|
|
105
|
+
this._elements.set(elId, element)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
removeFromMap(element: ElementInstanceTypes) {
|
|
109
|
+
const elId = element.get('id')
|
|
110
|
+
if (!element || !elId) {
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
this.addToDeleteMap(element)
|
|
115
|
+
this._elements.delete(elId)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
getRestoreElementById(elementId: string): ElementInstanceTypes {
|
|
119
|
+
const restoredElement = this._deletedMap.get(
|
|
120
|
+
elementId
|
|
121
|
+
) as ElementInstanceTypes
|
|
122
|
+
this.addChangeForAddElement(restoredElement)
|
|
123
|
+
return restoredElement
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
addToDeleteMap(element: ElementInstanceTypes) {
|
|
127
|
+
this._deletedMap.set(element.get('id'), element)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
removeFromDeleteMap(elementId: string) {
|
|
131
|
+
this._deletedMap.delete(elementId)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
addChangeForAddElement(element: ElementInstanceTypes) {
|
|
135
|
+
this.addChange({
|
|
136
|
+
eventName: EventTypes.ADD_ELEMENT,
|
|
137
|
+
data: element.save(),
|
|
138
|
+
action: SCENE_TREE_ACTIONS.ADD_ELEMENT,
|
|
139
|
+
owner: OWNER.SCENE_TREE,
|
|
140
|
+
undoType: EventTypes.REMOVE_ELEMENT,
|
|
141
|
+
undoAction: EventTypes.REMOVE_ELEMENT
|
|
142
|
+
})
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
addChangeForRemoveElement(element: ElementInstanceTypes) {
|
|
146
|
+
this.addChange({
|
|
147
|
+
eventName: EventTypes.REMOVE_ELEMENT,
|
|
148
|
+
data: element.save(),
|
|
149
|
+
action: SCENE_TREE_ACTIONS.REMOVE_ELEMENT,
|
|
150
|
+
owner: OWNER.SCENE_TREE,
|
|
151
|
+
undoType: EventTypes.ADD_ELEMENT,
|
|
152
|
+
undoAction: EventTypes.ADD_ELEMENT
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
get currentWorkspace() {
|
|
157
|
+
return this.getElementById(this.workspace)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
createElement(
|
|
161
|
+
elementData: Partial<ElementRawData>
|
|
162
|
+
): ElementInstanceTypes | null {
|
|
163
|
+
if (elementData.type === EntityTypes.WORKSPACE) {
|
|
164
|
+
return null
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const newElement = createElement(elementData) as ElementInstanceTypes
|
|
168
|
+
this.addChangeForAddElement(newElement)
|
|
169
|
+
return newElement
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
addNewElement(
|
|
173
|
+
elementData: CreateElementData,
|
|
174
|
+
parent?: GroupInstanceTypes,
|
|
175
|
+
index = -1,
|
|
176
|
+
inUndoRedo = false
|
|
177
|
+
): string {
|
|
178
|
+
const workspace = this.currentWorkspace as Workspace
|
|
179
|
+
if (!workspace) {
|
|
180
|
+
return ''
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
let newElement: ElementInstanceTypes | null = null
|
|
184
|
+
|
|
185
|
+
const propOverrides = stripNonRawFields(elementData)
|
|
186
|
+
if (inUndoRedo) {
|
|
187
|
+
newElement = this.getRestoreElementById(elementData.id as string)
|
|
188
|
+
} else {
|
|
189
|
+
newElement = this.createElement(elementData)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (newElement) {
|
|
193
|
+
// Override props after finish creating new instance
|
|
194
|
+
Object.keys(propOverrides).forEach((propKey) => {
|
|
195
|
+
newElement.updateComputedData(
|
|
196
|
+
propKey as keyof ComputedAttrs,
|
|
197
|
+
propOverrides[propKey]
|
|
198
|
+
)
|
|
199
|
+
})
|
|
200
|
+
propsManager.commitChanges()
|
|
201
|
+
|
|
202
|
+
workspace.addNewElement(newElement, parent, index)
|
|
203
|
+
|
|
204
|
+
this.commitSceneTreeTransaction()
|
|
205
|
+
|
|
206
|
+
return newElement.get('id')
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return ''
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
removeElement(
|
|
213
|
+
data: Partial<ElementRawData>,
|
|
214
|
+
index: number,
|
|
215
|
+
parent?: GroupInstanceTypes
|
|
216
|
+
) {
|
|
217
|
+
const workspace = this.currentWorkspace as Workspace
|
|
218
|
+
if (!workspace) {
|
|
219
|
+
return
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const elementId = data.id as string
|
|
223
|
+
const element = this.getElementById(elementId)
|
|
224
|
+
if (!element) {
|
|
225
|
+
return
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
this.addChangeForRemoveElement(element)
|
|
229
|
+
workspace.removeElement(element, index, parent)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
updateComputedData<K extends keyof ComputedAttrs>(
|
|
233
|
+
elementId: string,
|
|
234
|
+
key: K,
|
|
235
|
+
data: ComputedAttrs[K]
|
|
236
|
+
) {
|
|
237
|
+
const element = this.getElementById(elementId)
|
|
238
|
+
if (!element) {
|
|
239
|
+
return
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
element.updateComputedData(key, data)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
commitSceneTreeTransaction(options?: EVENT_OPTIONS) {
|
|
246
|
+
this.changes.forEach((change) => {
|
|
247
|
+
updateTransaction(change.eventName, change, options)
|
|
248
|
+
})
|
|
249
|
+
this.cleanChanges()
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export { SceneTree }
|
|
254
|
+
|
|
255
|
+
const sceneTree = new SceneTree()
|
|
256
|
+
export default sceneTree
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
subscribeToRemoveElement,
|
|
3
|
+
subscribeToChangeComputedData,
|
|
4
|
+
subscribeToUpdateComputedData,
|
|
5
|
+
subscribeToSceneTreeInit,
|
|
6
|
+
subscribeToSceneTreeLoadData,
|
|
7
|
+
subscribeToAddElement,
|
|
8
|
+
subscribeToUpdateUndoRedoStatus
|
|
9
|
+
} from '@asyra/reactive-events'
|
|
10
|
+
import { CreateElementData, UNDO, type ComputedAttrs } from '@asyra/utils'
|
|
11
|
+
import sceneTree from './sceneTree'
|
|
12
|
+
|
|
13
|
+
export const initSceneTreeSubscribes = () => {
|
|
14
|
+
let inUndoRedo = false
|
|
15
|
+
subscribeToUpdateUndoRedoStatus(({ payload }) => {
|
|
16
|
+
inUndoRedo = payload.status !== UNDO.NONE
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
subscribeToSceneTreeInit(() => {
|
|
20
|
+
sceneTree.init()
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
subscribeToSceneTreeLoadData(({ payload }) => {
|
|
24
|
+
sceneTree.load(payload.data)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
subscribeToAddElement(({ payload }) => {
|
|
28
|
+
const { data, parent, index } = payload
|
|
29
|
+
sceneTree.addNewElement(
|
|
30
|
+
data as CreateElementData,
|
|
31
|
+
parent,
|
|
32
|
+
index,
|
|
33
|
+
inUndoRedo
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
sceneTree.commitSceneTreeTransaction()
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
subscribeToRemoveElement(({ payload }) => {
|
|
40
|
+
const { data, parent, index } = payload
|
|
41
|
+
sceneTree.removeElement(data, index, parent)
|
|
42
|
+
|
|
43
|
+
sceneTree.commitSceneTreeTransaction()
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
subscribeToChangeComputedData(async ({ payload, options }) => {
|
|
47
|
+
const { elementIds, key, data } = payload
|
|
48
|
+
|
|
49
|
+
elementIds.forEach((elementId) => {
|
|
50
|
+
type KEY = keyof ComputedAttrs
|
|
51
|
+
sceneTree.updateComputedData(
|
|
52
|
+
elementId,
|
|
53
|
+
key as KEY,
|
|
54
|
+
data as ComputedAttrs[KEY]
|
|
55
|
+
)
|
|
56
|
+
})
|
|
57
|
+
sceneTree.commitSceneTreeTransaction(options)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
subscribeToUpdateComputedData(({ payload }) => {
|
|
61
|
+
const { id, key, after } = payload
|
|
62
|
+
|
|
63
|
+
sceneTree.updateComputedData(
|
|
64
|
+
id,
|
|
65
|
+
key as keyof ComputedAttrs,
|
|
66
|
+
after as ComputedAttrs[keyof ComputedAttrs]
|
|
67
|
+
)
|
|
68
|
+
sceneTree.commitSceneTreeTransaction()
|
|
69
|
+
})
|
|
70
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { ComputedAttrs, ElementRawData, EntityTypes } from '@asyra/utils'
|
|
2
|
+
import Frame from './components/frame'
|
|
3
|
+
import Group from './components/group'
|
|
4
|
+
import Rectangle from './components/rectangle'
|
|
5
|
+
import Workspace from './components/workspace'
|
|
6
|
+
|
|
7
|
+
const entityClassMap = {
|
|
8
|
+
[EntityTypes.UNDEFINED]: undefined,
|
|
9
|
+
[EntityTypes.FRAME]: Frame,
|
|
10
|
+
[EntityTypes.GROUP]: Group,
|
|
11
|
+
[EntityTypes.RECTANGLE]: Rectangle,
|
|
12
|
+
[EntityTypes.OVAL]: Rectangle // FIXME: Change this after finish OVAL component
|
|
13
|
+
} as const
|
|
14
|
+
|
|
15
|
+
const initWorkspaceData = {
|
|
16
|
+
type: EntityTypes.WORKSPACE
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const createElement = (elementData: Partial<ElementRawData>) => {
|
|
20
|
+
if (
|
|
21
|
+
elementData.type === EntityTypes.WORKSPACE ||
|
|
22
|
+
elementData.type === EntityTypes.ELEMENT ||
|
|
23
|
+
elementData.type === EntityTypes.UNDEFINED
|
|
24
|
+
) {
|
|
25
|
+
return null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const elementType = elementData.type ?? EntityTypes.UNDEFINED
|
|
29
|
+
const EntityClass = entityClassMap[elementType]
|
|
30
|
+
if (!EntityClass) {
|
|
31
|
+
throw new Error('Ivalid entity type.')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Already know what type we need, and it should be insert by the component itself
|
|
35
|
+
delete elementData.type
|
|
36
|
+
|
|
37
|
+
return new EntityClass(elementData)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const createWorkspace = (workspaceData = initWorkspaceData) => {
|
|
41
|
+
if (workspaceData.type !== EntityTypes.WORKSPACE) {
|
|
42
|
+
return null
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const newWorkspace = new Workspace()
|
|
46
|
+
newWorkspace.load(workspaceData)
|
|
47
|
+
return newWorkspace
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
|
51
|
+
type UnknownObject = Record<string, any>
|
|
52
|
+
|
|
53
|
+
const DefaultRawKeys: (keyof ElementRawData)[] = ['id', 'type', 'name', 'props']
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Removes non-raw fields from an element object and returns the stripped fields.
|
|
57
|
+
*
|
|
58
|
+
* @param elementData - The original element object which may contain extra fields.
|
|
59
|
+
* @param rawKeys - Keys that should be kept in the original object (defaults to ElementRawData keys).
|
|
60
|
+
* @returns An object containing the stripped (non-raw) fields.
|
|
61
|
+
*/
|
|
62
|
+
export const stripNonRawFields = (
|
|
63
|
+
elementData: UnknownObject,
|
|
64
|
+
rawKeys: (keyof ElementRawData)[] = DefaultRawKeys
|
|
65
|
+
): Record<string, ComputedAttrs[keyof ComputedAttrs]> => {
|
|
66
|
+
const stripped = {} as UnknownObject
|
|
67
|
+
|
|
68
|
+
for (const key in elementData) {
|
|
69
|
+
if (!rawKeys.includes(key as keyof ElementRawData)) {
|
|
70
|
+
stripped[key] = elementData[key]
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Remove all non-raw keys from the original object
|
|
75
|
+
Object.keys(stripped).forEach((key) => {
|
|
76
|
+
/* eslint-disable-next-line @typescript-eslint/no-dynamic-delete */
|
|
77
|
+
delete elementData[key]
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
return stripped
|
|
81
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"declarationMap": true,
|
|
7
|
+
"sourceMap": true,
|
|
8
|
+
"outDir": "./dist",
|
|
9
|
+
"strict": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"moduleResolution": "Node",
|
|
13
|
+
"resolveJsonModule": true
|
|
14
|
+
},
|
|
15
|
+
"include": ["src"],
|
|
16
|
+
"exclude": ["node_modules", "dist"]
|
|
17
|
+
}
|