Fix behaviour of modifyText

Since it was not correctly extracting the selected part of the text
string
pull/21833/head
Pedro Ferreira 2016-06-12 00:50:30 +02:00
parent 8f45f168d5
commit 294a8efdc4
1 changed files with 18 additions and 6 deletions

View File

@ -130,15 +130,27 @@ function findWithRegex(regex, contentBlock: ContentBlock, callback: (start: numb
/** /**
* Passes rangeToReplace to modifyFn and replaces it in contentState with the result. * 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 { export function modifyText(contentState: ContentState, rangeToReplace: SelectionState,
let startKey = rangeToReplace.getStartKey(), modifyFn: (text: string) => string, ...rest): ContentState {
endKey = contentState.getKeyAfter(rangeToReplace.getEndKey()), let getText = (key) => contentState.getBlockForKey(key).getText(),
startKey = rangeToReplace.getStartKey(),
startOffset = rangeToReplace.getStartOffset(),
endKey = rangeToReplace.getEndKey(),
endOffset = rangeToReplace.getEndOffset(),
text = ""; text = "";
for(let currentKey = startKey; currentKey && currentKey !== endKey; currentKey = contentState.getKeyAfter(currentKey)) {
let currentBlock = contentState.getBlockForKey(currentKey); for(let currentKey = startKey;
text += currentBlock.getText(); 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); return Modifier.replaceText(contentState, rangeToReplace, modifyFn(text), ...rest);
} }