-
Notifications
You must be signed in to change notification settings - Fork 19
/
data.ts
216 lines (183 loc) · 6.32 KB
/
data.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { ILeafData, ILeaf, IObject, __Value } from '@leafer/interface'
import { defineKey, getDescriptor } from './object'
// name
export function aliasType(name: string) {
return (target: ILeaf, key: string) => {
defineKey(target, key, {
get() { return this.__get(name) },
set(value: __Value) {
this.__set(name, value)
}
})
}
}
export function dataType(defaultValue?: __Value) {
return (target: ILeaf, key: string) => {
defineKey(target, key, {
get() { return this.__get(key) },
set(value: __Value) {
this.__set(key, value)
}
})
defineDataProcessor(target, key, defaultValue)
}
}
export function positionType(defaultValue?: __Value) {
return (target: ILeaf, key: string) => {
defineKey(target, key, {
get() { return this.__get(key) },
set(value: __Value) {
this.__set(key, value)
this.__layout.positionChanged || this.__layout.positionChange()
}
})
defineDataProcessor(target, key, defaultValue)
}
}
export function scaleType(defaultValue?: __Value) {
return (target: ILeaf, key: string) => {
defineKey(target, key, {
get() { return this.__get(key) },
set(value: __Value) {
this.__set(key, value)
this.__layout.scaleChanged || this.__layout.scaleChange()
}
})
defineDataProcessor(target, key, defaultValue)
}
}
export function rotationType(defaultValue?: __Value) {
return (target: ILeaf, key: string) => {
defineKey(target, key, {
get() { return this.__get(key) },
set(value: __Value) {
this.__set(key, value)
this.__layout.rotationChanged || this.__layout.rotationChange()
}
})
defineDataProcessor(target, key, defaultValue)
}
}
export function boundsType(defaultValue?: __Value) {
return (target: ILeaf, key: string) => {
defineKey(target, key, {
get() { return this.__get(key) },
set(value: __Value) {
this.__set(key, value)
this.__layout.boxBoundsChanged || this.__layout.boxBoundsChange()
}
})
defineDataProcessor(target, key, defaultValue)
}
}
export const pathType = boundsType
export function affectEventBoundsType(defaultValue?: __Value) {
return (target: ILeaf, key: string) => {
defineKey(target, key, {
get() { return this.__get(key) },
set(value: __Value) {
this.__set(key, value)
this.__layout.eventBoundsChanged || this.__layout.eventBoundsChange()
}
})
defineDataProcessor(target, key, defaultValue)
}
}
export function affectRenderBoundsType(defaultValue?: __Value) {
return (target: ILeaf, key: string) => {
defineKey(target, key, {
get() { return this.__get(key) },
set(value: __Value) {
this.__set(key, value)
this.__layout.renderBoundsChanged || this.__layout.renderBoundsChange()
}
})
defineDataProcessor(target, key, defaultValue)
}
}
export function surfaceType(defaultValue?: __Value) {
return (target: ILeaf, key: string) => {
defineKey(target, key, {
get() { return this.__get(key) },
set(value: __Value) {
this.__set(key, value)
this.__layout.surfaceChanged || this.__layout.surfaceChange()
}
})
defineDataProcessor(target, key, defaultValue)
}
}
export function opacityType(defaultValue?: __Value) {
return (target: ILeaf, key: string) => {
defineKey(target, key, {
get() { return this.__get(key) },
set(value: __Value) {
this.root ? this.__set(key, value) : this.__[key] = value
this.__layout.opacityChanged || this.__layout.opacityChange()
}
})
defineDataProcessor(target, key, defaultValue)
}
}
export function sortType(defaultValue?: __Value) {
return (target: ILeaf, key: string) => {
defineKey(target, key, {
get() { return this.__get(key) },
set(value: __Value) {
this.__set(key, value)
this.__layout.surfaceChanged || this.__layout.surfaceChange()
if (this.parent) {
this.parent.__layout.childrenSortChanged = true
} else {
this.__addParentWait(() => { this.parent.__layout.childrenSortChanged = true })
}
}
})
defineDataProcessor(target, key, defaultValue)
}
}
// get
export function dataProcessor(processor: IObject) {
return (target: ILeaf, _key: string) => {
defineKey(target, '__DataProcessor', {
get() { return processor }
})
}
}
export function layoutProcessor(processor: IObject) {
return (target: ILeaf, _key: string) => {
defineKey(target, '__LayoutProcessor', {
get() { return processor }
})
}
}
// other
function getSetMethodName(key: string): string {
return 'set' + key.charAt(0).toUpperCase() + key.slice(1)
}
// define leaf.__[key] getter/setter
export function defineDataProcessor(target: ILeaf, key: string, defaultValue: __Value): void {
const data = target.__DataProcessor.prototype as ILeafData
const computedKey = '_' + key
const setMethodName = getSetMethodName(key)
const property: IObject & ThisType<ILeafData> = {
get() {
const v = this[computedKey]
return v === undefined ? defaultValue : v
},
set(value: __Value) {
this[computedKey] = value
}
}
if (defaultValue === undefined) property.get = function () { return this[computedKey] }
const descriptor = getDescriptor(data, key)
if (descriptor) {
if (descriptor.set) property.set = descriptor.set // use custom set
if (descriptor.get) property.get = descriptor.get // use custom get
}
if (data[setMethodName]) {
property.set = data[setMethodName] // use custom setKey(value)
delete data[setMethodName]
}
Object.defineProperty(data, key, property)
}