feat: Cmd-A inside code block should select block contents, closes #6498

This commit is contained in:
Tom Moor
2024-02-06 20:44:38 -05:00
parent 7bf403356a
commit 68d4041b1c
4 changed files with 51 additions and 2 deletions

View File

@@ -174,13 +174,13 @@ export default function formattingMenuItems(
},
{
name: "separator",
visible: isCode,
visible: isCode && !isCodeBlock,
},
{
name: "copyToClipboard",
icon: <CopyIcon />,
tooltip: dictionary.copy,
visible: isCode,
visible: isCode && !isCodeBlock,
},
];
}

View File

@@ -0,0 +1,39 @@
import { NodeType } from "prosemirror-model";
import { Command, TextSelection } from "prosemirror-state";
import { findParentNode } from "../queries/findParentNode";
/**
* Selects all the content of the given node type.
*
* @param type The node type
* @returns A prosemirror command.
*/
export function selectAll(type: NodeType): Command {
return (state, dispatch) => {
const code = findParentNode((node) => node.type === type)(state.selection);
if (code) {
const start = code.pos;
const end = code.pos + code.node.nodeSize;
if (
start === state.selection.from - 1 &&
end === state.selection.to + 1
) {
return false;
}
dispatch?.(
state.tr.setSelection(
TextSelection.between(
state.doc.resolve(start),
state.doc.resolve(end)
)
)
);
return true;
}
return false;
};
}

View File

@@ -1,3 +1,5 @@
import { NodeType } from "prosemirror-model";
import { selectAll } from "../commands/selectAll";
import CodeFence from "./CodeFence";
export default class CodeBlock extends CodeFence {
@@ -8,4 +10,10 @@ export default class CodeBlock extends CodeFence {
get markdownToken() {
return "code_block";
}
keys({ type }: { type: NodeType }) {
return {
"Mod-a": selectAll(type),
};
}
}

View File

@@ -68,6 +68,7 @@ import {
moveToNextNewline,
moveToPreviousNewline,
} from "../commands/codeFence";
import { selectAll } from "../commands/selectAll";
import toggleBlockType from "../commands/toggleBlockType";
import Mermaid from "../extensions/Mermaid";
import Prism from "../extensions/Prism";
@@ -255,6 +256,7 @@ export default class CodeFence extends Node {
return newlineInCode(state, dispatch);
},
"Shift-Enter": newlineInCode,
"Mod-a": selectAll(type),
};
if (isMac()) {