@cc-component/cc-ex-component 1.6.4 → 1.6.6
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/assets/core/ViewSearch.ts +125 -0
- package/assets/core/ViewSearch.ts.meta +9 -0
- package/index.ts +3 -0
- package/package.json +1 -1
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { error, isValid, js } from "cc";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
const inject_vsearch = function (key, targetDataBinding) {
|
|
5
|
+
let propertys = targetDataBinding["vsearch"] || [];
|
|
6
|
+
if (propertys.length == 0) return;
|
|
7
|
+
|
|
8
|
+
let allNodeMap = new Map();
|
|
9
|
+
vsearchHandler(this.node, allNodeMap);
|
|
10
|
+
for (const property of propertys) {
|
|
11
|
+
let className = property.className;
|
|
12
|
+
let tag = property.tag || property.name;
|
|
13
|
+
|
|
14
|
+
let instance = allNodeMap.get(tag);
|
|
15
|
+
if (className.prototype.__classname__ != "cc.Node") {
|
|
16
|
+
instance = instance?.getComponent(className) ?? instance?.addComponent(className);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (instance == null) {
|
|
20
|
+
console.error(`没有找到属性 ${key}->${tag}`);
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
this[property.name] = instance;
|
|
24
|
+
}
|
|
25
|
+
allNodeMap = null;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const dataBindingContainer = new WeakMap();
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
export function ViewSearch(target: any) {
|
|
32
|
+
const onLoad = target.prototype.onLoad;
|
|
33
|
+
/**
|
|
34
|
+
* 重载onload 注入更新
|
|
35
|
+
*/
|
|
36
|
+
target.prototype.onLoad = function () {
|
|
37
|
+
let classKey = js.getClassName(target);
|
|
38
|
+
let targetDataBinding = dataBindingContainer.get(target.prototype) || {};
|
|
39
|
+
//console.log("vm onload111", classKey, targetDataBinding);
|
|
40
|
+
inject_vsearch.call(this, classKey, targetDataBinding);
|
|
41
|
+
//调用之前的onload
|
|
42
|
+
onLoad?.call(this);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function vsearchHandler(parent: any, map: Map<string, Node>) {
|
|
47
|
+
map.set(parent.name, parent);
|
|
48
|
+
let children = parent.children;
|
|
49
|
+
for (const key in children) {
|
|
50
|
+
if (!isValid(children[key])) continue;
|
|
51
|
+
vsearchHandler(children[key], map);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 通过 属性名称 或者标签查询对应的node 或组件
|
|
58
|
+
* @example
|
|
59
|
+
* @vsearch(Label) //查找名节点称为icon的Label组件
|
|
60
|
+
* icon:Label = null;
|
|
61
|
+
*
|
|
62
|
+
* @vsearch(Label,"aa") //查找名节点称为aa的Label组件,并赋值给version
|
|
63
|
+
* version:Label = null;
|
|
64
|
+
*
|
|
65
|
+
* @param className 组件名称
|
|
66
|
+
* @param tag 名称
|
|
67
|
+
* @returns
|
|
68
|
+
*/
|
|
69
|
+
export function vsearch(className: any, tag?: string) {
|
|
70
|
+
return (target: any, propertyKey: string) => {
|
|
71
|
+
let targetDataBinding = dataBindingContainer.get(target) || {};
|
|
72
|
+
let propertys = targetDataBinding["vsearch"] || [];
|
|
73
|
+
propertys.push({ tag: tag, name: propertyKey, className: className });
|
|
74
|
+
targetDataBinding["vsearch"] = propertys;
|
|
75
|
+
dataBindingContainer.set(target, targetDataBinding);
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
// export function BindUI(target: any, key: string, callback: (key: string, value: any) => void, superPath?: string) {
|
|
81
|
+
// const privateKey = `__${key}`;
|
|
82
|
+
// target[privateKey] = target[key];
|
|
83
|
+
// if (!superPath) {
|
|
84
|
+
// superPath = key;
|
|
85
|
+
// } else {
|
|
86
|
+
// superPath += `.${key}`;
|
|
87
|
+
// }
|
|
88
|
+
|
|
89
|
+
// Object.defineProperty(target, key, {
|
|
90
|
+
// get() {
|
|
91
|
+
// return this[privateKey];
|
|
92
|
+
// },
|
|
93
|
+
// set(value) {
|
|
94
|
+
// const path = superPath;
|
|
95
|
+
// this[privateKey] = value;
|
|
96
|
+
// if (callback) {
|
|
97
|
+
// callback(path, value);
|
|
98
|
+
// }
|
|
99
|
+
// },
|
|
100
|
+
// enumerable: true,
|
|
101
|
+
// configurable: true,
|
|
102
|
+
// });
|
|
103
|
+
|
|
104
|
+
// // **跳过数组,避免递归绑定数组**
|
|
105
|
+
// if (target[key] && typeof target[key] === "object" && !Array.isArray(target[key])) {
|
|
106
|
+
// BindViewModel(target[key], callback, superPath);
|
|
107
|
+
// }
|
|
108
|
+
// }
|
|
109
|
+
|
|
110
|
+
// export function BindViewModel(viewModel: any, callback: (key: string, value: any) => void, superPath?: string) {
|
|
111
|
+
// Object.keys(viewModel).forEach((key) => {
|
|
112
|
+
// if (!key.includes("$")) {
|
|
113
|
+
// BindUI(viewModel, key, callback, superPath);
|
|
114
|
+
// }
|
|
115
|
+
// });
|
|
116
|
+
// }
|
|
117
|
+
|
|
118
|
+
// export interface IViewModel {
|
|
119
|
+
// /**数据 */
|
|
120
|
+
// viewModel: any
|
|
121
|
+
// vmMessage: Map<string, any>;
|
|
122
|
+
// /**状态改变 */
|
|
123
|
+
// updateState(key: string, value: any);
|
|
124
|
+
// }
|
|
125
|
+
|
package/index.ts
CHANGED
|
@@ -6,6 +6,8 @@ export * from './assets/core/ReferenceComponent';
|
|
|
6
6
|
export * from './assets/core/BaseReference';
|
|
7
7
|
export * from './assets/core/BaseViewModelData';
|
|
8
8
|
export * from './assets/core/BaseWindow';
|
|
9
|
+
export * from './assets/core/ViewSearch';
|
|
10
|
+
|
|
9
11
|
export * from './assets/interface/Interface';
|
|
10
12
|
export * from './assets/interface/ExComponentModule';
|
|
11
13
|
|
|
@@ -13,4 +15,5 @@ export * from './assets/interface/ExComponentModule';
|
|
|
13
15
|
export * from './assets/lib/tableView/TableView';
|
|
14
16
|
export * from './assets/lib/collectView/lib_collect/yx-collection-view';
|
|
15
17
|
export * from './assets/lib/collectView/lib_collect/yx-flow-layout';
|
|
18
|
+
export * from './assets/lib/collectView/lib_collect/yx-flow-layout';
|
|
16
19
|
|