Account for `\n` after each block
when converting from text offsets to selection state. fixes vector-im/riot-web#4728pull/21833/head
parent
b632c9132c
commit
2c86086444
|
@ -201,10 +201,8 @@ export function selectionStateToTextOffsets(selectionState: SelectionState,
|
||||||
export function textOffsetsToSelectionState({start, end}: SelectionRange,
|
export function textOffsetsToSelectionState({start, end}: SelectionRange,
|
||||||
contentBlocks: Array<ContentBlock>): SelectionState {
|
contentBlocks: Array<ContentBlock>): SelectionState {
|
||||||
let selectionState = SelectionState.createEmpty();
|
let selectionState = SelectionState.createEmpty();
|
||||||
|
for (const block of contentBlocks) {
|
||||||
for (let block of contentBlocks) {
|
const blockLength = block.getLength();
|
||||||
let blockLength = block.getLength();
|
|
||||||
|
|
||||||
if (start !== -1 && start < blockLength) {
|
if (start !== -1 && start < blockLength) {
|
||||||
selectionState = selectionState.merge({
|
selectionState = selectionState.merge({
|
||||||
anchorKey: block.getKey(),
|
anchorKey: block.getKey(),
|
||||||
|
@ -212,9 +210,8 @@ export function textOffsetsToSelectionState({start, end}: SelectionRange,
|
||||||
});
|
});
|
||||||
start = -1;
|
start = -1;
|
||||||
} else {
|
} else {
|
||||||
start -= blockLength;
|
start -= blockLength + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (end !== -1 && end <= blockLength) {
|
if (end !== -1 && end <= blockLength) {
|
||||||
selectionState = selectionState.merge({
|
selectionState = selectionState.merge({
|
||||||
focusKey: block.getKey(),
|
focusKey: block.getKey(),
|
||||||
|
@ -222,10 +219,9 @@ export function textOffsetsToSelectionState({start, end}: SelectionRange,
|
||||||
});
|
});
|
||||||
end = -1;
|
end = -1;
|
||||||
} else {
|
} else {
|
||||||
end -= blockLength;
|
end -= blockLength + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return selectionState;
|
return selectionState;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue