From 844ca249d0276691436e0b8d8fc2388756b386db Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Mon, 14 Aug 2017 17:31:16 +0100 Subject: [PATCH 1/3] 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 --- src/RichText.js | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/RichText.js b/src/RichText.js index 9876fcc93f..8d1177cdd2 100644 --- a/src/RichText.js +++ b/src/RichText.js @@ -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; From 5f00bbbff6e87968eac0d22e12e45f58b17a7632 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Tue, 15 Aug 2017 09:22:50 +0100 Subject: [PATCH 2/3] Add comments --- src/RichText.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/RichText.js b/src/RichText.js index 8d1177cdd2..a34f8b6b30 100644 --- a/src/RichText.js +++ b/src/RichText.js @@ -204,24 +204,26 @@ export function textOffsetsToSelectionState({start, end}: SelectionRange, let selectionState = SelectionState.createEmpty(); for (const block of contentBlocks) { const blockLength = block.getLength(); + // -1 indicating that the position start position has been found if (start !== -1) { if (start < blockLength + 1) { selectionState = selectionState.merge({ anchorKey: block.getKey(), anchorOffset: start, }); - start = -1; + start = -1; // selection state for the start calculated } else { start -= blockLength + 1; // +1 to account for newline between blocks } } + // -1 indicating that the position end position has been found if (end !== -1) { if (end < blockLength + 1) { selectionState = selectionState.merge({ focusKey: block.getKey(), focusOffset: end, }); - end = -1; + end = -1; // selection state for the start calculated } else { end -= blockLength + 1; // +1 to account for newline between blocks } From 65dc9fda6e774584db572eefca5b0ed9e566a52f Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Tue, 15 Aug 2017 09:25:23 +0100 Subject: [PATCH 3/3] Alter comments --- src/RichText.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/RichText.js b/src/RichText.js index a34f8b6b30..cbd3b9ae18 100644 --- a/src/RichText.js +++ b/src/RichText.js @@ -202,6 +202,9 @@ export function selectionStateToTextOffsets(selectionState: SelectionState, export function textOffsetsToSelectionState({start, end}: SelectionRange, contentBlocks: Array): SelectionState { let selectionState = SelectionState.createEmpty(); + // Subtract block lengths from `start` and `end` until they are less than the current + // block length (accounting for the NL at the end of each block). Set them to -1 to + // indicate that the corresponding selection state has been determined. for (const block of contentBlocks) { const blockLength = block.getLength(); // -1 indicating that the position start position has been found @@ -223,7 +226,7 @@ export function textOffsetsToSelectionState({start, end}: SelectionRange, focusKey: block.getKey(), focusOffset: end, }); - end = -1; // selection state for the start calculated + end = -1; // selection state for the end calculated } else { end -= blockLength + 1; // +1 to account for newline between blocks }