import * as React from 'react'; import { ICommand, TextState, TextApi } from './'; import { selectWord } from '../utils/markdownUtils'; export const bold: ICommand = { name: 'bold', keyCommand: 'bold', shortcuts: 'ctrlcmd+b', buttonProps: { 'aria-label': 'Add bold text', title: 'Add bold text' }, icon: ( ), execute: (state: TextState, api: TextApi) => { // Adjust the selection to encompass the whole word if the caret is inside one const newSelectionRange = selectWord({ text: state.text, selection: state.selection }); const state1 = api.setSelectionRange(newSelectionRange); // Replaces the current selection with the bold mark up const state2 = api.replaceSelection(`**${state1.selectedText}**`); // Adjust the selection to not contain the ** api.setSelectionRange({ start: state2.selection.end - 2 - state1.selectedText.length, end: state2.selection.end - 2, }); }, };