From 294a8efdc46c1c81a43fa103c391dd6eeb350972 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Sun, 12 Jun 2016 00:50:30 +0200 Subject: [PATCH] Fix behaviour of modifyText Since it was not correctly extracting the selected part of the text string --- src/RichText.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/RichText.js b/src/RichText.js index 2334bbf2d6..d218e698a6 100644 --- a/src/RichText.js +++ b/src/RichText.js @@ -130,15 +130,27 @@ function findWithRegex(regex, contentBlock: ContentBlock, callback: (start: numb /** * Passes rangeToReplace to modifyFn and replaces it in contentState with the result. */ -export function modifyText(contentState: ContentState, rangeToReplace: SelectionState, modifyFn: (text: string) => string, ...rest): ContentState { - let startKey = rangeToReplace.getStartKey(), - endKey = contentState.getKeyAfter(rangeToReplace.getEndKey()), +export function modifyText(contentState: ContentState, rangeToReplace: SelectionState, + modifyFn: (text: string) => string, ...rest): ContentState { + let getText = (key) => contentState.getBlockForKey(key).getText(), + startKey = rangeToReplace.getStartKey(), + startOffset = rangeToReplace.getStartOffset(), + endKey = rangeToReplace.getEndKey(), + endOffset = rangeToReplace.getEndOffset(), text = ""; - for(let currentKey = startKey; currentKey && currentKey !== endKey; currentKey = contentState.getKeyAfter(currentKey)) { - let currentBlock = contentState.getBlockForKey(currentKey); - text += currentBlock.getText(); + + for(let currentKey = startKey; + currentKey && currentKey !== endKey; + currentKey = contentState.getKeyAfter(currentKey)) { + text += getText(currentKey).substring(startOffset, blockText.length); + + // from now on, we'll take whole blocks + startOffset = 0; } + // add remaining part of last block + text += getText(endKey).substring(startOffset, endOffset); + return Modifier.replaceText(contentState, rangeToReplace, modifyFn(text), ...rest); }