@asyra/props-manager 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 (77) hide show
  1. package/.turbo/turbo-test$colon$ci.log +130 -0
  2. package/.turbo/turbo-test$colon$local.log +46 -0
  3. package/.turbo/turbo-test.log +0 -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 +368 -0
  8. package/coverage/coverage-final.json +5904 -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/base.ts.html +262 -0
  16. package/coverage/src/components/dimension.ts.html +253 -0
  17. package/coverage/src/components/index.html +176 -0
  18. package/coverage/src/components/index.ts.html +106 -0
  19. package/coverage/src/components/position.ts.html +250 -0
  20. package/coverage/src/components/props-change-handler.ts.html +145 -0
  21. package/coverage/src/index.html +161 -0
  22. package/coverage/src/index.ts.html +106 -0
  23. package/coverage/src/props-manager.ts.html +574 -0
  24. package/coverage/src/subscribes.ts.html +247 -0
  25. package/coverage/src/utils.ts.html +226 -0
  26. package/dist/__tests__/props-manager.test.d.ts +2 -0
  27. package/dist/__tests__/props-manager.test.d.ts.map +1 -0
  28. package/dist/__tests__/props-manager.test.js +290 -0
  29. package/dist/__tests__/props-manager.test.js.map +1 -0
  30. package/dist/components/base.d.ts +15 -0
  31. package/dist/components/base.d.ts.map +1 -0
  32. package/dist/components/base.js +35 -0
  33. package/dist/components/base.js.map +1 -0
  34. package/dist/components/dimension.d.ts +12 -0
  35. package/dist/components/dimension.d.ts.map +1 -0
  36. package/dist/components/dimension.js +43 -0
  37. package/dist/components/dimension.js.map +1 -0
  38. package/dist/components/index.d.ts +5 -0
  39. package/dist/components/index.d.ts.map +1 -0
  40. package/dist/components/index.js +4 -0
  41. package/dist/components/index.js.map +1 -0
  42. package/dist/components/position.d.ts +12 -0
  43. package/dist/components/position.d.ts.map +1 -0
  44. package/dist/components/position.js +42 -0
  45. package/dist/components/position.js.map +1 -0
  46. package/dist/components/props-change-handler.d.ts +10 -0
  47. package/dist/components/props-change-handler.d.ts.map +1 -0
  48. package/dist/components/props-change-handler.js +14 -0
  49. package/dist/components/props-change-handler.js.map +1 -0
  50. package/dist/index.d.ts +4 -0
  51. package/dist/index.d.ts.map +1 -0
  52. package/dist/index.js +6 -0
  53. package/dist/index.js.map +1 -0
  54. package/dist/props-manager.d.ts +28 -0
  55. package/dist/props-manager.d.ts.map +1 -0
  56. package/dist/props-manager.js +120 -0
  57. package/dist/props-manager.js.map +1 -0
  58. package/dist/subscribes.d.ts +2 -0
  59. package/dist/subscribes.d.ts.map +1 -0
  60. package/dist/subscribes.js +35 -0
  61. package/dist/subscribes.js.map +1 -0
  62. package/dist/utils.d.ts +3 -0
  63. package/dist/utils.d.ts.map +1 -0
  64. package/dist/utils.js +31 -0
  65. package/dist/utils.js.map +1 -0
  66. package/package.json +29 -0
  67. package/src/__tests__/props-manager.test.ts +362 -0
  68. package/src/components/base.ts +59 -0
  69. package/src/components/dimension.ts +56 -0
  70. package/src/components/index.ts +7 -0
  71. package/src/components/position.ts +55 -0
  72. package/src/components/props-change-handler.ts +20 -0
  73. package/src/index.ts +7 -0
  74. package/src/props-manager.ts +163 -0
  75. package/src/subscribes.ts +54 -0
  76. package/src/utils.ts +47 -0
  77. package/tsconfig.json +17 -0
