mirror of https://github.com/vector-im/riot-web
Fix behaviour of modifyText
Since it was not correctly extracting the selected part of the text stringpull/21833/head
parent
8f45f168d5
commit
294a8efdc4
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue