diff --git a/shared/editor/nodes/CodeFence.ts b/shared/editor/nodes/CodeFence.ts index 68de028c39..bca0828da6 100644 --- a/shared/editor/nodes/CodeFence.ts +++ b/shared/editor/nodes/CodeFence.ts @@ -480,7 +480,7 @@ export default class CodeFence extends Node { // 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 { 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);