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();
for (const block of contentBlocks) {
const blockLength = block.getLength();
if (start !== -1 && start < blockLength) {
selectionState = selectionState.merge({
anchorKey: block.getKey(),
anchorOffset: start,
});
start = -1;
} else {
start -= blockLength + 1; // +1 to account for newline between blocks
if (start !== -1) {
if (start < blockLength + 1) {
selectionState = selectionState.merge({
anchorKey: block.getKey(),
anchorOffset: start,
});
start = -1;
} else {
start -= blockLength + 1; // +1 to account for newline between blocks
}
}
if (end !== -1 && end <= blockLength) {
selectionState = selectionState.merge({
focusKey: block.getKey(),
focusOffset: end,
});
end = -1;
} else {
end -= blockLength + 1; // +1 to account for newline between blocks
if (end !== -1) {
if (end < blockLength + 1) {
selectionState = selectionState.merge({
focusKey: block.getKey(),
focusOffset: end,
});
end = -1;
} else {
end -= blockLength + 1; // +1 to account for newline between blocks
}
}
}
return selectionState;