Match-id-093ef93954199cd2cfffed7fd140bbac91c716d9
This commit is contained in:
parent
f76a09544d
commit
d7a211a45a
|
@ -19,8 +19,11 @@ import {
|
||||||
postMessageToBackground, removeBackgroundMessageListener,
|
postMessageToBackground, removeBackgroundMessageListener,
|
||||||
} from '../panelConnection';
|
} from '../panelConnection';
|
||||||
import { IAttr } from '../parser/parseAttr';
|
import { IAttr } from '../parser/parseAttr';
|
||||||
|
import { createLogger } from '../utils/logUtil';
|
||||||
|
|
||||||
const parseVNodeData = (rawData) => {
|
const logger = createLogger('panelApp');
|
||||||
|
|
||||||
|
const parseVNodeData = (rawData, idToTreeNodeMap , nextIdToTreeNodeMap) => {
|
||||||
const idIndentationMap: {
|
const idIndentationMap: {
|
||||||
[id: string]: number;
|
[id: string]: number;
|
||||||
} = {};
|
} = {};
|
||||||
|
@ -37,10 +40,18 @@ const parseVNodeData = (rawData) => {
|
||||||
i++;
|
i++;
|
||||||
const indentation = parentId === '' ? 0 : idIndentationMap[parentId] + 1;
|
const indentation = parentId === '' ? 0 : idIndentationMap[parentId] + 1;
|
||||||
idIndentationMap[id] = indentation;
|
idIndentationMap[id] = indentation;
|
||||||
const item = {
|
const lastItem = idToTreeNodeMap[id];
|
||||||
id, name, indentation, userKey
|
if (lastItem) {
|
||||||
};
|
// 由于 diff 算法限制,一个 vNode 的 name,userKey,indentation 属性不会发生变化
|
||||||
data.push(item);
|
nextIdToTreeNodeMap[id] = lastItem;
|
||||||
|
data.push(lastItem);
|
||||||
|
} else {
|
||||||
|
const item = {
|
||||||
|
id, name, indentation, userKey
|
||||||
|
};
|
||||||
|
nextIdToTreeNodeMap[id] = item;
|
||||||
|
data.push(item);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
@ -62,15 +73,19 @@ const getParents = (item: IData | null, parsedVNodeData: IData[]) => {
|
||||||
return parents;
|
return parents;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
interface IIdToNodeMap {
|
||||||
|
[id: number]: IData;
|
||||||
|
}
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [parsedVNodeData, setParsedVNodeData] = useState([]);
|
const [parsedVNodeData, setParsedVNodeData] = useState([]);
|
||||||
const [componentAttrs, setComponentAttrs] = useState<{
|
const [componentAttrs, setComponentAttrs] = useState<{
|
||||||
parsedProps?: IAttr[],
|
parsedProps?: IAttr[],
|
||||||
parsedState?: IAttr[],
|
parsedState?: IAttr[],
|
||||||
parsedHooks?: IAttr[],
|
parsedHooks?: IAttr[],
|
||||||
}>();
|
}>({});
|
||||||
const [selectComp, setSelectComp] = useState(null);
|
const [selectComp, setSelectComp] = useState(null);
|
||||||
const treeRootInfos = useRef<{id: number, length: number}[]>([]); // 记录保存的根节点 id 和长度,
|
const idToTreeNodeMapRef = useRef<IIdToNodeMap>({});
|
||||||
|
|
||||||
const {
|
const {
|
||||||
filterValue,
|
filterValue,
|
||||||
|
@ -87,7 +102,9 @@ function App() {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isDev) {
|
if (isDev) {
|
||||||
const parsedData = parseVNodeData(mockParsedVNodeData);
|
const nextIdToTreeNodeMap: IIdToNodeMap = {};
|
||||||
|
const parsedData = parseVNodeData(mockParsedVNodeData, idToTreeNodeMapRef.current, nextIdToTreeNodeMap);
|
||||||
|
idToTreeNodeMapRef.current = nextIdToTreeNodeMap;
|
||||||
setParsedVNodeData(parsedData);
|
setParsedVNodeData(parsedData);
|
||||||
setComponentAttrs({
|
setComponentAttrs({
|
||||||
parsedProps: parsedMockState,
|
parsedProps: parsedMockState,
|
||||||
|
@ -96,19 +113,18 @@ function App() {
|
||||||
} else {
|
} else {
|
||||||
const handleBackgroundMessage = (message) => {
|
const handleBackgroundMessage = (message) => {
|
||||||
const { payload } = message;
|
const { payload } = message;
|
||||||
|
// 对象数据只是记录了引用,内容可能在后续被修改,打印字符串可以获取当前真正内容,不被后续修改影响
|
||||||
|
logger.info(JSON.stringify(payload));
|
||||||
if (payload) {
|
if (payload) {
|
||||||
const { type, data } = payload;
|
const { type, data } = payload;
|
||||||
if (type === AllVNodeTreesInfos) {
|
if (type === AllVNodeTreesInfos) {
|
||||||
|
const idToTreeNodeMap = idToTreeNodeMapRef.current;
|
||||||
|
const nextIdToTreeNodeMap: IIdToNodeMap = {};
|
||||||
const allTreeData = data.reduce((pre, current) => {
|
const allTreeData = data.reduce((pre, current) => {
|
||||||
const parsedTreeData = parseVNodeData(current);
|
const parsedTreeData = parseVNodeData(current, idToTreeNodeMap, nextIdToTreeNodeMap);
|
||||||
const length = parsedTreeData.length;
|
|
||||||
treeRootInfos.current.length = 0;
|
|
||||||
if (length) {
|
|
||||||
const treeRoot = parsedTreeData[0];
|
|
||||||
treeRootInfos.current.push({id: treeRoot.id, length: length});
|
|
||||||
}
|
|
||||||
return pre.concat(parsedTreeData);
|
return pre.concat(parsedTreeData);
|
||||||
}, []);
|
}, []);
|
||||||
|
idToTreeNodeMapRef.current = nextIdToTreeNodeMap;
|
||||||
setParsedVNodeData(allTreeData);
|
setParsedVNodeData(allTreeData);
|
||||||
} else if (type === ComponentAttrs) {
|
} else if (type === ComponentAttrs) {
|
||||||
const {parsedProps, parsedState, parsedHooks} = data;
|
const {parsedProps, parsedState, parsedHooks} = data;
|
||||||
|
@ -120,7 +136,6 @@ function App() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
console.log('handle connection');
|
|
||||||
// 在页面渲染后初始化连接
|
// 在页面渲染后初始化连接
|
||||||
initBackgroundConnection();
|
initBackgroundConnection();
|
||||||
// 监听 background消息
|
// 监听 background消息
|
||||||
|
|
Loading…
Reference in New Issue