@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,120 @@
1
+ import { OWNER, PROPS_ACTIONS } from '@asyra/utils';
2
+ import { EventTypes, updateTransaction } from '@asyra/reactive-events';
3
+ import { createProperty } from './utils';
4
+ class PropsManager {
5
+ constructor() {
6
+ this._components = new Map();
7
+ this._deletedMap = new Map();
8
+ this.changes = [];
9
+ }
10
+ load(data) {
11
+ Object.keys(data).forEach((componentId) => {
12
+ const newProperty = createProperty(data[componentId]);
13
+ this.addToMap(newProperty);
14
+ });
15
+ }
16
+ save() {
17
+ const data = {};
18
+ this._components.forEach((component, componentId) => {
19
+ data[componentId] = component.save();
20
+ });
21
+ return data;
22
+ }
23
+ addChange(change) {
24
+ this.changes.push(change);
25
+ }
26
+ cleanChanges() {
27
+ this.changes = [];
28
+ }
29
+ getComponentById(componentId) {
30
+ return this._components.get(componentId);
31
+ }
32
+ addToMap(component) {
33
+ const comId = component.get('id');
34
+ if (!component || !comId) {
35
+ return;
36
+ }
37
+ this.removeFromDeletedMap(comId);
38
+ this._components.set(comId, component);
39
+ }
40
+ removeFromMap(componentId) {
41
+ const component = this.getComponentById(componentId);
42
+ if (!component) {
43
+ return;
44
+ }
45
+ this.addToDeletedMap(component);
46
+ this._components.delete(componentId);
47
+ }
48
+ addToDeletedMap(component) {
49
+ this._deletedMap.set(component.get('id'), component);
50
+ }
51
+ removeFromDeletedMap(componentId) {
52
+ this._deletedMap.delete(componentId);
53
+ }
54
+ getRestoreComponentById(componentId) {
55
+ return this._deletedMap.get(componentId);
56
+ }
57
+ addChangeForAddProperty(property) {
58
+ this.addChange({
59
+ eventName: EventTypes.ADD_PROPERTY,
60
+ data: [property.save()],
61
+ action: PROPS_ACTIONS.ADD_PROPERTY,
62
+ owner: OWNER.PROPS,
63
+ undoType: EventTypes.REMOVE_PROPERTY,
64
+ undoAction: EventTypes.REMOVE_PROPERTY
65
+ });
66
+ }
67
+ addChangeForRemoveProperty(property) {
68
+ this.addChange({
69
+ eventName: EventTypes.REMOVE_PROPERTY,
70
+ data: [property.save()],
71
+ action: PROPS_ACTIONS.REMOVE_PROPERTY,
72
+ owner: OWNER.PROPS,
73
+ undoType: EventTypes.ADD_PROPERTY,
74
+ undoAction: EventTypes.ADD_PROPERTY
75
+ });
76
+ }
77
+ createProperty(propData) {
78
+ if (!propData.type) {
79
+ throw new Error('Type is required!');
80
+ }
81
+ const newProperty = createProperty({
82
+ ...propData,
83
+ type: propData.type
84
+ });
85
+ this.addChangeForAddProperty(newProperty);
86
+ return newProperty;
87
+ }
88
+ addProperty(propComponents) {
89
+ return propComponents.reduce((acc, com) => {
90
+ if (!com) {
91
+ return acc;
92
+ }
93
+ this.addToMap(com);
94
+ acc[com.get('type')] = com.get('id');
95
+ return acc;
96
+ }, {});
97
+ }
98
+ removeProperty(propComponentIds) {
99
+ propComponentIds.forEach((propComponentId) => {
100
+ this.removeFromMap(propComponentId);
101
+ });
102
+ }
103
+ updatePropsData(componentId, key, data) {
104
+ const component = this.getComponentById(componentId);
105
+ if (!component) {
106
+ return;
107
+ }
108
+ component.set(key, data);
109
+ }
110
+ commitChanges() {
111
+ this.changes.forEach((change) => {
112
+ updateTransaction(change.eventName, change);
113
+ });
114
+ this.cleanChanges();
115
+ }
116
+ }
117
+ const propsManager = new PropsManager();
118
+ export default propsManager;
119
+ export { PropsManager };
120
+ //# sourceMappingURL=props-manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"props-manager.js","sourceRoot":"","sources":["../src/props-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAiB,aAAa,EAAE,MAAM,cAAc,CAAA;AAQlE,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AACtE,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAExC,MAAM,YAAY;IAAlB;QACE,gBAAW,GAAgD,IAAI,GAAG,EAAE,CAAA;QACpE,gBAAW,GAAgD,IAAI,GAAG,EAAE,CAAA;QACpE,YAAO,GAAkB,EAAE,CAAA;IA+I7B,CAAC;IA7IC,IAAI,CAAC,IAA2B;QAC9B,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;YACxC,MAAM,WAAW,GAAG,cAAc,CAChC,IAAI,CAAC,WAAW,CAAC,CACgB,CAAA;YACnC,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAA;QAC5B,CAAC,CAAC,CAAA;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ot":"","sources":["../src/subscribes.ts"],"names":[],"mappings":"AASA,eAAO,MAAM,mBAAmB,YA4C/B,CAAA"}
@@ -0,0 +1,35 @@
1
+ import { subscribeToUpdateUndoRedoStatus, subscribeToAddProperty, subscribeToRemoveProperty, updateTransaction } from '@asyra/reactive-events';
2
+ import { UNDO } from '@asyra/utils';
3
+ import propsManager from './props-manager';
4
+ export const initPropXSubscribes = () => {
5
+ let inUndoRedo = false;
6
+ subscribeToUpdateUndoRedoStatus(({ payload }) => {
7
+ inUndoRedo = payload.status !== UNDO.NONE;
8
+ });
9
+ subscribeToAddProperty(({ payload }) => {
10
+ const propComponents = payload.data.map((propData) => {
11
+ let newProperty;
12
+ if (inUndoRedo) {
13
+ newProperty = propsManager.getRestoreComponentById(propData.id);
14
+ }
15
+ if (!newProperty) {
16
+ newProperty = propsManager.createProperty(propData);
17
+ }
18
+ return newProperty;
19
+ });
20
+ propsManager.addProperty(propComponents);
21
+ propsManager.changes.forEach((change) => {
22
+ updateTransaction(change.eventName, change);
23
+ });
24
+ propsManager.cleanChanges();
25
+ });
26
+ subscribeToRemoveProperty(({ payload }) => {
27
+ const removedPropertyIds = payload.data.map((propertyData) => propertyData.id);
28
+ propsManager.removeProperty(removedPropertyIds);
29
+ propsManager.changes.forEach((change) => {
30
+ updateTransaction(change.eventName, change);
31
+ });
32
+ propsManager.cleanChanges();
33
+ });
34
+ };
35
+ //# sourceMappingURL=subscribes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subscribes.js","sourceRoot":"","sources":["../src/subscribes.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,+BAA+B,EAC/B,sBAAsB,EACtB,yBAAyB,EACzB,iBAAiB,EAClB,MAAM,wBAAwB,CAAA;AAC/B,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAA;AACnC,OAAO,YAAY,MAAM,iBAAiB,CAAA;AAE1C,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAAG,EAAE;IACtC,IAAI,UAAU,GAA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CAAC,CAAC,CAAA;QAEF,YAAY,CAAC,YAAY,EAAE,CAAA;IAC7B,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA"}
@@ -0,0 +1,3 @@
1
+ import { PropertyComponentInstanceTypes, PropertyComponentRawData } from '@asyra/utils';
2
+ export declare const createProperty: (data: Partial<PropertyComponentRawData>) => PropertyComponentInstanceTypes | undefined;
3
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,8BAA8B,EAM9B,wBAAwB,EAEzB,MAAM,cAAc,CAAA;AAgBrB,eAAO,MAAM,cAAc,GAAI,MAAM,OAAO,CAAC,wBAAwB,CAAC,+CAoBrE,CAAA"}
package/dist/utils.js ADDED
@@ -0,0 +1,31 @@
1
+ import { PropertyTypes, DefaultPositionData, DefaultDimensionData, id, loadId, IDTypes, isNil } from '@asyra/utils';
2
+ import { PositionComponent, DimensionComponent } from './components';
3
+ const PropClassMap = {
4
+ [PropertyTypes.POSITION]: PositionComponent,
5
+ [PropertyTypes.DIMENSION]: DimensionComponent
6
+ };
7
+ const DefaultDataMap = {
8
+ [PropertyTypes.POSITION]: DefaultPositionData,
9
+ [PropertyTypes.DIMENSION]: DefaultDimensionData
10
+ };
11
+ export const createProperty = (data) => {
12
+ const type = data.type;
13
+ const PropClass = PropClassMap[type];
14
+ if (!PropClass) {
15
+ return;
16
+ }
17
+ let comId = data.id;
18
+ if (isNil(comId)) {
19
+ comId = id(IDTypes.PROPS);
20
+ }
21
+ else {
22
+ loadId(data.id, IDTypes.PROPS);
23
+ }
24
+ const defaultData = DefaultDataMap[type];
25
+ return new PropClass({
26
+ id: comId,
27
+ ...defaultData,
28
+ ...data
29
+ });
30
+ };
31
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EAEb,mBAAmB,EACnB,oBAAoB,EACpB,EAAE,EACF,MAAM,EACN,OAAO,EAEP,KAAK,EACN,MAAM,cAAc,CAAA;AACrB,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAEnB,MAAM,cAAc,CAAA;AAErB,MAAM,YAAY,GAAiD;IACjE,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,iBAAiB;IAC3C,CAAC,aAAa,CAAC,SAAS,CAAC,EAAE,kBAAkB;CAC9C,CAAA;AACD,MAAM,cAAc,GAAkC;IACpD,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,mBAAmB;IAC7C,CAAC,aAAa,CAAC,SAAS,CAAC,EAAE,oBAAoB;CAChD,CAAA;AAED,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,IAAuC,EAAE,EAAE;IACxE,MAAM,IAAI,GAAG,IAAI,CAAC,IAAqB,CAAA;IACvC,MAAM,SAAS,GAAG,YAAY,CAAC,IAAI,CAAC,CAAA;IACpC,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,OAAM;IACR,CAAC;IAED,IAAI,KAAK,GAAG,IAAI,CAAC,EAAE,CAAA;IACnB,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;QACjB,KAAK,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC3B,CAAC;SAAM,CAAC;QACN,MAAM,CAAC,IAAI,CAAC,EAAY,EAAE,OAAO,CAAC,KAAK,CAAC,CAAA;IAC1C,CAAC;IACD,MAAM,WAAW,GAAG,cAAc,CAAC,IAAI,CAAC,CAAA;IAExC,OAAO,IAAI,SAAS,CAAC;QACnB,EAAE,EAAE,KAAK;QACT,GAAG,WAAW;QACd,GAAG,IAAI;KACR,CAAmC,CAAA;AACtC,CAAC,CAAA"}
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@asyra/props-manager",
3
+ "version": "0.2.0",
4
+ "description": "A structured property management library that provides API-based control over prop components, separate from the scene tree.",
5
+ "author": "Asa Tsai",
6
+ "license": "MIT",
7
+ "packageManager": "yarn@4.3.1",
8
+ "keywords": [],
9
+ "main": "dist/index.js",
10
+ "types": "dist/index.d.ts",
11
+ "scripts": {
12
+ "dev": "tsc --watch --preserveWatchOutput",
13
+ "build:props-manager": "tsc",
14
+ "clean": "rm -rf dist",
15
+ "has:test": "find . -type f \\( -name \"*.test.ts\" -o -name \"*.test.tsx\" \\) | grep -q .",
16
+ "test": "yarn has:test && vitest || echo 'No test files found. Skipping test.'",
17
+ "test:local": "yarn has:test && vitest run || echo 'No test files found. Skipping test:local.'",
18
+ "test:ci": "yarn has:test && vitest run --coverage --reporter=default --reporter=junit || echo 'No test files found. Skipping test:ci.'"
19
+ },
20
+ "devDependencies": {
21
+ "@vitest/coverage-v8": "^2.1.8",
22
+ "typescript": "^5.7.2",
23
+ "vitest": "^2.1.8"
24
+ },
25
+ "dependencies": {
26
+ "@asyra/reactive-events": "workspace:*",
27
+ "@asyra/utils": "workspace:*"
28
+ }
29
+ }
@@ -0,0 +1,362 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest'
2
+ import * as ReactiveEventsModule from '@asyra/reactive-events'
3
+ import {
4
+ PROPS_ACTIONS,
5
+ PropertyComponentInstanceTypes,
6
+ PropertyComponentInstanceDataTypes,
7
+ PropertyTypes,
8
+ Unit,
9
+ PropsChange
10
+ } from '@asyra/utils'
11
+ import { PropsManager } from '../props-manager'
12
+ import { createProperty } from '../utils'
13
+
14
+ vi.mock('@asyra/reactive-events', async (importOriginal) => {
15
+ const actual = await importOriginal<typeof import('@asyra/reactive-events')>()
16
+
17
+ return {
18
+ ...actual,
19
+ updateTransaction: vi.fn()
20
+ }
21
+ })
22
+
23
+ describe('PropsManager', () => {
24
+ let propsManager: PropsManager
25
+
26
+ beforeEach(() => {
27
+ vi.clearAllMocks()
28
+
29
+ propsManager = new PropsManager()
30
+ })
31
+
32
+ // Test load and save
33
+ it('should load data correctly', () => {
34
+ const dataToLoad = {
35
+ 'pp-1': {
36
+ id: 'pp-1',
37
+ type: PropertyTypes.POSITION,
38
+ x: 0,
39
+ y: 0,
40
+ xUnit: Unit.PX,
41
+ yUnit: Unit.PX
42
+ },
43
+ 'pp-2': {
44
+ id: 'pp-2',
45
+ type: PropertyTypes.DIMENSION,
46
+ width: 100,
47
+ height: 100,
48
+ widthUnit: Unit.PX,
49
+ heightUnit: Unit.PX
50
+ }
51
+ }
52
+
53
+ propsManager.load(dataToLoad)
54
+
55
+ expect(propsManager.getComponentById('pp-1')?.get('id')).toBe('pp-1')
56
+ expect(propsManager.getComponentById('pp-2')?.get('id')).toBe('pp-2')
57
+ })
58
+
59
+ it('should save data correctly', () => {
60
+ const dataToLoad = {
61
+ 'pp-1': {
62
+ id: 'pp-1',
63
+ type: PropertyTypes.POSITION,
64
+ x: 0,
65
+ y: 0,
66
+ xUnit: Unit.PX,
67
+ yUnit: Unit.PX
68
+ }
69
+ }
70
+ propsManager.load(dataToLoad)
71
+
72
+ const savedData = propsManager.save()
73
+
74
+ expect(savedData['pp-1']).toEqual(dataToLoad['pp-1'])
75
+ })
76
+
77
+ // Test change tracking
78
+ it('should add a change to the changes array', () => {
79
+ const change = {
80
+ eventName: ReactiveEventsModule.EventTypes.ADD_PROPERTY
81
+ } as unknown as PropsChange
82
+
83
+ propsManager.addChange(change)
84
+
85
+ expect(propsManager.changes).toEqual([change])
86
+ })
87
+
88
+ it('should clean all changes', () => {
89
+ propsManager.addChange({} as unknown as PropsChange)
90
+
91
+ propsManager.cleanChanges()
92
+
93
+ expect(propsManager.changes).toEqual([])
94
+ })
95
+
96
+ it('should add a change for adding a property', () => {
97
+ const p1Data = {
98
+ id: 'pp-1',
99
+ type: PropertyTypes.POSITION,
100
+ x: 0,
101
+ y: 0,
102
+ xUnit: Unit.PX,
103
+ yUnit: Unit.PX
104
+ }
105
+ const p1Component = createProperty(p1Data) as PropertyComponentInstanceTypes
106
+
107
+ propsManager.addChangeForAddProperty(p1Component)
108
+
109
+ expect(propsManager.changes.length).toBe(1)
110
+ expect(propsManager.changes[0].action).toBe(PROPS_ACTIONS.ADD_PROPERTY)
111
+ })
112
+
113
+ it('should add a change for removing a property', () => {
114
+ const p1Data = {
115
+ id: 'pp-1',
116
+ type: PropertyTypes.POSITION,
117
+ x: 0,
118
+ y: 0,
119
+ xUnit: Unit.PX,
120
+ yUnit: Unit.PX
121
+ }
122
+ const p1Component = createProperty(p1Data) as PropertyComponentInstanceTypes
123
+
124
+ propsManager.addChangeForRemoveProperty(p1Component)
125
+
126
+ expect(propsManager.changes.length).toBe(1)
127
+ expect(propsManager.changes[0].action).toBe(PROPS_ACTIONS.REMOVE_PROPERTY)
128
+ })
129
+
130
+ // Test component management
131
+ it('should get a component by ID', () => {
132
+ const p1Data = {
133
+ id: 'pp-1',
134
+ type: PropertyTypes.POSITION,
135
+ x: 0,
136
+ y: 0,
137
+ xUnit: Unit.PX,
138
+ yUnit: Unit.PX
139
+ }
140
+ const p1Component = createProperty(p1Data) as PropertyComponentInstanceTypes
141
+
142
+ propsManager.addToMap(p1Component)
143
+
144
+ expect(propsManager.getComponentById('pp-1')).toBe(p1Component)
145
+ })
146
+
147
+ it('should add a component to the map', () => {
148
+ const p1Data = {
149
+ id: 'pp-1',
150
+ type: PropertyTypes.POSITION,
151
+ x: 0,
152
+ y: 0,
153
+ xUnit: Unit.PX,
154
+ yUnit: Unit.PX
155
+ }
156
+ const p1Component = createProperty(p1Data) as PropertyComponentInstanceTypes
157
+
158
+ propsManager.addToMap(p1Component)
159
+
160
+ expect(propsManager._components.has('pp-1')).toBe(true)
161
+ })
162
+
163
+ it('should remove a component from the map', () => {
164
+ const p1Data = {
165
+ id: 'pp-1',
166
+ type: PropertyTypes.POSITION,
167
+ x: 0,
168
+ y: 0,
169
+ xUnit: Unit.PX,
170
+ yUnit: Unit.PX
171
+ }
172
+ const p1Component = createProperty(p1Data) as PropertyComponentInstanceTypes
173
+ propsManager.addToMap(p1Component)
174
+
175
+ propsManager.removeFromMap('pp-1')
176
+
177
+ expect(propsManager._components.has('pp-1')).toBe(false)
178
+ })
179
+
180
+ // Test deleted map functionality
181
+ it('should add a component to the deleted map', () => {
182
+ const p1Data = {
183
+ id: 'pp-1',
184
+ type: PropertyTypes.POSITION,
185
+ x: 0,
186
+ y: 0,
187
+ xUnit: Unit.PX,
188
+ yUnit: Unit.PX
189
+ }
190
+ const p1Component = createProperty(p1Data) as PropertyComponentInstanceTypes
191
+
192
+ propsManager.addToDeletedMap(p1Component)
193
+
194
+ expect(propsManager._deletedMap.has('pp-1')).toBe(true)
195
+ })
196
+
197
+ it('should remove a component from the deleted map', () => {
198
+ const p1Data = {
199
+ id: 'pp-1',
200
+ type: PropertyTypes.POSITION,
201
+ x: 0,
202
+ y: 0,
203
+ xUnit: Unit.PX,
204
+ yUnit: Unit.PX
205
+ }
206
+ const p1Component = createProperty(p1Data) as PropertyComponentInstanceTypes
207
+ propsManager.addToDeletedMap(p1Component)
208
+
209
+ propsManager.removeFromDeletedMap('pp-1')
210
+
211
+ expect(propsManager._deletedMap.has('pp-1')).toBe(false)
212
+ })
213
+
214
+ it('should get a restored component by ID', () => {
215
+ const p1Data = {
216
+ id: 'pp-1',
217
+ type: PropertyTypes.POSITION,
218
+ x: 0,
219
+ y: 0,
220
+ xUnit: Unit.PX,
221
+ yUnit: Unit.PX
222
+ }
223
+ const p1Component = createProperty(p1Data) as PropertyComponentInstanceTypes
224
+
225
+ propsManager.addToDeletedMap(p1Component)
226
+ expect(propsManager.getRestoreComponentById('pp-1')).toBe(p1Component)
227
+ })
228
+
229
+ // Test createProperty
230
+ it('should create a property and add a change', () => {
231
+ const p1Data = {
232
+ id: 'pp-1',
233
+ type: PropertyTypes.POSITION,
234
+ x: 0,
235
+ y: 0,
236
+ xUnit: Unit.PX,
237
+ yUnit: Unit.PX
238
+ }
239
+ const newProp = propsManager.createProperty(p1Data)
240
+
241
+ expect(newProp?.get('id')).toBe('pp-1')
242
+ expect(propsManager.changes.length).toBe(1)
243
+ expect(propsManager.changes[0].action).toBe(PROPS_ACTIONS.ADD_PROPERTY)
244
+ })
245
+
246
+ it('should throw error if type is not provided for createProperty', () => {
247
+ expect(() => propsManager.createProperty({})).toThrow('Type is required!')
248
+ })
249
+
250
+ // Test addProperty
251
+ it('should add multiple properties and return their IDs mapped by type', () => {
252
+ const p1Data = {
253
+ id: 'pp-1',
254
+ type: PropertyTypes.POSITION,
255
+ x: 0,
256
+ y: 0,
257
+ xUnit: Unit.PX,
258
+ yUnit: Unit.PX
259
+ }
260
+ const p1Component = createProperty(p1Data) as PropertyComponentInstanceTypes
261
+ const p2Data = {
262
+ id: 'pp-2',
263
+ type: PropertyTypes.DIMENSION,
264
+ width: 100,
265
+ height: 100,
266
+ widthUnit: Unit.PX,
267
+ heightUnit: Unit.PX
268
+ }
269
+ const p2Component = createProperty(p2Data) as PropertyComponentInstanceTypes
270
+
271
+ const result = propsManager.addProperty([p1Component, p2Component])
272
+
273
+ expect(propsManager._components.has('pp-1')).toBe(true)
274
+ expect(propsManager._components.has('pp-2')).toBe(true)
275
+ expect(result).toEqual({
276
+ [PropertyTypes.POSITION]: 'pp-1',
277
+ [PropertyTypes.DIMENSION]: 'pp-2'
278
+ })
279
+ })
280
+
281
+ // Test removeProperty
282
+ it('should remove multiple properties', () => {
283
+ const p1Data = {
284
+ id: 'pp-1',
285
+ type: PropertyTypes.POSITION,
286
+ x: 0,
287
+ y: 0,
288
+ xUnit: Unit.PX,
289
+ yUnit: Unit.PX
290
+ }
291
+ const p1Component = createProperty(p1Data) as PropertyComponentInstanceTypes
292
+ propsManager.addToMap(p1Component)
293
+ const p2Data = {
294
+ id: 'pp-2',
295
+ type: PropertyTypes.POSITION,
296
+ x: 0,
297
+ y: 0,
298
+ xUnit: Unit.PX,
299
+ yUnit: Unit.PX
300
+ }
301
+ const p2Component = createProperty(p2Data) as PropertyComponentInstanceTypes
302
+ propsManager.addToMap(p2Component)
303
+
304
+ propsManager.removeProperty(['pp-1', 'pp-2'])
305
+
306
+ expect(propsManager._components.has('pp-1')).toBe(false)
307
+ expect(propsManager._components.has('pp-2')).toBe(false)
308
+ expect(propsManager._deletedMap.has('pp-1')).toBe(true)
309
+ expect(propsManager._deletedMap.has('pp-2')).toBe(true)
310
+ })
311
+
312
+ // Test updatePropsData
313
+ it('should update props data on a component', () => {
314
+ const positionData = {
315
+ id: 'pp-1',
316
+ type: PropertyTypes.POSITION,
317
+ x: 0,
318
+ y: 0,
319
+ xUnit: Unit.PX,
320
+ yUnit: Unit.PX
321
+ }
322
+ const positionComponent = createProperty(
323
+ positionData
324
+ ) as PropertyComponentInstanceTypes
325
+ vi.spyOn(positionComponent, 'set')
326
+
327
+ propsManager.addToMap(positionComponent)
328
+ // Type assertion needed because updatePropsData uses union type for keys
329
+ // 'x' is a valid key for PositionAttrs, which is part of PropertyComponentInstanceDataTypes
330
+ propsManager.updatePropsData(
331
+ 'pp-1',
332
+ 'x' as unknown as keyof PropertyComponentInstanceDataTypes,
333
+ 100 as unknown as PropertyComponentInstanceDataTypes[keyof PropertyComponentInstanceDataTypes]
334
+ )
335
+ expect(positionComponent.set).toHaveBeenCalledWith('x', 100)
336
+ })
337
+
338
+ // Test commitChanges
339
+ it('should commit changes and clean the changes array', () => {
340
+ const change1 = {
341
+ eventName: ReactiveEventsModule.EventTypes.ADD_PROPERTY
342
+ } as unknown as PropsChange
343
+ const change2 = {
344
+ eventName: ReactiveEventsModule.EventTypes.REMOVE_PROPERTY
345
+ } as unknown as PropsChange
346
+ propsManager.addChange(change1)
347
+ propsManager.addChange(change2)
348
+
349
+ propsManager.commitChanges()
350
+
351
+ expect(ReactiveEventsModule.updateTransaction).toHaveBeenCalledTimes(2)
352
+ expect(ReactiveEventsModule.updateTransaction).toHaveBeenCalledWith(
353
+ change1.eventName,
354
+ change1
355
+ )
356
+ expect(ReactiveEventsModule.updateTransaction).toHaveBeenCalledWith(
357
+ change2.eventName,
358
+ change2
359
+ )
360
+ expect(propsManager.changes).toEqual([])
361
+ })
362
+ })
@@ -0,0 +1,59 @@
1
+ import type {
2
+ IProperty,
3
+ PositionAttrs,
4
+ PropertyComponentInstanceDataTypes,
5
+ PropertyComponentRawData
6
+ } from '@asyra/utils'
7
+ import { Setter, Unit, isNil } from '@asyra/utils'
8
+ import PropsChangeHandler from './props-change-handler'
9
+
10
+ const propsChangeHandler = new PropsChangeHandler()
11
+
12
+ abstract class BaseComponent<
13
+ T extends PropertyComponentInstanceDataTypes = PositionAttrs
14
+ >
15
+ extends Setter<T>
16
+ implements IProperty
17
+ {
18
+ propNames!: string[]
19
+
20
+ constructor() {
21
+ super(propsChangeHandler.addChange)
22
+ }
23
+
24
+ _init(data: Partial<T>) {
25
+ Object.keys(data).forEach((dataKey) => {
26
+ const key = dataKey as keyof T
27
+ if (this.isValidKey(key) && !isNil(data[key])) {
28
+ this.data[key] = data[key] as T[Extract<keyof T, string>]
29
+ }
30
+ })
31
+ }
32
+
33
+ update(data: Partial<T>) {
34
+ Object.keys(data).forEach((dataKey) => {
35
+ const key = dataKey as keyof T
36
+ if (this.isValidKey(key) && !isNil(data[key])) {
37
+ this.set(key, data[key] as T[Extract<keyof T, string>])
38
+ }
39
+ })
40
+ }
41
+
42
+ abstract getValue(): Record<string, number>
43
+ abstract getUnit(): Record<string, Unit>
44
+
45
+ protected isValidKey(key: keyof T) {
46
+ return key in this.data
47
+ }
48
+
49
+ abstract load(data: PropertyComponentRawData): void
50
+
51
+ save(): PropertyComponentRawData {
52
+ return {
53
+ id: this.get('id'),
54
+ type: this.get('type')
55
+ } as PropertyComponentRawData
56
+ }
57
+ }
58
+
59
+ export default BaseComponent