@leafer/decorator 1.0.0-alpha.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023-present, Chao (Leafer) Wan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @leafer/decorator
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@leafer/decorator",
3
+ "version": "1.0.0-alpha.1",
4
+ "description": "@leafer/decorator",
5
+ "author": "Chao (Leafer) Wan",
6
+ "license": "MIT",
7
+ "main": "src/index.ts",
8
+ "files": ["src"],
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/leaferjs/leafer.git"
12
+ },
13
+ "homepage": "https://github.com/leaferjs/leafer/tree/main/packages/decorator",
14
+ "bugs": "https://github.com/leaferjs/leafer/issues",
15
+ "keywords": [
16
+ "leafer",
17
+ "leaferjs"
18
+ ],
19
+ "dependencies": {
20
+ "@leafer/platform": "1.0.0-alpha.1",
21
+ "@leafer/debug": "1.0.0-alpha.1"
22
+ },
23
+ "devDependencies": {
24
+ "@leafer/interface": "1.0.0-alpha.1"
25
+ }
26
+ }
package/src/class.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { IObject } from '@leafer/interface'
2
+ import { EventCreator, UICreator } from '@leafer/platform'
3
+
4
+ export function registerUI() {
5
+ return (target: IObject) => {
6
+ UICreator.register(target)
7
+ }
8
+ }
9
+
10
+ export function registerEvent() {
11
+ return (target: IObject) => {
12
+ EventCreator.register(target)
13
+ }
14
+ }
package/src/data.ts ADDED
@@ -0,0 +1,216 @@
1
+ import { ILeafData, ILeaf, IObject, __Value } from '@leafer/interface'
2
+ import { defineKey, getDescriptor } from './object'
3
+
4
+ // name
5
+
6
+ export function aliasType(name: string) {
7
+ return (target: ILeaf, key: string) => {
8
+ defineKey(target, key, {
9
+ get() { return this.__get(name) },
10
+ set(value: __Value) {
11
+ this.root ? this.__set(name, value) : this.__[name] = value
12
+ }
13
+ })
14
+ }
15
+ }
16
+
17
+ export function dataType(defaultValue?: __Value) {
18
+ return (target: ILeaf, key: string) => {
19
+ defineKey(target, key, {
20
+ get() { return this.__get(key) },
21
+ set(value: __Value) {
22
+ this.root ? this.__set(key, value) : this.__[key] = value
23
+ }
24
+ })
25
+ defineDataProcessor(target, key, defaultValue)
26
+ }
27
+ }
28
+
29
+ export function positionType(defaultValue?: __Value) {
30
+ return (target: ILeaf, key: string) => {
31
+ defineKey(target, key, {
32
+ get() { return this.__get(key) },
33
+ set(value: __Value) {
34
+ this.root ? this.__set(key, value) : this.__[key] = value
35
+ this.__layout.positionChanged || this.__layout.positionChange()
36
+ }
37
+ })
38
+ defineDataProcessor(target, key, defaultValue)
39
+ }
40
+ }
41
+
42
+ export function scaleType(defaultValue?: __Value) {
43
+ return (target: ILeaf, key: string) => {
44
+ defineKey(target, key, {
45
+ get() { return this.__get(key) },
46
+ set(value: __Value) {
47
+ this.root ? this.__set(key, value) : this.__[key] = value
48
+ this.__layout.scaleChanged || this.__layout.scaleChange()
49
+ }
50
+ })
51
+ defineDataProcessor(target, key, defaultValue)
52
+ }
53
+ }
54
+
55
+ export function rotationType(defaultValue?: __Value) {
56
+ return (target: ILeaf, key: string) => {
57
+ defineKey(target, key, {
58
+ get() { return this.__get(key) },
59
+ set(value: __Value) {
60
+ this.root ? this.__set(key, value) : this.__[key] = value
61
+ this.__layout.rotationChanged || this.__layout.rotationChange()
62
+
63
+ }
64
+ })
65
+ defineDataProcessor(target, key, defaultValue)
66
+ }
67
+ }
68
+
69
+ export function boundsType(defaultValue?: __Value) {
70
+ return (target: ILeaf, key: string) => {
71
+ defineKey(target, key, {
72
+ get() { return this.__get(key) },
73
+ set(value: __Value) {
74
+ this.root ? this.__set(key, value) : this.__[key] = value
75
+ this.__layout.boxBoundsChanged || this.__layout.boxBoundsChange()
76
+ }
77
+ })
78
+ defineDataProcessor(target, key, defaultValue)
79
+ }
80
+ }
81
+
82
+ export const pathType = boundsType
83
+
84
+
85
+ export function affectEventBoundsType(defaultValue?: __Value) {
86
+ return (target: ILeaf, key: string) => {
87
+ defineKey(target, key, {
88
+ get() { return this.__get(key) },
89
+ set(value: __Value) {
90
+ this.root ? this.__set(key, value) : this.__[key] = value
91
+ this.__layout.eventBoundsChanged || this.__layout.eventBoundsChange()
92
+ }
93
+ })
94
+ defineDataProcessor(target, key, defaultValue)
95
+ }
96
+ }
97
+
98
+ export function affectRenderBoundsType(defaultValue?: __Value) {
99
+ return (target: ILeaf, key: string) => {
100
+ defineKey(target, key, {
101
+ get() { return this.__get(key) },
102
+ set(value: __Value) {
103
+ this.root ? this.__set(key, value) : this.__[key] = value
104
+ this.__layout.renderBoundsChanged || this.__layout.renderBoundsChange()
105
+ }
106
+ })
107
+ defineDataProcessor(target, key, defaultValue)
108
+ }
109
+ }
110
+
111
+ export function surfaceType(defaultValue?: __Value) {
112
+ return (target: ILeaf, key: string) => {
113
+ defineKey(target, key, {
114
+ get() { return this.__get(key) },
115
+ set(value: __Value) {
116
+ this.root ? this.__set(key, value) : this.__[key] = value
117
+ this.__layout.surfaceChanged || this.__layout.surfaceChange()
118
+ }
119
+ })
120
+ defineDataProcessor(target, key, defaultValue)
121
+ }
122
+ }
123
+
124
+ export function opacityType(defaultValue?: __Value) {
125
+ return (target: ILeaf, key: string) => {
126
+ defineKey(target, key, {
127
+ get() { return this.__get(key) },
128
+ set(value: __Value) {
129
+ this.root ? this.__set(key, value) : this.__[key] = value
130
+ this.__layout.opacityChanged || this.__layout.opacityChange()
131
+ }
132
+ })
133
+ defineDataProcessor(target, key, defaultValue)
134
+ }
135
+ }
136
+
137
+ export function sortType(defaultValue?: __Value) {
138
+ return (target: ILeaf, key: string) => {
139
+ defineKey(target, key, {
140
+ get() { return this.__get(key) },
141
+ set(value: __Value) {
142
+ this.root ? this.__set(key, value) : this.__[key] = value
143
+ this.__layout.surfaceChanged || this.__layout.surfaceChange()
144
+
145
+ if (this.parent) {
146
+ this.parent.__layout.childrenSortChanged || (this.parent.__layout.childrenSortChanged = true)
147
+ } else {
148
+ this.__addParentWait(() => { this.parent.__layout.childrenSortChanged || (this.parent.__layout.childrenSortChanged = true) })
149
+ }
150
+ }
151
+ })
152
+ defineDataProcessor(target, key, defaultValue)
153
+ }
154
+ }
155
+
156
+
157
+ // get
158
+
159
+ export function dataProcessor(processor: IObject) {
160
+ return (target: ILeaf, key: string) => {
161
+ defineKey(target, '__DataProcessor', {
162
+ get() { return processor }
163
+ })
164
+ }
165
+ }
166
+
167
+ export function layoutProcessor(processor: IObject) {
168
+ return (target: ILeaf, key: string) => {
169
+ defineKey(target, '__LayoutProcessor', {
170
+ get() { return processor }
171
+ })
172
+ }
173
+ }
174
+
175
+
176
+ // other
177
+
178
+ function getSetMethodName(key: string): string {
179
+ return 'set' + key.charAt(0).toUpperCase() + key.slice(1)
180
+ }
181
+
182
+ // define leaf.__[key] getter/setter
183
+ export function defineDataProcessor(target: ILeaf, key: string, defaultValue: __Value): void {
184
+
185
+ const data = target.__DataProcessor.prototype as ILeafData
186
+ const computedKey = '_' + key
187
+ const setMethodName = getSetMethodName(key)
188
+
189
+ const property: IObject & ThisType<ILeafData> = {
190
+ get() {
191
+ const v = this[computedKey]
192
+ return v === undefined ? defaultValue : v
193
+ },
194
+ set(value: __Value) {
195
+ this[computedKey] = value
196
+ }
197
+ }
198
+
199
+ if (defaultValue === undefined) property.get = function () { return this[computedKey] }
200
+
201
+ const descriptor = getDescriptor(data, key)
202
+
203
+ if (descriptor) {
204
+ if (descriptor.set) property.set = descriptor.set // use custom set
205
+ if (descriptor.get) property.get = descriptor.get // use custom get
206
+ }
207
+
208
+ if (data[setMethodName]) {
209
+ property.set = data[setMethodName] // use custom setKey(value)
210
+ delete data[setMethodName]
211
+ }
212
+
213
+ Object.defineProperty(data, key, property)
214
+
215
+ }
216
+
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export { aliasType, dataType, positionType, boundsType, affectEventBoundsType, affectRenderBoundsType, scaleType, rotationType, surfaceType, opacityType, sortType, pathType, dataProcessor, layoutProcessor, defineDataProcessor } from './data'
2
+ export { useModule, rewrite, rewriteAble } from './rewrite'
3
+ export { defineKey, getDescriptor } from './object'
4
+ export { registerUI, registerEvent } from './class'
package/src/object.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { IObject } from '@leafer/interface'
2
+
3
+ export function defineKey<T>(target: T, key: string, descriptor: IObject & ThisType<T>): void {
4
+ Object.defineProperty(target, key, descriptor)
5
+ }
6
+
7
+ export function getDescriptor(object: IObject, name: string) {
8
+ return Object.getOwnPropertyDescriptor(object, name)
9
+ }
10
+
11
+ export function getNames(object: IObject): string[] {
12
+ return Object.getOwnPropertyNames(object)
13
+ }
14
+
package/src/rewrite.ts ADDED
@@ -0,0 +1,59 @@
1
+ import { IObject, IFunction } from '@leafer/interface'
2
+ import { Debug } from '@leafer/debug'
3
+ import { getDescriptor, getNames } from './object'
4
+
5
+ interface IRewriteItem {
6
+ name: string
7
+ run: IFunction
8
+ }
9
+
10
+ const debug = new Debug('rewrite')
11
+
12
+ const list: IRewriteItem[] = []
13
+ const excludeNames = ['destroy', 'constructor']
14
+
15
+
16
+ // method
17
+
18
+ export function rewrite(method: IFunction) {
19
+ return (target: IObject, key: string) => {
20
+ list.push({ name: target.constructor.name + '.' + key, run: () => { target[key] = method } })
21
+ }
22
+ }
23
+
24
+ export function rewriteAble() {
25
+ return (target: IObject) => {
26
+ doRewrite()
27
+ }
28
+ }
29
+
30
+ function doRewrite(error?: boolean): void {
31
+ if (list.length) {
32
+ list.forEach(item => {
33
+ if (error) debug.error(item.name, '需在Class上装饰@rewriteAble()')
34
+ item.run()
35
+ })
36
+ list.length = 0
37
+ }
38
+ }
39
+
40
+ setTimeout(() => doRewrite(true))
41
+
42
+
43
+ // class
44
+
45
+ export function useModule(child: IObject) {
46
+ return (target: IObject) => {
47
+ const names = child.prototype ? getNames(child.prototype) : Object.keys(child)
48
+ names.forEach(name => {
49
+ if (!excludeNames.includes(name)) {
50
+ if (child.prototype) {
51
+ const d = getDescriptor(child.prototype, name)
52
+ if (d.writable) target.prototype[name] = child.prototype[name]
53
+ } else {
54
+ target.prototype[name] = child[name]
55
+ }
56
+ }
57
+ })
58
+ }
59
+ }