feat: Cmd-A inside code block should select block contents, closes #6498
This commit is contained in:
@@ -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,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
39
shared/editor/commands/selectAll.ts
Normal file
39
shared/editor/commands/selectAll.ts
Normal 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;
|
||||
};
|
||||
}
|
||||
@@ -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),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()) {
|
||||
|
||||
Reference in New Issue
Block a user