@leafer/decorator 1.5.1 → 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 +18 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/decorator",
3
- "version": "1.5.1",
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.1",
26
- "@leafer/data": "1.5.1",
27
- "@leafer/debug": "1.5.1"
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.1"
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'
@@ -277,27 +278,34 @@ export function defineDataProcessor(target: ILeaf, key: string, defaultValue?: I
277
278
  let v = this[computedKey]
278
279
  return v === undefined ? defaultValue((this as ILeafData).__leaf) : v
279
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
286
+ }
280
287
  }
281
288
 
289
+ const isBox = target.isBranchLeaf // 仅用于 width / height
282
290
  if (key === 'width') {
283
291
  property.get = function () {
284
292
  const v = this[computedKey]
285
293
  if (v === undefined) {
286
- const t = this as ILeafData
287
- return (t as IObject)._height && t.__naturalWidth && t.__useNaturalRatio ? (t as IObject)._height * t.__naturalWidth / t.__naturalHeight : t.__naturalWidth || defaultValue
288
- } else {
289
- return v
290
- }
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
291
299
  }
292
300
  } else if (key === 'height') {
293
301
  property.get = function () {
294
302
  const v = this[computedKey]
295
303
  if (v === undefined) {
296
- const t = this as ILeafData
297
- return (t as IObject)._width && t.__naturalHeight && t.__useNaturalRatio ? (t as IObject)._width * t.__naturalHeight / t.__naturalWidth : t.__naturalHeight || defaultValue
298
- } else {
299
- return v
300
- }
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
301
309
  }
302
310
  }
303
311