Bug: Redo is not working properly when nested editor exists
- Dominant language
- TypeScript
- Stars
- 23.9k
- Forks
- 2.2k
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 61
Description
Lexical version: 0.12.1
MacOS + Chrome
## Steps To Reproduce
### Example 1
https://github.com/facebook/lexical/assets/36890796/bc7151d2-0943-4f4e-a18e-087c46ae3835
1. type some texts
2. add an image
3. click the caption, which initiates a nested editor
4. PRESS command + Z (click the undo button works as expected, but keyboard events are not handled correctly)
I did add some logs to the fork of the latest version, so here is what I observed:
When the editor is initiated, `addRootElementEvents` is called.
When I click the caption (step 3), `addRootElementEvents` is called again.
Then, press `command` button (step 4), the event handler `keydown` is not called. To be precise, the log is set before [this line](https://github.com/facebook/lexical/blob/75c9ae53e4961042527416ace9f7d05b54986b2e/packages/lexical/src/LexicalEvents.ts#L1154).
Then, press `z` button (step 4), the event handler `keydown` is not called, either. However, the weird behavior in the video occurs. No matter how many texts I type, it will always go back to the first character. Handlers in `lexical-history` indicates this is a normal editor update, instead of an undo operation.
### Example 2
https://github.com/facebook/lexical/assets/36890796/0f67b851-949a-47da-bd36-a3a47f9af34a
1. type some texts
2. add an image
3. click the caption, which initiates a nested editor
4. type some texts
5. press command + Z (rewind step 4)
6. press command + Z (rewind step 3)
7. press command + Z, then similar issue occurs
Observation:
At step 3, `addRootElementEvents` is called. At step 6, `removeRootElementEvents` is called. They are working as expected, but then, neither the `command` key down event nor the `z` key down event was captured by the log of the same line mentioned above.
I suspect this is something related to event handler registration process when multiple editor exists, but I don't have further clues.
Contributor guide
Research direction
Reproduce the nested-editor scenarios on macOS and Chrome using the reported steps, then inspect addRootElementEvents, removeRootElementEvents, and the keydown handling around packages/lexical/src/LexicalEvents.ts line 1154. Check how lexical-history classifies the resulting updates. Done means Command+Z is captured and rewinds the expected nested-editor history steps in both examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100