fix: restore saved gridstack layout faithfully (#13)
CI / build-and-test (pull_request) Successful in 1m17s

The layout was saved to localStorage, but restoring it did not reliably
reproduce it:

- restore() applied the saved nodes one by one via grid.update(). A later
  item could collide with one already put back and push it off its saved
  spot, and nothing moved it back — so the restored layout was not the one
  the user left. Use grid.load(nodes, false) instead: it sorts the nodes,
  removes them from the engine before re-placing them, and runs in a single
  batch. addRemove is off because Flow owns which children exist.
- gridstack's save() omits w/h when they are 1. The manual restore passed
  them through as undefined, and the server read them as 0 and wrote a
  zero-sized widget back onto the item. load() re-applies gridstack's own
  defaults; onLayoutChange now reads a missing w/h as 1.
- A drag followed immediately by navigating to another route detached the
  element inside the 150ms persist debounce, dropping the pending save.
  disconnectedCallback now flushes it to localStorage first.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ue9ZtWUBQF4SuSHpzZ3zwq
This commit is contained in:
Pit Friedrich
2026-07-26 20:00:16 +02:00
parent f9860fd701
commit 85bb0c8c0b
2 changed files with 46 additions and 21 deletions
@@ -179,18 +179,17 @@ public class GridStackLayout extends Component implements HasSize, HasStyle {
String id = node.path("id").asString(null);
if (id == null) continue;
findItem(id).ifPresent(item -> item.setPosition(
node.path("x").asInt(),
node.path("y").asInt(),
node.path("w").asInt(),
node.path("h").asInt()));
// gridstack omits w/h from its saved layout when they are 1, so a
// missing value means 1 — defaulting to 0 wrote a zero-sized widget
// back onto the item and lost its size on the next attach.
int x = node.path("x").asInt(0);
int y = node.path("y").asInt(0);
int w = node.path("w").asInt(1);
int h = node.path("h").asInt(1);
positions.add(new GridStackItem.Position(
id,
node.path("x").asInt(),
node.path("y").asInt(),
node.path("w").asInt(),
node.path("h").asInt()));
findItem(id).ifPresent(item -> item.setPosition(x, y, w, h));
positions.add(new GridStackItem.Position(id, x, y, w, h));
}
fireEvent(new LayoutChangeEvent(this, true, positions));