diff --git a/src/MarkdownTextInput.web.tsx b/src/MarkdownTextInput.web.tsx index a13eeac9..acdbadc8 100644 --- a/src/MarkdownTextInput.web.tsx +++ b/src/MarkdownTextInput.web.tsx @@ -328,9 +328,10 @@ const MarkdownTextInput = React.forwardRef( if (!divRef.current || !(e.target instanceof HTMLElement)) { return; } + const changedText = e.target.innerText; if (compositionRef.current) { - updateTextColor(divRef.current, e.target.innerText); + updateTextColor(divRef.current, changedText); compositionRef.current = false; return; } @@ -344,14 +345,22 @@ const MarkdownTextInput = React.forwardRef( case 'historyRedo': text = redo(divRef.current); break; + case 'insertFromPaste': + // if there is no newline at the end of the copied text, contentEditable adds invisible
tag at the end of the text, so we need to normalize it + if (changedText.length > 2 && changedText[changedText.length - 2] !== '\n' && changedText[changedText.length - 1] === '\n') { + text = parseText(divRef.current, normalizeValue(changedText), processedMarkdownStyle).text; + break; + } + text = parseText(divRef.current, changedText, processedMarkdownStyle).text; + break; default: - text = parseText(divRef.current, e.target.innerText, processedMarkdownStyle).text; + text = parseText(divRef.current, changedText, processedMarkdownStyle).text; } if (pasteRef?.current) { pasteRef.current = false; updateSelection(e); } - updateTextColor(divRef.current, e.target.innerText); + updateTextColor(divRef.current, changedText); if (onChange) { const event = e as unknown as NativeSyntheticEvent; diff --git a/src/web/InputHistory.ts b/src/web/InputHistory.ts index d94d30f9..b9b55dc1 100644 --- a/src/web/InputHistory.ts +++ b/src/web/InputHistory.ts @@ -103,7 +103,7 @@ export default class InputHistory { ? { text: previousHistoryItem.text, cursorPosition: Math.min( - (currentHistoryItem?.cursorPosition ?? 0) - ((currentHistoryItem?.text ?? '').replaceAll('\n', '').length - (previousHistoryItem?.text ?? '').replaceAll('\n', '').length), + (currentHistoryItem?.cursorPosition ?? 0) - ((currentHistoryItem?.text ?? '').length - (previousHistoryItem?.text ?? '').length), (previousHistoryItem?.text ?? '').length, ), }