Skip to content

Commit

Permalink
Merge pull request ajaxorg#2555 from AStoker/master
Browse files Browse the repository at this point in the history
Scroll support for mobile
  • Loading branch information
nightwing committed Jun 22, 2015
2 parents f664986 + c342ba1 commit de6f75f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/ace/lib/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,29 @@ exports.capture = function(el, eventHandler, releaseCaptureHandler) {
return onMouseUp;
};

exports.addTouchMoveListener = function (el, callback) {
if ("ontouchmove" in el) {
var startx, starty;
exports.addListener(el, "touchstart", function (e) {
var touchObj = e.changedTouches[0];
startx = touchObj.clientX;
starty = touchObj.clientY;
});
exports.addListener(el, "touchmove", function (e) {
var factor = 1,
touchObj = e.changedTouches[0];

e.wheelX = -(touchObj.clientX - startx) / factor;
e.wheelY = -(touchObj.clientY - starty) / factor;

startx = touchObj.clientX;
starty = touchObj.clientY;

callback(e);
});
}
};

exports.addMouseWheelListener = function(el, callback) {
if ("onmousewheel" in el) {
exports.addListener(el, "mousewheel", function(e) {
Expand Down
14 changes: 14 additions & 0 deletions lib/ace/mouse/default_handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function DefaultHandlers(mouseHandler) {
editor.setDefaultHandler("tripleclick", this.onTripleClick.bind(mouseHandler));
editor.setDefaultHandler("quadclick", this.onQuadClick.bind(mouseHandler));
editor.setDefaultHandler("mousewheel", this.onMouseWheel.bind(mouseHandler));
editor.setDefaultHandler("touchmove", this.onTouchMove.bind(mouseHandler));

var exports = ["select", "startSelect", "selectEnd", "selectAllEnd", "selectByWordsEnd",
"selectByLinesEnd", "dragWait", "dragWaitEnd", "focusWait"];
Expand Down Expand Up @@ -253,6 +254,19 @@ function DefaultHandlers(mouseHandler) {
return ev.stop();
}
};

this.onTouchMove = function (ev) {
var t = ev.domEvent.timeStamp;
var dt = t - (this.$lastScrollTime || 0);

var editor = this.editor;
var isScrolable = editor.renderer.isScrollableBy(ev.wheelX * ev.speed, ev.wheelY * ev.speed);
if (isScrolable || dt < 200) {
this.$lastScrollTime = t;
editor.renderer.scrollBy(ev.wheelX * ev.speed, ev.wheelY * ev.speed);
return ev.stop();
}
};

}).call(DefaultHandlers.prototype);

Expand Down
9 changes: 9 additions & 0 deletions lib/ace/mouse/mouse_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ var MouseHandler = function(editor) {
}
}
event.addMouseWheelListener(editor.container, this.onMouseWheel.bind(this, "mousewheel"));
event.addTouchMoveListener(editor.container, this.onTouchMove.bind(this, "touchmove"));

var gutterEl = editor.renderer.$gutter;
event.addListener(gutterEl, "mousedown", this.onMouseEvent.bind(this, "guttermousedown"));
Expand Down Expand Up @@ -120,6 +121,14 @@ var MouseHandler = function(editor) {

this.editor._emit(name, mouseEvent);
};

this.onTouchMove = function (name, e) {
var mouseEvent = new MouseEvent(e, this.editor);
mouseEvent.speed = 1;//this.$scrollSpeed * 2;
mouseEvent.wheelX = e.wheelX;
mouseEvent.wheelY = e.wheelY;
this.editor._emit(name, mouseEvent);
};

this.setState = function(state) {
this.state = state;
Expand Down

0 comments on commit de6f75f

Please sign in to comment.