Skip to content

fix: normalize viewport shape on FlowState.load#61

Merged
trmquang93 merged 1 commit into
mainfrom
fix/normalize-viewport-on-load
May 17, 2026
Merged

fix: normalize viewport shape on FlowState.load#61
trmquang93 merged 1 commit into
mainfrom
fix/normalize-viewport-on-load

Conversation

@trmquang93
Copy link
Copy Markdown
Collaborator

Summary

  • Add a viewport normalizer at the read boundary in FlowState.load that accepts the canonical {pan,zoom} shape, the legacy {x,y,scale} shape, partial inputs, and missing-entirely inputs — always returning the canonical shape.
  • Keeps buildPayload as the single source of truth for the persisted shape; fixes the immediate Cannot read properties of undefined (reading 'x') crash on autosave after loading a legacy- or hand-written .drawd file.
  • Add 5 regression tests in state.test.js, including a load → save round-trip that asserts the file is rewritten in canonical form.

Why

Discovered while smoke-testing today's MCP merges (#55, #57, #58, #59, #60). Bootstrapping a new .drawd by hand with viewport:{x,y,scale} opened cleanly but crashed the next mutation. The previous data.viewport || default only guarded the missing-key branch, not wrong-shape inputs — so any drift in the persisted viewport shape would silently load and then explode on the next save.

The existing 28 state.test.js cases never exercised load() (they all went through createNew()), which is why the gap survived. The new tests cover load directly.

Test plan

  • npm test — 1049 passed (1044 → 1049, +5 new, zero regressions)
  • npm run build from drawd-private/ — lint + Vite build clean
  • Manually reproduced the original crash on main, confirmed gone on this branch

Legacy/hand-written .drawd files may persist viewport as `{x,y,scale}` or
omit pan/zoom entirely. The previous `data.viewport || default` only caught
the missing-key branch, so files with the wrong shape passed through and
crashed the next autosave with "Cannot read properties of undefined (reading
'x')" inside buildPayload.

Add a normalizer at the read boundary that accepts canonical `{pan,zoom}`,
legacy `{x,y,scale}`, partial, and missing inputs and always returns the
canonical shape — keeping buildPayload as the single source of truth for the
output shape. Add 5 regression tests including a load → save round-trip that
rewrites a legacy-shape file in canonical form.
@trmquang93 trmquang93 merged commit bd28431 into main May 17, 2026
1 check passed
@github-actions github-actions Bot deleted the fix/normalize-viewport-on-load branch May 17, 2026 01:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant