From 2c86086444f2b9977198ba20b20e0f2dae403120 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Wed, 2 Aug 2017 10:51:34 +0100 Subject: [PATCH 1/2] Account for `\n` after each block when converting from text offsets to selection state. fixes vector-im/riot-web#4728 --- src/RichText.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/RichText.js b/src/RichText.js index c060565e2f..ff223525d4 100644 --- a/src/RichText.js +++ b/src/RichText.js @@ -201,10 +201,8 @@ export function selectionStateToTextOffsets(selectionState: SelectionState, export function textOffsetsToSelectionState({start, end}: SelectionRange, contentBlocks: Array): SelectionState { let selectionState = SelectionState.createEmpty(); - - for (let block of contentBlocks) { - let blockLength = block.getLength(); - + for (const block of contentBlocks) { + const blockLength = block.getLength(); if (start !== -1 && start < blockLength) { selectionState = selectionState.merge({ anchorKey: block.getKey(), @@ -212,9 +210,8 @@ export function textOffsetsToSelectionState({start, end}: SelectionRange, }); start = -1; } else { - start -= blockLength; + start -= blockLength + 1; } - if (end !== -1 && end <= blockLength) { selectionState = selectionState.merge({ focusKey: block.getKey(), @@ -222,10 +219,9 @@ export function textOffsetsToSelectionState({start, end}: SelectionRange, }); end = -1; } else { - end -= blockLength; + end -= blockLength + 1; } } - return selectionState; } From 1512aff326d41157c8861dd782163fd958569e39 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Wed, 2 Aug 2017 11:06:02 +0100 Subject: [PATCH 2/2] Add comments --- src/RichText.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/RichText.js b/src/RichText.js index ff223525d4..225a1c212a 100644 --- a/src/RichText.js +++ b/src/RichText.js @@ -210,7 +210,7 @@ export function textOffsetsToSelectionState({start, end}: SelectionRange, }); start = -1; } else { - start -= blockLength + 1; + start -= blockLength + 1; // +1 to account for newline between blocks } if (end !== -1 && end <= blockLength) { selectionState = selectionState.merge({ @@ -219,7 +219,7 @@ export function textOffsetsToSelectionState({start, end}: SelectionRange, }); end = -1; } else { - end -= blockLength + 1; + end -= blockLength + 1; // +1 to account for newline between blocks } } return selectionState;