Skip to content

fix(muya): don't drop blocks when a cut ends inside a code block (#4903, #5148) - #5357

Merged
Jocs merged 3 commits into
developfrom
fix/4903-lang-input-paste-drift
Sep 15, 2026
Merged

Jocs merged 3 commits into
developfrom
fix/4903-lang-input-paste-drift

Conversation

@Jocs

@Jocs Jocs commented Sep 15, 2026 •

Copy link
Copy Markdown
Member

Closes #4903
Closes #5148

Summary

Both issues are crash reports without steps: Cannot insert into out of bounds index (#4903) and Cannot pick up or remove undefined (#5148), thrown by ot-json1 inside JSONState._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 with source = '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:

  1. 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. removeBlocks removed the inner code node; 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, so removeBlocks is skipped.
  2. 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 with pruneAfterBranch. 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)

Intro paragraph

```js
const a = 1
```

Outro paragraph
  1. Click in the code line const a = 1. The language box js appears above the code.
  2. Click right after js in the language box.
  3. Press Shift+↓. The selection now runs from the language line into the code line.
  4. Type any letter, or paste any text. The code block turns into a paragraph (e.g. jsqnst a = 1). Outro paragraph is still on screen, but it is already missing from the document that gets saved.
  5. Then one of:

If the code block is the last block of the document (drop Outro paragraph), step 4 itself throws Cannot 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

- item

  ```js
  code
  ```

  tail
  1. Drag-select from inside intro (after in) to inside code (after co). Keyboard alternative: click after in and press Shift+↓ three times, until the selection reaches the code line. (Shift+click puts the caret into the code instead of extending the selection.)
  2. Press Backspace. tail is still on screen, but it is already missing from the document that gets saved.
  3. Click at the end of 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 undefined as soon as the selection is replaced (covered by the unit tests below).

With this PR: no error; the document becomes inde followed by the list item tail.

Type of change

  • Bug fix (non-breaking, fixes an issue)
  • New feature (non-breaking, adds functionality)
  • Breaking change (causes existing functionality to change)
  • Documentation update

Test plan

  • Unit tests (each compares the json state with the block tree):
    • clipboard/__tests__/languageInputCodeSelection4903.spec.ts: cut, cut with the code block last, paste through a ClipboardEvent. All 3 fail on develop.
    • 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.
  • e2e specs, failing on the matching pre-fix code:
    • 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.
    • Chromium repeated 30/30 and 20/20; WebKit passes (the paste case is Chromium-only, like the other clipboard specs).
  • Real desktop app, real mouse and keyboard input (Playwright _electron): the steps above on the packaged 0.20.0-rc.2 give exactly the errors listed; on a build:unpack of 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.
  • muya: vitest run 231 files / 1635 tests; lint:types, eslint on the changed files, and check-circular pass. 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).
  • Fuzzing with random edits, pastes, cuts, undo/redo, search/replace and paragraph commands on nested documents, checking the tree against the json state after every step: 90 seeds × 60 steps with this PR and fix(muya): run paragraph commands on the whole table or code block (#4944) #5358 produced neither error.
  • Manually tested on: macOS

Notes for reviewers

Found while testing, not changed here:

  • A cut from a nested code block's language line into its code still replaces the whole outermost list or quote (collapseLanguageInputCut uses startBlock.outMostBlock). The tree and json state stay in sync, but the other items are lost.
  • After one failed flush, _flushOperationCache keeps 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

Jocs and others added 2 commits September 15, 2026 07:33
…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
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
@Jocs
Jocs merged commit f10f817 into develop Sep 15, 2026
7 checks passed
@Jocs
Jocs deleted the fix/4903-lang-input-paste-drift branch September 15, 2026 02:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected error with message Cannot pick up or remove undefined Unexpected error with message Cannot insert into out of bounds index

1 participant