Skip to content

fix: prevent code block deletion when adding inline comments in read mode - #2146

Merged
Philipinho merged 1 commit into
docmost:mainfrom
mayhemking007:fix/codeblock-comment-deletion
May 4, 2026
Merged

Philipinho merged 1 commit into
docmost:mainfrom
mayhemking007:fix/codeblock-comment-deletion

Conversation

@mayhemking007

Copy link
Copy Markdown
Contributor

Fix: prevent code block deletion when adding inline comments in read mode

Summary

Fixes an issue where adding an inline comment in read mode on text inside a codeBlock causes the entire code block to be deleted.


Root Cause

Inline comments in read mode use yjsSelection, which is passed to the backend and applied via:

  • setCommentMark → setYjsMark → applyMarkToYFragment

This directly applies a mark to Yjs text nodes:

item.format(...)

However, codeBlock nodes do not allow marks in the ProseMirror schema.
Applying marks inside them leads to an invalid document state and results in the code block being removed.

Fix

Skip applying comment marks when the selection is inside a codeBlock.

This is done by detecting the parent node (nodeName) in applyMarkToYFragment and avoiding item.format(...) when inside a codeBlock.

Result

✅ Code block no longer gets deleted in read mode
✅ Paragraph inline comments continue to work (highlight + scroll)
⚠️ Inline highlighting inside code blocks is not supported (consistent with existing edit-mode behavior)

After Change -

AfterChange.mp4

Alternative considered (frontend-only fix)

I also tested skipping yjsSelection in the frontend:

if (!isCodeBlockSelection) {
  payload.yjsSelection = readOnlyCommentData.yjsSelection;
}

This approach was not chosen because:

  • It only protects one UI path
  • The issue originates from the backend applying invalid marks

Handling it in the backend ensures:

  • Centralized safety
  • No regression in existing functionality

Notes

This PR focuses on preventing document corruption.

Inline highlighting inside code blocks is currently not supported (also not working in edit mode). Supporting it would require changes to the schema or decoration.

Happy to explore that separately if needed.

Fixes #2122

@Philipinho
Philipinho merged commit fe18f22 into docmost:main May 4, 2026
1 check passed
@Philipinho

Copy link
Copy Markdown
Member

Thanks for contributing.

Yundi339 pushed a commit to Yundi339/docmost that referenced this pull request Jun 18, 2026
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.

[BUG] Codeblock get deleted in Readmode when writing a Comment

2 participants