Match-id-14bd1b592a5b4fba4420cfbf2a18d92b33b5c237

This commit is contained in:
* 2022-03-29 17:58:41 +08:00 committed by *
parent 4afe0f75cd
commit f8fb9c5aef
1 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,59 @@
import { travelVNodeTree } from "../../../../libs/horizon/src/renderer/vnode/VNodeUtils";
import { VNode } from "../../../../libs/horizon/src/renderer/Types";
import { ClassComponent, FunctionComponent } from "../../../../libs/horizon/src/renderer/vnode/VNodeTags";
const VNodeToIdMap = new Map<VNode, number>();
const IdToVNodeMap = new Map<number, VNode>();
let uid = 0;
function generateUid () {
uid++;
return uid;
}
function isUserComponent(tag: string) {
// TODO: 添加其他组件
return tag === ClassComponent || tag === FunctionComponent;
}
function getParentUserComponent(node: VNode) {
let parent = node.parent;
while(parent) {
if (isUserComponent(parent.tag)) {
break;
}
parent = parent.parent;
}
return parent;
}
function parseTreeRoot(treeRoot: VNode) {
const result: any[] = [];
travelVNodeTree(treeRoot, (node: VNode) => {
const tag = node.tag;
if (isUserComponent(tag)) {
const id = generateUid();
result.push(id);
const name = (node.type as Function).name;
result.push(name);
const parent = getParentUserComponent(node);
if (parent) {
const parentId = VNodeToIdMap.get(parent);
result.push(parentId);
} else {
result.push('');
}
const key = node.key;
if (key !== null) {
result.push(key);
} else {
result.push('');
}
VNodeToIdMap.set(node, id);
IdToVNodeMap.set(id, node);
}
}, null, treeRoot, null);
return result;
}
export default parseTreeRoot;