Fix bugs in textOffsetsToSelectionState

This just had some thinkos in it. Namely the conditionals were slightly wrong and this lead to negative offset selection state being returned, causing vector-im/riot-web#4792

fixes vector-im/riot-web#4792
pull/21833/head
Luke Barnard 2017-08-14 17:31:16 +01:00
parent 7d10a7556b
commit 844ca249d0
1 changed files with 20 additions and 16 deletions

View File

@ -204,23 +204,27 @@ export function textOffsetsToSelectionState({start, end}: SelectionRange,
let selectionState = SelectionState.createEmpty(); let selectionState = SelectionState.createEmpty();
for (const block of contentBlocks) { for (const block of contentBlocks) {
const blockLength = block.getLength(); const blockLength = block.getLength();
if (start !== -1 && start < blockLength) { if (start !== -1) {
selectionState = selectionState.merge({ if (start < blockLength + 1) {
anchorKey: block.getKey(), selectionState = selectionState.merge({
anchorOffset: start, anchorKey: block.getKey(),
}); anchorOffset: start,
start = -1; });
} else { start = -1;
start -= blockLength + 1; // +1 to account for newline between blocks } else {
start -= blockLength + 1; // +1 to account for newline between blocks
}
} }
if (end !== -1 && end <= blockLength) { if (end !== -1) {
selectionState = selectionState.merge({ if (end < blockLength + 1) {
focusKey: block.getKey(), selectionState = selectionState.merge({
focusOffset: end, focusKey: block.getKey(),
}); focusOffset: end,
end = -1; });
} else { end = -1;
end -= blockLength + 1; // +1 to account for newline between blocks } else {
end -= blockLength + 1; // +1 to account for newline between blocks
}
} }
} }
return selectionState; return selectionState;