fix: Not possible to collapse code block at beginning of doc (#12381)

This commit is contained in:
Tom Moor
2026-05-18 22:00:45 -04:00
committed by GitHub
parent 597b6d801c
commit d2b719a5a9
+9 -1
View File
@@ -480,7 +480,7 @@ export default class CodeFence extends Node<CodeFenceOptions> {
// Click handler for toggle button + auto-expand on focus
new Plugin({
key: new PluginKey("collapse-toggle"),
appendTransaction: (transactions, _oldState, newState) => {
appendTransaction: (transactions, oldState, newState) => {
const hasCollapseMeta = transactions.some((tr) =>
tr.getMeta(collapseKey)
);
@@ -498,6 +498,14 @@ export default class CodeFence extends Node<CodeFenceOptions> {
return null;
}
// Only auto-expand when the selection moved INTO the block. If the
// selection was already inside this block (e.g. after the user just
// clicked Collapse while the cursor was inside), don't re-expand.
const oldCodeBlock = findParentNode(isCode)(oldState.selection);
if (oldCodeBlock?.pos === codeBlock.pos) {
return null;
}
return newState.tr
.setMeta(collapseKey, { expand: codeBlock.pos })
.setMeta("addToHistory", false);