@leafer/decorator 1.5.0 → 1.5.2

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.
Files changed (2) hide show
  1. package/package.json +5 -5
  2. package/src/data.ts +19 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/decorator",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
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.5.0",
26
- "@leafer/data": "1.5.0",
27
- "@leafer/debug": "1.5.0"
25
+ "@leafer/platform": "1.5.2",
26
+ "@leafer/data": "1.5.2",
27
+ "@leafer/debug": "1.5.2"
28
28
  },
29
29
  "devDependencies": {
30
- "@leafer/interface": "1.5.0"
30
+ "@leafer/interface": "1.5.2"
31
31
  }
32
32
  }
package/src/data.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ILeafData, ILeaf, IObject, IValue, ILeafAttrDescriptor, ILeafAttrDescriptorFn, IValueFunction } from '@leafer/interface'
2
+ import { DataHelper, isEmptyData } from '@leafer/data'
2
3
  import { Debug } from '@leafer/debug'
3
4
 
4
5
  import { defineKey, getDescriptor } from './object'
@@ -275,30 +276,36 @@ export function defineDataProcessor(target: ILeaf, key: string, defaultValue?: I
275
276
  } else if (typeof defaultValue === 'function') {
276
277
  property.get = function () {
277
278
  let v = this[computedKey]
278
- if (v === undefined) this[computedKey] = v = defaultValue((this as ILeafData).__leaf)
279
- return v
279
+ return v === undefined ? defaultValue((this as ILeafData).__leaf) : v
280
+ }
281
+ } else if (typeof defaultValue === 'object') {
282
+ const isEmpty = isEmptyData(defaultValue)
283
+ property.get = function () {
284
+ let v = this[computedKey]
285
+ return v === undefined ? this[computedKey] = isEmpty ? {} : DataHelper.clone(defaultValue) : v
280
286
  }
281
287
  }
282
288
 
289
+ const isBox = target.isBranchLeaf // 仅用于 width / height
283
290
  if (key === 'width') {
284
291
  property.get = function () {
285
292
  const v = this[computedKey]
286
293
  if (v === undefined) {
287
- const t = this as ILeafData
288
- return (t as IObject)._height && t.__naturalWidth && t.__useNaturalRatio ? (t as IObject)._height * t.__naturalWidth / t.__naturalHeight : t.__naturalWidth || defaultValue
289
- } else {
290
- return v
291
- }
294
+ const t = this as ILeafData, naturalWidth = t.__naturalWidth, leaf = t.__leaf
295
+ if (!defaultValue || leaf.pathInputed) return leaf.boxBounds.width
296
+ if (naturalWidth) return (t as IObject)._height && t.__useNaturalRatio ? (t as IObject)._height * naturalWidth / t.__naturalHeight : naturalWidth
297
+ return (isBox && leaf.children.length) ? leaf.boxBounds.width : defaultValue // 返回 Box / Group / Text 等的实际宽度
298
+ } else return v
292
299
  }
293
300
  } else if (key === 'height') {
294
301
  property.get = function () {
295
302
  const v = this[computedKey]
296
303
  if (v === undefined) {
297
- const t = this as ILeafData
298
- return (t as IObject)._width && t.__naturalHeight && t.__useNaturalRatio ? (t as IObject)._width * t.__naturalHeight / t.__naturalWidth : t.__naturalHeight || defaultValue
299
- } else {
300
- return v
301
- }
304
+ const t = this as ILeafData, naturalHeight = t.__naturalHeight, leaf = t.__leaf
305
+ if (!defaultValue || leaf.pathInputed) return leaf.boxBounds.height
306
+ if (naturalHeight) return (t as IObject)._width && t.__useNaturalRatio ? (t as IObject)._width * naturalHeight / t.__naturalWidth : naturalHeight
307
+ return (isBox && leaf.children.length) ? leaf.boxBounds.height : defaultValue // 返回 Box / Group / Text 等的实际高度
308
+ } else return v
302
309
  }
303
310
  }
304
311