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

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,14 @@
1
1
  {
2
2
  "name": "@leafer/decorator",
3
- "version": "1.0.0-beta.12",
3
+ "version": "1.0.0-beta.15",
4
4
  "description": "@leafer/decorator",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
7
7
  "main": "src/index.ts",
8
+ "types": "types/index.d.ts",
8
9
  "files": [
9
- "src"
10
+ "types",
11
+ "dist"
10
12
  ],
11
13
  "repository": {
12
14
  "type": "git",
@@ -19,10 +21,10 @@
19
21
  "leaferjs"
20
22
  ],
21
23
  "dependencies": {
22
- "@leafer/platform": "1.0.0-beta.12",
23
- "@leafer/debug": "1.0.0-beta.12"
24
+ "@leafer/platform": "1.0.0-beta.15",
25
+ "@leafer/debug": "1.0.0-beta.15"
24
26
  },
25
27
  "devDependencies": {
26
- "@leafer/interface": "1.0.0-beta.12"
28
+ "@leafer/interface": "1.0.0-beta.15"
27
29
  }
28
30
  }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { aliasType, defineLeafAttr, dataType, positionType, boundsType, affectStrokeBoundsType, strokeType, affectRenderBoundsType, scaleType, rotationType, surfaceType, opacityType, sortType, maskType, eraserType, hitType, pathType, dataProcessor, layoutProcessor, setDefaultValue, defineDataProcessor } from './data'
1
+ export { aliasType, defineLeafAttr, dataType, positionType, boundsType, affectStrokeBoundsType, strokeType, affectRenderBoundsType, scaleType, rotationType, surfaceType, opacityType, sortType, maskType, eraserType, hitType, pathType, cursorType, dataProcessor, layoutProcessor, setDefaultValue, defineDataProcessor } from './data'
2
2
  export { useModule, rewrite, rewriteAble } from './rewrite'
3
3
  export { defineKey, getDescriptor } from './object'
4
4
  export { registerUI, registerUIEvent } from './class'
