Skip to content

Commit 8899cd1

Browse files
authored
imp(dom-adapters): fix caret adapter selection comparison (#103)
1 parent 01c883b commit 8899cd1

File tree

1 file changed

+13
-5
lines changed
  • packages/dom-adapters/src/CaretAdapter

1 file changed

+13
-5
lines changed

packages/dom-adapters/src/CaretAdapter/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,22 @@ export class CaretAdapter extends EventTarget {
236236
}
237237

238238
const selection = document.getSelection()!;
239-
const currentRange = selection.getRangeAt(0);
239+
240+
let absoluteStartOffset = getAbsoluteRangeOffset(input, selection.anchorNode!, selection.anchorOffset);
241+
let absoluteEndOffset = getAbsoluteRangeOffset(input, selection.focusNode!, selection.focusOffset);
242+
243+
/**
244+
* For right-to-left selection, we need to swap start and end offsets to compare with model range
245+
*/
246+
if (absoluteStartOffset > absoluteEndOffset) {
247+
[absoluteStartOffset, absoluteEndOffset] = [absoluteEndOffset, absoluteStartOffset];
248+
}
240249

241250
const start = getBoundaryPointByAbsoluteOffset(input, textRange[0]);
242251
const end = getBoundaryPointByAbsoluteOffset(input, textRange[1]);
243252

244-
const isStartEqualsCurrent = start[0] === currentRange.startContainer && start[1] === currentRange.startOffset;
245-
const isEndEqualsCurrent = end[0] === currentRange.endContainer && end[1] === currentRange.endOffset;
253+
const isStartEqualsCurrent = textRange[0] === absoluteStartOffset;
254+
const isEndEqualsCurrent = textRange[1] === absoluteEndOffset;
246255

247256
/**
248257
* If selection is already the same, we don't need to update it to not interrupt browser's behaviour
@@ -256,8 +265,7 @@ export class CaretAdapter extends EventTarget {
256265
range.setStart(...start);
257266
range.setEnd(...end);
258267

259-
currentRange.detach();
260-
268+
selection.removeAllRanges();
261269
selection.addRange(range);
262270
}
263271
}

0 commit comments

Comments
 (0)