import * as React from 'react'; import { ICommand, TextState, TextApi } from './'; import { selectWord } from '../utils/markdownUtils'; export const strikethrough: ICommand = { name: 'strikethrough', keyCommand: 'strikethrough', shortcuts: 'ctrl+shift+x', buttonProps: { 'aria-label': 'Add strikethrough text', title: 'Add strikethrough 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 strikethrough 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, }); }, };