@@ -0,0 +1,36 @@
1
+ import { ILeaf, __Value, IObject, IFunction } from '@leafer/interface';
2
+
3
+ declare function aliasType(name: string): (target: ILeaf, key: string) => void;
4
+ declare function defineLeafAttr(target: ILeaf, key: string, defaultValue?: __Value, mergeDescriptor?: IObject & ThisType<ILeaf>): void;
5
+ declare function dataType(defaultValue?: __Value): (target: ILeaf, key: string) => void;
6
+ declare function positionType(defaultValue?: __Value): (target: ILeaf, key: string) => void;
7
+ declare function scaleType(defaultValue?: __Value): (target: ILeaf, key: string) => void;
8
+ declare function rotationType(defaultValue?: __Value): (target: ILeaf, key: string) => void;
9
+ declare function boundsType(defaultValue?: __Value): (target: ILeaf, key: string) => void;
10
+ declare const pathType: typeof boundsType;
11
+ declare function affectStrokeBoundsType(defaultValue?: __Value): (target: ILeaf, key: string) => void;
12
+ declare const strokeType: typeof affectStrokeBoundsType;
13
+ declare function affectRenderBoundsType(defaultValue?: __Value): (target: ILeaf, key: string) => void;
14
+ declare function surfaceType(defaultValue?: __Value): (target: ILeaf, key: string) => void;
15
+ declare function opacityType(defaultValue?: __Value): (target: ILeaf, key: string) => void;
16
+ declare function sortType(defaultValue?: __Value): (target: ILeaf, key: string) => void;
17
+ declare function maskType(defaultValue?: __Value): (target: ILeaf, key: string) => void;
18
+ declare function eraserType(defaultValue?: __Value): (target: ILeaf, key: string) => void;
19
+ declare function hitType(defaultValue?: __Value): (target: ILeaf, key: string) => void;
20
+ declare function cursorType(defaultValue?: __Value): (target: ILeaf, key: string) => void;
21
+ declare function dataProcessor(processor: IObject): (target: ILeaf, _key: string) => void;
22
+ declare function layoutProcessor(processor: IObject): (target: ILeaf, _key: string) => void;
23
+ declare function setDefaultValue(target: IObject, key: string, defaultValue: __Value): void;
24
+ declare function defineDataProcessor(target: ILeaf, key: string, defaultValue?: __Value): void;
25
+
26
+ declare function rewrite(method: IFunction): (target: IObject, key: string) => void;
27
+ declare function rewriteAble(): (_target: IObject) => void;
28
+ declare function useModule(module: IObject, exclude?: string[]): (target: IObject) => void;
29
+
30
+ declare function defineKey<T>(target: T, key: string, descriptor: IObject & ThisType<T>): void;
31
+ declare function getDescriptor(object: IObject, name: string): PropertyDescriptor;
32
+
33
+ declare function registerUI(): (target: IObject) => void;
34
+ declare function registerUIEvent(): (target: IObject) => void;
35
+
36
+ export { affectRenderBoundsType, affectStrokeBoundsType, aliasType, boundsType, cursorType, dataProcessor, dataType, defineDataProcessor, defineKey, defineLeafAttr, eraserType, getDescriptor, hitType, layoutProcessor, maskType, opacityType, pathType, positionType, registerUI, registerUIEvent, rewrite, rewriteAble, rotationType, scaleType, setDefaultValue, sortType, strokeType, surfaceType, useModule };
package/src/class.ts DELETED
@@ -1,14 +0,0 @@
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 DELETED
@@ -1,252 +0,0 @@
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
- }
78
- })
79
- }
80
- }
81
-
82
- export const pathType = boundsType
83
-
84
-
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
- }
94
- }
95
-
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
- }
107
- }
108
-
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
- }
118
- }
119
-
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
- }
129
- }
130
-
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
- this.waitParent(() => { this.parent.__layout.childrenSortChange() })
138
- }
139
- })
140
- }
141
- }
142
-
143
- export function maskType(defaultValue?: __Value) {
144
- return (target: ILeaf, key: string) => {
145
- defineLeafAttr(target, key, defaultValue, {
146
- set(value: boolean) {
147
- this.__setAttr(key, value)
148
- this.__layout.boxChanged || this.__layout.boxChange()
149
- this.waitParent(() => { this.parent.__updateMask(value) })
150
- }
151
- })
152
- }
153
- }
154
-
155
- export function eraserType(defaultValue?: __Value) {
156
- return (target: ILeaf, key: string) => {
157
- defineLeafAttr(target, key, defaultValue, {
158
- set(value: boolean) {
159
- this.__setAttr(key, value)
160
- this.waitParent(() => { this.parent.__updateEraser(value) })
161
- }
162
- })
163
- }
164
- }
165
-
166
- export function hitType(defaultValue?: __Value) {
167
- return (target: ILeaf, key: string) => {
168
- defineLeafAttr(target, key, defaultValue, {
169
- set(value: __Value) {
170
- this.__setAttr(key, value)
171
- if (Debug.showHitView) { this.__layout.surfaceChanged || this.__layout.surfaceChange() }
172
- }
173
- })
174
- }
175
- }
176
-
177
-
178
- // get
179
-
180
- export function dataProcessor(processor: IObject) {
181
- return (target: ILeaf, _key: string) => {
182
- defineKey(target, '__DataProcessor', {
183
- get() { return processor }
184
- })
185
- }
186
- }
187
-
188
- export function layoutProcessor(processor: IObject) {
189
- return (target: ILeaf, _key: string) => {
190
- defineKey(target, '__LayoutProcessor', {
191
- get() { return processor }
192
- })
193
- }
194
- }
195
-
196
-
197
- // other
198
-
199
- function getSetMethodName(key: string): string {
200
- return 'set' + key.charAt(0).toUpperCase() + key.slice(1)
201
- }
202
-
203
- export function setDefaultValue(target: IObject, key: string, defaultValue: __Value): void {
204
- defineDataProcessor(target.prototype, key, defaultValue)
205
- }
206
-
207
- // define leaf.__[key] getter/setter
208
- export function defineDataProcessor(target: ILeaf, key: string, defaultValue?: __Value): void {
209
-
210
- const data = target.__DataProcessor.prototype as ILeafData
211
- const computedKey = '_' + key
212
- const setMethodName = getSetMethodName(key)
213
-
214
- const property: IObject & ThisType<ILeafData> = {
215
- get() {
216
- const v = this[computedKey]
217
- return v === undefined ? defaultValue : v
218
- },
219
- set(value: __Value) {
220
- this[computedKey] = value
221
- },
222
- configurable: true,
223
- enumerable: true
224
- }
225
-
226
- if (defaultValue === undefined) {
227
- property.get = function () { return this[computedKey] }
228
- } else if (key === 'width') {
229
- property.get = function () {
230
- const v = this[computedKey]
231
- return v === undefined ? ((this as ILeafData).__naturalWidth || defaultValue) : v
232
- }
233
- } else if (key === 'height') {
234
- property.get = function () {
235
- const v = this[computedKey]
236
- return v === undefined ? ((this as ILeafData).__naturalHeight || defaultValue) : v
237
- }
238
- }
239
-
240
- const descriptor = getDescriptor(data, key)
241
-
242
- if (descriptor && descriptor.set) property.set = descriptor.set // use custom set
243
-
244
- if (data[setMethodName]) {
245
- property.set = data[setMethodName] // use custom setKey(value)
246
- delete data[setMethodName]
247
- }
248
-
249
- Object.defineProperty(data, key, property)
250
-
251
- }
252
-
package/src/define.ts DELETED
@@ -1,35 +0,0 @@
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 DELETED
@@ -1,14 +0,0 @@
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 DELETED
@@ -1,59 +0,0 @@
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
- }