import * as React from 'react'; import { ICommand, TextState, TextApi } from './'; import { selectWord } from '../utils/markdownUtils'; export const link: ICommand = { name: 'link', keyCommand: 'link', shortcuts: 'ctrlcmd+k', buttonProps: { 'aria-label': 'Add a link', title: 'Add a link' }, 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}](url)`); // Adjust the selection to not contain the ** api.setSelectionRange({ start: state2.selection.end - 6 - state1.selectedText.length, end: state2.selection.end - 6, }); }, };