fix: roundtrip empty Lexical nodes on mode switch#3112
Conversation
|
Tested to work with ul, ol, tables, code blocks. Odd behaviors spotted:
|
Forgot to change this in the MDAST code block visitor! Before #2795, Lexical Shiki didn't support a
Per GFM spec, checklist items that don't have trailing text are not checklist items. The behavior in prod is wrong, and got fixed by #3105. imo, I prefer the behavior that we have now post-3105, it's correct and spec-compliant:
Thank you opti!! |
Agreed. Thanks! |
|
I added a cherry-pickable regression test for the code-language part of this PR: zhailong8845-art@c888ed82 The parameterized test verifies that Lexical export preserves Verification: Feel free to cherry-pick |
Description
Fixes #3109 by not detecting empty nodes when switching editor modes.
Mode switches are based on the current Formik value. Triggered on each mode switch and submit,
syncFormikchecks if the Lexical editor state contains nodes but no text content. If so, it resets thetextfield, helping Formik validation catch empty item creation.This should happen only on submission, not on mode switches. This PR makes mode switches call
syncFormikwith the flagswitchMode: true, avoiding cleanup.Screenshots
empty-tables-roundtrip.mp4
Additional Context
This issue exacerbated another, bigger, issue: empty items can be created via
markdownmode!markdownmode always contains text content,syncFormikand$isTextEmpty()can't catch empty markdown nodes.I'll open a separate issue for this.
Checklist
Are your changes backward compatible? Please answer below:
Yes
On a scale of 1-10 how well and how have you QA'd this change and any features it might affect? Please answer below:
7, empty content roundtrips on mode switches, submission from
composestill doesn't allow empty nodesFor frontend changes: Tested on mobile, light and dark mode? Please answer below:
n/a
Did you introduce any new environment variables? If so, call them out explicitly here:
n/a
Did you use AI for this? If so, how much did it assist you?
No
Note
Low Risk
Localized editor/Formik sync and markdown export behavior; submit-time empty validation is unchanged when
switchModeis false.Overview
Fixes mode-switch roundtripping where compose content with empty structural nodes (e.g. blank paragraphs) was wiped because
syncFormiktreated “no text” like submit-time validation and cleared Formik’stext.syncFormikandSYNC_FORMIK_COMMANDnow take an options object (switchMode,flush). The empty-text →''shortcut is skipped whenswitchModeis true. The toolbar dispatches sync with{ switchMode: true }before toggling modes.Code blocks: Lexical → mdast export no longer forces
lang: nullfortextor missing language—it writes the Lexical language value through for better roundtrip fidelity.Reviewed by Cursor Bugbot for commit fdc431e. Bugbot is set up for automated code reviews on this repo. Configure here.