Files
excalidraw/packages/element/tests
Mark Tolmacs 9e63f70211 fix(editor): re-measure bound labels with the host's render environment
Review finding 12.

The finding: the `renderEnvironment` option this branch added to
`updateBoundElements` (a9b2d2878) was passed only by the transform, and
since e0f198c15 not even by that (it calls `updateBoundArrow` directly),
so the option was dead while documenting that it "measures the labels of
the updated arrows". Meanwhile every editor path re-measured bound labels
with the default environment, ignoring the host's
`<Excalidraw renderEnvironment>` (its custom `textMetrics`), so a label
laid out with the host's metrics re-wrapped the first time its arrow was
touched.

The re-wrap itself predates this branch: master's `updateBoundElements`
never took an environment and called `handleBindTextResize(element,
scene, false)`, as `LinearElementEditor`'s point drag still did. Rather
than drop the dead option, this wires it (and the other label-measuring
editor paths) to the host's environment.

Reproduction: convert two rectangles A, B and an arrow bound to both,
labelled "aaaa bbbb cccc dddd eeee ffff", with
`convertToExcalidrawElements(..., { renderEnvironment })`, whose
`textMetrics.getLineWidth` is `text.length * 5`. Move B by 1px and call
`updateBoundElements(B, scene)` as the drag path does. In the editor:
render `<Excalidraw renderEnvironment={env}>` with the same scene and drag
or arrow-key nudge B, align A and B, undo/redo the drag, or drag the
arrow's point.

Failure case (this branch and master alike):
  observed: "aaaa bbbb cccc dddd eeee ffff", width 145, becomes
            "aaaa bbbb cccc dddd\neeee ffff", width 190
  expected: unchanged, "aaaa bbbb cccc dddd eeee ffff", width 145

Cause: no editor caller passed `updateBoundElements` an environment, so
it called `updateBoundArrow` without one; `ElementsDelta`'s redraw,
`LinearElementEditor`'s point drag and `listMarker`'s re-numbering
likewise measured without one.

Fix: optional `renderEnvironment` threaded the way resize already does it
(a trailing param, or an option where the function takes an options bag):
- `updateBoundElements`: its existing option is now passed by the editor
  (doc comment updated); `updateBindings` gains the same option and
  forwards it. Both pass it to `updateBoundArrow`.
- drag: `dragSelectedElements` (new trailing param), from the selection tool.
- arrow-key nudge (App.tsx).
- align / distribute: `alignElements` / `distributeElements` (new trailing
  param), from their actions.
- rotate/resize: the `updateBoundElements` calls in `transformElements`,
  `rotateMultipleElements`, `resizeSingleElement`, `resizeMultipleElements`
  now pass the environment they already had.
- sticky notes: `updateStickyNoteLayout` passes its `renderEnvironment` on
  to its bound-arrow pass.
- undo/redo: `ApplyToOptions.renderEnvironment` reaches
  `ElementsDelta.redrawElements` (both the bound-text redraw and the
  bound-arrow pass); `History.undo/redo` and `HistoryDelta.applyTo` take
  it, the history actions pass `app.renderEnvironment`, and
  `App.applyDeltas` defaults to it.
- `LinearElementEditor` point drag: `handleBindTextResize` gets
  `app.renderEnvironment`.
- list-marker re-numbering on duplicate (`advanceDuplicatedListMarkers`,
  `applyListMarkerAdvances`, new trailing param): both its label redraw and
  its free-text refresh.
- the remaining editor callers of `updateBoundElements` / `updateBindings`,
  each of which can move a labelled arrow bound to the element: text
  editing (App.text, textWysiwyg), duplicate, crop/uncrop, wrap/unwrap
  (also its `redrawTextBoundingBox`), font/style/auto-resize actions,
  element type conversion, and the Stats position/angle/dimension inputs
  (`moveElement` gets a trailing param).

Left alone:
- `updateBindings`' arrow branch (`updateArrowBindings`): it only rebinds
  the arrow's ends and measures no label.
- `transform.ts`: already measured with its environment (via
  `updateBoundArrow`).
- callers that already passed `app.renderEnvironment` to
  `redrawTextBoundingBox` / `handleBindTextResize` (resize, Stats font
  size, font/bound-text actions, textWysiwyg submit, type conversion).
- `updateBoundElements` re-measures only arrow labels (its visitor skips
  non-arrows), so no non-arrow container label path needed changing there.
- free-text measurement helpers called directly (`measureText`, `wrapText`,
  `refreshTextDimensions`, ...) outside these bound-label paths: not part
  of this finding.

Without the prop, `app.renderEnvironment` is the owner-window environment
with no `textMetrics`, so measurement still falls back to the global
provider or a canvas; single-window behaviour is unchanged (no snapshot
changes). All new params are optional, so external callers are unaffected.

Tests: a direct `updateBoundElements` test (packages/element) and editor
tests rendering `<Excalidraw renderEnvironment>` for drag, nudge, align,
undo/redo and arrow point drag; each fails without its path's change.
2026-10-23 15:09:59 +00:00
..