From fab5a9325452356aaf41dde64ec683ea6240e520 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 9 Mar 2017 19:01:18 -0700 Subject: [PATCH] Set cursor position on single click --- spec/text-editor-component-spec.js | 52 ++++++++++++++++++++++++++++++ src/text-editor-component.js | 6 +++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/spec/text-editor-component-spec.js b/spec/text-editor-component-spec.js index 10043a185..cb86207ea 100644 --- a/spec/text-editor-component-spec.js +++ b/spec/text-editor-component-spec.js @@ -592,6 +592,58 @@ describe('TextEditorComponent', () => { } }) }) + + describe('mouse input', () => { + it('positions the cursor on single click', async () => { + const {component, element, editor} = buildComponent() + const {lineHeight, baseCharacterWidth} = component.measurements + + component.didMouseDownOnLines({ + detail: 1, + clientX: clientLeftForCharacter(component, 0, editor.lineLengthForScreenRow(0)) + 1, + clientY: clientTopForLine(component, 0) + lineHeight / 2 + }) + expect(editor.getCursorScreenPosition()).toEqual([0, editor.lineLengthForScreenRow(0)]) + + component.didMouseDownOnLines({ + detail: 1, + clientX: (clientLeftForCharacter(component, 3, 0) + clientLeftForCharacter(component, 3, 1)) / 2, + clientY: clientTopForLine(component, 1) + lineHeight / 2 + }) + expect(editor.getCursorScreenPosition()).toEqual([1, 0]) + + component.didMouseDownOnLines({ + detail: 1, + clientX: (clientLeftForCharacter(component, 3, 14) + clientLeftForCharacter(component, 3, 15)) / 2, + clientY: clientTopForLine(component, 3) + lineHeight / 2 + }) + expect(editor.getCursorScreenPosition()).toEqual([3, 14]) + + component.didMouseDownOnLines({ + detail: 1, + clientX: (clientLeftForCharacter(component, 3, 14) + clientLeftForCharacter(component, 3, 15)) / 2 + 1, + clientY: clientTopForLine(component, 3) + lineHeight / 2 + }) + expect(editor.getCursorScreenPosition()).toEqual([3, 15]) + + editor.getBuffer().setTextInRange([[3, 14], [3, 15]], '🐣') + await component.getNextUpdatePromise() + + component.didMouseDownOnLines({ + detail: 1, + clientX: (clientLeftForCharacter(component, 3, 14) + clientLeftForCharacter(component, 3, 16)) / 2, + clientY: clientTopForLine(component, 3) + lineHeight / 2 + }) + expect(editor.getCursorScreenPosition()).toEqual([3, 14]) + + component.didMouseDownOnLines({ + detail: 1, + clientX: (clientLeftForCharacter(component, 3, 14) + clientLeftForCharacter(component, 3, 16)) / 2 + 1, + clientY: clientTopForLine(component, 3) + lineHeight / 2 + }) + expect(editor.getCursorScreenPosition()).toEqual([3, 16]) + }) + }) }) function buildComponent (params = {}) { diff --git a/src/text-editor-component.js b/src/text-editor-component.js index 9835e8782..69e03c760 100644 --- a/src/text-editor-component.js +++ b/src/text-editor-component.js @@ -736,7 +736,11 @@ class TextEditorComponent { } didMouseDownOnLines (event) { - console.log(this.screenPositionForMouseEvent(event)) + const screenPosition = this.screenPositionForMouseEvent(event) + + if (event.detail === 1) { + this.props.model.setCursorScreenPosition(screenPosition) + } } screenPositionForMouseEvent ({clientX, clientY}) {