mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-16 20:24:01 +02:00
fix inconsistent render reversed selection (#3804)
* fix inconsistent render reversed selection * fix type error * add comment for setReverseDomSelection * add comment for setReverseDomSelection * add comment for toDOMRange
This commit is contained in:
@@ -33,6 +33,7 @@ import {
|
|||||||
isDOMText,
|
isDOMText,
|
||||||
DOMStaticRange,
|
DOMStaticRange,
|
||||||
isPlainTextOnlyPaste,
|
isPlainTextOnlyPaste,
|
||||||
|
setReverseDomSelection,
|
||||||
} from '../utils/dom'
|
} from '../utils/dom'
|
||||||
import {
|
import {
|
||||||
EDITOR_TO_ELEMENT,
|
EDITOR_TO_ELEMENT,
|
||||||
@@ -184,7 +185,11 @@ export const Editable = (props: EditableProps) => {
|
|||||||
const newDomRange = selection && ReactEditor.toDOMRange(editor, selection)
|
const newDomRange = selection && ReactEditor.toDOMRange(editor, selection)
|
||||||
|
|
||||||
if (newDomRange) {
|
if (newDomRange) {
|
||||||
domSelection.addRange(newDomRange!)
|
if (Range.isBackward(selection!)) {
|
||||||
|
setReverseDomSelection(newDomRange, domSelection)
|
||||||
|
} else {
|
||||||
|
domSelection.addRange(newDomRange!)
|
||||||
|
}
|
||||||
const leafEl = newDomRange.startContainer.parentElement!
|
const leafEl = newDomRange.startContainer.parentElement!
|
||||||
scrollIntoView(leafEl, {
|
scrollIntoView(leafEl, {
|
||||||
scrollMode: 'if-needed',
|
scrollMode: 'if-needed',
|
||||||
|
@@ -269,6 +269,11 @@ export const ReactEditor = {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a native DOM range from a Slate `range`.
|
* Find a native DOM range from a Slate `range`.
|
||||||
|
*
|
||||||
|
* Notice: the returned range will always be ordinal regardless of the direction of Slate `range` due to DOM API limit.
|
||||||
|
*
|
||||||
|
* there is no way to create a reverse DOM Range using Range.setStart/setEnd
|
||||||
|
* according to https://dom.spec.whatwg.org/#concept-range-bp-set.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
toDOMRange(editor: ReactEditor, range: Range): DOMRange {
|
toDOMRange(editor: ReactEditor, range: Range): DOMRange {
|
||||||
|
@@ -174,3 +174,24 @@ export const getPlainText = (domNode: DOMNode) => {
|
|||||||
|
|
||||||
return text
|
return text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* there is no way to create a reverse DOM Range using Range.setStart/setEnd
|
||||||
|
* according to https://dom.spec.whatwg.org/#concept-range-bp-set.
|
||||||
|
* Luckily it's possible to create a reverse selection via Selection.extend
|
||||||
|
*
|
||||||
|
* Note: Selection.extend is not implement in any version of IE (up to and including version 11)
|
||||||
|
*/
|
||||||
|
|
||||||
|
export const setReverseDomSelection = (
|
||||||
|
domRange: DOMRange,
|
||||||
|
domSelection: Selection
|
||||||
|
) => {
|
||||||
|
const newRange = domRange.cloneRange()
|
||||||
|
// collapses the range to end
|
||||||
|
newRange.collapse()
|
||||||
|
// set both anchor and focus of the selection to domRange's focus
|
||||||
|
domSelection.addRange(newRange)
|
||||||
|
// moves the focus of the selection to domRange's anchor
|
||||||
|
domSelection.extend(domRange.startContainer, domRange.startOffset)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user