From 57624cbe738bee2956b667ee63cae6f7c021c3c0 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 13 Jan 2015 09:15:48 -0500 Subject: [PATCH] feature(edit) rm Ace, Session --- lib/client/edit.js | 102 +++++++++++++++++++------------------------ lib/client/edward.js | 99 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 137 insertions(+), 64 deletions(-) diff --git a/lib/client/edit.js b/lib/client/edit.js index a1149206..69f18e70 100644 --- a/lib/client/edit.js +++ b/lib/client/edit.js @@ -19,8 +19,6 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; Edit = this, Diff, Emmet, - Ace, - Session, Modelist, Events = DOM.Events, RESTful = DOM.RESTful, @@ -33,9 +31,10 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; exec.ifExist(Menu, 'hide'); }, afterShow: function() { - Ace.clearSelection(); - Ace.moveCursorTo(0, 0); - Ace.focus(); + edward + .clearSelection() + .moveCursorTo(0, 0) + .focus(); } }; @@ -97,17 +96,18 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; var cursor, value; if (hash === hashLocal) { - cursor = Ace.selection.getCursor(), - value = Edit.getValue(); + cursor = edward.getCursor(), + value = edward.getValue(); value = Diff.applyPatch(value, data); - Ace.setValue(value); + edward.setValue(value); sha(function(error, hash) { DOM.saveDataToStorage(name, value, hash); - Ace.clearSelection(); - Ace.moveCursorTo(cursor.row, cursor.column); - Ace.scrollToLine(cursor.row, true); + edward + .clearSelection() + .moveCursorTo(cursor.row, cursor.column) + .scrollToLine(cursor.row, true); }); } }); @@ -164,7 +164,7 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; if (error) { alert(error); } else { - Edit.setValue(data); + Edit.setValueFirst(data); CloudCmd.View.show(Element, ConfigView); } }); @@ -172,17 +172,9 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; return this; }; - this.getValue = function() { - return Ace.getValue(); - }; - - this.setValue = function(value) { - var UndoManager = ace.require('ace/undomanager').UndoManager; - - Value = value; - - Ace.setValue(value); - Session.setUndoManager(new UndoManager()); + this.setValueFirst = function(value) { + Value = value; + edward.setValueFirst(value); }; this.hide = function() { @@ -190,19 +182,12 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; }; this.goToLine = function() { - var msg = 'Enter line number:', - cursor = Ace.selection.getCursor(), - number = cursor.row + 1, - line = Dialog.prompt(msg, number); - - number = line - 0; - - if (number) - Ace.gotoLine(number); + edward.goToLine(); }; this.setModeForPath = function(name) { - var modesByName = Modelist.modesByName, + var session = edward.getSession(), + modesByName = Modelist.modesByName, mode = Modelist.getModeForPath(name).mode, htmlMode = modesByName.html.mode, @@ -211,13 +196,13 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; isHTML = mode === htmlMode, isJS = mode === jsMode; - Session.setMode(mode, function() { + session.setMode(mode, function() { setUseOfWorker(mode); if (isHTML) setEmmet(); - if (isJS && Session.getUseWorker()) + if (isJS && session.getUseWorker()) setJsHintConfig(); }); }; @@ -234,7 +219,7 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; function isChanged() { var is, - value = Ace.getValue(), + value = edward.getValue(), isEqual = value === Value, msg = 'Do you want to save changes to ' + name + '?'; @@ -253,7 +238,7 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; if (isEmmet) exec.if(Emmet, function() { - Ace.setOption('enableEmmet', true); + edward.setOption('enableEmmet', true); }, function(callback) { var url; @@ -274,6 +259,7 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; function setUseOfWorker(mode) { var isMatch, + session = edward.getSession(), isStr = typeof mode === 'string', regStr = 'coffee|css|html|javascript|json|lua|php|xquery', regExp = new RegExp(regStr); @@ -281,15 +267,13 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; if (isStr) isMatch = regExp.test(mode); - Session.setUseWorker(isMatch); + session.setUseWorker(isMatch); } this.create = function(element) { Element = element; - Ace = ace.edit(element); - Session = Ace.getSession(); - Ace.commands.addCommand({ + edward.addCommand({ name : 'hide', bindKey : { win: 'Esc', mac: 'Esc' }, exec : function () { @@ -297,7 +281,7 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; } }); - Ace.commands.addCommand({ + edward.addCommand({ name : 'goToLine', bindKey : { win: 'Ctrl-G', mac: 'Command-G' }, exec : function () { @@ -305,7 +289,7 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; } }); - Ace.commands.addCommand({ + edward.addCommand({ name : 'save', bindKey : { win: 'Ctrl-S', mac: 'Command-S' }, exec : function() { @@ -313,7 +297,7 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; } }); - Ace.commands.addCommand({ + edward.addCommand({ name : 'saveMC', bindKey : { win: 'F2', mac: 'F2' }, exec : function() { @@ -321,7 +305,7 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; } }); - Ace.commands.addCommand({ + edward.addCommand({ name : 'beautify', bindKey : { win: 'Ctrl-B', mac: 'Command-B' }, exec : function() { @@ -329,7 +313,7 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; } }); - Ace.commands.addCommand({ + edward.addCommand({ name : 'minify', bindKey : { win: 'Ctrl-M', mac: 'Command-M' }, exec : function() { @@ -343,14 +327,14 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; Files.get('edit', function(error, config) { var options = config.options; - Ace.setOptions(options); + edward.setOptions(options); }); return this; }; Edit.save = function() { - var value = Ace.getValue(); + var value = edward.getValue(); Files.get('config', function(error, config) { var isDiff = config.diff, @@ -401,7 +385,7 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; } function doDiff(path, callback) { - var value = Ace.getValue(); + var value = edward.getValue(); diff(value, function(patch) { var isAllowed = Storage.isAllowed(); @@ -479,7 +463,7 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; DOM.load.js(url, function() { var shaObj, hash, error, - value = Ace.getValue(); + value = edward.getValue(); error = exec.try(function() { shaObj = new window.jsSHA(value, 'TEXT'); @@ -493,7 +477,8 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; function setJsHintConfig(callback) { var JSHINT_PATH = CloudCmd.PREFIX + '/.jshintrc', func = function() { - var worker = Session.$worker; + var session = edward.getSession(), + worker = session.$worker; if (worker) worker.send('changeOptions', [JSHintConfig]); @@ -525,7 +510,7 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; params.y -= 27; }, afterClick: function() { - Ace.focus(); + edward.focus(); } }, menuData = { @@ -536,10 +521,10 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; Edit.goToLine(); }, 'Select All Ctrl+A' : function() { - Ace.selectAll(); + edward.selectAll(); }, 'Delete Del' : function() { - Ace.remove('right'); + edward.remove('right'); }, 'Beautify Ctrl+B' : function() { Edit.beautify(); @@ -618,9 +603,10 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; Util.check(arguments, ['flag']); RESTful.read(path + '?' + flag, function(data) { - Ace.setValue(data); - Ace.clearSelection(); - Ace.moveCursorTo(0, 0); + edward + .setValue(data) + .clearSelection() + .moveCursorTo(0, 0); }); } @@ -629,7 +615,7 @@ var CloudCmd, Util, DOM, CloudFunc, io, ace, Zip, MenuIO, Format, edward; onLoad = function(event) { var data = event.target.result; - Ace.setValue(data); + edward.setValue(data); }; event.preventDefault(); diff --git a/lib/client/edward.js b/lib/client/edward.js index 264e1849..8380c38b 100644 --- a/lib/client/edward.js +++ b/lib/client/edward.js @@ -1,6 +1,6 @@ -var join, DOM; +var join, ace, Util, DOM; -(function(global, join, DOM, load) { +(function(global, join, DOM, exec, load) { 'use strict'; if (typeof module !== 'undefined' && module.exports) @@ -11,11 +11,19 @@ var join, DOM; function edward() { var Element, ElementMsg, + Value, + Ace, ed = function(el, callback) { - Element = el || document.body; - loadFiles(callback); - }; + Element = el || document.body; + exec.series([ + loadFiles, + function() { + Ace = ace.edit(Element); + callback(); + }, + ]); + }; function createMsg() { var msg, @@ -28,6 +36,85 @@ var join, DOM; return msg; } + ed.addCommand = function(options) { + Ace.commands.addCommand(options); + }; + + ed.clearSelection = function() { + Ace.clearSelection(); + return ed; + }; + + ed.goToLine = function() { + var msg = 'Enter line number:', + cursor = edward.getCursor(), + number = cursor.row + 1, + line = prompt(msg, number); + + number = line - 0; + + if (number) + Ace.gotoLine(number); + }; + + ed.moveCursorTo = function(row, column) { + Ace.moveCursorTo(row, column); + return ed; + }; + + ed.focus = function() { + Ace.focus(); + return ed; + }; + + ed.remove = function(direction) { + Ace.remove(direction); + }; + + ed.getCursor = function() { + return Ace.selection.getCursor(); + }; + + ed.getValue = function() { + return Ace.getValue(); + }; + + ed.setValue = function(value) { + Ace.setValue(value); + }; + + ed.setValueFirst = function(value) { + var session = ed.getSession(), + UndoManager = ace.require('ace/undomanager').UndoManager; + + Value = value; + + Ace.setValue(value); + + session.setUndoManager(new UndoManager()); + }; + + ed.setOption = function(name, value) { + Ace.setOption(name, value); + }; + + ed.setOptions = function(options) { + Ace.setOptions(options); + }; + + ed.selectAll = function() { + Ace.selectAll(); + }; + + ed.scrollToLine = function(row) { + Ace.scrollToLine(row, true); + return ed; + }; + + ed.getSession = function() { + return Ace.getSession(); + }; + ed.showMessage = function(text) { var HIDE_TIME = 2000; @@ -72,4 +159,4 @@ var join, DOM; return ed; } -})(this, join, DOM, DOM.load); +})(this, join, DOM, Util.exec, DOM.load);