@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 +4 -4
- package/src/class.ts +8 -6
- package/src/data.ts +161 -128
- package/src/define.ts +35 -0
- package/src/index.ts +4 -4
- package/src/rewrite.ts +14 -13
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer/decorator",
|
|
3
|
-
"version": "1.0.0-
|
|
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-
|
|
23
|
-
"@leafer/debug": "1.0.0-
|
|
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-
|
|
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() {
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
export function registerUI() {
|
|
5
|
+
return (target: IObject) => {
|
|
6
|
+
UICreator.register(target)
|
|
7
|
+
}
|
|
7
8
|
}
|
|
8
9
|
|
|
9
|
-
export function
|
|
10
|
-
|
|
11
|
-
|
|
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) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
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) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
-
|
|
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) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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
|
|
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 {
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
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
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
+
}
|