1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-20 06:01:24 +02:00

cleanup and add compat comments

This commit is contained in:
Ian Storm Taylor
2017-10-31 18:10:45 -07:00
parent ac8df1cfff
commit 10a077059d

View File

@@ -161,21 +161,30 @@ class Content extends React.Component {
return return
} }
// If the new range matches the current selection, do nothing. const {
startContainer,
startOffset,
endContainer,
endOffset,
} = range
// If the new range matches the current selection, there is nothing to fix.
// COMPAT: The native `Range` object always has it's "start" first and "end"
// last in the DOM. It has no concept of "backwards/forwards", so we have
// to check both orientations here. (2017/10/31)
if (current) { if (current) {
if ( if (
range.startContainer == current.startContainer && (
range.startOffset == current.startOffset && startContainer == current.startContainer &&
range.endContainer == current.endContainer && startOffset == current.startOffset &&
range.endOffset == current.endOffset endContainer == current.endContainer &&
) { endOffset == current.endOffset
return ) || (
} startContainer == current.endContainer &&
if ( startOffset == current.endOffset &&
range.startContainer == current.endContainer && endContainer == current.startContainer &&
range.startOffset == current.endOffset && endOffset == current.startOffset
range.endContainer == current.startContainer && )
range.endOffset == current.startOffset
) { ) {
return return
} }
@@ -184,11 +193,16 @@ class Content extends React.Component {
// Otherwise, set the `isUpdatingSelection` flag and update the selection. // Otherwise, set the `isUpdatingSelection` flag and update the selection.
this.tmp.isUpdatingSelection = true this.tmp.isUpdatingSelection = true
native.removeAllRanges() native.removeAllRanges()
// COMPAT: Again, since the DOM range has no concept of backwards/forwards
// we need to check and do the right thing here.
if (isBackward) { if (isBackward) {
native.setBaseAndExtent(range.endContainer, range.endOffset, range.startContainer, range.startOffset) native.setBaseAndExtent(endContainer, endOffset, startContainer, startOffset)
} else { } else {
native.setBaseAndExtent(range.startContainer, range.startOffset, range.endContainer, range.endOffset) native.setBaseAndExtent(startContainer, startOffset, endContainer, endOffset)
} }
// Scroll to the selection, in case it's out of view.
scrollToSelection(native) scrollToSelection(native)
// Then unset the `isUpdatingSelection` flag after a delay. // Then unset the `isUpdatingSelection` flag after a delay.