Set cursor position on single click

This commit is contained in:
Nathan Sobo 2017-03-09 19:01:18 -07:00 committed by Antonio Scandurra
parent 88f3a5b468
commit fab5a93254
2 changed files with 57 additions and 1 deletions

View File

@ -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 = {}) {

View File

@ -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}) {