Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

fix(canvas): keep dragged groups in place after save and reload

Andras Bacsaid9890500eda5
diff --git a/resources/js/canvas/environment/EnvironmentCanvas.svelte b/resources/js/canvas/environment/EnvironmentCanvas.svelte
index 3baf907f12..f5f70d76ad 100644
--- a/resources/js/canvas/environment/EnvironmentCanvas.svelte
+++ b/resources/js/canvas/environment/EnvironmentCanvas.svelte
@@ -17,6 +17,7 @@
filterGraph,
filterOptions,
fitView,
+ GROUP_GAP,
GROUP_HEADER,
GROUP_PADDING,
groupByUse,
@@ -86,6 +87,10 @@
const interactions = new Set(); // active drags; refetches wait until they end
let deferred = null;
const unsaved = {};
+ // Saved moves stay on the canvas until a load that started after the save returns them, so an older
+ // response (a poll or status refresh during the drag or the request) cannot put the cards back for a moment.
+ const settling = { positions: {}, saved: {} }; // id -> { at, after: load number the save finished at }
+ let loads = 0;
let reloadQueued = false;
let fitted = false;
let destroyed = false;
@@ -547,13 +552,14 @@
track(header, ev => {
if (!moved && Math.hypot(ev.clientX - origin.cx, ev.clientY - origin.cy) < 4) return;
const wanted = { ...box, x: Math.max(meta.inner.x, box.x + (ev.clientX - origin.cx) / view.z), y: Math.max(meta.inner.y, box.y + (ev.clientY - origin.cy) / view.z) };
- const spot = freeSpot(wanted, others, BOX_GAP, fits);
+ // Same gap as layoutServerView, so a reload does not move the group.
+ const spot = freeSpot(wanted, others, GROUP_GAP, fits);
if (!spot) return;
moved = true;
for (const id of ids) positions[id] = { x: starts[id].x + spot.x - box.x, y: starts[id].y + spot.y - box.y };
}, () => {
- end('group');
if (moved) saveGroup(ids, meta.shift);
+ end('group');
});
}
@@ -563,10 +569,13 @@
*/
async function saveGroup(ids, shift) {
const saved = Object.fromEntries(ids.map(id => [id, { x: Math.round(positions[id].x - shift.x), y: Math.round(positions[id].y - shift.y) }]));
+ hold(settling.saved, saved);
try {
await api('PATCH', `${environmentBase(nodeById(ids[0]))}/positions`, { positions: saved });
- if (!destroyed) applyGraph({ ...full, nodes: full.nodes.map(n => (saved[n.id] ? { ...n, saved: saved[n.id] } : n)) });
+ settle(settling.saved, saved);
+ if (!destroyed) applyGraph(full);
} catch (e) {
+ release(settling.saved, saved);
if (destroyed) return;
toast(e.message, true);
load({ quiet: true });
@@ -663,21 +672,36 @@
const ids = Object.keys(batch);
if (!ids.length || !graph) return;
const rounded = Object.fromEntries(ids.map(id => [id, { x: Math.round(batch[id].x), y: Math.round(batch[id].y) }]));
- const forget = () => {
- for (const id of ids) if (unsaved[id] === batch[id]) delete unsaved[id];
- };
+ hold(settling.positions, rounded);
+ for (const id of ids) if (unsaved[id] === batch[id]) delete unsaved[id];
try {
await api('PATCH', `${base}/positions`, { positions: rounded }, options);
+ settle(settling.positions, rounded);
if (graph && !destroyed) patchGraph({ positions: { ...(graph.positions || {}), ...rounded } });
- forget();
} catch (e) {
- forget();
+ release(settling.positions, rounded);
if (destroyed) return;
toast(e.message, true);
load({ quiet: true });
}
}
+ /** Keeps moves that are being saved on the canvas (see settling). */
+ function hold(map, moves) {
+ for (const [id, at] of Object.entries(moves)) map[id] = { at, after: Infinity };
+ }
+ /** The save is done: loads that start from now on return the moves, so they can replace them. */
+ function settle(map, moves) {
+ for (const id of Object.keys(moves)) if (map[id]?.at === moves[id]) map[id].after = loads;
+ }
+ function release(map, moves) {
+ for (const id of Object.keys(moves)) if (map[id]?.at === moves[id]) delete map[id];
+ }
+ /** A load that started after the save returned the saved moves. */
+ function dropSettled(started) {
+ for (const map of Object.values(settling)) for (const [id, entry] of Object.entries(map)) if (entry.after < started) delete map[id];
+ }
+
// ── Data ──
/** Shared variables view: a link change moves its resource in or out of the connected zone right away. */
function setLinks(links) {
@@ -694,8 +718,10 @@
next.nodes ||= [];
next.links ||= [];
next.positions = next.positions && !Array.isArray(next.positions) ? next.positions : {};
- // Local moves that are not saved yet stay where the user put them.
- next.positions = { ...next.positions, ...Object.fromEntries(Object.entries(unsaved).map(([id, p]) => [id, { x: p.x, y: p.y }])) };
+ // Local moves that are not saved yet (or not loaded back yet) stay where the user put them.
+ const kept = Object.fromEntries(Object.entries(settling.positions).map(([id, s]) => [id, s.at]));
+ next.positions = { ...next.positions, ...kept, ...Object.fromEntries(Object.entries(unsaved).map(([id, p]) => [id, { x: p.x, y: p.y }])) };
+ if (Object.keys(settling.saved).length) next.nodes = next.nodes.map(n => (settling.saved[n.id] ? { ...n, saved: settling.saved[n.id].at } : n));
full = next;
const shown = groupByUse(filterGraph(next, filter));
groupMeta = {};
@@ -727,8 +753,10 @@
return;
}
loading = true;
+ const started = ++loads;
try {
const next = await api('GET', base);
+ dropSettled(started);
if (next && containers) next.nodes = [...(next.nodes || []), ...containers];
applyGraph(next);
if (!quiet && fitted && next) toast('Canvas refreshed.');
diff --git a/resources/js/environment-canvas.js b/resources/js/environment-canvas.js
index 9faeb3b3fe..1d29816809 100644
--- a/resources/js/environment-canvas.js
+++ b/resources/js/environment-canvas.js
@@ -456,7 +456,8 @@ export function dragRules(graph, positions, sizes, id) {
return {
others: [...rects(graph.nodes), ...zones.map(z => room(z, side, top)), ...groups.map(g => room(g, groupSide, groupTop))],
fits: box => zones.every(z => apart(frameAround([...zoneMates, box], side, top), z, BOX_GAP))
- && (!grouped || groups.every(g => apart(frameAround([...groupMates, box], groupSide, groupTop), g, BOX_GAP))),
+ // Same gap between groups as layoutServerView, so a reload does not move the group.
+ && (!grouped || groups.every(g => apart(frameAround([...groupMates, box], groupSide, groupTop), g, GROUP_GAP))),
};
}
diff --git a/resources/js/environment-canvas.test.js b/resources/js/environment-canvas.test.js
index e4240b62bf..6786d577af 100644
--- a/resources/js/environment-canvas.test.js
+++ b/resources/js/environment-canvas.test.js
@@ -2,6 +2,7 @@ import test from 'node:test';
import assert from 'node:assert/strict';
import {
CARD_WIDTH,
+ GROUP_GAP,
MAX_ROWS,
COLUMN_GAP,
ROW_GAP,
@@ -586,6 +587,39 @@ test('server view: a dragged card stops before its group covers another group',
assert.ok(apart(f1, f2), 'the project / environment frames do not overlap');
});
+test('server view: a group dropped as close to another group as a drag allows stays there after a reload', () => {
+ const nodes = [envNode('a', 'application', 'z1', 'prod'), envNode('b', 'application', 'z1', 'staging')];
+ const g = { layout: 'server', zones: [{ id: 'z1', name: 'web' }], nodes, links: [] };
+ const groups = {};
+ const before = layoutServerView(g, groups);
+ const [prod, staging] = groupFrames(nodes, before);
+ // Drag the staging group to the left of the prod group: it stops at the gap the drag keeps.
+ const box = { x: staging.x, y: staging.y, w: staging.width, h: staging.height };
+ const others = [{ x: prod.x, y: prod.y, w: prod.width, h: prod.height }];
+ const { inner } = groups[groupKey([nodes[1]], 'z1')];
+ const spot = freeSpot({ ...box, x: prod.x + 40 }, others, GROUP_GAP, b => b.x >= inner.x && b.y >= inner.y);
+ const dropped = { ...before, b: { x: before.b.x + spot.x - box.x, y: before.b.y + spot.y - box.y } };
+ const shift = id => groups[groupKey([nodes.find(n => n.id === id)], 'z1')].shift;
+ const saved = Object.fromEntries(nodes.map(n => [n.id, { x: dropped[n.id].x - shift(n.id).x, y: dropped[n.id].y - shift(n.id).y }]));
+ const after = layoutServerView({ ...g, nodes: nodes.map(n => ({ ...n, saved: saved[n.id] })) });
+ assert.deepEqual(after, dropped);
+});
+
+test('server view: a dragged card keeps its group as far from another group as the layout does', () => {
+ const g = {
+ layout: 'server',
+ zones: [{ id: 'z1', name: 'web' }],
+ nodes: [srv('a', 'application', 'Shop / prod'), srv('c', 'application', 'Blog / prod')],
+ links: [],
+ positions: {},
+ };
+ const before = layoutServerView(g);
+ const { others, fits } = dragRules(g, before, {}, 'a');
+ const start = { ...before.a, w: CARD_WIDTH, h: estimateNodeHeight(g.nodes[0]) };
+ // The layout puts the groups GROUP_GAP apart: a card cannot move its group closer to the other one.
+ assert.equal(freeSpot({ ...start, x: start.x - 10 }, others, undefined, fits), null);
+});
+
test('groupKey: a group with only an empty-environment placeholder has no key, so it cannot be moved', () => {
const environment = { uuid: 'env', name: 'production' };
assert.equal(groupKey([{ id: 'empty:env', type: 'empty', environment }], 'empty'), null);