@leafer/decorator 1.0.0-rc.9 → 1.0.1

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-rc.9",
3
+ "version": "1.0.1",
4
4
  "description": "@leafer/decorator",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -22,10 +22,10 @@
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/platform": "1.0.0-rc.9",
26
- "@leafer/debug": "1.0.0-rc.9"
25
+ "@leafer/platform": "1.0.1",
26
+ "@leafer/debug": "1.0.1"
27
27
  },
28
28
  "devDependencies": {
29
- "@leafer/interface": "1.0.0-rc.9"
29
+ "@leafer/interface": "1.0.1"
30
30
  }
31
31
  }
package/src/data.ts CHANGED
@@ -1,191 +1,208 @@
1
- import { ILeafData, ILeaf, IObject, IValue } from '@leafer/interface'
1
+ import { ILeafData, ILeaf, IObject, IValue, ILeafAttrDescriptor, ILeafAttrDescriptorFn } from '@leafer/interface'
2
2
  import { defineKey, getDescriptor } from './object'
3
3
  import { Debug } from '@leafer/debug'
4
4
 
5
5
 
6
6
  // name
7
7
 
8
- export function defineLeafAttr(target: ILeaf, key: string, defaultValue?: IValue, mergeDescriptor?: IObject & ThisType<ILeaf>): void {
9
- const defaultDescriptor: IObject & ThisType<ILeaf> = {
8
+ export function decorateLeafAttr(defaultValue?: IValue, descriptorFn?: ILeafAttrDescriptorFn) {
9
+ return (target: ILeaf, key: string) => defineLeafAttr(target, key, defaultValue, descriptorFn && descriptorFn(key))
10
+ }
11
+
12
+ export function attr(partDescriptor?: ILeafAttrDescriptor): ILeafAttrDescriptor {
13
+ return partDescriptor
14
+ }
15
+
16
+
17
+ export function defineLeafAttr(target: ILeaf, key: string, defaultValue?: IValue, partDescriptor?: ILeafAttrDescriptor): void {
18
+ const defaultDescriptor: ILeafAttrDescriptor = {
10
19
  get() { return this.__getAttr(key) },
11
- set(value: IValue) { this.__setAttr(key, value) },
12
- configurable: true,
13
- enumerable: true
20
+ set(value: IValue) { this.__setAttr(key, value) }
14
21
  }
15
- defineKey(target, key, Object.assign(defaultDescriptor, mergeDescriptor || {}))
22
+ defineKey(target, key, Object.assign(defaultDescriptor, partDescriptor || {}))
16
23
  defineDataProcessor(target, key, defaultValue)
17
24
  }
18
25
 
26
+
19
27
  export function dataType(defaultValue?: IValue) {
20
- return (target: ILeaf, key: string) => {
21
- defineLeafAttr(target, key, defaultValue)
22
- }
28
+ return decorateLeafAttr(defaultValue)
23
29
  }
24
30
 
25
- export function positionType(defaultValue?: IValue) {
26
- return (target: ILeaf, key: string) => {
27
- defineLeafAttr(target, key, defaultValue, {
28
- set(value: IValue) {
29
- this.__setAttr(key, value)
30
- this.__layout.matrixChanged || this.__layout.matrixChange()
31
- }
32
- })
33
- }
31
+ export function positionType(defaultValue?: IValue, checkFiniteNumber?: boolean) {
32
+ return decorateLeafAttr(defaultValue, (key: string) => attr({
33
+ set(value: IValue) {
34
+ this.__setAttr(key, value, checkFiniteNumber) && (this.__layout.matrixChanged || this.__layout.matrixChange())
35
+ }
36
+ }))
34
37
  }
35
38
 
36
39
  export function autoLayoutType(defaultValue?: IValue) {
37
- return (target: ILeaf, key: string) => {
38
- defineLeafAttr(target, key, defaultValue, {
39
- set(value: IValue) {
40
- this.__setAttr(key, value)
40
+ return decorateLeafAttr(defaultValue, (key: string) => attr({
41
+ set(value: IValue) {
42
+ if (this.__setAttr(key, value)) {
41
43
  this.__layout.matrixChanged || this.__layout.matrixChange()
42
44
  this.__hasAutoLayout = !!value
45
+ if (!this.__local) this.__layout.createLocal()
43
46
  }
44
- })
45
- }
47
+ }
48
+ }))
46
49
  }
47
50
 
48
- export function scaleType(defaultValue?: IValue) {
49
- return (target: ILeaf, key: string) => {
50
- defineLeafAttr(target, key, defaultValue, {
51
- set(value: IValue) {
52
- this.__setAttr(key, value)
53
- this.__layout.scaleChanged || this.__layout.scaleChange()
54
- }
55
- })
56
- }
51
+ export function scaleType(defaultValue?: IValue, checkFiniteNumber?: boolean) {
52
+ return decorateLeafAttr(defaultValue, (key: string) => attr({
53
+ set(value: IValue) {
54
+ this.__setAttr(key, value, checkFiniteNumber) && (this.__layout.scaleChanged || this.__layout.scaleChange())
55
+
56
+ }
57
+ }))
57
58
  }
58
59
 
60
+ export function rotationType(defaultValue?: IValue, checkFiniteNumber?: boolean) {
61
+ return decorateLeafAttr(defaultValue, (key: string) => attr({
62
+ set(value: IValue) {
63
+ this.__setAttr(key, value, checkFiniteNumber) && (this.__layout.rotationChanged || this.__layout.rotationChange())
64
+ }
59
65
 
60
- export function rotationType(defaultValue?: IValue) {
61
- return (target: ILeaf, key: string) => {
62
- defineLeafAttr(target, key, defaultValue, {
63
- set(value: IValue) {
64
- this.__setAttr(key, value)
65
- this.__layout.rotationChanged || this.__layout.rotationChange()
66
+ }))
67
+ }
66
68
 
67
- }
68
- })
69
- }
69
+ export function boundsType(defaultValue?: IValue, checkFiniteNumber?: boolean) {
70
+ return decorateLeafAttr(defaultValue, (key: string) => attr({
71
+ set(value: IValue) {
72
+ this.__setAttr(key, value, checkFiniteNumber) && doBoundsType(this)
73
+ }
74
+ }))
70
75
  }
71
76
 
72
- export function boundsType(defaultValue?: IValue) {
73
- return (target: ILeaf, key: string) => {
74
- defineLeafAttr(target, key, defaultValue, {
75
- set(value: IValue) {
76
- this.__setAttr(key, value)
77
- this.__layout.boxChanged || this.__layout.boxChange()
78
- if (this.__hasAutoLayout) this.__layout.matrixChanged || this.__layout.matrixChange()
79
- }
80
- })
81
- }
77
+ export function naturalBoundsType(defaultValue?: IValue) {
78
+ return decorateLeafAttr(defaultValue, (key: string) => attr({
79
+ set(value: IValue) {
80
+ this.__setAttr(key, value) && (doBoundsType(this), this.__.__removeNaturalSize())
81
+ }
82
+ }))
83
+ }
84
+
85
+ export function doBoundsType(leaf: ILeaf): void {
86
+ leaf.__layout.boxChanged || leaf.__layout.boxChange()
87
+ if (leaf.__hasAutoLayout) leaf.__layout.matrixChanged || leaf.__layout.matrixChange()
88
+ }
89
+
90
+ export function pathInputType(defaultValue?: IValue) {
91
+ return decorateLeafAttr(defaultValue, (key: string) => attr({
92
+ set(value: IValue) {
93
+ const data = this.__
94
+ if (data.__pathInputed !== 2) data.__pathInputed = value ? 1 : 0
95
+ if (!value) data.__pathForRender = undefined
96
+ this.__setAttr(key, value)
97
+ doBoundsType(this)
98
+ }
99
+ }))
82
100
  }
83
101
 
84
102
  export const pathType = boundsType
85
103
 
86
104
 
87
105
  export function affectStrokeBoundsType(defaultValue?: IValue) {
88
- return (target: ILeaf, key: string) => {
89
- defineLeafAttr(target, key, defaultValue, {
90
- set(value: IValue) {
91
- this.__setAttr(key, value)
92
- this.__layout.strokeChanged || this.__layout.strokeChange()
93
- }
94
- })
95
- }
106
+ return decorateLeafAttr(defaultValue, (key: string) => attr({
107
+ set(value: IValue) {
108
+ this.__setAttr(key, value) && doStrokeType(this)
109
+ }
110
+ }))
111
+ }
112
+
113
+ export function doStrokeType(leaf: ILeaf): void {
114
+ leaf.__layout.strokeChanged || leaf.__layout.strokeChange()
115
+ if (leaf.__.__useArrow) doBoundsType(leaf)
96
116
  }
97
117
 
98
118
  export const strokeType = affectStrokeBoundsType
99
119
 
100
120
  export function affectRenderBoundsType(defaultValue?: IValue) {
101
- return (target: ILeaf, key: string) => {
102
- defineLeafAttr(target, key, defaultValue, {
103
- set(value: IValue) {
104
- this.__setAttr(key, value)
105
- this.__layout.renderChanged || this.__layout.renderChange()
106
- }
107
- })
108
- }
121
+ return decorateLeafAttr(defaultValue, (key: string) => attr({
122
+ set(value: IValue) {
123
+ this.__setAttr(key, value)
124
+ this.__layout.renderChanged || this.__layout.renderChange()
125
+ }
126
+ }))
109
127
  }
110
128
 
111
129
  export function surfaceType(defaultValue?: IValue) {
112
- return (target: ILeaf, key: string) => {
113
- defineLeafAttr(target, key, defaultValue, {
114
- set(value: IValue) {
115
- this.__setAttr(key, value)
116
- this.__layout.surfaceChanged || this.__layout.surfaceChange()
117
- }
118
- })
119
- }
130
+ return decorateLeafAttr(defaultValue, (key: string) => attr({
131
+ set(value: IValue) {
132
+ this.__setAttr(key, value) && (this.__layout.surfaceChanged || this.__layout.surfaceChange())
133
+ }
134
+ }))
120
135
  }
121
136
 
122
137
  export function opacityType(defaultValue?: IValue) {
123
- return (target: ILeaf, key: string) => {
124
- defineLeafAttr(target, key, defaultValue, {
125
- set(value: IValue) {
126
- this.__setAttr(key, value)
138
+ return decorateLeafAttr(defaultValue, (key: string) => attr({
139
+ set(value: IValue) {
140
+ this.__setAttr(key, value) && (this.__layout.opacityChanged || this.__layout.opacityChange())
141
+ }
142
+ }))
143
+ }
144
+
145
+ export function visibleType(defaultValue?: IValue) {
146
+ return decorateLeafAttr(defaultValue, (key: string) => attr({
147
+ set(value: IValue) {
148
+ const oldValue = this.visible
149
+ if (this.__setAttr(key, value)) {
127
150
  this.__layout.opacityChanged || this.__layout.opacityChange()
151
+ if (oldValue === 0 || value === 0) doBoundsType(this) // 0 = display: none
128
152
  }
129
- })
130
- }
153
+ }
154
+ }))
131
155
  }
132
156
 
133
157
  export function sortType(defaultValue?: IValue) {
134
- return (target: ILeaf, key: string) => {
135
- defineLeafAttr(target, key, defaultValue, {
136
- set(value: IValue) {
137
- this.__setAttr(key, value)
158
+ return decorateLeafAttr(defaultValue, (key: string) => attr({
159
+ set(value: IValue) {
160
+ if (this.__setAttr(key, value)) {
138
161
  this.__layout.surfaceChanged || this.__layout.surfaceChange()
139
162
  this.waitParent(() => { this.parent.__layout.childrenSortChange() })
140
163
  }
141
- })
142
- }
164
+ }
165
+ }))
143
166
  }
144
167
 
145
168
  export function maskType(defaultValue?: IValue) {
146
- return (target: ILeaf, key: string) => {
147
- defineLeafAttr(target, key, defaultValue, {
148
- set(value: boolean) {
149
- this.__setAttr(key, value)
169
+ return decorateLeafAttr(defaultValue, (key: string) => attr({
170
+ set(value: boolean) {
171
+ if (this.__setAttr(key, value)) {
150
172
  this.__layout.boxChanged || this.__layout.boxChange()
151
173
  this.waitParent(() => { this.parent.__updateMask(value) })
152
174
  }
153
- })
154
- }
175
+ }
176
+ }))
155
177
  }
156
178
 
157
179
  export function eraserType(defaultValue?: IValue) {
158
- return (target: ILeaf, key: string) => {
159
- defineLeafAttr(target, key, defaultValue, {
160
- set(value: boolean) {
161
- this.__setAttr(key, value)
162
- this.waitParent(() => { this.parent.__updateEraser(value) })
163
- }
164
- })
165
- }
180
+ return decorateLeafAttr(defaultValue, (key: string) => attr({
181
+ set(value: boolean) {
182
+ this.__setAttr(key, value) && this.waitParent(() => { this.parent.__updateEraser(value) })
183
+ }
184
+ }))
166
185
  }
167
186
 
168
187
  export function hitType(defaultValue?: IValue) {
169
- return (target: ILeaf, key: string) => {
170
- defineLeafAttr(target, key, defaultValue, {
171
- set(value: IValue) {
172
- this.__setAttr(key, value)
188
+ return decorateLeafAttr(defaultValue, (key: string) => attr({
189
+ set(value: IValue) {
190
+ if (this.__setAttr(key, value)) {
191
+ this.__layout.hitCanvasChanged = true
173
192
  if (Debug.showHitView) { this.__layout.surfaceChanged || this.__layout.surfaceChange() }
174
193
  if (this.leafer) this.leafer.updateCursor()
175
194
  }
176
- })
177
- }
195
+ }
196
+ }))
178
197
  }
179
198
 
180
199
  export function cursorType(defaultValue?: IValue) {
181
- return (target: ILeaf, key: string) => {
182
- defineLeafAttr(target, key, defaultValue, {
183
- set(value: IValue) {
184
- this.__setAttr(key, value)
185
- if (this.leafer) this.leafer.updateCursor()
186
- }
187
- })
188
- }
200
+ return decorateLeafAttr(defaultValue, (key: string) => attr({
201
+ set(value: IValue) {
202
+ this.__setAttr(key, value)
203
+ if (this.leafer) this.leafer.updateCursor()
204
+ }
205
+ }))
189
206
  }
190
207
 
191
208
 
@@ -229,9 +246,7 @@ export function defineDataProcessor(target: ILeaf, key: string, defaultValue?: I
229
246
  },
230
247
  set(value: IValue) {
231
248
  (this as IObject)[computedKey] = value
232
- },
233
- configurable: true,
234
- enumerable: true
249
+ }
235
250
  }
236
251
 
237
252
  if (defaultValue === undefined) {
@@ -239,12 +254,22 @@ export function defineDataProcessor(target: ILeaf, key: string, defaultValue?: I
239
254
  } else if (key === 'width') {
240
255
  property.get = function () {
241
256
  const v = this[computedKey]
242
- return v === undefined ? ((this as ILeafData).__naturalWidth || defaultValue) : v
257
+ if (v === undefined) {
258
+ const t = this as ILeafData
259
+ return (t as IObject)._height && t.__naturalWidth && t.__useNaturalRatio ? (t as IObject)._height * t.__naturalWidth / t.__naturalHeight : t.__naturalWidth || defaultValue
260
+ } else {
261
+ return v
262
+ }
243
263
  }
244
264
  } else if (key === 'height') {
245
265
  property.get = function () {
246
266
  const v = this[computedKey]
247
- return v === undefined ? ((this as ILeafData).__naturalHeight || defaultValue) : v
267
+ if (v === undefined) {
268
+ const t = this as ILeafData
269
+ return (t as IObject)._width && t.__naturalHeight && t.__useNaturalRatio ? (t as IObject)._width * t.__naturalHeight / t.__naturalWidth : t.__naturalHeight || defaultValue
270
+ } else {
271
+ return v
272
+ }
248
273
  }
249
274
  }
250
275
 
@@ -262,7 +287,7 @@ export function defineDataProcessor(target: ILeaf, key: string, defaultValue?: I
262
287
  delete data[setMethodName]
263
288
  }
264
289
 
265
- Object.defineProperty(data, key, property)
290
+ defineKey(data, key, property)
266
291
 
267
292
  }
268
293
 
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { defineLeafAttr, dataType, positionType, autoLayoutType, boundsType, affectStrokeBoundsType, strokeType, affectRenderBoundsType, scaleType, rotationType, surfaceType, opacityType, sortType, maskType, eraserType, hitType, pathType, cursorType, dataProcessor, layoutProcessor, defineDataProcessor } from './data'
1
+ export { defineLeafAttr, decorateLeafAttr, attr, dataType, positionType, autoLayoutType, boundsType, doBoundsType, naturalBoundsType, affectStrokeBoundsType, doStrokeType, strokeType, affectRenderBoundsType, scaleType, rotationType, surfaceType, opacityType, visibleType, sortType, maskType, eraserType, hitType, pathType, pathInputType, cursorType, dataProcessor, layoutProcessor, 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'
package/src/object.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { IObject } from '@leafer/interface'
2
2
 
3
- export function defineKey<T>(target: T, key: string, descriptor: IObject & ThisType<T>): void {
3
+ export function defineKey<T>(target: T, key: string, descriptor: IObject & ThisType<T>, noConfigurable?: boolean): void {
4
+ if (!noConfigurable) descriptor.configurable = descriptor.enumerable = true
4
5
  Object.defineProperty(target, key, descriptor)
5
6
  }
6
7
 
package/types/index.d.ts CHANGED
@@ -1,18 +1,25 @@
1
- import { ILeaf, IValue, IObject, IFunction } from '@leafer/interface';
1
+ import { IValue, ILeafAttrDescriptorFn, ILeaf, ILeafAttrDescriptor, IObject, IFunction } from '@leafer/interface';
2
2
 
3
- declare function defineLeafAttr(target: ILeaf, key: string, defaultValue?: IValue, mergeDescriptor?: IObject & ThisType<ILeaf>): void;
3
+ declare function decorateLeafAttr(defaultValue?: IValue, descriptorFn?: ILeafAttrDescriptorFn): (target: ILeaf, key: string) => void;
4
+ declare function attr(partDescriptor?: ILeafAttrDescriptor): ILeafAttrDescriptor;
5
+ declare function defineLeafAttr(target: ILeaf, key: string, defaultValue?: IValue, partDescriptor?: ILeafAttrDescriptor): void;
4
6
  declare function dataType(defaultValue?: IValue): (target: ILeaf, key: string) => void;
5
- declare function positionType(defaultValue?: IValue): (target: ILeaf, key: string) => void;
7
+ declare function positionType(defaultValue?: IValue, checkFiniteNumber?: boolean): (target: ILeaf, key: string) => void;
6
8
  declare function autoLayoutType(defaultValue?: IValue): (target: ILeaf, key: string) => void;
7
- declare function scaleType(defaultValue?: IValue): (target: ILeaf, key: string) => void;
8
- declare function rotationType(defaultValue?: IValue): (target: ILeaf, key: string) => void;
9
- declare function boundsType(defaultValue?: IValue): (target: ILeaf, key: string) => void;
9
+ declare function scaleType(defaultValue?: IValue, checkFiniteNumber?: boolean): (target: ILeaf, key: string) => void;
10
+ declare function rotationType(defaultValue?: IValue, checkFiniteNumber?: boolean): (target: ILeaf, key: string) => void;
11
+ declare function boundsType(defaultValue?: IValue, checkFiniteNumber?: boolean): (target: ILeaf, key: string) => void;
12
+ declare function naturalBoundsType(defaultValue?: IValue): (target: ILeaf, key: string) => void;
13
+ declare function doBoundsType(leaf: ILeaf): void;
14
+ declare function pathInputType(defaultValue?: IValue): (target: ILeaf, key: string) => void;
10
15
  declare const pathType: typeof boundsType;
11
16
  declare function affectStrokeBoundsType(defaultValue?: IValue): (target: ILeaf, key: string) => void;
17
+ declare function doStrokeType(leaf: ILeaf): void;
12
18
  declare const strokeType: typeof affectStrokeBoundsType;
13
19
  declare function affectRenderBoundsType(defaultValue?: IValue): (target: ILeaf, key: string) => void;
14
20
  declare function surfaceType(defaultValue?: IValue): (target: ILeaf, key: string) => void;
15
21
  declare function opacityType(defaultValue?: IValue): (target: ILeaf, key: string) => void;
22
+ declare function visibleType(defaultValue?: IValue): (target: ILeaf, key: string) => void;
16
23
  declare function sortType(defaultValue?: IValue): (target: ILeaf, key: string) => void;
17
24
  declare function maskType(defaultValue?: IValue): (target: ILeaf, key: string) => void;
18
25
  declare function eraserType(defaultValue?: IValue): (target: ILeaf, key: string) => void;
@@ -26,10 +33,10 @@ declare function rewrite(method: IFunction): (target: IObject, key: string) => v
26
33
  declare function rewriteAble(): (_target: IObject) => void;
27
34
  declare function useModule(module: IObject, exclude?: string[]): (target: IObject) => void;
28
35
 
29
- declare function defineKey<T>(target: T, key: string, descriptor: IObject & ThisType<T>): void;
36
+ declare function defineKey<T>(target: T, key: string, descriptor: IObject & ThisType<T>, noConfigurable?: boolean): void;
30
37
  declare function getDescriptor(object: IObject, name: string): PropertyDescriptor;
31
38
 
32
39
  declare function registerUI(): (target: IObject) => void;
33
40
  declare function registerUIEvent(): (target: IObject) => void;
34
41
 
35
- export { affectRenderBoundsType, affectStrokeBoundsType, autoLayoutType, boundsType, cursorType, dataProcessor, dataType, defineDataProcessor, defineKey, defineLeafAttr, eraserType, getDescriptor, hitType, layoutProcessor, maskType, opacityType, pathType, positionType, registerUI, registerUIEvent, rewrite, rewriteAble, rotationType, scaleType, sortType, strokeType, surfaceType, useModule };
42
+ export { affectRenderBoundsType, affectStrokeBoundsType, attr, autoLayoutType, boundsType, cursorType, dataProcessor, dataType, decorateLeafAttr, defineDataProcessor, defineKey, defineLeafAttr, doBoundsType, doStrokeType, eraserType, getDescriptor, hitType, layoutProcessor, maskType, naturalBoundsType, opacityType, pathInputType, pathType, positionType, registerUI, registerUIEvent, rewrite, rewriteAble, rotationType, scaleType, sortType, strokeType, surfaceType, useModule, visibleType };