Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

fix(editor): restore centered flowchart node creation (#12500)

David Luzara758e93d6a03
diff --git a/packages/element/src/flowchart.ts b/packages/element/src/flowchart.ts
index aa59a5ff00..abc266e947 100644
--- a/packages/element/src/flowchart.ts
+++ b/packages/element/src/flowchart.ts
@@ -4,6 +4,7 @@ import {
clamp,
type GlobalPoint,
pointFrom,
+ PRECISION,
type LocalPoint,
} from "@excalidraw/math";
@@ -155,9 +156,10 @@ const getConnectedFlowchartNodes = (
// away from the parent, forming a search band the cluster will occupy
// - along the cross axis the cluster slides into the free slot nearest the
// parent-centered ideal, treating band obstacles as immovable
-// - `stickyCrossStart` anchors an already-visible pending cluster: growing it
-// extends it at either end so the existing pending nodes keep their
-// positions, unless the grown cluster no longer fits there
+// - `stickyCrossStart` anchors an already-visible pending cluster that an
+// obstacle pushed off-center: growing it extends it at either end so the
+// existing pending nodes keep their positions, unless the grown cluster
+// centers on the parent again or no longer fits there
const placeCluster = (
parent: ExcalidrawFlowchartNodeElement,
direction: LinkDirection,
@@ -182,14 +184,26 @@ const placeCluster = (
? parentPrimaryStart + nodePrimarySize + primaryGap
: parentPrimaryStart - primaryGap - nodePrimarySize;
- // cross-axis intervals of the obstacles sharing the band, inflated so the
+ // the band widened by one gap on both sides, so nodes the cluster would end
+ // up closer than a gap to count as obstacles too; the near side stops
+ // exactly at the parent's facing edge, keeping the parent's row out
+ const [zoneStart, zoneEnd] =
+ direction === "right" || direction === "down"
+ ? [
+ parentPrimaryStart + nodePrimarySize,
+ primaryStart + nodePrimarySize + primaryGap,
+ ]
+ : [primaryStart - primaryGap, parentPrimaryStart];
+
+ // cross-axis intervals of the obstacles in that zone, inflated so the
// cluster keeps at least one gap of clearance
const occupied = mergeIntervals(
obstacles
.filter((bounds) => {
const start = horizontal ? bounds[0] : bounds[1];
const end = horizontal ? bounds[2] : bounds[3];
- return start < primaryStart + nodePrimarySize && end > primaryStart;
+ // Ignore rounding noise so nodes a full gap away stay out of the zone.
+ return start < zoneEnd - PRECISION && end > zoneStart + PRECISION;
})
.map((bounds) => ({
start: (horizontal ? bounds[1] : bounds[0]) - crossGap,
@@ -199,11 +213,14 @@ const placeCluster = (
const step = nodeCrossSize + crossGap;
const clusterCrossSize = count * nodeCrossSize + (count - 1) * crossGap;
+ const centeredStart = parentCrossCenter - clusterCrossSize / 2;
+ // the centered slot sorts first (zero distance), so the cluster re-centers
+ // whenever it fits there
const anchoredStart =
stickyCrossStart === null
? null
- : [stickyCrossStart, stickyCrossStart - step]
+ : [centeredStart, stickyCrossStart, stickyCrossStart - step]
.filter((start) => intervalIsFree(start, clusterCrossSize, occupied))
.sort(
(a, b) =>
@@ -213,11 +230,7 @@ const placeCluster = (
const crossStart =
anchoredStart ??
- findNearestFreeSlot(
- parentCrossCenter - clusterCrossSize / 2,
- clusterCrossSize,
- occupied,
- );
+ findNearestFreeSlot(centeredStart, clusterCrossSize, occupied);
const positions = Array.from({ length: count }, (_, index) => {
const cross = crossStart + index * step;
diff --git a/packages/element/tests/flowchart.test.tsx b/packages/element/tests/flowchart.test.tsx
index b3c36489aa..9aa2732bc2 100644
--- a/packages/element/tests/flowchart.test.tsx
+++ b/packages/element/tests/flowchart.test.tsx
@@ -363,7 +363,7 @@ describe("flow chart band-search placement", () => {
expect({ x: c2.x, y: c2.y }).toEqual({ x: 600, y: 200 });
});
- it("keeps pending nodes in place while the cluster grows", () => {
+ it("centers the pending cluster on the parent while it grows", () => {
API.clearSelection();
const parent = API.createElement({
type: "rectangle",
@@ -373,6 +373,41 @@ describe("flow chart band-search placement", () => {
API.setElements([parent]);
API.setSelectedElements([parent]);
+ Keyboard.withModifierKeys({ ctrl: true }, () => {
+ Keyboard.keyPress(KEYS.ARROW_RIGHT);
+ expect(pendingRects()).toEqual([{ x: 300, y: 0 }]);
+
+ Keyboard.keyPress(KEYS.ARROW_RIGHT);
+ expect(pendingRects()).toEqual([
+ { x: 300, y: -100 },
+ { x: 300, y: 100 },
+ ]);
+
+ Keyboard.keyPress(KEYS.ARROW_RIGHT);
+ expect(pendingRects()).toEqual([
+ { x: 300, y: -200 },
+ { x: 300, y: 0 },
+ { x: 300, y: 200 },
+ ]);
+ });
+ Keyboard.keyUp(KEYS.CTRL_OR_CMD);
+ });
+
+ it("keeps pending nodes in place while an obstacle blocks the centered slot", () => {
+ API.clearSelection();
+ const parent = API.createElement({
+ type: "rectangle",
+ width: 200,
+ height: 100,
+ });
+ API.setElements([parent]);
+
+ // an obstacle above the band leaves room for the first node only
+ addChild(parent, KEYS.ARROW_RIGHT);
+ const [c1] = children(parent);
+ API.updateElement(c1, { y: -250 });
+
+ API.setSelectedElements([parent]);
Keyboard.withModifierKeys({ ctrl: true }, () => {
Keyboard.keyPress(KEYS.ARROW_RIGHT);
expect(pendingRects()).toEqual([{ x: 300, y: 0 }]);
@@ -385,14 +420,75 @@ describe("flow chart band-search placement", () => {
Keyboard.keyPress(KEYS.ARROW_RIGHT);
expect(pendingRects()).toEqual([
- { x: 300, y: -200 },
{ x: 300, y: 0 },
{ x: 300, y: 200 },
+ { x: 300, y: 400 },
]);
});
Keyboard.keyUp(KEYS.CTRL_OR_CMD);
});
+ it("keeps a gap from nodes between the parent's row and the band", () => {
+ API.clearSelection();
+ const parent = API.createElement({
+ type: "rectangle",
+ width: 200,
+ height: 100,
+ });
+ API.setElements([parent]);
+ API.setSelectedElements([parent]);
+
+ // the centered right children: the lower one ends exactly where the
+ // band below the parent begins (y: 100..200)
+ Keyboard.withModifierKeys({ ctrl: true }, () => {
+ Keyboard.keyPress(KEYS.ARROW_RIGHT);
+ Keyboard.keyPress(KEYS.ARROW_RIGHT);
+ });
+ Keyboard.keyUp(KEYS.CTRL_OR_CMD);
+
+ API.setSelectedElements([parent]);
+ Keyboard.withModifierKeys({ ctrl: true }, () => {
+ Keyboard.keyPress(KEYS.ARROW_DOWN);
+ Keyboard.keyPress(KEYS.ARROW_DOWN);
+ // the centered slot (x: -150..350) would sit flush under the lower
+ // right child (x: 300..500)
+ expect(pendingRects()).toEqual([
+ { x: -300, y: 200 },
+ { x: 0, y: 200 },
+ ]);
+ });
+ Keyboard.keyUp(KEYS.CTRL_OR_CMD);
+ });
+
+ it.each([
+ { key: KEYS.ARROW_UP, y: -0.1, height: 100 },
+ { key: KEYS.ARROW_DOWN, y: 0.1, height: 100.3 },
+ ])(
+ "keeps children centered beside a node in the parent's row ($key, y=$y)",
+ ({ key, y, height }) => {
+ API.clearSelection();
+ const parent = API.createElement({
+ type: "rectangle",
+ y,
+ width: 200,
+ height,
+ });
+ API.setElements([parent]);
+
+ // Centering the right child can round its top or bottom edge slightly
+ // past the parent's, even though both nodes occupy the same row.
+ addChild(parent, KEYS.ARROW_RIGHT);
+
+ API.setSelectedElements([parent]);
+ Keyboard.withModifierKeys({ ctrl: true }, () => {
+ Keyboard.keyPress(key);
+ Keyboard.keyPress(key);
+ expect(pendingRects().map(({ x }) => x)).toEqual([-150, 150]);
+ });
+ Keyboard.keyUp(KEYS.CTRL_OR_CMD);
+ },
+ );
+
it("repositions the whole pending cluster when it no longer fits", () => {
API.clearSelection();
const parent = API.createElement({