1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-11 09:43:58 +02:00

Mark some events as 'user actions' to prevent scrolling when a programatic selection tranformation is performed (#3019)

This commit is contained in:
Guillaume Racicot
2019-10-06 09:22:00 -04:00
committed by Ian Storm Taylor
parent 7f5ad9c1fc
commit d28f78c060
2 changed files with 27 additions and 2 deletions

View File

@@ -199,6 +199,7 @@ class Content extends React.Component {
debug.update('componentDidUpdate') debug.update('componentDidUpdate')
this.updateSelection() this.updateSelection()
this.props.editor.clearUserActionPerformed()
this.props.onEvent('onComponentDidUpdate') this.props.onEvent('onComponentDidUpdate')
} }
@@ -342,8 +343,11 @@ class Content extends React.Component {
native.addRange(range) native.addRange(range)
} }
// Scroll to the selection, in case it's out of view. // Only scroll to selection when a user action is performed
scrollToSelection(native) if (editor.userActionPerformed() === true) {
// Scroll to the selection, in case it's out of view.
scrollToSelection(native)
}
// Then unset the `isUpdatingSelection` flag after a delay, to ensure that // Then unset the `isUpdatingSelection` flag after a delay, to ensure that
// it is still set when selection-related events from updating it fire. // it is still set when selection-related events from updating it fire.

View File

@@ -30,6 +30,7 @@ function BeforePlugin() {
let isComposing = false let isComposing = false
let isCopying = false let isCopying = false
let isDragging = false let isDragging = false
let isUserActionPerformed = false
/** /**
* On before input. * On before input.
@@ -42,6 +43,7 @@ function BeforePlugin() {
function onBeforeInput(event, editor, next) { function onBeforeInput(event, editor, next) {
const isSynthetic = !!event.nativeEvent const isSynthetic = !!event.nativeEvent
if (editor.readOnly) return if (editor.readOnly) return
isUserActionPerformed = true
// COMPAT: If the browser supports Input Events Level 2, we will have // COMPAT: If the browser supports Input Events Level 2, we will have
// attached a custom handler for the real `beforeinput` events, instead of // attached a custom handler for the real `beforeinput` events, instead of
@@ -111,6 +113,7 @@ function BeforePlugin() {
function onCompositionEnd(event, editor, next) { function onCompositionEnd(event, editor, next) {
const n = compositionCount const n = compositionCount
isUserActionPerformed = true
// The `count` check here ensures that if another composition starts // The `count` check here ensures that if another composition starts
// before the timeout has closed out this one, we will abort unsetting the // before the timeout has closed out this one, we will abort unsetting the
@@ -134,6 +137,7 @@ function BeforePlugin() {
function onClick(event, editor, next) { function onClick(event, editor, next) {
debug('onClick', { event }) debug('onClick', { event })
isUserActionPerformed = true
next() next()
} }
@@ -151,6 +155,7 @@ function BeforePlugin() {
const { value } = editor const { value } = editor
const { selection } = value const { selection } = value
isUserActionPerformed = true
if (!selection.isCollapsed) { if (!selection.isCollapsed) {
// https://github.com/ianstormtaylor/slate/issues/1879 // https://github.com/ianstormtaylor/slate/issues/1879
@@ -322,6 +327,7 @@ function BeforePlugin() {
function onDrop(event, editor, next) { function onDrop(event, editor, next) {
if (editor.readOnly) return if (editor.readOnly) return
isUserActionPerformed = true
// Prevent default so the DOM's value isn't corrupted. // Prevent default so the DOM's value isn't corrupted.
event.preventDefault() event.preventDefault()
@@ -371,6 +377,7 @@ function BeforePlugin() {
function onInput(event, editor, next) { function onInput(event, editor, next) {
if (isComposing) return if (isComposing) return
if (editor.value.selection.isBlurred) return if (editor.value.selection.isBlurred) return
isUserActionPerformed = true
debug('onInput', { event }) debug('onInput', { event })
next() next()
} }
@@ -415,6 +422,7 @@ function BeforePlugin() {
event.preventDefault() event.preventDefault()
} }
isUserActionPerformed = true
debug('onKeyDown', { event }) debug('onKeyDown', { event })
next() next()
} }
@@ -429,6 +437,7 @@ function BeforePlugin() {
function onPaste(event, editor, next) { function onPaste(event, editor, next) {
if (editor.readOnly) return if (editor.readOnly) return
isUserActionPerformed = true
// Prevent defaults so the DOM state isn't corrupted. // Prevent defaults so the DOM state isn't corrupted.
event.preventDefault() event.preventDefault()
@@ -454,11 +463,21 @@ function BeforePlugin() {
// Save the new `activeElement`. // Save the new `activeElement`.
const window = getWindow(event.target) const window = getWindow(event.target)
activeElement = window.document.activeElement activeElement = window.document.activeElement
isUserActionPerformed = true
debug('onSelect', { event }) debug('onSelect', { event })
next() next()
} }
function userActionPerformed() {
return isUserActionPerformed
}
function clearUserActionPerformed() {
isUserActionPerformed = false
return null
}
/** /**
* Return the plugin. * Return the plugin.
* *
@@ -485,6 +504,8 @@ function BeforePlugin() {
onKeyDown, onKeyDown,
onPaste, onPaste,
onSelect, onSelect,
queries: { userActionPerformed },
commands: { clearUserActionPerformed },
} }
} }