Repository navigation
fix(muya): don't drop blocks when a cut ends inside a code block (#4903, #5148) - #5357
Merged
Merged
Conversation
…ock (#4903, #5148) Replacing a selection that runs from a code fence's language input into that block's own code (paste, typing or Backspace over it; Shift+ArrowDown in the language input selects exactly this) goes through the #918 collapse path. It called `removeBlocks`, which removed the code block's inner `code` node. That node has no json node of its own: its json path is the code block's, so the json state lost the whole code block while the tree kept it. Replacing the code block with the merged paragraph then removed the *following* block from the json state instead. From then on the block tree showed a block the json state no longer had. Editing it built ops for a missing index, and the per-frame json flush threw "Cannot insert into out of bounds index" (Enter, #4903) or "Cannot pick up or remove undefined" (Backspace, #5148). When the code block was the last block the flush threw straight away. The saved markdown lacked the block as well. When the selection ends inside the language line's own code block there is nothing between the two leaves to remove, so skip `removeBlocks`; the code block replacement already removes the code. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01WEPLCvFTgbYf35z85XUJop
…nds in it (#4903, #5148) A cross-block cut (paste, typing or Backspace over the selection) that ends inside a code, math, html or diagram block nested in a list item or quote, with more content after that block in the same container, prunes the end side with `pruneAfterBranch`. It walked up from the code leaf and removed each emptied ancestor. The block's inner `code` / container nodes share its json path, so every level removed the block's json node again. Each extra remove took the next sibling (the paragraph after the block), or, when nothing was left at that index, the json flush threw "Cannot pick up or remove undefined". Until the document was reloaded the flush kept throwing and no later edit reached the saved markdown. Where the flush survived, the tree still held the lost sibling, and editing it hit "Cannot insert into out of bounds index". Start the walk at the block that owns the code leaf, so the block is removed as one node. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01WEPLCvFTgbYf35z85XUJop
7 of 10 tasks
The last step pressed Enter then Backspace in `tail` and expected the document unchanged. That only held when both keys landed in one frame: Enter adds a list item and Backspace turns it into an empty paragraph of the item before, so the slower CI run kept that paragraph. Type after Enter instead and assert the new item, which checks the same thing (an insert after the cut reaches the saved markdown) at any timing. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01WEPLCvFTgbYf35z85XUJop
This was referenced Sep 15, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #4903
Closes #5148
Summary
Both issues are crash reports without steps:
Cannot insert into out of bounds index(#4903) andCannot pick up or remove undefined(#5148), thrown by ot-json1 insideJSONState._flushOperationCache. Both mean the block tree and the json state have drifted: the editor still shows a block the json state no longer has, so the next edit builds an op for an index that does not exist.Two cut paths caused that drift. The inner node of a code block (
code, and the math / html / diagram containers) has no json node of its own: its json path is the code block's. Removing it withsource = 'user'removed the whole code block from the json state while the tree kept it. A cut runs for Cut, and for paste, typing, Backspace and Enter over a cross-block selection.Two commits:
fix(muya): stop a language-line-to-code cut from dropping the next block. A selection from a code fence's language input into that block's own code goes through the Code block no longer be input #918 collapse path.removeBlocksremoved the innercodenode; replacing the code block with the merged paragraph then removed the following block from the json state. When the selection ends inside the language line's own code block there is nothing between the two leaves to remove, soremoveBlocksis skipped.fix(muya): remove a nested code, math or html block once when a cut ends in it. A selection from above into a code, math or html block nested in a list item or quote, with more content after that block, prunes the end side withpruneAfterBranch. Walking up from the code leaf removed the block's json node once per inner level, taking the sibling after it too. The walk now starts at the block.Steps to reproduce
Checked on the released MarkText 0.20.0-rc.2 (macOS) and on a desktop build of this PR, with real mouse and keyboard input. To set up each document, open a new tab, switch to source code mode (⌘⌥S / Ctrl+Alt+S), paste the markdown, and switch back.
1. Selection from a code block's language line into its code (#4903, #5148)
const a = 1. The language boxjsappears above the code.jsin the language box.jsqnst a = 1).Outro paragraphis still on screen, but it is already missing from the document that gets saved.Outro paragraphand press Enter → 0.20.0-rc.2: "Unexpected error" withCannot insert into out of bounds index(Unexpected error with message Cannot insert into out of bounds index #4903).Outro paragraphand press Backspace → 0.20.0-rc.2:Cannot pick up or remove undefined(Unexpected error with message Cannot pick up or remove undefined #5148).If the code block is the last block of the document (drop
Outro paragraph), step 4 itself throwsCannot pick up or remove undefined. After the first error every later edit throws again until the file is reopened, and none of those edits are saved.With this PR: no error at any step; the saved document keeps
Outro paragraph.2. Selection from above into a code block nested in a list item
intro(afterin) to insidecode(afterco). Keyboard alternative: click afterinand press Shift+↓ three times, until the selection reaches the code line. (Shift+click puts the caret into the code instead of extending the selection.)tailis still on screen, but it is already missing from the document that gets saved.tail, press Enter, then Backspace → 0.20.0-rc.2:Cannot insert into out of bounds index.When the selection instead ends in a math block inside a quote or an html block inside a list item, the flush throws
Cannot pick up or remove undefinedas soon as the selection is replaced (covered by the unit tests below).With this PR: no error; the document becomes
indefollowed by the list itemtail.Type of change
Test plan
clipboard/__tests__/languageInputCodeSelection4903.spec.ts: cut, cut with the code block last, paste through aClipboardEvent. All 3 fail ondevelop.clipboard/__tests__/cutIntoNestedCodeBlock4903.spec.ts: code block in a list item, math block in a quote, html block in a list item. All 3 fail with only the first commit.editing/language-input-code-selection-4903.spec.ts: real paste; typing, then Backspace in the next paragraph.editing/cut-into-nested-code-block-4903.spec.ts: typing; Backspace, then Enter and typing in the paragraph after the block._electron): the steps above on the packaged 0.20.0-rc.2 give exactly the errors listed; on abuild:unpackof this branch (with fix(muya): run paragraph commands on the whole table or code block (#4944) #5358) they give no error and the saved markdown keeps the block.vitest run231 files / 1635 tests;lint:types, eslint on the changed files, andcheck-circularpass. Chromium e2e: the new specs plus clipboard, paste-as-plain-text, table-rect-clipboard, undo-redo, selection, code block and language selector specs (57 passed).Notes for reviewers
Found while testing, not changed here:
collapseLanguageInputCutusesstartBlock.outMostBlock). The tree and json state stay in sync, but the other items are lost._flushOperationCachekeeps the queued ops, so every later edit throws the same error until the document is reopened. That is why these reports come with repeated errors; making the flush recover would be a separate change.By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.
🤖 Generated with Claude Code
https://claude.ai/code/session_01WEPLCvFTgbYf35z85XUJop