${html2Escape(\n markdown || '',\n )}
`,\n );\n return ;\n }, [highlightEnable, markdown, preRef, prefixCls]);\n}\n","import { TextRange } from '../commands';\nimport getSurroundingWord from './getSurroundingWord';\n\nexport interface TextSection {\n text: string;\n selection: TextRange;\n}\n\nexport function selectWord({ text, selection }: TextSection): TextRange {\n if (text && text.length && selection.start === selection.end) {\n // the user is pointing to a word\n return getSurroundingWord(text, selection.start);\n }\n return selection;\n}\n\n/**\n * Gets the number of line-breaks that would have to be inserted before the given 'startPosition'\n * to make sure there's an empty line between 'startPosition' and the previous text\n */\nexport function getBreaksNeededForEmptyLineBefore(text = '', startPosition: number): number {\n if (startPosition === 0) return 0;\n\n // rules:\n // - If we're in the first line, no breaks are needed\n // - Otherwise there must be 2 breaks before the previous character. Depending on how many breaks exist already, we\n // may need to insert 0, 1 or 2 breaks\n\n let neededBreaks = 2;\n let isInFirstLine = true;\n for (let i = startPosition - 1; i >= 0 && neededBreaks >= 0; i--) {\n switch (text.charCodeAt(i)) {\n case 32: // blank space\n continue;\n case 10: // line break\n neededBreaks--;\n isInFirstLine = false;\n break;\n default:\n return neededBreaks;\n }\n }\n return isInFirstLine ? 0 : neededBreaks;\n}\n\n/**\n * Gets the number of line-breaks that would have to be inserted after the given 'startPosition'\n * to make sure there's an empty line between 'startPosition' and the next text\n */\nexport function getBreaksNeededForEmptyLineAfter(text = '', startPosition: number): number {\n if (startPosition === text.length - 1) return 0;\n\n // rules:\n // - If we're in the first line, no breaks are needed\n // - Otherwise there must be 2 breaks before the previous character. Depending on how many breaks exist already, we\n // may need to insert 0, 1 or 2 breaks\n\n let neededBreaks = 2;\n let isInLastLine = true;\n for (let i = startPosition; i < text.length && neededBreaks >= 0; i++) {\n switch (text.charCodeAt(i)) {\n case 32:\n continue;\n case 10: {\n neededBreaks--;\n isInLastLine = false;\n break;\n }\n default:\n return neededBreaks;\n }\n }\n return isInLastLine ? 0 : neededBreaks;\n}\n","import { TextRange } from '../commands';\n\nexport default function getSurroundingWord(text: string, position: number): TextRange {\n if (!text) throw Error(\"Argument 'text' should be truthy\");\n\n const isWordDelimiter = (c: string) => c === ' ' || c.charCodeAt(0) === 10;\n\n // leftIndex is initialized to 0 because if selection is 0, it won't even enter the iteration\n let start = 0;\n // rightIndex is initialized to text.length because if selection is equal to text.length it won't even enter the interation\n let end = text.length;\n\n // iterate to the left\n for (let i = position; i - 1 > -1; i--) {\n if (isWordDelimiter(text[i - 1])) {\n start = i;\n break;\n }\n }\n\n // iterate to the right\n for (let i = position; i < text.length; i++) {\n if (isWordDelimiter(text[i])) {\n end = i;\n break;\n }\n }\n\n return { start, end };\n}\n","import * as React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\nimport { selectWord } from '../utils/markdownUtils';\n\nexport const bold: ICommand = {\n name: 'bold',\n keyCommand: 'bold',\n shortcuts: 'ctrlcmd+b',\n buttonProps: { 'aria-label': 'Add bold text', title: 'Add bold text' },\n icon: (\n \n ),\n execute: (state: TextState, api: TextAreaTextApi) => {\n // Adjust the selection to encompass the whole word if the caret is inside one\n const newSelectionRange = selectWord({ text: state.text, selection: state.selection });\n const state1 = api.setSelectionRange(newSelectionRange);\n // Replaces the current selection with the bold mark up\n const state2 = api.replaceSelection(`**${state1.selectedText}**`);\n // Adjust the selection to not contain the **\n api.setSelectionRange({\n start: state2.selection.end - 2 - state1.selectedText.length,\n end: state2.selection.end - 2,\n });\n },\n};\n","import * as React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\nimport {\n selectWord,\n getBreaksNeededForEmptyLineBefore,\n getBreaksNeededForEmptyLineAfter,\n} from '../utils/markdownUtils';\n\nexport const code: ICommand = {\n name: 'code',\n keyCommand: 'code',\n shortcuts: 'ctrlcmd+j',\n buttonProps: { 'aria-label': 'Insert code' },\n icon: (\n \n ),\n execute: (tate: TextState, api: TextAreaTextApi) => {\n // Adjust the selection to encompass the whole word if the caret is inside one\n const newSelectionRange = selectWord({ text: tate.text, selection: tate.selection });\n const state1 = api.setSelectionRange(newSelectionRange);\n // when there's no breaking line\n if (state1.selectedText.indexOf('\\n') === -1) {\n api.replaceSelection(`\\`${state1.selectedText}\\``);\n // Adjust the selection to not contain the **\n\n const selectionStart = state1.selection.start + 1;\n const selectionEnd = selectionStart + state1.selectedText.length;\n\n api.setSelectionRange({\n start: selectionStart,\n end: selectionEnd,\n });\n return;\n }\n\n const breaksBeforeCount = getBreaksNeededForEmptyLineBefore(state1.text, state1.selection.start);\n const breaksBefore = Array(breaksBeforeCount + 1).join('\\n');\n\n const breaksAfterCount = getBreaksNeededForEmptyLineAfter(state1.text, state1.selection.end);\n const breaksAfter = Array(breaksAfterCount + 1).join('\\n');\n\n api.replaceSelection(`${breaksBefore}\\`\\`\\`\\n${state1.selectedText}\\n\\`\\`\\`${breaksAfter}`);\n\n const selectionStart = state1.selection.start + breaksBeforeCount + 4;\n const selectionEnd = selectionStart + state1.selectedText.length;\n\n api.setSelectionRange({\n start: selectionStart,\n end: selectionEnd,\n });\n },\n};\n\nexport const codeBlock: ICommand = {\n name: 'codeBlock',\n keyCommand: 'codeBlock',\n shortcuts: 'ctrlcmd+shift+j',\n execute: (tate: TextState, api: TextAreaTextApi) => {\n // Adjust the selection to encompass the whole word if the caret is inside one\n const newSelectionRange = selectWord({ text: tate.text, selection: tate.selection });\n const state1 = api.setSelectionRange(newSelectionRange);\n\n const breaksBeforeCount = getBreaksNeededForEmptyLineBefore(state1.text, state1.selection.start);\n const breaksBefore = Array(breaksBeforeCount + 1).join('\\n');\n\n const breaksAfterCount = getBreaksNeededForEmptyLineAfter(state1.text, state1.selection.end);\n const breaksAfter = Array(breaksAfterCount + 1).join('\\n');\n\n api.replaceSelection(`${breaksBefore}\\`\\`\\`\\n${state1.selectedText}\\n\\`\\`\\`${breaksAfter}`);\n\n const selectionStart = state1.selection.start + breaksBeforeCount + 4;\n const selectionEnd = selectionStart + state1.selectedText.length;\n\n api.setSelectionRange({\n start: selectionStart,\n end: selectionEnd,\n });\n },\n};\n","import * as React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\nimport { selectWord } from '../utils/markdownUtils';\n\nexport const italic: ICommand = {\n name: 'italic',\n keyCommand: 'italic',\n shortcuts: 'ctrlcmd+i',\n buttonProps: { 'aria-label': 'Add italic text', title: 'Add italic text' },\n icon: (\n \n ),\n execute: (state: TextState, api: TextAreaTextApi) => {\n // Adjust the selection to encompass the whole word if the caret is inside one\n const newSelectionRange = selectWord({ text: state.text, selection: state.selection });\n const state1 = api.setSelectionRange(newSelectionRange);\n // Replaces the current selection with the italic mark up\n const state2 = api.replaceSelection(`*${state1.selectedText}*`);\n // Adjust the selection to not contain the *\n api.setSelectionRange({\n start: state2.selection.end - 1 - state1.selectedText.length,\n end: state2.selection.end - 1,\n });\n },\n};\n","import * as React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\nimport { selectWord } from '../utils/markdownUtils';\n\nexport const link: ICommand = {\n name: 'link',\n keyCommand: 'link',\n shortcuts: 'ctrlcmd+k',\n buttonProps: { 'aria-label': 'Add a link', title: 'Add a link' },\n icon: (\n \n ),\n execute: (state: TextState, api: TextAreaTextApi) => {\n // Adjust the selection to encompass the whole word if the caret is inside one\n const newSelectionRange = selectWord({ text: state.text, selection: state.selection });\n const state1 = api.setSelectionRange(newSelectionRange);\n // Replaces the current selection with the bold mark up\n const state2 = api.replaceSelection(`[${state1.selectedText}](url)`);\n // Adjust the selection to not contain the **\n api.setSelectionRange({\n start: state2.selection.end - 6 - state1.selectedText.length,\n end: state2.selection.end - 6,\n });\n },\n};\n","import * as React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\nimport {\n selectWord,\n getBreaksNeededForEmptyLineBefore,\n getBreaksNeededForEmptyLineAfter,\n} from '../utils/markdownUtils';\n\nexport type AlterLineFunction = (line: string, index: number) => string;\n\n/**\n * Inserts insertionString before each line\n */\nexport function insertBeforeEachLine(\n selectedText: string,\n insertBefore: string | AlterLineFunction,\n): { modifiedText: string; insertionLength: number } {\n const lines = selectedText.split(/\\n/);\n\n let insertionLength = 0;\n const modifiedText = lines\n .map((item, index) => {\n if (typeof insertBefore === 'string') {\n insertionLength += insertBefore.length;\n return insertBefore + item;\n } else if (typeof insertBefore === 'function') {\n const insertionResult = insertBefore(item, index);\n insertionLength += insertionResult.length;\n return insertBefore(item, index) + item;\n }\n throw Error('insertion is expected to be either a string or a function');\n })\n .join('\\n');\n\n return { modifiedText, insertionLength };\n}\n\nexport const makeList = (state: TextState, api: TextAreaTextApi, insertBefore: string | AlterLineFunction) => {\n // Adjust the selection to encompass the whole word if the caret is inside one\n const newSelectionRange = selectWord({ text: state.text, selection: state.selection });\n const state1 = api.setSelectionRange(newSelectionRange);\n\n const breaksBeforeCount = getBreaksNeededForEmptyLineBefore(state1.text, state1.selection.start);\n const breaksBefore = Array(breaksBeforeCount + 1).join('\\n');\n\n const breaksAfterCount = getBreaksNeededForEmptyLineAfter(state1.text, state1.selection.end);\n const breaksAfter = Array(breaksAfterCount + 1).join('\\n');\n\n const modifiedText = insertBeforeEachLine(state1.selectedText, insertBefore);\n\n api.replaceSelection(`${breaksBefore}${modifiedText.modifiedText}${breaksAfter}`);\n\n // Specifically when the text has only one line, we can exclude the \"- \", for example, from the selection\n const oneLinerOffset = state1.selectedText.indexOf('\\n') === -1 ? modifiedText.insertionLength : 0;\n\n const selectionStart = state1.selection.start + breaksBeforeCount + oneLinerOffset;\n const selectionEnd = selectionStart + modifiedText.modifiedText.length - oneLinerOffset;\n\n // Adjust the selection to not contain the **\n api.setSelectionRange({\n start: selectionStart,\n end: selectionEnd,\n });\n};\n\nexport const unorderedListCommand: ICommand = {\n name: 'unordered-list',\n keyCommand: 'list',\n shortcuts: 'ctrl+shift+l',\n buttonProps: { 'aria-label': 'Add unordered list', title: 'Add unordered list' },\n icon: (\n \n ),\n execute: (state: TextState, api: TextAreaTextApi) => {\n makeList(state, api, '- ');\n },\n};\n\nexport const orderedListCommand: ICommand = {\n name: 'ordered-list',\n keyCommand: 'list',\n shortcuts: 'ctrl+shift+o',\n buttonProps: { 'aria-label': 'Add ordered list', title: 'Add ordered list' },\n icon: (\n \n ),\n execute: (state: TextState, api: TextAreaTextApi) => {\n makeList(state, api, (item, index) => `${index + 1}. `);\n },\n};\n\nexport const checkedListCommand: ICommand = {\n name: 'checked-list',\n keyCommand: 'list',\n shortcuts: 'ctrl+shift+c',\n buttonProps: { 'aria-label': 'Add checked list', title: 'Add checked list' },\n icon: (\n \n ),\n execute: (state: TextState, api: TextAreaTextApi) => {\n makeList(state, api, (item, index) => `- [ ] `);\n },\n};\n","/**\n * The MIT License\n * Copyright (c) 2018 Dmitriy Kubyshkin\n * Copied from https://github.com/grassator/insert-text-at-cursor\n */\n\nlet browserSupportsTextareaTextNodes: any;\n\n/**\n * @param {HTMLElement} input\n * @return {boolean}\n */\nfunction canManipulateViaTextNodes(input: HTMLTextAreaElement | HTMLInputElement) {\n if (input.nodeName !== 'TEXTAREA') {\n return false;\n }\n if (typeof browserSupportsTextareaTextNodes === 'undefined') {\n const textarea: HTMLTextAreaElement = document.createElement('textarea');\n textarea.value = '1';\n browserSupportsTextareaTextNodes = !!textarea.firstChild;\n }\n return browserSupportsTextareaTextNodes;\n}\n\n/**\n * @param {HTMLTextAreaElement|HTMLInputElement} input\n * @param {string} text\n * @returns {void}\n */\nexport default function insertTextAtPosition(input: HTMLTextAreaElement | HTMLInputElement, text: string) {\n // Most of the used APIs only work with the field selected\n input.focus();\n\n // IE 8-10\n if ((document as any).selection) {\n const ieRange = (document as any).selection.createRange();\n ieRange.text = text;\n\n // Move cursor after the inserted text\n ieRange.collapse(false /* to the end */);\n ieRange.select();\n\n return;\n }\n\n // Webkit + Edge\n const isSuccess = document.execCommand('insertText', false, text);\n if (!isSuccess) {\n const start = input.selectionStart!;\n const end = input.selectionEnd!;\n // Firefox (non-standard method)\n if (typeof input.setRangeText === 'function') {\n input.setRangeText(text);\n } else {\n // To make a change we just need a Range, not a Selection\n const range = document.createRange();\n const textNode = document.createTextNode(text);\n\n if (canManipulateViaTextNodes(input)) {\n let node = input.firstChild;\n\n // If textarea is empty, just insert the text\n if (!node) {\n input.appendChild(textNode);\n } else {\n // Otherwise we need to find a nodes for start and end\n let offset = 0;\n let startNode = null;\n let endNode = null;\n\n while (node && (startNode === null || endNode === null)) {\n const nodeLength = node.nodeValue!.length;\n\n // if start of the selection falls into current node\n if (start >= offset && start <= offset + nodeLength) {\n range.setStart((startNode = node), start - offset);\n }\n\n // if end of the selection falls into current node\n if (end >= offset && end <= offset + nodeLength) {\n range.setEnd((endNode = node), end - offset);\n }\n\n offset += nodeLength;\n node = node.nextSibling;\n }\n\n // If there is some text selected, remove it as we should replace it\n if (start !== end) {\n range.deleteContents();\n }\n }\n }\n\n // If the node is a textarea and the range doesn't span outside the element\n //\n // Get the commonAncestorContainer of the selected range and test its type\n // If the node is of type `#text` it means that we're still working with text nodes within our textarea element\n // otherwise, if it's of type `#document` for example it means our selection spans outside the textarea.\n if (canManipulateViaTextNodes(input) && range.commonAncestorContainer.nodeName === '#text') {\n // Finally insert a new node. The browser will automatically split start and end nodes into two if necessary\n range.insertNode(textNode);\n } else {\n // If the node is not a textarea or the range spans outside a textarea the only way is to replace the whole value\n const value = input.value;\n input.value = value.slice(0, start) + text + value.slice(end);\n }\n }\n\n // Correct the cursor position to be at the end of the insertion\n input.setSelectionRange(start + text.length, start + text.length);\n\n // Notify any possible listeners of the change\n const e = document.createEvent('UIEvent');\n e.initEvent('input', true, false);\n input.dispatchEvent(e);\n }\n}\n","import * as React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\nimport {\n getBreaksNeededForEmptyLineBefore,\n getBreaksNeededForEmptyLineAfter,\n selectWord,\n} from '../utils/markdownUtils';\n\nexport const quote: ICommand = {\n name: 'quote',\n keyCommand: 'quote',\n shortcuts: 'ctrlcmd+q',\n buttonProps: { 'aria-label': 'Insert a quote', title: 'Insert a quote' },\n icon: (\n \n ),\n execute: (state: TextState, api: TextAreaTextApi) => {\n // Adjust the selection to encompass the whole word if the caret is inside one\n const newSelectionRange = selectWord({ text: state.text, selection: state.selection });\n const state1 = api.setSelectionRange(newSelectionRange);\n const breaksBeforeCount = getBreaksNeededForEmptyLineBefore(state1.text, state1.selection.start);\n const breaksBefore = Array(breaksBeforeCount + 1).join('\\n');\n\n const breaksAfterCount = getBreaksNeededForEmptyLineAfter(state1.text, state1.selection.end);\n const breaksAfter = Array(breaksAfterCount + 1).join('\\n');\n\n // Replaces the current selection with the quote mark up\n api.replaceSelection(`${breaksBefore}> ${state1.selectedText}${breaksAfter}`);\n\n const selectionStart = state1.selection.start + breaksBeforeCount + 2;\n const selectionEnd = selectionStart + state1.selectedText.length;\n\n api.setSelectionRange({\n start: selectionStart,\n end: selectionEnd,\n });\n },\n};\n","import * as React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const hr: ICommand = {\n name: 'hr',\n keyCommand: 'hr',\n shortcuts: 'ctrlcmd+h',\n buttonProps: { 'aria-label': 'Insert HR', title: 'Insert HR' },\n icon: (\n \n ),\n execute: (state: TextState, api: TextAreaTextApi) => {\n api.replaceSelection(`${state.selectedText}\\n\\n----------\\n\\n`);\n },\n};\n","import * as React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title2: ICommand = {\n name: 'title2',\n keyCommand: 'title2',\n shortcuts: 'ctrlcmd+2',\n buttonProps: { 'aria-label': 'Insert title2', title: 'Insert title 2' },\n icon: