@leafer/data 1.0.0-beta.15 → 1.0.0-beta.16

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,13 @@
1
1
  {
2
2
  "name": "@leafer/data",
3
- "version": "1.0.0-beta.15",
3
+ "version": "1.0.0-beta.16",
4
4
  "description": "@leafer/data",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
7
7
  "main": "src/index.ts",
8
8
  "types": "types/index.d.ts",
9
9
  "files": [
10
+ "src",
10
11
  "types",
11
12
  "dist"
12
13
  ],
@@ -21,6 +22,6 @@
21
22
  "leaferjs"
22
23
  ],
23
24
  "devDependencies": {
24
- "@leafer/interface": "1.0.0-beta.15"
25
+ "@leafer/interface": "1.0.0-beta.16"
25
26
  }
26
27
  }
@@ -0,0 +1,37 @@
1
+ import { IObject } from '@leafer/interface'
2
+
3
+
4
+ export const DataHelper = {
5
+
6
+ default<T>(t: T, defaultData: IObject): T {
7
+ assign(defaultData, t)
8
+ assign(t, defaultData)
9
+ return t
10
+ },
11
+
12
+ assign(t: IObject, merge: IObject): void {
13
+ let value: unknown
14
+ Object.keys(merge).forEach(key => {
15
+ value = merge[key]
16
+ if (value?.constructor === Object) {
17
+ (t[key]?.constructor === Object) ? assign(t[key], merge[key]) : t[key] = merge[key]
18
+ } else {
19
+ t[key] = merge[key]
20
+ }
21
+ })
22
+ },
23
+
24
+ copyAttrs(t: IObject, from: IObject, include: string[]): IObject {
25
+ include.forEach(key => {
26
+ if (from[key] !== undefined) t[key] = from[key]
27
+ })
28
+ return t
29
+ },
30
+
31
+ clone(data: unknown): IObject {
32
+ return JSON.parse(JSON.stringify(data))
33
+ }
34
+
35
+ }
36
+
37
+ const { assign } = DataHelper
@@ -0,0 +1,82 @@
1
+ import { ILeafData, ILeaf, IObject, __Value } from '@leafer/interface'
2
+
3
+
4
+ export class LeafData implements ILeafData {
5
+
6
+ public __leaf: ILeaf
7
+ public __input: IObject
8
+ public __middle: IObject
9
+
10
+ public __single: boolean
11
+
12
+ public __naturalWidth?: number
13
+ public __naturalHeight?: number
14
+
15
+ constructor(leaf: ILeaf) {
16
+ this.__leaf = leaf
17
+ }
18
+
19
+ public __get(name: string): any {
20
+ if (this.__input) {
21
+ const value = this.__input[name]
22
+ if (value !== undefined) return value
23
+ }
24
+ return (this as IObject)[name]
25
+ }
26
+
27
+ public __setInput(name: string, value: any): void {
28
+ this.__input || (this.__input = {})
29
+ this.__input[name] = value
30
+ }
31
+
32
+ public __getInput(name: string): any {
33
+ if (this.__input) {
34
+ const value = this.__input[name]
35
+ if (value !== undefined) return value
36
+ }
37
+ return (this as IObject)['_' + name]
38
+ }
39
+
40
+ public __removeInput(name: string): void {
41
+ if (this.__input && this.__input[name] !== undefined) this.__input[name] = undefined
42
+ }
43
+
44
+ public __getInputData(): IObject {
45
+ const data: IObject = { tag: this.__leaf.tag }, { __input } = this
46
+ let realKey: string, value: __Value
47
+
48
+ for (let key in this) {
49
+ realKey = key.substring(1)
50
+ if ((this as any)[realKey] !== undefined) {
51
+ value = __input ? __input[realKey] : undefined
52
+ data[realKey] = value === undefined ? this[key] : value
53
+ }
54
+ }
55
+ return data
56
+ }
57
+
58
+ public __setMiddle(name: string, value: any): void {
59
+ this.__middle || (this.__middle = {})
60
+ this.__middle[name] = value
61
+ }
62
+
63
+ public __getMiddle(name: string): any {
64
+ return this.__middle && this.__middle[name]
65
+ }
66
+
67
+ public __checkSingle(): void {
68
+ if ((this as ILeafData).blendMode === 'pass-through') {
69
+ if (this.__leaf.__hasEraser || (this as ILeafData).isEraser) {
70
+ this.__single = true
71
+ } else if (this.__single) {
72
+ this.__single = false
73
+ }
74
+ } else {
75
+ this.__single = true
76
+ }
77
+ }
78
+
79
+ public destroy(): void {
80
+ this.__input = this.__middle = null
81
+ }
82
+ }