@leafer/decorator 1.7.0 → 1.9.0

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.7.0",
3
+ "version": "1.9.0",
4
4
  "description": "@leafer/decorator",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -22,11 +22,11 @@
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/platform": "1.7.0",
26
- "@leafer/data": "1.7.0",
27
- "@leafer/debug": "1.7.0"
25
+ "@leafer/platform": "1.9.0",
26
+ "@leafer/data": "1.9.0",
27
+ "@leafer/debug": "1.9.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@leafer/interface": "1.7.0"
30
+ "@leafer/interface": "1.9.0"
31
31
  }
32
32
  }
package/src/data.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { ILeafData, ILeaf, IObject, IValue, ILeafAttrDescriptor, ILeafAttrDescriptorFn, IValueFunction } from '@leafer/interface'
2
- import { DataHelper, isEmptyData } from '@leafer/data'
2
+ import { DataHelper, isEmptyData, isObject, isUndefined } from '@leafer/data'
3
3
  import { Debug } from '@leafer/debug'
4
4
 
5
- import { defineKey, getDescriptor } from './object'
5
+ import { defineKey, getDescriptor, createDescriptor } from './object'
6
6
 
7
7
 
8
8
  // name
@@ -264,24 +264,16 @@ export function defineDataProcessor(target: ILeaf, key: string, defaultValue?: I
264
264
  const computedKey = '_' + key
265
265
  const setMethodName = getSetMethodName(key)
266
266
 
267
- const property: IObject & ThisType<ILeafData> = {
268
- get() {
269
- const v = (this as IObject)[computedKey]
270
- return v === undefined ? defaultValue : v
271
- },
272
- set(value: IValue) {
273
- (this as IObject)[computedKey] = value
274
- }
275
- }
267
+ const property: IObject & ThisType<ILeafData> = createDescriptor(key, defaultValue)
276
268
 
277
- if (defaultValue === undefined) {
269
+ if (isUndefined(defaultValue)) {
278
270
  property.get = function () { return this[computedKey] }
279
271
  } else if (typeof defaultValue === 'function') {
280
272
  property.get = function () {
281
273
  let v = this[computedKey]
282
274
  return v === undefined ? defaultValue((this as ILeafData).__leaf) : v
283
275
  }
284
- } else if (typeof defaultValue === 'object') {
276
+ } else if (isObject(defaultValue)) {
285
277
  const isEmpty = isEmptyData(defaultValue)
286
278
  property.get = function () {
287
279
  let v = this[computedKey]
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
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
- export { defineKey, getDescriptor } from './object'
3
+ export { defineKey, getDescriptor, createDescriptor } from './object'
4
4
  export { registerUI, registerUIEvent } from './class'
package/src/object.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { IObject } from '@leafer/interface'
1
+ import { IObject, IValue } from '@leafer/interface'
2
+ import { isUndefined } from '@leafer/data'
2
3
 
3
4
  export function defineKey<T>(target: T, key: string, descriptor: IObject & ThisType<T>, noConfigurable?: boolean): void {
4
5
  if (!noConfigurable) descriptor.configurable = descriptor.enumerable = true
@@ -9,6 +10,14 @@ export function getDescriptor(object: IObject, name: string) {
9
10
  return Object.getOwnPropertyDescriptor(object, name)
10
11
  }
11
12
 
13
+ export function createDescriptor(key: string, defaultValue?: IValue) {
14
+ const privateKey = '_' + key
15
+ return {
16
+ get() { const v = (this as IObject)[privateKey]; return isUndefined(v) ? defaultValue : v },
17
+ set(value: IValue) { (this as IObject)[privateKey] = value }
18
+ }
19
+ }
20
+
12
21
  export function getNames(object: IObject): string[] {
13
22
  return Object.getOwnPropertyNames(object)
14
23
  }
package/types/index.d.ts CHANGED
@@ -35,8 +35,12 @@ declare function useModule(module: IObject, exclude?: string[]): (target: IObjec
35
35
 
36
36
  declare function defineKey<T>(target: T, key: string, descriptor: IObject & ThisType<T>, noConfigurable?: boolean): void;
37
37
  declare function getDescriptor(object: IObject, name: string): PropertyDescriptor;
38
+ declare function createDescriptor(key: string, defaultValue?: IValue): {
39
+ get(): any;
40
+ set(value: IValue): void;
41
+ };
38
42
 
39
43
  declare function registerUI(): (target: IObject) => void;
40
44
  declare function registerUIEvent(): (target: IObject) => void;
41
45
 
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 };
46
+ export { affectRenderBoundsType, affectStrokeBoundsType, attr, autoLayoutType, boundsType, createDescriptor, 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 };