gaoyakang / online_logisim

仿制 logisim 的在线网站版本
https://mrx.wiki/
MIT License
3 stars 0 forks source link

restorage时edge丢失 #3

Closed gaoyakang closed 2 hours ago

gaoyakang commented 2 hours ago

online_logisim/src/core/nodes

// 序列化节点数据
function saveTreeToLocalStorage(treeData: { lf?: LogicFlow; treeNode: TreeNode[]; }) {
  // ...
  const edgesArray = getEdgesFromTree(treeData.treeNode); // 从树结构中提取边的信息
  // ...
}

// 从树结构中提取边的信息
function getEdgesFromTree(treeNodes: TreeNode[], parent?: TreeNode): any[] {
  return treeNodes.reduce((edges:EdgeType[], node) => {
    if (parent) {
      edges.push({ sourceNodeId: node.id, targetNodeId: parent.id, type: "bezier" });
    }
    node.children.forEach((child: TreeNode) => {
      edges = edges.concat(getEdgesFromTree([child], node));
    });
    return edges;
  }, []);
}

以上代码会下面这种case中的edge丢失

截屏2024-11-18 17 47 05
gaoyakang commented 2 hours ago

可以在buildTree时直接将整体edges传过来,不用再从nodes中解析边了

gaoyakang commented 2 hours ago

已修复:https://github.com/gaoyakang/online_logisim/commit/baedaf2db4cc1978aac1e94c341b0793396190db