编辑器体验改进
- 导入文件后直接关闭弹窗 + toast 提示导入数量 - 自动布局改为按出口顺序的子树居中:选项1/2/3 分支顺序正确且对齐
This commit is contained in:
@ -91,21 +91,36 @@
|
||||
(ir.nodes || []).forEach(n => nodes[n.id] = n);
|
||||
(ir.endings || []).forEach(e => nodes[e.id] = e);
|
||||
const ids = Object.keys(nodes);
|
||||
// 层级:最长路径(决定横向 x,左→右)
|
||||
const edges = [];
|
||||
(ir.nodes || []).forEach(n => rawTargets(n).forEach(t => { if (t && nodes[t]) edges.push([n.id, t]); }));
|
||||
ids.forEach(id => rawTargets(nodes[id]).forEach(t => { if (t && nodes[t]) edges.push([id, t]); }));
|
||||
const layer = {}; ids.forEach(id => layer[id] = 0);
|
||||
let changed = true, guard = 0;
|
||||
while (changed && guard++ < 9999) {
|
||||
changed = false;
|
||||
edges.forEach(([u, v]) => { if (layer[v] < layer[u] + 1) { layer[v] = layer[u] + 1; changed = true; } });
|
||||
}
|
||||
const perLayer = {}, pos = {};
|
||||
ids.forEach(id => {
|
||||
const l = layer[id]; perLayer[l] = perLayer[l] || 0;
|
||||
// 横向分层(左→右),契合 Drawflow 端口左右朝向
|
||||
pos[id] = { x: 40 + l * COL, y: 30 + perLayer[l] * ROW };
|
||||
perLayer[l]++;
|
||||
});
|
||||
// 有序子表(按出口顺序,去重)+ 入度求根
|
||||
const childMap = {}, indeg = {}; ids.forEach(id => { childMap[id] = []; indeg[id] = 0; });
|
||||
const seen = new Set();
|
||||
ids.forEach(id => rawTargets(nodes[id]).forEach(t => {
|
||||
if (t && nodes[t]) { const k = id + ">" + t; if (!seen.has(k)) { seen.add(k); childMap[id].push(t); indeg[t]++; } }
|
||||
}));
|
||||
let roots = ids.filter(id => indeg[id] === 0);
|
||||
if (!roots.length && ids.length) roots = [ids[0]];
|
||||
// 子树居中:按出口顺序递归分配纵向 y,父节点居子范围中点 → 选项1上/2中/3下且对齐
|
||||
const ypos = {}; let nextY = 0; const vis = new Set();
|
||||
function assignY(id) {
|
||||
if (!id || vis.has(id)) return; vis.add(id);
|
||||
if (!childMap[id].length) { ypos[id] = nextY; nextY += ROW; return; }
|
||||
childMap[id].forEach(assignY);
|
||||
const placed = childMap[id].map(c => ypos[c]).filter(v => v !== undefined);
|
||||
ypos[id] = placed.length ? (Math.min(...placed) + Math.max(...placed)) / 2 : (nextY += ROW, nextY - ROW);
|
||||
}
|
||||
roots.forEach(assignY);
|
||||
ids.forEach(id => { if (ypos[id] === undefined) { ypos[id] = nextY; nextY += ROW; } });
|
||||
const pos = {};
|
||||
ids.forEach(id => { pos[id] = { x: 40 + layer[id] * COL, y: 30 + ypos[id] }; });
|
||||
return pos;
|
||||
}
|
||||
function ensureLayout(ir) {
|
||||
|
||||
Reference in New Issue
Block a user