Slate: In selection, anchor is always left of the focus

Created on 17 Dec 2019  Â·  2Comments  Â·  Source: ianstormtaylor/slate

Bug (probably)

When a selection is made, even from right to left, anchor is always left of the focus. So isForward always returns true.

What's the current behavior?

https://codesandbox.io/s/slate-reproductions-qm103
http://g.recordit.co/WRwnEkbDQF.gif
Slate: 0.55
Browser: Chrome
OS: Windows

What's the expected behavior?

Anchor should stay where selection begins, and focus should be where it ends.

bug ♥ help

Most helpful comment

Workaround at this time:

const selection = window.getSelection();
const range = ReactEditor.toSlateRange(editor, selection);
const isBackward = Range.isBackward(selection);

Main problem is located in onDOMSelectionChange:359 method.
For some reason DOMSelection is "converted" to DOMRange, which is not store any information about direction of selection. As a result Editor.selection always has forward direction.
https://github.com/ianstormtaylor/slate/blob/1d7ab974292a3e831908a2ba0aab9fdd8a66fe10/packages/slate-react/src/components/editable.tsx#L353-L384

All 2 comments

Workaround at this time:

const selection = window.getSelection();
const range = ReactEditor.toSlateRange(editor, selection);
const isBackward = Range.isBackward(selection);

Main problem is located in onDOMSelectionChange:359 method.
For some reason DOMSelection is "converted" to DOMRange, which is not store any information about direction of selection. As a result Editor.selection always has forward direction.
https://github.com/ianstormtaylor/slate/blob/1d7ab974292a3e831908a2ba0aab9fdd8a66fe10/packages/slate-react/src/components/editable.tsx#L353-L384

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ianstormtaylor picture ianstormtaylor  Â·  3Comments

JSH3R0 picture JSH3R0  Â·  3Comments

YurkaninRyan picture YurkaninRyan  Â·  3Comments

Slapbox picture Slapbox  Â·  3Comments

chrpeter picture chrpeter  Â·  3Comments