import { EditorSelection } from '@codemirror/state';
import { ICommand } from '.';
export const code: ICommand = {
name: 'code',
keyCommand: 'code',
button: { 'aria-label': 'Insert code' },
icon: (
),
execute: ({ state, view }) => {
if (!state || !view) return;
view.dispatch(
view.state.changeByRange((range) => ({
changes: [
{ from: range.from, insert: '`' },
{ from: range.to, insert: '`' },
],
range: EditorSelection.range(range.from + 1, range.to + 1),
})),
);
},
};
export const codeBlock: ICommand = {
name: 'codeBlock',
keyCommand: 'codeBlock',
button: { 'aria-label': 'Insert Code Block' },
icon: (
),
execute: ({ state, view }) => {
if (!state || !view) return;
const main = view.state.selection.main;
const txt = view.state.sliceDoc(view.state.selection.main.from, view.state.selection.main.to);
view.dispatch({
changes: {
from: main.from,
to: main.to,
insert: `\`\`\`js\n${txt}\n\`\`\``,
},
selection: EditorSelection.range(main.from + 3, main.from + 5),
});
},
};