From 0ddbfa66ef3a038aeb21a8187a01111e09ebf653 Mon Sep 17 00:00:00 2001 From: Julian Viereck Date: Fri, 20 May 2011 21:24:28 +0200 Subject: [PATCH] First pass of split implementation --- demo/demo.js | 11 +++- lib/ace/split.js | 142 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 lib/ace/split.js diff --git a/demo/demo.js b/demo/demo.js index 7b0646db..0b4b144c 100644 --- a/demo/demo.js +++ b/demo/demo.js @@ -154,7 +154,13 @@ exports.launch = function(env) { var container = document.getElementById("editor"); var cockpitInput = document.getElementById("cockpitInput"); - env.editor = new Editor(new Renderer(container, theme)); + + // Splitting. + var Split = require("ace/split").Split; + var split = new Split(container, theme, 1); + env.editor = split.getEditor(0); + env.split = split; +// env.editor = new Editor(new Renderer(container, theme)); window.env = env; window.ace = env.editor; @@ -343,7 +349,8 @@ exports.launch = function(env) { container.style.width = width + "px"; cockpitInput.style.width = width + "px"; container.style.height = (document.documentElement.clientHeight - 22) + "px"; - env.editor.resize(); + env.split.resize(); +// env.editor.resize(); }; window.onresize = onResize; diff --git a/lib/ace/split.js b/lib/ace/split.js new file mode 100644 index 00000000..b5aea2b3 --- /dev/null +++ b/lib/ace/split.js @@ -0,0 +1,142 @@ +/* vim:ts=4:sts=4:sw=4: + * ***** 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 + * Julian Viereck + * + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * 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 oop = require("pilot/oop"); +var EventEmitter = require("pilot/event_emitter").EventEmitter; + +var Editor = require("ace/editor").Editor; +var Renderer = require("ace/virtual_renderer").VirtualRenderer; +var EditSession = require("ace/edit_session").EditSession; + +var Split = function(container, theme, splits) { + this.$container = container; + this.$theme = theme; + this.$splits = 0; + this.$editorCSS = ""; + this.$cEditor = null; + this.$editors = []; + + this.setSplits(splits || 1); + + this.on("focus", function(editor) { + this.$cEditor = editor; + }.bind(this)); +}; + +(function(){ + + oop.implement(this, EventEmitter); + + this.$createEditor = function() { + var dom = document.createElement("div"); + dom.className = this.$editorCSS; + dom.style = "position: absolute; top:0px; bottom:0px"; + this.$container.appendChild(dom); + + var session = new EditSession(""); + var editor = new Editor(new Renderer(dom, this.$theme)); + + editor.on("focus", function() { + this._emit("focus", editor); + }.bind(this)); + + this.$editors.push(editor); + return editor; + } + + this.setSplits = function(splits) { + var editor; + if (splits < 1) { + throw "The number of splits have to be > 0!"; + } + + if (splits == this.$splits) { + return; + } else if (splits > this.$splits) { + while (this.$splits < splits) { + this.$createEditor(); + this.$splits ++; + } + } else { + while (this.$splits > splits) { + editor = this.$editors[this.$splits - 1]; + this.$container.removeChild(editor.container); + this.$editors.pop(); + this.$splits --; + } + } + this.resize(); + } + + this.getEditor = function(idx) { + return this.$editors[idx]; + } + + this.setTheme = function(theme) { + this.$editors.forEach(function(editor) { + editor.setTheme(theme); + }); + } + + this.setKeyboardHandler = function(keybinding) { + this.$editors.forEach(function(editor) { + editor.setKeyboardHandler(keybinding); + }); + } + + this.resize = function() { + var width = this.$container.clientWidth; + var height = this.$container.clientHeight; + var editor; + var editorWidth = width / this.$splits; + for (var i = 0; i < this.$splits; i++) { + editor = this.$editors[i]; + editor.container.style.width = editorWidth + "px"; + editor.container.style.left = i * editorWidth + "px"; + editor.container.style.height = height + "px"; + editor.resize(); + } + } + +}).call(Split.prototype); + + +exports.Split = Split; +});