mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-12-06 03:05:18 +03:00
Review finding 7.
The text-to-diagram dialog (the Mermaid tab, and the AI chat's preview and
insert) converted mermaid's skeletons with `convertToExcalidrawElements`
without the host's `renderEnvironment`, while pasting a mermaid definition
(App.clipboard) passes `app.renderEnvironment`. So with a host's custom
`textMetrics`, every label in the dialog's conversion, container and arrow
labels alike, was measured with the default (canvas) metrics. The gap
predates this branch: d3a9c2a19 added the option and passed it from the
paste path, but not from the dialog.
Reproduction: render `<Excalidraw renderEnvironment={env}>`, whose
`textMetrics.getLineWidth` is `text.length * 5`, open the Mermaid dialog
and insert a diagram with a 100x40 rectangle labelled "aaaa bbbb cccc", and
an arrow labelled "aaaa bbbb cccc dddd eeee ffff" bound to it.
Failure case:
preview: both labels wrapped by the default metrics ("aaaa bbbb\ncccc",
"aaaa bbbb cccc dddd\neeee ffff"), and the rectangle grown to
height 60 to fit two lines
inserted: `addElementsFromPasteOrLibrary` redraws the labels with the
host's environment, so they end up on one line, but the
rectangle stays 60 high, around a one-line label
Expected: as when pasting the same definition: one-line labels (70px and
145px wide) in the preview and the scene, and the rectangle 40 high.
Fix: `convertMermaidToExcalidraw` takes an optional `renderEnvironment` and
passes it to `convertToExcalidrawElements`. Its three callers pass
`app.renderEnvironment`: the Mermaid tab's preview (whose result is what
it inserts), and the AI chat's preview (`useMermaidRenderer`, now using
`useApp`) and insert (`TextToDiagram`).
Left alone: `exportScene`, which renders the preview, takes no environment;
it draws already measured elements, so the preview's layout now matches the
inserted one.
Without the prop, `app.renderEnvironment` has no `textMetrics`, so
measurement falls back as before (no snapshot changes).
Tests: inserting from the Mermaid dialog under a host environment
(renderEnvironmentLabels.test.tsx); the rectangle's height fails without
the fix.