@leafer/decorator 1.0.0-alpha.7 → 1.0.0-bate

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/decorator",
3
- "version": "1.0.0-alpha.7",
3
+ "version": "1.0.0-bate",
4
4
  "description": "@leafer/decorator",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -19,10 +19,10 @@
19
19
  "leaferjs"
20
20
  ],
21
21
  "dependencies": {
22
- "@leafer/platform": "1.0.0-alpha.7",
23
- "@leafer/debug": "1.0.0-alpha.7"
22
+ "@leafer/platform": "1.0.0-bate",
23
+ "@leafer/debug": "1.0.0-bate"
24
24
  },
25
25
  "devDependencies": {
26
- "@leafer/interface": "1.0.0-alpha.7"
26
+ "@leafer/interface": "1.0.0-bate"
27
27
  }
28
28
  }
package/src/class.ts CHANGED
@@ -1,12 +1,14 @@
1
1
  import { IObject } from '@leafer/interface'
2
2
  import { EventCreator, UICreator } from '@leafer/platform'
3
3
 
4
- export function registerUI() { return (target: IObject) => registerUI__(target) }
5
- export function registerUI__(target: IObject) {
6
- UICreator.register(target)
4
+ export function registerUI() {
5
+ return (target: IObject) => {
6
+ UICreator.register(target)
7
+ }
7
8
  }
8
9
 
9
- export function registerEvent() { return (target: IObject) => registerEvent__(target) }
10
- export function registerEvent__(target: IObject) {
11
- EventCreator.register(target)
10
+ export function registerUIEvent() {
11
+ return (target: IObject) => {
12
+ EventCreator.register(target)
13
+ }
12
14
  }
package/src/data.ts CHANGED
@@ -1,165 +1,195 @@
1
1
  import { ILeafData, ILeaf, IObject, __Value } from '@leafer/interface'
2
2
  import { defineKey, getDescriptor } from './object'
3
+ import { Debug } from '@leafer/debug'
4
+
3
5
 
4
6
  // name
5
7
 
6
- export function aliasType(name: string) { return (target: ILeaf, key: string) => aliasType__(target, key, name) }
7
- export function aliasType__(target: ILeaf, key: string, name: string) {
8
- defineKey(target, key, {
9
- get() { return this.__get(name) },
10
- set(value: __Value) {
11
- this.root ? this.__set(name, value) : this.__[name] = value
12
- }
13
- })
8
+ export function aliasType(name: string) {
9
+ return (target: ILeaf, key: string) => {
10
+ defineKey(target, key, {
11
+ get() { return this.__getAttr(name) },
12
+ set(value: __Value) {
13
+ this.__setAttr(name, value)
14
+ }
15
+ })
16
+ }
14
17
  }
15
18
 
16
- export function dataType(defaultValue?: __Value) { return (target: ILeaf, key: string) => dataType__(target, key, defaultValue) }
17
- export function dataType__(target: ILeaf, key: string, defaultValue?: __Value) {
18
- defineKey(target, key, {
19
- get() { return this.__get(key) },
20
- set(value: __Value) {
21
- this.root ? this.__set(key, value) : this.__[key] = value
22
- }
23
- })
19
+ export function defineLeafAttr(target: ILeaf, key: string, defaultValue?: __Value, mergeDescriptor?: IObject & ThisType<ILeaf>): void {
20
+ const defaultDescriptor: IObject & ThisType<ILeaf> = {
21
+ get() { return this.__getAttr(key) },
22
+ set(value: __Value) { this.__setAttr(key, value) },
23
+ configurable: true,
24
+ enumerable: true
25
+ }
26
+ defineKey(target, key, Object.assign(defaultDescriptor, mergeDescriptor || {}))
24
27
  defineDataProcessor(target, key, defaultValue)
25
28
  }
26
29
 
27
-
28
- export function positionType(defaultValue?: __Value) { return (target: ILeaf, key: string) => positionType__(target, key, defaultValue) }
29
- export function positionType__(target: ILeaf, key: string, defaultValue?: __Value) {
30
- defineKey(target, key, {
31
- get() { return this.__get(key) },
32
- set(value: __Value) {
33
- this.root ? this.__set(key, value) : this.__[key] = value
34
- this.__layout.positionChanged || this.__layout.positionChange()
35
- }
36
- })
37
- defineDataProcessor(target, key, defaultValue)
30
+ export function dataType(defaultValue?: __Value) {
31
+ return (target: ILeaf, key: string) => {
32
+ defineLeafAttr(target, key, defaultValue)
33
+ }
38
34
  }
39
35
 
36
+ export function positionType(defaultValue?: __Value) {
37
+ return (target: ILeaf, key: string) => {
38
+ defineLeafAttr(target, key, defaultValue, {
39
+ set(value: __Value) {
40
+ this.__setAttr(key, value)
41
+ this.__layout.positionChanged || this.__layout.positionChange()
42
+ }
43
+ })
44
+ }
45
+ }
40
46
 
41
- export function scaleType(defaultValue?: __Value) { return (target: ILeaf, key: string) => scaleType__(target, key, defaultValue) }
42
- export function scaleType__(target: ILeaf, key: string, defaultValue?: __Value) {
43
- defineKey(target, key, {
44
- get() { return this.__get(key) },
45
- set(value: __Value) {
46
- this.root ? this.__set(key, value) : this.__[key] = value
47
- this.__layout.scaleChanged || this.__layout.scaleChange()
48
- }
49
- })
50
- defineDataProcessor(target, key, defaultValue)
47
+ export function scaleType(defaultValue?: __Value) {
48
+ return (target: ILeaf, key: string) => {
49
+ defineLeafAttr(target, key, defaultValue, {
50
+ set(value: __Value) {
51
+ this.__setAttr(key, value)
52
+ this.__layout.scaleChanged || this.__layout.scaleChange()
53
+ }
54
+ })
55
+ }
51
56
  }
52
57
 
53
- export function rotationType(defaultValue?: __Value) { return (target: ILeaf, key: string) => rotationType__(target, key, defaultValue) }
54
- export function rotationType__(target: ILeaf, key: string, defaultValue?: __Value) {
55
- defineKey(target, key, {
56
- get() { return this.__get(key) },
57
- set(value: __Value) {
58
- this.root ? this.__set(key, value) : this.__[key] = value
59
- this.__layout.rotationChanged || this.__layout.rotationChange()
60
58
 
61
- }
62
- })
63
- defineDataProcessor(target, key, defaultValue)
59
+ export function rotationType(defaultValue?: __Value) {
60
+ return (target: ILeaf, key: string) => {
61
+ defineLeafAttr(target, key, defaultValue, {
62
+ set(value: __Value) {
63
+ this.__setAttr(key, value)
64
+ this.__layout.rotationChanged || this.__layout.rotationChange()
65
+
66
+ }
67
+ })
68
+ }
64
69
  }
65
70
 
66
- export function boundsType(defaultValue?: __Value) { return (target: ILeaf, key: string) => boundsType__(target, key, defaultValue) }
67
- export function boundsType__(target: ILeaf, key: string, defaultValue?: __Value) {
68
- defineKey(target, key, {
69
- get() { return this.__get(key) },
70
- set(value: __Value) {
71
- this.root ? this.__set(key, value) : this.__[key] = value
72
- this.__layout.boxBoundsChanged || this.__layout.boxBoundsChange()
73
- }
74
- })
75
- defineDataProcessor(target, key, defaultValue)
71
+ export function boundsType(defaultValue?: __Value) {
72
+ return (target: ILeaf, key: string) => {
73
+ defineLeafAttr(target, key, defaultValue, {
74
+ set(value: __Value) {
75
+ this.__setAttr(key, value)
76
+ this.__layout.boxChanged || this.__layout.boxChange()
77
+ }
78
+ })
79
+ }
76
80
  }
77
81
 
78
82
  export const pathType = boundsType
79
- export const pathType__ = boundsType__
80
83
 
81
84
 
82
- export function affectEventBoundsType(defaultValue?: __Value) { return (target: ILeaf, key: string) => affectEventBoundsType__(target, key, defaultValue) }
83
- export function affectEventBoundsType__(target: ILeaf, key: string, defaultValue?: __Value) {
84
- defineKey(target, key, {
85
- get() { return this.__get(key) },
86
- set(value: __Value) {
87
- this.root ? this.__set(key, value) : this.__[key] = value
88
- this.__layout.eventBoundsChanged || this.__layout.eventBoundsChange()
89
- }
90
- })
91
- defineDataProcessor(target, key, defaultValue)
85
+ export function affectStrokeBoundsType(defaultValue?: __Value) {
86
+ return (target: ILeaf, key: string) => {
87
+ defineLeafAttr(target, key, defaultValue, {
88
+ set(value: __Value) {
89
+ this.__setAttr(key, value)
90
+ this.__layout.strokeChanged || this.__layout.strokeChange()
91
+ }
92
+ })
93
+ }
92
94
  }
93
95
 
94
- export function affectRenderBoundsType(defaultValue?: __Value) { return (target: ILeaf, key: string) => affectRenderBoundsType__(target, key, defaultValue) }
95
- export function affectRenderBoundsType__(target: ILeaf, key: string, defaultValue?: __Value) {
96
- defineKey(target, key, {
97
- get() { return this.__get(key) },
98
- set(value: __Value) {
99
- this.root ? this.__set(key, value) : this.__[key] = value
100
- this.__layout.renderBoundsChanged || this.__layout.renderBoundsChange()
101
- }
102
- })
103
- defineDataProcessor(target, key, defaultValue)
96
+ export const strokeType = affectStrokeBoundsType
97
+
98
+ export function affectRenderBoundsType(defaultValue?: __Value) {
99
+ return (target: ILeaf, key: string) => {
100
+ defineLeafAttr(target, key, defaultValue, {
101
+ set(value: __Value) {
102
+ this.__setAttr(key, value)
103
+ this.__layout.renderChanged || this.__layout.renderChange()
104
+ }
105
+ })
106
+ }
104
107
  }
105
108
 
106
- export function surfaceType(defaultValue?: __Value) { return (target: ILeaf, key: string) => surfaceType__(target, key, defaultValue) }
107
- export function surfaceType__(target: ILeaf, key: string, defaultValue?: __Value) {
108
- defineKey(target, key, {
109
- get() { return this.__get(key) },
110
- set(value: __Value) {
111
- this.root ? this.__set(key, value) : this.__[key] = value
112
- this.__layout.surfaceChanged || this.__layout.surfaceChange()
113
- }
114
- })
115
- defineDataProcessor(target, key, defaultValue)
109
+ export function surfaceType(defaultValue?: __Value) {
110
+ return (target: ILeaf, key: string) => {
111
+ defineLeafAttr(target, key, defaultValue, {
112
+ set(value: __Value) {
113
+ this.__setAttr(key, value)
114
+ this.__layout.surfaceChanged || this.__layout.surfaceChange()
115
+ }
116
+ })
117
+ }
116
118
  }
117
119
 
118
- export function opacityType(defaultValue?: __Value) { return (target: ILeaf, key: string) => opacityType__(target, key, defaultValue) }
119
- export function opacityType__(target: ILeaf, key: string, defaultValue?: __Value) {
120
- defineKey(target, key, {
121
- get() { return this.__get(key) },
122
- set(value: __Value) {
123
- this.root ? this.__set(key, value) : this.__[key] = value
124
- this.__layout.opacityChanged || this.__layout.opacityChange()
125
- }
126
- })
127
- defineDataProcessor(target, key, defaultValue)
120
+ export function opacityType(defaultValue?: __Value) {
121
+ return (target: ILeaf, key: string) => {
122
+ defineLeafAttr(target, key, defaultValue, {
123
+ set(value: __Value) {
124
+ this.__setAttr(key, value)
125
+ this.__layout.opacityChanged || this.__layout.opacityChange()
126
+ }
127
+ })
128
+ }
128
129
  }
129
130
 
130
- export function sortType(defaultValue?: __Value) { return (target: ILeaf, key: string) => { sortType__(target, key, defaultValue) } }
131
- export function sortType__(target: ILeaf, key: string, defaultValue?: __Value) {
132
- defineKey(target, key, {
133
- get() { return this.__get(key) },
134
- set(value: __Value) {
135
- this.root ? this.__set(key, value) : this.__[key] = value
136
- this.__layout.surfaceChanged || this.__layout.surfaceChange()
131
+ export function sortType(defaultValue?: __Value) {
132
+ return (target: ILeaf, key: string) => {
133
+ defineLeafAttr(target, key, defaultValue, {
134
+ set(value: __Value) {
135
+ this.__setAttr(key, value)
136
+ this.__layout.surfaceChanged || this.__layout.surfaceChange()
137
+
138
+ if (this.parent) {
139
+ this.parent.__layout.childrenSortChanged = true
140
+ } else {
141
+ this.waitParent(() => { this.parent.__layout.childrenSortChanged = true })
142
+ }
143
+ }
144
+ })
145
+ }
146
+ }
137
147
 
138
- if (this.parent) {
139
- this.parent.__layout.childrenSortChanged || (this.parent.__layout.childrenSortChanged = true)
140
- } else {
141
- this.__addParentWait(() => { this.parent.__layout.childrenSortChanged || (this.parent.__layout.childrenSortChanged = true) })
148
+ export function maskType(defaultValue?: __Value) {
149
+ return (target: ILeaf, key: string) => {
150
+ defineLeafAttr(target, key, defaultValue, {
151
+ set(value: boolean) {
152
+ this.__setAttr(key, value)
153
+ this.__layout.boxChanged || this.__layout.boxChange()
154
+
155
+ if (this.parent) {
156
+ this.parent.__updateMask(value)
157
+ } else {
158
+ this.waitParent(() => { this.parent.__updateMask(value) })
159
+ }
142
160
  }
143
- }
144
- })
145
- defineDataProcessor(target, key, defaultValue)
161
+ })
162
+ }
163
+ }
164
+
165
+ export function hitType(defaultValue?: __Value) {
166
+ return (target: ILeaf, key: string) => {
167
+ defineLeafAttr(target, key, defaultValue, {
168
+ set(value: __Value) {
169
+ this.__setAttr(key, value)
170
+ if (Debug.showHitView) { this.__layout.surfaceChanged || this.__layout.surfaceChange() }
171
+ }
172
+ })
173
+ }
146
174
  }
147
175
 
148
176
 
149
177
  // get
150
178
 
151
- export function dataProcessor(processor: IObject) { return (target: ILeaf, _key: string) => dataProcessor__(target, processor) }
152
- export function dataProcessor__(target: ILeaf, processor: IObject) {
153
- defineKey(target, '__DataProcessor', {
154
- get() { return processor }
155
- })
179
+ export function dataProcessor(processor: IObject) {
180
+ return (target: ILeaf, _key: string) => {
181
+ defineKey(target, '__DataProcessor', {
182
+ get() { return processor }
183
+ })
184
+ }
156
185
  }
157
186
 
158
- export function layoutProcessor(processor: IObject) { return (target: ILeaf, _key: string) => layoutProcessor__(target, processor) }
159
- export function layoutProcessor__(target: ILeaf, processor: IObject) {
160
- defineKey(target, '__LayoutProcessor', {
161
- get() { return processor }
162
- })
187
+ export function layoutProcessor(processor: IObject) {
188
+ return (target: ILeaf, _key: string) => {
189
+ defineKey(target, '__LayoutProcessor', {
190
+ get() { return processor }
191
+ })
192
+ }
163
193
  }
164
194
 
165
195
 
@@ -169,8 +199,12 @@ function getSetMethodName(key: string): string {
169
199
  return 'set' + key.charAt(0).toUpperCase() + key.slice(1)
170
200
  }
171
201
 
202
+ export function setDefaultValue(target: IObject, key: string, defaultValue: __Value): void {
203
+ defineDataProcessor(target.prototype, key, defaultValue)
204
+ }
205
+
172
206
  // define leaf.__[key] getter/setter
173
- export function defineDataProcessor(target: ILeaf, key: string, defaultValue: __Value): void {
207
+ export function defineDataProcessor(target: ILeaf, key: string, defaultValue?: __Value): void {
174
208
 
175
209
  const data = target.__DataProcessor.prototype as ILeafData
176
210
  const computedKey = '_' + key
@@ -183,17 +217,16 @@ export function defineDataProcessor(target: ILeaf, key: string, defaultValue: __
183
217
  },
184
218
  set(value: __Value) {
185
219
  this[computedKey] = value
186
- }
220
+ },
221
+ configurable: true,
222
+ enumerable: true
187
223
  }
188
224
 
189
225
  if (defaultValue === undefined) property.get = function () { return this[computedKey] }
190
226
 
191
227
  const descriptor = getDescriptor(data, key)
192
228
 
193
- if (descriptor) {
194
- if (descriptor.set) property.set = descriptor.set // use custom set
195
- if (descriptor.get) property.get = descriptor.get // use custom get
196
- }
229
+ if (descriptor && descriptor.set) property.set = descriptor.set // use custom set
197
230
 
198
231
  if (data[setMethodName]) {
199
232
  property.set = data[setMethodName] // use custom setKey(value)
package/src/define.ts ADDED
@@ -0,0 +1,35 @@
1
+ import { ILeaf, IObject, __Value } from '@leafer/interface'
2
+
3
+ import * as types from './data'
4
+
5
+ interface IDataTypeFunction {
6
+ (target: ILeaf, key: string): void
7
+ }
8
+
9
+ interface IDataTypeDecorator {
10
+ (...arg: any): IDataTypeFunction
11
+ }
12
+
13
+ interface IDataTypeDecoratorMap {
14
+ [key: string]: IDataTypeDecorator
15
+ }
16
+
17
+ export const DataTypeDecorator = {
18
+
19
+ list: {} as IDataTypeDecoratorMap,
20
+
21
+ register(name: string, decorator: IDataTypeDecorator): void {
22
+ this.list[name] = decorator
23
+ },
24
+
25
+ get(name: string): IDataTypeDecorator {
26
+ const decorator = this.list[name]
27
+ return decorator
28
+ }
29
+ }
30
+
31
+ Object.keys(types).forEach(key => {
32
+ if (key.includes('Type')) {
33
+ DataTypeDecorator.register(key, (types as IObject)[key] as IDataTypeDecorator)
34
+ }
35
+ })
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { registerUI, registerUI__, registerEvent, registerEvent__ } from './class'
2
- export { aliasType, aliasType__, dataType, dataType__, positionType, positionType__, boundsType, boundsType__, affectEventBoundsType, affectEventBoundsType__, affectRenderBoundsType, affectRenderBoundsType__, scaleType, scaleType__, rotationType, rotationType__, surfaceType, surfaceType__, opacityType, opacityType__, sortType, sortType__, pathType, pathType__, dataProcessor, dataProcessor__, layoutProcessor, layoutProcessor__, defineDataProcessor } from './data'
3
- export { useModule, useModule__, rewrite, rewriteAble } from './rewrite'
4
- export { defineKey, getDescriptor } from './object'
1
+ export { aliasType, defineLeafAttr, dataType, positionType, boundsType, affectStrokeBoundsType, strokeType, affectRenderBoundsType, scaleType, rotationType, surfaceType, opacityType, sortType, maskType, hitType, pathType, dataProcessor, layoutProcessor, setDefaultValue, defineDataProcessor } from './data'
2
+ export { useModule, rewrite, rewriteAble } from './rewrite'
3
+ export { defineKey, getDescriptor } from './object'
4
+ export { registerUI, registerUIEvent } from './class'
package/src/rewrite.ts CHANGED
@@ -42,17 +42,18 @@ setTimeout(() => doRewrite(true))
42
42
 
43
43
  // class
44
44
 
45
- export function useModule(module: IObject) { return (target: IObject) => useModule__(target, module) }
46
- export function useModule__(target: IObject, module: IObject) {
47
- const names = module.prototype ? getNames(module.prototype) : Object.keys(module)
48
- names.forEach(name => {
49
- if (!excludeNames.includes(name)) {
50
- if (module.prototype) {
51
- const d = getDescriptor(module.prototype, name)
52
- if (d.writable) target.prototype[name] = module.prototype[name]
53
- } else {
54
- target.prototype[name] = module[name]
45
+ export function useModule(module: IObject, exclude?: string[]) {
46
+ return (target: IObject) => {
47
+ const names = module.prototype ? getNames(module.prototype) : Object.keys(module)
48
+ names.forEach(name => {
49
+ if (!excludeNames.includes(name) && (!exclude || !exclude.includes(name))) {
50
+ if (module.prototype) {
51
+ const d = getDescriptor(module.prototype, name)
52
+ if (d.writable) target.prototype[name] = module.prototype[name]
53
+ } else {
54
+ target.prototype[name] = module[name]
55
+ }
55
56
  }
56
- }
57
- })
58
- }
57
+ })
58
+ }
59
+ }