marktext / marktext/marktext

[Bug] Pressing Enter on a selected image inserts an empty line at the top of the document and keeps the image

Open
#5,396 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

🐛 bug 🔭 area/muya 🚧 needs triage
Dominant language
TypeScript
Stars
61.6k
Forks
4.6k
Avg merge
23h 49m
Merged PRs (30d)
89

Description

Description

Selecting an image and pressing Enter does not act on the image. Instead an empty paragraph is inserted at the very top of the document, pushing everything down, and the image stays selected. The extra empty line is written to the saved file.

Steps to Reproduce
  1. Put pic.png (400×300) in a folder, and next to it create doc.md:
    # Title
    
    Some text before the image.
    
    ![](pic.png)
    
    Text after the image.
    
  2. Open doc.md in MarkText.
  3. Click the image once so it is selected (the toolbar and resize handles appear).
  4. Press Enter.
  5. Choose File > Save and look at the file.

Clicking somewhere in Text after the image. before step 3 gives the same result.

Expected Behavior

Enter acts on the selected image (the image selection treats Backspace, Delete and Enter as "delete image") or does nothing. The top of the document is not touched.

Actual Behavior
  • An empty paragraph is inserted above # Title, and everything shifts down one line.
  • The image stays, with its toolbar and resize handles still shown.
  • The saved file starts with two newline characters, followed by the unchanged content: \n\n# Title\n\nSome text before the image.\n\n![](pic.png)\n\nText after the image.\n.
  • The same happens when the document starts with a paragraph instead of a heading.
Screenshots / Logs

No error is shown.

Operating System

macOS

MarkText Version

0.20.0-rc.3 (also reproduced on the current develop branch)

OS Version

macOS 27.0 (arm64)

Reproducibility
  • I can reliably reproduce this issue
Related

Typing and pressing Backspace on a selected image misbehave in the same state and are reported separately: #5390 (typing opens an error dialog for every key) and #5395 (Backspace also turns the first heading into a paragraph).

Possible cause (unverified): selecting an image removes the DOM selection (TextSelection.collapse()removeAllRanges()) while the editor keeps focus, so Enter is handled at the start of the first block, which inserts an empty paragraph and switches the selection back to text. ImageSelection._handleKeydown (packages/muya/src/selection/ImageSelection.ts), which deletes the image on Backspace, Delete or Enter, then no longer sees a selected image.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the provided doc.md steps, then read packages/muya/src/selection/ImageSelection.ts and trace how Enter is handled after image selection removes the DOM selection. Done means pressing Enter on the selected image either deletes it or does nothing, without adding a paragraph at the document top or changing the saved Markdown.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
desktop, frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
74/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.