@@ -0,0 +1,56 @@
1
+ import {
2
+ Unit,
3
+ PropertyTypes,
4
+ DefaultDimensionData,
5
+ DimensionAttrs,
6
+ DimensionComponentRawData
7
+ } from '@asyra/utils'
8
+ import BaseComponent from './base'
9
+
10
+ class DimensionComponent extends BaseComponent<DimensionAttrs> {
11
+ data: DimensionAttrs = {
12
+ id: '',
13
+ type: PropertyTypes.DIMENSION,
14
+ ...DefaultDimensionData
15
+ }
16
+
17
+ constructor(data: Partial<DimensionAttrs>) {
18
+ super()
19
+
20
+ this.data.type = PropertyTypes.DIMENSION
21
+ this._init(data)
22
+ }
23
+
24
+ save(): DimensionComponentRawData {
25
+ return {
26
+ ...super.save(),
27
+ width: this.get('width'),
28
+ height: this.get('height'),
29
+ widthUnit: this.get('widthUnit'),
30
+ heightUnit: this.get('heightUnit')
31
+ }
32
+ }
33
+
34
+ load(data: DimensionComponentRawData): void {
35
+ this.data.width = data.width
36
+ this.data.height = data.height
37
+ this.data.widthUnit = data.widthUnit
38
+ this.data.heightUnit = data.heightUnit
39
+ }
40
+
41
+ getValue(): Record<string, number> {
42
+ return {
43
+ width: this.data.width,
44
+ height: this.data.height
45
+ }
46
+ }
47
+
48
+ getUnit(): Record<string, Unit> {
49
+ return {
50
+ widthUnit: this.data.widthUnit,
51
+ heightUnit: this.data.heightUnit
52
+ }
53
+ }
54
+ }
55
+
56
+ export default DimensionComponent
@@ -0,0 +1,7 @@
1
+ import PositionComponent from './position'
2
+ import DimensionComponent from './dimension'
3
+
4
+ export { PositionComponent, DimensionComponent }
5
+ export type PropertyComponentType =
6
+ | typeof PositionComponent
7
+ | typeof DimensionComponent
@@ -0,0 +1,55 @@
1
+ import {
2
+ DefaultPositionData,
3
+ PositionAttrs,
4
+ PositionComponentRawData,
5
+ PropertyTypes,
6
+ Unit
7
+ } from '@asyra/utils'
8
+ import BaseComponent from './base'
9
+
10
+ class PositionComponent extends BaseComponent<PositionAttrs> {
11
+ data: PositionAttrs = {
12
+ id: '',
13
+ type: PropertyTypes.POSITION,
14
+ ...DefaultPositionData
15
+ }
16
+
17
+ constructor(data: Partial<PositionAttrs>) {
18
+ super()
19
+
20
+ this._init(data)
21
+ }
22
+
23
+ save(): PositionComponentRawData {
24
+ return {
25
+ ...super.save(),
26
+ x: this.get('x'),
27
+ y: this.get('y'),
28
+ xUnit: this.get('xUnit'),
29
+ yUnit: this.get('yUnit')
30
+ }
31
+ }
32
+
33
+ load(data: PositionComponentRawData): void {
34
+ this.data.x = data.x
35
+ this.data.y = data.y
36
+ this.data.xUnit = data.xUnit
37
+ this.data.yUnit = data.yUnit
38
+ }
39
+
40
+ getValue(): Record<string, number> {
41
+ return {
42
+ x: this.data.x,
43
+ y: this.data.y
44
+ }
45
+ }
46
+
47
+ getUnit(): Record<string, Unit> {
48
+ return {
49
+ xUnit: this.data.xUnit,
50
+ yUnit: this.data.yUnit
51
+ }
52
+ }
53
+ }
54
+
55
+ export default PositionComponent
@@ -0,0 +1,20 @@
1
+ import type { ChangeHandler, DataTypes } from '@asyra/utils'
2
+ import { OWNER, PROPS_ACTIONS } from '@asyra/utils'
3
+ import { EventTypes } from '@asyra/reactive-events'
4
+ import propsManager from '../props-manager'
5
+
6
+ export default class PropsChangeHandler implements ChangeHandler {
7
+ addChange(data: {
8
+ id: string
9
+ key: string
10
+ before: DataTypes
11
+ after: DataTypes
12
+ }): void {
13
+ propsManager.addChange({
14
+ action: PROPS_ACTIONS.UPDATE_PROPERTY,
15
+ owner: OWNER.SCENE_TREE,
16
+ eventName: EventTypes.UPDATE_PROPERTY,
17
+ ...data
18
+ })
19
+ }
20
+ }
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { initPropXSubscribes } from './subscribes'
2
+ import propsManager, { PropsManager } from './props-manager'
3
+
4
+ initPropXSubscribes()
5
+
6
+ export default propsManager
7
+ export { PropsManager }
@@ -0,0 +1,163 @@
1
+ import { OWNER, PropertyTypes, PROPS_ACTIONS } from '@asyra/utils'
2
+ import type {
3
+ PropertyComponentInstanceDataTypes,
4
+ PropertyComponentInstanceTypes,
5
+ PropertyComponentRawData,
6
+ PropsChange,
7
+ PropsComponentRawData
8
+ } from '@asyra/utils'
9
+ import { EventTypes, updateTransaction } from '@asyra/reactive-events'
10
+ import { createProperty } from './utils'
11
+
12
+ class PropsManager {
13
+ _components: Map<string, PropertyComponentInstanceTypes> = new Map()
14
+ _deletedMap: Map<string, PropertyComponentInstanceTypes> = new Map()
15
+ changes: PropsChange[] = []
16
+
17
+ load(data: PropsComponentRawData) {
18
+ Object.keys(data).forEach((componentId) => {
19
+ const newProperty = createProperty(
20
+ data[componentId]
21
+ ) as PropertyComponentInstanceTypes
22
+ this.addToMap(newProperty)
23
+ })
24
+ }
25
+
26
+ save(): PropsComponentRawData {
27
+ const data = {} as PropsComponentRawData
28
+ this._components.forEach((component, componentId) => {
29
+ data[componentId] = component.save()
30
+ })
31
+
32
+ return data
33
+ }
34
+
35
+ addChange(change: PropsChange) {
36
+ this.changes.push(change)
37
+ }
38
+
39
+ cleanChanges() {
40
+ this.changes = []
41
+ }
42
+
43
+ getComponentById(
44
+ componentId: string
45
+ ): PropertyComponentInstanceTypes | undefined {
46
+ return this._components.get(componentId)
47
+ }
48
+
49
+ addToMap(component: PropertyComponentInstanceTypes) {
50
+ const comId = component.get('id')
51
+ if (!component || !comId) {
52
+ return
53
+ }
54
+
55
+ this.removeFromDeletedMap(comId)
56
+ this._components.set(comId, component)
57
+ }
58
+
59
+ removeFromMap(componentId: string) {
60
+ const component = this.getComponentById(componentId)
61
+ if (!component) {
62
+ return
63
+ }
64
+
65
+ this.addToDeletedMap(component)
66
+ this._components.delete(componentId)
67
+ }
68
+
69
+ addToDeletedMap(component: PropertyComponentInstanceTypes) {
70
+ this._deletedMap.set(component.get('id'), component)
71
+ }
72
+
73
+ removeFromDeletedMap(componentId: string) {
74
+ this._deletedMap.delete(componentId)
75
+ }
76
+
77
+ getRestoreComponentById(componentId: string) {
78
+ return this._deletedMap.get(componentId)
79
+ }
80
+
81
+ addChangeForAddProperty(property: PropertyComponentInstanceTypes) {
82
+ this.addChange({
83
+ eventName: EventTypes.ADD_PROPERTY,
84
+ data: [property.save()],
85
+ action: PROPS_ACTIONS.ADD_PROPERTY,
86
+ owner: OWNER.PROPS,
87
+ undoType: EventTypes.REMOVE_PROPERTY,
88
+ undoAction: EventTypes.REMOVE_PROPERTY
89
+ })
90
+ }
91
+
92
+ addChangeForRemoveProperty(property: PropertyComponentInstanceTypes) {
93
+ this.addChange({
94
+ eventName: EventTypes.REMOVE_PROPERTY,
95
+ data: [property.save()],
96
+ action: PROPS_ACTIONS.REMOVE_PROPERTY,
97
+ owner: OWNER.PROPS,
98
+ undoType: EventTypes.ADD_PROPERTY,
99
+ undoAction: EventTypes.ADD_PROPERTY
100
+ })
101
+ }
102
+
103
+ createProperty(propData: Partial<PropertyComponentRawData>) {
104
+ if (!propData.type) {
105
+ throw new Error('Type is required!')
106
+ }
107
+
108
+ const newProperty = createProperty({
109
+ ...propData,
110
+ type: propData.type as PropertyTypes
111
+ }) as PropertyComponentInstanceTypes
112
+ this.addChangeForAddProperty(newProperty)
113
+ return newProperty
114
+ }
115
+
116
+ addProperty(
117
+ propComponents: PropertyComponentInstanceTypes[]
118
+ ): Record<PropertyTypes, string> {
119
+ return propComponents.reduce(
120
+ (acc, com) => {
121
+ if (!com) {
122
+ return acc
123
+ }
124
+
125
+ this.addToMap(com)
126
+ acc[com.get('type')] = com.get('id')
127
+ return acc
128
+ },
129
+ {} as Record<PropertyTypes, string>
130
+ )
131
+ }
132
+
133
+ removeProperty(propComponentIds: string[]) {
134
+ propComponentIds.forEach((propComponentId) => {
135
+ this.removeFromMap(propComponentId)
136
+ })
137
+ }
138
+
139
+ updatePropsData<K extends keyof PropertyComponentInstanceDataTypes>(
140
+ componentId: string,
141
+ key: K,
142
+ data: PropertyComponentInstanceDataTypes[K]
143
+ ) {
144
+ const component = this.getComponentById(componentId)
145
+ if (!component) {
146
+ return
147
+ }
148
+
149
+ component.set(key, data)
150
+ }
151
+
152
+ commitChanges() {
153
+ this.changes.forEach((change) => {
154
+ updateTransaction(change.eventName, change)
155
+ })
156
+ this.cleanChanges()
157
+ }
158
+ }
159
+
160
+ const propsManager = new PropsManager()
161
+
162
+ export default propsManager
163
+ export { PropsManager }
@@ -0,0 +1,54 @@
1
+ import {
2
+ subscribeToUpdateUndoRedoStatus,
3
+ subscribeToAddProperty,
4
+ subscribeToRemoveProperty,
5
+ updateTransaction
6
+ } from '@asyra/reactive-events'
7
+ import { UNDO } from '@asyra/utils'
8
+ import propsManager from './props-manager'
9
+
10
+ export const initPropXSubscribes = () => {
11
+ let inUndoRedo = false
12
+ subscribeToUpdateUndoRedoStatus(({ payload }) => {
13
+ inUndoRedo = payload.status !== UNDO.NONE
14
+ })
15
+
16
+ subscribeToAddProperty(({ payload }) => {
17
+ const propComponents = payload.data.map((propData) => {
18
+ let newProperty
19
+ if (inUndoRedo) {
20
+ newProperty = propsManager.getRestoreComponentById(
21
+ propData.id as string
22
+ )
23
+ }
24
+
25
+ if (!newProperty) {
26
+ newProperty = propsManager.createProperty(propData)
27
+ }
28
+
29
+ return newProperty
30
+ })
31
+
32
+ propsManager.addProperty(propComponents)
33
+
34
+ propsManager.changes.forEach((change) => {
35
+ updateTransaction(change.eventName, change)
36
+ })
37
+
38
+ propsManager.cleanChanges()
39
+ })
40
+
41
+ subscribeToRemoveProperty(({ payload }) => {
42
+ const removedPropertyIds = payload.data.map(
43
+ (propertyData) => propertyData.id as string
44
+ )
45
+
46
+ propsManager.removeProperty(removedPropertyIds)
47
+
48
+ propsManager.changes.forEach((change) => {
49
+ updateTransaction(change.eventName, change)
50
+ })
51
+
52
+ propsManager.cleanChanges()
53
+ })
54
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,47 @@
1
+ import {
2
+ PropertyTypes,
3
+ PropertyComponentInstanceTypes,
4
+ DefaultPositionData,
5
+ DefaultDimensionData,
6
+ id,
7
+ loadId,
8
+ IDTypes,
9
+ PropertyComponentRawData,
10
+ isNil
11
+ } from '@asyra/utils'
12
+ import {
13
+ PositionComponent,
14
+ DimensionComponent,
15
+ PropertyComponentType
16
+ } from './components'
17
+
18
+ const PropClassMap: Record<PropertyTypes, PropertyComponentType> = {
19
+ [PropertyTypes.POSITION]: PositionComponent,
20
+ [PropertyTypes.DIMENSION]: DimensionComponent
21
+ }
22
+ const DefaultDataMap: Record<PropertyTypes, object> = {
23
+ [PropertyTypes.POSITION]: DefaultPositionData,
24
+ [PropertyTypes.DIMENSION]: DefaultDimensionData
25
+ }
26
+
27
+ export const createProperty = (data: Partial<PropertyComponentRawData>) => {
28
+ const type = data.type as PropertyTypes
29
+ const PropClass = PropClassMap[type]
30
+ if (!PropClass) {
31
+ return
32
+ }
33
+
34
+ let comId = data.id
35
+ if (isNil(comId)) {
36
+ comId = id(IDTypes.PROPS)
37
+ } else {
38
+ loadId(data.id as string, IDTypes.PROPS)
39
+ }
40
+ const defaultData = DefaultDataMap[type]
41
+
42
+ return new PropClass({
43
+ id: comId,
44
+ ...defaultData,
45
+ ...data
46
+ }) as PropertyComponentInstanceTypes
47
+ }
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
+ }