Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #15916 from atom/as-jr-stop-dragging-when-interact…
Browse files Browse the repository at this point in the history
…ing-with-keyboard

Stop dragging only when user interacts with keyboard
  • Loading branch information
Antonio Scandurra committed Oct 18, 2017
2 parents d6a3a60 + 65af9e9 commit 280253f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
10 changes: 8 additions & 2 deletions spec/text-editor-component-spec.js
Expand Up @@ -4422,7 +4422,7 @@ describe('TextEditorComponent', () => {
expect(dragEvents).toEqual([])
})

it('calls `didStopDragging` if the buffer changes while dragging', async () => {
it('calls `didStopDragging` if the user interacts with the keyboard while dragging', async () => {
const {component, editor} = buildComponent()

let dragging = false
Expand All @@ -4435,8 +4435,14 @@ describe('TextEditorComponent', () => {
await getNextAnimationFramePromise()
expect(dragging).toBe(true)

editor.delete()
// Buffer changes don't cause dragging to be stopped.
editor.insertText('X')
expect(dragging).toBe(true)

// Keyboard interaction prevents users from dragging further.
component.didKeydown({code: 'KeyX'})
expect(dragging).toBe(false)

window.dispatchEvent(new MouseEvent('mousemove'))
await getNextAnimationFramePromise()
expect(dragging).toBe(false)
Expand Down
13 changes: 7 additions & 6 deletions src/text-editor-component.js
Expand Up @@ -1638,6 +1638,11 @@ class TextEditorComponent {
// keypress, meaning we're *holding* the _same_ key we intially pressed.
// Got that?
didKeydown (event) {
// Stop dragging when user interacts with the keyboard. This prevents
// unwanted selections in the case edits are performed while selecting text
// at the same time.
if (this.stopDragging) this.stopDragging()

if (this.lastKeydownBeforeKeypress != null) {
if (this.lastKeydownBeforeKeypress.code === event.code) {
this.accentedCharacterMenuIsOpen = true
Expand Down Expand Up @@ -1862,7 +1867,6 @@ class TextEditorComponent {
handleMouseDragUntilMouseUp ({didDrag, didStopDragging}) {
let dragging = false
let lastMousemoveEvent
let bufferWillChangeDisposable

const animationFrameLoop = () => {
window.requestAnimationFrame(() => {
Expand All @@ -1882,9 +1886,9 @@ class TextEditorComponent {
}

function didMouseUp () {
this.stopDragging = null
window.removeEventListener('mousemove', didMouseMove)
window.removeEventListener('mouseup', didMouseUp, {capture: true})
bufferWillChangeDisposable.dispose()
if (dragging) {
dragging = false
didStopDragging()
Expand All @@ -1893,10 +1897,7 @@ class TextEditorComponent {

window.addEventListener('mousemove', didMouseMove)
window.addEventListener('mouseup', didMouseUp, {capture: true})
// Simulate a mouse-up event if the buffer is about to change. This prevents
// unwanted selections when users perform edits while holding the left mouse
// button at the same time.
bufferWillChangeDisposable = this.props.model.getBuffer().onWillChange(didMouseUp)
this.stopDragging = didMouseUp
}

autoscrollOnMouseDrag ({clientX, clientY}, verticalOnly = false) {
Expand Down

0 comments on commit 280253f

Please sign in to comment.