@leafer/decorator 1.0.0-beta.15 → 1.0.0-beta.16

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,12 +1,13 @@
1
1
  {
2
2
  "name": "@leafer/decorator",
3
- "version": "1.0.0-beta.15",
3
+ "version": "1.0.0-beta.16",
4
4
  "description": "@leafer/decorator",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
7
7
  "main": "src/index.ts",
8
8
  "types": "types/index.d.ts",
9
9
  "files": [
10
+ "src",
10
11
  "types",
11
12
  "dist"
12
13
  ],
@@ -21,10 +22,10 @@
21
22
  "leaferjs"
22
23
  ],
23
24
  "dependencies": {
24
- "@leafer/platform": "1.0.0-beta.15",
25
- "@leafer/debug": "1.0.0-beta.15"
25
+ "@leafer/platform": "1.0.0-beta.16",
26
+ "@leafer/debug": "1.0.0-beta.16"
26
27
  },
27
28
  "devDependencies": {
28
- "@leafer/interface": "1.0.0-beta.15"
29
+ "@leafer/interface": "1.0.0-beta.16"
29
30
  }
30
31
  }
package/src/class.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { IObject } from '@leafer/interface'
2
+ import { EventCreator, UICreator } from '@leafer/platform'
3
+
4
+ export function registerUI() {
5
+ return (target: IObject) => {
6
+ UICreator.register(target)
7
+ }
8
+ }
9
+
10
+ export function registerUIEvent() {
11
+ return (target: IObject) => {
12
+ EventCreator.register(target)
13
+ }
14
+ }
package/src/data.ts ADDED
@@ -0,0 +1,265 @@
1
+ import { ILeafData, ILeaf, IObject, __Value } from '@leafer/interface'
2
+ import { defineKey, getDescriptor } from './object'
3
+ import { Debug } from '@leafer/debug'
4
+
5
+
6
+ // name
7
+
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
+ }
17
+ }
18
+
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 || {}))
27
+ defineDataProcessor(target, key, defaultValue)
28
+ }
29
+
30
+ export function dataType(defaultValue?: __Value) {
31
+ return (target: ILeaf, key: string) => {
32
+ defineLeafAttr(target, key, defaultValue)
33
+ }
34
+ }
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
+ }
46
+
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
+ }
56
+ }
57
+
58
+
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
+ }
69
+ }
70
+
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
+ if (this.__.around) this.__layout.positionChanged || this.__layout.positionChange()
78
+ }
79
+ })
80
+ }
81
+ }
82
+
83
+ export const pathType = boundsType
84
+
85
+
86
+ export function affectStrokeBoundsType(defaultValue?: __Value) {
87
+ return (target: ILeaf, key: string) => {
88
+ defineLeafAttr(target, key, defaultValue, {
89
+ set(value: __Value) {
90
+ this.__setAttr(key, value)
91
+ this.__layout.strokeChanged || this.__layout.strokeChange()
92
+ }
93
+ })
94
+ }
95
+ }
96
+
97
+ export const strokeType = affectStrokeBoundsType
98
+
99
+ export function affectRenderBoundsType(defaultValue?: __Value) {
100
+ return (target: ILeaf, key: string) => {
101
+ defineLeafAttr(target, key, defaultValue, {
102
+ set(value: __Value) {
103
+ this.__setAttr(key, value)
104
+ this.__layout.renderChanged || this.__layout.renderChange()
105
+ }
106
+ })
107
+ }
108
+ }
109
+
110
+ export function surfaceType(defaultValue?: __Value) {
111
+ return (target: ILeaf, key: string) => {
112
+ defineLeafAttr(target, key, defaultValue, {
113
+ set(value: __Value) {
114
+ this.__setAttr(key, value)
115
+ this.__layout.surfaceChanged || this.__layout.surfaceChange()
116
+ }
117
+ })
118
+ }
119
+ }
120
+
121
+ export function opacityType(defaultValue?: __Value) {
122
+ return (target: ILeaf, key: string) => {
123
+ defineLeafAttr(target, key, defaultValue, {
124
+ set(value: __Value) {
125
+ this.__setAttr(key, value)
126
+ this.__layout.opacityChanged || this.__layout.opacityChange()
127
+ }
128
+ })
129
+ }
130
+ }
131
+
132
+ export function sortType(defaultValue?: __Value) {
133
+ return (target: ILeaf, key: string) => {
134
+ defineLeafAttr(target, key, defaultValue, {
135
+ set(value: __Value) {
136
+ this.__setAttr(key, value)
137
+ this.__layout.surfaceChanged || this.__layout.surfaceChange()
138
+ this.waitParent(() => { this.parent.__layout.childrenSortChange() })
139
+ }
140
+ })
141
+ }
142
+ }
143
+
144
+ export function maskType(defaultValue?: __Value) {
145
+ return (target: ILeaf, key: string) => {
146
+ defineLeafAttr(target, key, defaultValue, {
147
+ set(value: boolean) {
148
+ this.__setAttr(key, value)
149
+ this.__layout.boxChanged || this.__layout.boxChange()
150
+ this.waitParent(() => { this.parent.__updateMask(value) })
151
+ }
152
+ })
153
+ }
154
+ }
155
+
156
+ export function eraserType(defaultValue?: __Value) {
157
+ return (target: ILeaf, key: string) => {
158
+ defineLeafAttr(target, key, defaultValue, {
159
+ set(value: boolean) {
160
+ this.__setAttr(key, value)
161
+ this.waitParent(() => { this.parent.__updateEraser(value) })
162
+ }
163
+ })
164
+ }
165
+ }
166
+
167
+ export function hitType(defaultValue?: __Value) {
168
+ return (target: ILeaf, key: string) => {
169
+ defineLeafAttr(target, key, defaultValue, {
170
+ set(value: __Value) {
171
+ this.__setAttr(key, value)
172
+ if (Debug.showHitView) { this.__layout.surfaceChanged || this.__layout.surfaceChange() }
173
+ if (this.leafer) this.leafer.updateCursor()
174
+ }
175
+ })
176
+ }
177
+ }
178
+
179
+ export function cursorType(defaultValue?: __Value) {
180
+ return (target: ILeaf, key: string) => {
181
+ defineLeafAttr(target, key, defaultValue, {
182
+ set(value: __Value) {
183
+ this.__setAttr(key, value)
184
+ if (this.leafer) this.leafer.updateCursor()
185
+ }
186
+ })
187
+ }
188
+ }
189
+
190
+
191
+ // get
192
+
193
+ export function dataProcessor(processor: IObject) {
194
+ return (target: ILeaf, _key: string) => {
195
+ defineKey(target, '__DataProcessor', {
196
+ get() { return processor }
197
+ })
198
+ }
199
+ }
200
+
201
+ export function layoutProcessor(processor: IObject) {
202
+ return (target: ILeaf, _key: string) => {
203
+ defineKey(target, '__LayoutProcessor', {
204
+ get() { return processor }
205
+ })
206
+ }
207
+ }
208
+
209
+
210
+ // other
211
+
212
+ function getSetMethodName(key: string): string {
213
+ return 'set' + key.charAt(0).toUpperCase() + key.slice(1)
214
+ }
215
+
216
+ export function setDefaultValue(target: IObject, key: string, defaultValue: __Value): void {
217
+ defineDataProcessor(target.prototype, key, defaultValue)
218
+ }
219
+
220
+ // define leaf.__[key] getter/setter
221
+ export function defineDataProcessor(target: ILeaf, key: string, defaultValue?: __Value): void {
222
+
223
+ const data = target.__DataProcessor.prototype as ILeafData
224
+ const computedKey = '_' + key
225
+ const setMethodName = getSetMethodName(key)
226
+
227
+ const property: IObject & ThisType<ILeafData> = {
228
+ get() {
229
+ const v = this[computedKey]
230
+ return v === undefined ? defaultValue : v
231
+ },
232
+ set(value: __Value) {
233
+ this[computedKey] = value
234
+ },
235
+ configurable: true,
236
+ enumerable: true
237
+ }
238
+
239
+ if (defaultValue === undefined) {
240
+ property.get = function () { return this[computedKey] }
241
+ } else if (key === 'width') {
242
+ property.get = function () {
243
+ const v = this[computedKey]
244
+ return v === undefined ? ((this as ILeafData).__naturalWidth || defaultValue) : v
245
+ }
246
+ } else if (key === 'height') {
247
+ property.get = function () {
248
+ const v = this[computedKey]
249
+ return v === undefined ? ((this as ILeafData).__naturalHeight || defaultValue) : v
250
+ }
251
+ }
252
+
253
+ const descriptor = getDescriptor(data, key)
254
+
255
+ if (descriptor && descriptor.set) property.set = descriptor.set // use custom set
256
+
257
+ if (data[setMethodName]) {
258
+ property.set = data[setMethodName] // use custom setKey(value)
259
+ delete data[setMethodName]
260
+ }
261
+
262
+ Object.defineProperty(data, key, property)
263
+
264
+ }
265
+
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/object.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { IObject } from '@leafer/interface'
2
+
3
+ export function defineKey<T>(target: T, key: string, descriptor: IObject & ThisType<T>): void {
4
+ Object.defineProperty(target, key, descriptor)
5
+ }
6
+
7
+ export function getDescriptor(object: IObject, name: string) {
8
+ return Object.getOwnPropertyDescriptor(object, name)
9
+ }
10
+
11
+ export function getNames(object: IObject): string[] {
12
+ return Object.getOwnPropertyNames(object)
13
+ }
14
+
package/src/rewrite.ts ADDED
@@ -0,0 +1,59 @@
1
+ import { IObject, IFunction } from '@leafer/interface'
2
+ import { Debug } from '@leafer/debug'
3
+ import { getDescriptor, getNames } from './object'
4
+
5
+ interface IRewriteItem {
6
+ name: string
7
+ run: IFunction
8
+ }
9
+
10
+ const debug = new Debug('rewrite')
11
+
12
+ const list: IRewriteItem[] = []
13
+ const excludeNames = ['destroy', 'constructor']
14
+
15
+
16
+ // method
17
+
18
+ export function rewrite(method: IFunction) {
19
+ return (target: IObject, key: string) => {
20
+ list.push({ name: target.constructor.name + '.' + key, run: () => { target[key] = method } })
21
+ }
22
+ }
23
+
24
+ export function rewriteAble() {
25
+ return (_target: IObject) => {
26
+ doRewrite()
27
+ }
28
+ }
29
+
30
+ function doRewrite(error?: boolean): void {
31
+ if (list.length) {
32
+ list.forEach(item => {
33
+ if (error) debug.error(item.name, '需在Class上装饰@rewriteAble()')
34
+ item.run()
35
+ })
36
+ list.length = 0
37
+ }
38
+ }
39
+
40
+ setTimeout(() => doRewrite(true))
41
+
42
+
43
+ // class
44
+
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
+ }
56
+ }
57
+ })
58
+ }
59
+ }