do not compute pageX only to convert it back to clientX

This commit is contained in:
nightwing 2012-04-17 19:36:31 +04:00
commit 6411152d00
4 changed files with 20 additions and 31 deletions

View file

@ -87,22 +87,6 @@ exports.preventDefault = function(e) {
e.returnValue = false; e.returnValue = false;
}; };
exports.getDocumentX = function(e) {
if (e.clientX) {
return e.clientX + dom.getPageScrollLeft();
} else {
return e.pageX;
}
};
exports.getDocumentY = function(e) {
if (e.clientY) {
return e.clientY + dom.getPageScrollTop();
} else {
return e.pageY;
}
};
/* /*
* @return {Number} 0 for left button, 1 for middle button, 2 for right button * @return {Number} 0 for left button, 1 for middle button, 2 for right button
*/ */

View file

@ -48,11 +48,8 @@ var MouseEvent = exports.MouseEvent = function(domEvent, editor) {
this.domEvent = domEvent; this.domEvent = domEvent;
this.editor = editor; this.editor = editor;
this.pageX = event.getDocumentX(domEvent); this.x = this.clientX = domEvent.clientX;
this.pageY = event.getDocumentY(domEvent); this.y = this.clientY = domEvent.clientY;
this.clientX = domEvent.clientX;
this.clientY = domEvent.clientY;
this.$pos = null; this.$pos = null;
this.$inSelection = null; this.$inSelection = null;
@ -86,10 +83,8 @@ var MouseEvent = exports.MouseEvent = function(domEvent, editor) {
this.getDocumentPosition = function() { this.getDocumentPosition = function() {
if (this.$pos) if (this.$pos)
return this.$pos; return this.$pos;
var pageX = event.getDocumentX(this.domEvent); this.$pos = this.editor.renderer.screenToTextCoordinates(this.clientX, this.clientY);
var pageY = event.getDocumentY(this.domEvent);
this.$pos = this.editor.renderer.screenToTextCoordinates(pageX, pageY);
return this.$pos; return this.$pos;
}; };

View file

@ -76,10 +76,10 @@ function onMouseDown(e) {
var inSelection = e.inSelection() || (selection.isEmpty() && isSamePoint(pos, cursor)); var inSelection = e.inSelection() || (selection.isEmpty() && isSamePoint(pos, cursor));
var mouseX = e.pageX, mouseY = e.pageY; var mouseX = e.x, mouseY = e.y;
var onMouseSelection = function(e) { var onMouseSelection = function(e) {
mouseX = event.getDocumentX(e); mouseX = e.clientX;
mouseY = event.getDocumentY(e); mouseY = e.clientY;
}; };
var blockSelect = function() { var blockSelect = function() {

View file

@ -1103,14 +1103,24 @@ var VirtualRenderer = function(container, theme) {
// todo: handle horizontal scrolling // todo: handle horizontal scrolling
}; };
this.screenToTextCoordinates = function(pageX, pageY) { this.pixelToScreenCoordinates = function(x, y) {
var canvasPos = this.scroller.getBoundingClientRect();
var offset = (x + this.scrollLeft - canvasPos.left - this.$padding) / this.characterWidth;
var row = Math.floor((y + this.scrollTop - canvasPos.top) / this.lineHeight);
var col = Math.round(offset);
return {row: row, column: col, side: offset - col};
};
this.screenToTextCoordinates = function(x, y) {
var canvasPos = this.scroller.getBoundingClientRect(); var canvasPos = this.scroller.getBoundingClientRect();
var col = Math.round( var col = Math.round(
(pageX + this.scrollLeft - canvasPos.left - this.$padding - dom.getPageScrollLeft()) / this.characterWidth (x + this.scrollLeft - canvasPos.left - this.$padding) / this.characterWidth
); );
var row = Math.floor( var row = Math.floor(
(pageY + this.scrollTop - canvasPos.top - dom.getPageScrollTop()) / this.lineHeight (y + this.scrollTop - canvasPos.top) / this.lineHeight
); );
return this.session.screenToDocumentPosition(row, Math.max(col, 0)); return this.session.screenToDocumentPosition(row, Math.max(col, 0));