Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 844ca24

Browse files
author
Luke Barnard
committed
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 element-hq/element-web#4792 fixes element-hq/element-web#4792
1 parent 7d10a75 commit 844ca24

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/RichText.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -204,23 +204,27 @@ export function textOffsetsToSelectionState({start, end}: SelectionRange,
204204
let selectionState = SelectionState.createEmpty();
205205
for (const block of contentBlocks) {
206206
const blockLength = block.getLength();
207-
if (start !== -1 && start < blockLength) {
208-
selectionState = selectionState.merge({
209-
anchorKey: block.getKey(),
210-
anchorOffset: start,
211-
});
212-
start = -1;
213-
} else {
214-
start -= blockLength + 1; // +1 to account for newline between blocks
207+
if (start !== -1) {
208+
if (start < blockLength + 1) {
209+
selectionState = selectionState.merge({
210+
anchorKey: block.getKey(),
211+
anchorOffset: start,
212+
});
213+
start = -1;
214+
} else {
215+
start -= blockLength + 1; // +1 to account for newline between blocks
216+
}
215217
}
216-
if (end !== -1 && end <= blockLength) {
217-
selectionState = selectionState.merge({
218-
focusKey: block.getKey(),
219-
focusOffset: end,
220-
});
221-
end = -1;
222-
} else {
223-
end -= blockLength + 1; // +1 to account for newline between blocks
218+
if (end !== -1) {
219+
if (end < blockLength + 1) {
220+
selectionState = selectionState.merge({
221+
focusKey: block.getKey(),
222+
focusOffset: end,
223+
});
224+
end = -1;
225+
} else {
226+
end -= blockLength + 1; // +1 to account for newline between blocks
227+
}
224228
}
225229
}
226230
return selectionState;

0 commit comments

Comments
 (0)