fix: Automatically expand code block if find result is within (#12346)

This commit is contained in:
Tom Moor
2026-05-13 17:18:16 -04:00
committed by GitHub
parent 9603e6d7c8
commit 0d76dfc9f4
2 changed files with 37 additions and 0 deletions
+14
View File
@@ -163,6 +163,7 @@ export default class FindAndReplaceExtension extends Extension<FindAndReplaceOpt
dispatch?.(state.tr.setMeta(pluginKey, {}));
this.expandFoldedTogglesForCurrentMatch();
this.expandCollapsedCodeBlockForCurrentMatch();
this.scrollToCurrentMatch();
return true;
@@ -213,6 +214,7 @@ export default class FindAndReplaceExtension extends Extension<FindAndReplaceOpt
dispatch?.(state.tr.setMeta(pluginKey, {}));
this.expandFoldedTogglesForCurrentMatch();
this.expandCollapsedCodeBlockForCurrentMatch();
this.scrollToCurrentMatch();
return true;
};
@@ -298,6 +300,18 @@ export default class FindAndReplaceExtension extends Extension<FindAndReplaceOpt
});
}
/**
* Expand a collapsed code block if it contains the current match.
*/
private expandCollapsedCodeBlockForCurrentMatch() {
const result = this.results[this.currentResultIndex];
if (!result) {
return;
}
this.editor.commands.expandCodeBlockAt(result.from);
}
private rebaseNextResult(replace: string, index: number, lastOffset = 0) {
const nextIndex = index + 1;
+23
View File
@@ -244,6 +244,29 @@ export default class CodeFence extends Node<CodeFenceOptions> {
...attrs,
});
},
expandCodeBlockAt:
(pos: number): Command =>
(state, dispatch) => {
const $pos = state.doc.resolve(pos);
const codeBlock = findParentNodeClosestToPos($pos, isCode);
if (!codeBlock) {
return false;
}
const collapseState = CodeFence.collapseKey.getState(state);
if (!collapseState?.collapsedBlocks.has(codeBlock.pos)) {
return false;
}
if (dispatch) {
dispatch(
state.tr
.setMeta(CodeFence.collapseKey, { expand: codeBlock.pos })
.setMeta("addToHistory", false)
);
}
return true;
},
toggleCodeBlockCollapse: (): Command => (state, dispatch) => {
const codeBlock = findParentNode(isCode)(state.selection);
if (!codeBlock) {