From 7b8181ca8b79c01c36d518455159e615a228b477 Mon Sep 17 00:00:00 2001 From: huhaojie Date: Sun, 18 Feb 2024 11:41:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=B4=A7=E5=87=91=E6=A0=91=E5=B8=83?= =?UTF-8?q?=E5=B1=80=E5=9C=A8=E5=88=A4=E6=96=AD=E6=98=AF=E5=90=A6=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E7=A7=BB=E5=8A=A8=E5=AD=90=E8=8A=82=E7=82=B9=E6=97=B6?= =?UTF-8?q?=E5=8F=AA=E5=88=A4=E6=96=AD=E4=BA=86=E9=87=8D=E5=8F=A0=E6=83=85?= =?UTF-8?q?=E5=86=B5,=E6=B2=A1=E6=9C=89=E5=88=A4=E6=96=AD=E9=97=B4?= =?UTF-8?q?=E8=B7=9D=E8=BF=87=E5=A4=A7=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- src/layout/non-layered-tidy.js | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 72cf428..2ada220 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,8 @@ const NODE_SIZE = 16; const PEM = 5; const ctx = document.getElementById('id-of-canvas-element').getContext('2d'); const rootNode = Hierarchy.compactBox(root, { - direction: 'H', // H / V / LR / RL / TB / BT + direction: 'H', // H / V / LR / RL / TB / BT, + forceCompact: true, // 开启强制紧凑,节点会以最小宽度和最小高度排列,不论节点在布局过程中是否重叠都会执行子树移动的行为 getId(d) { return d.id; }, diff --git a/src/layout/non-layered-tidy.js b/src/layout/non-layered-tidy.js index 24e9c89..4470ab9 100644 --- a/src/layout/non-layered-tidy.js +++ b/src/layout/non-layered-tidy.js @@ -93,6 +93,7 @@ function layer(node, isHorizontal, d = 0) { module.exports = (root, options = {}) => { const isHorizontal = options.isHorizontal; + const forceCompact = options.forceCompact; function firstWalk(t) { if (t.cs === 0) { setExtremes(t); @@ -103,7 +104,7 @@ module.exports = (root, options = {}) => { for (let i = 1; i < t.cs; ++i) { firstWalk(t.c[i]); const min = bottom(t.c[i].er); - separate(t, i, ih); + separate(t, i, ih, forceCompact); ih = updateIYL(min, i, ih); } positionRoot(t); @@ -123,7 +124,7 @@ module.exports = (root, options = {}) => { } } - function separate(t, i, ih) { + function separate(t, i, ih, forceCompact) { let sr = t.c[i - 1]; let mssr = sr.mod; let cl = t.c[i]; @@ -131,7 +132,8 @@ module.exports = (root, options = {}) => { while (sr !== null && cl !== null) { if (bottom(sr) > ih.low) ih = ih.nxt; const dist = (mssr + sr.prelim + sr.w) - (mscl + cl.prelim); - if (dist > 0) { + // if forceCompact, the moveSubTree method is executed no matter what + if (forceCompact || dist > 0) { mscl += dist; moveSubtree(t, i, ih.index, dist); }