From f6e9cce4287d5f03ceb3d7cc0097178015a1f519 Mon Sep 17 00:00:00 2001 From: nightwing Date: Sat, 1 Sep 2012 18:16:00 +0400 Subject: [PATCH] update ipad demo --- demo/ipad.html | 57 +++++++++++++++ demo/ipad.js | 72 +++++++++++++++++++ lib/ace/editor.js | 6 +- lib/ace/mouse/touch_handler.js | 126 +++++++++++++++++++++++++++++++++ 4 files changed, 258 insertions(+), 3 deletions(-) create mode 100644 demo/ipad.html create mode 100644 demo/ipad.js create mode 100644 lib/ace/mouse/touch_handler.js diff --git a/demo/ipad.html b/demo/ipad.html new file mode 100644 index 00000000..9e8742f5 --- /dev/null +++ b/demo/ipad.html @@ -0,0 +1,57 @@ + + + + + + + + + + Ace on iPad + + + +
+
+ani + + + + diff --git a/demo/ipad.js b/demo/ipad.js new file mode 100644 index 00000000..45da8861 --- /dev/null +++ b/demo/ipad.js @@ -0,0 +1,72 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla Skywriter. + * + * The Initial Developer of the Original Code is + * Mozilla. + * Portions created by the Initial Developer are Copyright (C) 2009 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Fabian Jakobs + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + + +define(function(require, exports, module) { + +var Dom = require("ace/lib/dom"); +var Event = require("ace/lib/event"); + +var Editor = require("ace/editor").Editor; +var EditSession = require("ace/edit_session").EditSession; +var UndoManager = require("ace/undomanager").UndoManager; +var Renderer = require("ace/virtual_renderer").VirtualRenderer; +var JavaScriptMode = require("ace/mode/javascript").Mode; + +exports.launch = function(el, text) { + if (typeof(el) == "string") { + el = document.getElementById(el); + } + + editor = new Editor(new Renderer(el, "ace/theme/twilight")); + + var doc = new EditSession(text); + doc.setMode(new JavaScriptMode()); + doc.setUndoManager(new UndoManager()); + editor.setSession(doc); + + editor.resize(); + + /* setTimeout(function(){ + editor.setAnimatedScroll(true) + editor.renderer.scrollToLine(editor.renderer.scrollTop == 0 ? 200 :0 ,0,true) + }, 2000);*/ + + return editor; +}; + +}); \ No newline at end of file diff --git a/lib/ace/editor.js b/lib/ace/editor.js index be072a57..54becfcf 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -49,7 +49,7 @@ var useragent = require("./lib/useragent"); var TextInput = require("./keyboard/textinput").TextInput; var MouseHandler = require("./mouse/mouse_handler").MouseHandler; var FoldHandler = require("./mouse/fold_handler").FoldHandler; -//var TouchHandler = require("./touch_handler").TouchHandler; +var TouchHandler = require("./mouse/touch_handler").TouchHandler; var KeyBinding = require("./keyboard/keybinding").KeyBinding; var EditSession = require("./edit_session").EditSession; var Search = require("./search").Search; @@ -84,8 +84,8 @@ var Editor = function(renderer, session) { this.keyBinding = new KeyBinding(this); // TODO detect touch event support - if (useragent.isIPad) { - //this.$mouseHandler = new TouchHandler(this); + if ('ontouchstart' in this.container || 1) { + this.$mouseHandler = new TouchHandler(this); } else { this.$mouseHandler = new MouseHandler(this); new FoldHandler(this); diff --git a/lib/ace/mouse/touch_handler.js b/lib/ace/mouse/touch_handler.js new file mode 100644 index 00000000..2f942f5d --- /dev/null +++ b/lib/ace/mouse/touch_handler.js @@ -0,0 +1,126 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Ajax.org Code Editor (ACE). + * + * The Initial Developer of the Original Code is + * Ajax.org B.V. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Fabian Jakobs + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +define(function(require, exports, module) { + +var event = require("../lib/event"); + +var TouchHandler = function(editor) { + this.editor = editor; + event.addListener(editor.container, "click", function(e) { + // does this only work on click? + editor.focus(); + }); + var mouseTarget = editor.renderer.getMouseEventTarget(); + mouseTarget.ontouchstart = this.onTouchStart.bind(this); + mouseTarget.ontouchmove = this.onTouchMove.bind(this); + mouseTarget.ontouchend = this.onTouchEnd.bind(this); +}; + +(function() { + + this.$scrollSpeed = 1; + this.setScrollSpeed = function(speed) { + this.$scrollSpeed = speed; + }; + + this.getScrollSpeed = function() { + return this.$scrollSpeed; + }; + + this.onTouchMove = function(e) { + e.preventDefault(); + if (e.touches.length == 1) { + this.$moveCursor(e.touches[0]); + } + else if (e.touches.length == 2) { + if (!this.$scroll) + return; + + var touch = e.touches[0]; + var diffX = this.$scroll.pageX - touch.pageX; + var diffY = this.$scroll.pageY - touch.pageY; + this.editor.renderer.scrollBy(diffX, diffY); + + this.$scroll = { + pageX: touch.pageX, + pageY: touch.pageY, + ts: new Date().getTime() + } + } + }; + + this.$moveCursor = function(touch) { + var pageX = touch.pageX; + var pageY = touch.pageY; + + var editor = this.editor; + var pos = editor.renderer.screenToTextCoordinates(pageX, pageY); + pos.row = Math.max(0, Math.min(pos.row, editor.session.getLength()-1)); + + editor.moveCursorToPosition(pos); + editor.renderer.scrollCursorIntoView(); + }; + + this.onTouchEnd = function(e) { + //if (e.touches.length == 1) { + console.log("focus") + // editor.focus(); + //e.preventDefault(); + //} + + + }; + + this.onTouchStart = function(e) { + if (e.touches.length == 1) { + this.$moveCursor(e.touches[0]); + } + else if (e.touches.length == 2) { + e.preventDefault(); + var touch = e.touches[0]; + this.$scroll = { + pageX: touch.pageX, + pageY: touch.pageY + } + } + }; + +}).call(TouchHandler.prototype); + +exports.TouchHandler = TouchHandler; +}); \ No newline at end of file