From ce92cddf0e667290063f03a8fd0ef0b54b7ee8a6 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 27 Dec 2016 12:54:40 +0200 Subject: [PATCH] feature(edit-file) add --- client/edit-file.js | 192 +++++++++++++++++++++++++++++++++++++++ client/edit.js | 215 ++++++++++---------------------------------- client/key.js | 2 +- client/listeners.js | 2 +- json/modules.json | 3 +- package.json | 1 + server/cloudcmd.js | 5 ++ 7 files changed, 247 insertions(+), 173 deletions(-) create mode 100644 client/edit-file.js diff --git a/client/edit-file.js b/client/edit-file.js new file mode 100644 index 00000000..82075e78 --- /dev/null +++ b/client/edit-file.js @@ -0,0 +1,192 @@ +var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format; + +(function(CloudCmd, Util, DOM) { + 'use strict'; + + CloudCmd.EditFile = function EditFileProto(callback) { + var Info = DOM.CurrentInfo; + var Dialog = DOM.Dialog; + var exec = Util.exec; + var EditFile = this; + + var Menu, + + TITLE = 'Edit', + + Images = DOM.Images, + MSG_CHANGED, + ConfigView = { + beforeClose: function() { + exec.ifExist(Menu, 'hide'); + isChanged(EditFile.hide); + } + }; + + function init(callback) { + var editor; + + exec.series([ + CloudCmd.Edit, + function(callback) { + editor = CloudCmd.Edit.getEditor(); + callback(); + }, + function(callback) { + authCheck(editor); + callback(); + }, + + function(callback) { + setListeners(editor); + callback(); + }, + function(callback) { + EditFile.show(callback); + }, + ], callback); + } + + this.show = function() { + Images.show.load(); + + Info.getData(function(error, data) { + var path = Info.path; + var isDir = Info.isDir; + var name = Info.name; + + if (isDir) + name += '.json'; + + if (error) + return Images.hide(); + + setMsgChanged(name); + + CloudCmd.Edit + .getEditor() + .setValueFirst(path, data) + .setModeForPath(name) + .setOption('fontSize', 16); + + CloudCmd.Edit.show(ConfigView); + }); + }; + + this.hide = function() { + CloudCmd.Edit.hide(); + }; + + function setListeners(editor) { + var element = CloudCmd.Edit.getElement(); + + DOM.Events.addOnce('contextmenu', element, setMenu); + + editor.on('save', function(value) { + DOM.setCurrentSize(Format.size(value)); + }); + } + + function authCheck(spawn) { + DOM.Files.get('config', function(error, config) { + if (error) + return Dialog.alert(TITLE, error); + + if (!config.auth) + return; + + spawn.emit('auth', config.username, config.password); + spawn.on('reject', function() { + Dialog.alert(TITLE, 'Wrong credentials!'); + }); + }); + } + + function setMenu(event) { + var position = { + x: event.clientX, + y: event.clientY + }; + + event.preventDefault(); + + !Menu && DOM.loadRemote('menu', function(error) { + var noFocus; + var options = { + beforeShow: function(params) { + params.x -= 18; + params.y -= 27; + }, + + afterClick: function() { + !noFocus && editor.focus(); + } + }; + + var editor = CloudCmd.Edit.getEditor(); + + var menuData = { + 'Save Ctrl+S' : function() { + editor.save(); + }, + 'Go To Line Ctrl+G' : function() { + noFocus = true; + editor.goToLine(); + }, + 'Cut Ctrl+X' : function() { + editor.cutToClipboard(); + }, + 'Copy Ctrl+C' : function() { + editor.copyToClipboard(); + }, + 'Paste Ctrl+V' : function() { + editor.pasteFromClipboard(); + }, + 'Delete Del' : function() { + editor.remove('right'); + }, + 'Select All Ctrl+A' : function() { + editor.selectAll(); + }, + 'Beautify Ctrl+B' : function() { + editor.beautify(); + }, + 'Minify Ctrl+M' : function() { + editor.minify(); + }, + 'Close Esc' : function() { + EditFile.hide(); + } + }; + + if (error) + return Dialog.alert(TITLE, error); + + if (Menu || !MenuIO) + return; + + var element = CloudCmd.Edit.getElement(); + + Menu = new MenuIO(element, options, menuData); + Menu.show(position.x, position.y); + }); + } + + function setMsgChanged(name) { + MSG_CHANGED = 'Do you want to save changes to ' + name + '?'; + } + + function isChanged() { + var editor = CloudCmd.Edit.getEditor(); + var is = editor.isChanged(); + + is && Dialog.confirm(TITLE, MSG_CHANGED, {cancel: false}) + .then(function() { + editor.save(); + }); + } + + init(callback); + }; + +})(CloudCmd, Util, DOM, CloudFunc); + diff --git a/client/edit.js b/client/edit.js index a85d4175..db621df9 100644 --- a/client/edit.js +++ b/client/edit.js @@ -1,4 +1,4 @@ -var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format; +var CloudCmd, Util, DOM, CloudFunc; (function(CloudCmd, Util, DOM) { 'use strict'; @@ -6,30 +6,19 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format; CloudCmd.Edit = EditProto; function EditProto(callback) { - var Name = 'Edit', - Loading = true, - - Info = DOM.CurrentInfo, - Dialog = DOM.Dialog, - + var Name = 'Edit'; + var Loading = true; + + var Dialog = DOM.Dialog, exec = Util.exec, - Edit = this, + Element, - Menu, - - EditorName = 'edward', + EditorName = 'edward', editor, - TITLE = 'Edit', + TITLE = 'Edit', - Images = DOM.Images, - MSG_CHANGED, - Element, ConfigView = { - beforeClose: function() { - exec.ifExist(Menu, 'hide'); - isChanged(Edit.hide); - }, afterShow: function() { editor .moveCursorTo(0, 0) @@ -40,24 +29,13 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format; function init(callback) { var element = createElement(); - DOM.Events.addOnce('contextmenu', element, setMenu); - exec.series([ CloudCmd.View, getConfig, function(callback) { loadFiles(element, callback); }, - function(callback) { - authCheck(editor); - callback(); - }, - function(callback) { - Edit.create(element) - .show(callback); - }, - callback - ]); + ], callback); } function createElement() { @@ -71,80 +49,55 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format; notAppend: true }); + Element = element; + return element; } - this.show = function(callback) { + function checkFn(name, fn) { + if (typeof fn !== 'function') + throw Error(name + ' should be a function!'); + } + + function initConfig(config, options) { + Util.copyObj(config, ConfigView); + + if (!options) + return config; + + if (options.afterShow) { + checkFn('options.afterShow', options.afterShow); + + var afterShow = config.afterShow; + + config.afterShow = function() { + afterShow(); + options.afterShow(); + }; + } + + return config; + } + + this.show = function(options) { if (Loading) return; - - Images.show.load(); - - if (callback) - ConfigView.beforeShow = callback; - - Info.getData(function(error, data) { - var path = Info.path; - var isDir = Info.isDir; - var name = Info.name; - - if (isDir) - name += '.json'; - - if (error) - return Images.hide(); - - Edit.setValue(data, { - name: name, - path: path - }); - - CloudCmd.View.show(Element, ConfigView); - }); + + CloudCmd.View.show(Element, initConfig(options)); }; - this.setValue = function(value, info) { - var path = info.path; - var name = info.name; - - editor - .setValueFirst(path, value) - .setModeForPath(name) - .setOption('fontSize', 16); - - setMsgChanged(name); + this.getEditor = function() { + return editor; + }; + + this.getElement = function() { + return Element; }; this.hide = function() { CloudCmd.View.hide(); }; - this.create = function(element) { - Element = element; - - editor.on('save', function(value) { - var size = Format.size(value); - DOM.setCurrentSize(size); - }); - - return this; - }; - - function authCheck(spawn) { - DOM.Files.get('config', function(error, config) { - if (error) - return Dialog.alert(TITLE, error); - - if (!config.auth) - return; - - spawn.emit('auth', config.username, config.password); - spawn.on('reject', function() { - Dialog.alert(TITLE, 'Wrong credentials!'); - }); - }); - } - function getConfig(callback) { DOM.Files.get('config', function(error, config) { if (error) @@ -181,84 +134,6 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format; }); } - function setMenu(event) { - var position = { - x: event.clientX, - y: event.clientY - }; - - event.preventDefault(); - - !Menu && DOM.loadRemote('menu', function(error) { - var noFocus; - var options = { - beforeShow: function(params) { - params.x -= 18; - params.y -= 27; - }, - - afterClick: function() { - !noFocus && editor.focus(); - } - }; - var menuData = { - 'Save Ctrl+S' : function() { - editor.save(); - }, - 'Go To Line Ctrl+G' : function() { - noFocus = true; - editor.goToLine(); - }, - 'Cut Ctrl+X' : function() { - editor.cutToClipboard(); - }, - 'Copy Ctrl+C' : function() { - editor.copyToClipboard(); - }, - 'Paste Ctrl+V' : function() { - editor.pasteFromClipboard(); - }, - 'Delete Del' : function() { - editor.remove('right'); - }, - 'Select All Ctrl+A' : function() { - editor.selectAll(); - }, - 'Beautify Ctrl+B' : function() { - editor.beautify(); - }, - 'Minify Ctrl+M' : function() { - editor.minify(); - }, - 'Close Esc' : function() { - Edit.hide(); - } - }; - - if (error) - return Dialog.alert(TITLE, error); - - if (Menu || !MenuIO) - return; - - Menu = new MenuIO(Element, options, menuData); - Menu.show(position.x, position.y); - }); - } - - function setMsgChanged(name) { - MSG_CHANGED = 'Do you want to save changes to ' + name + '?'; - } - - function isChanged() { - var is = editor.isChanged(); - - is && Dialog.confirm(TITLE, MSG_CHANGED, {cancel: false}) - .then(function() { - editor.save(); - }); - } - init(callback); } diff --git a/client/key.js b/client/key.js index 447fcf62..ebdc4ac8 100644 --- a/client/key.js +++ b/client/key.js @@ -294,7 +294,7 @@ var CloudCmd, Util, DOM; break; case Key.F4: - CloudCmd.Edit.show(); + CloudCmd.EditFile.show(); event.preventDefault(); break; diff --git a/client/listeners.js b/client/listeners.js index 7d2a8e3f..159a323e 100644 --- a/client/listeners.js +++ b/client/listeners.js @@ -53,7 +53,7 @@ var Util, DOM, CloudFunc, CloudCmd; 'f1' : CloudCmd.Help.show, 'f2' : DOM.renameCurrent, 'f3' : CloudCmd.View.show, - 'f4' : CloudCmd.Edit.show, + 'f4' : CloudCmd.EditFile.show, 'f5' : operation('copy'), 'f6' : operation('move'), 'f7' : DOM.promptNewDir, diff --git a/json/modules.json b/json/modules.json index 203e8ea0..c653ea58 100644 --- a/json/modules.json +++ b/json/modules.json @@ -1,5 +1,6 @@ [ "edit", + "edit-file", "menu", "view", "help", @@ -8,7 +9,7 @@ "contact", "upload", "operation", - "konsole", + "konsole", "cloud", [{ "name": "remote", "data": [{ diff --git a/package.json b/package.json index 865680fe..3132fcc2 100644 --- a/package.json +++ b/package.json @@ -122,6 +122,7 @@ "minify": "^2.0.0", "minimist": "^1.2.0", "mollify": "^1.0.0", + "nomine": "^1.0.1", "onezip": "^1.0.5", "opn": "^4.0.1", "os-homedir": "^1.0.0", diff --git a/server/cloudcmd.js b/server/cloudcmd.js index 8fec3d3f..aa32e66b 100644 --- a/server/cloudcmd.js +++ b/server/cloudcmd.js @@ -23,6 +23,7 @@ const konsole = require('console-io/legacy'); const edward = require('edward/legacy'); const dword = require('dword/legacy'); const deepword = require('deepword/legacy'); +const nomine = require('nomine/legacy'); const spero = require('spero'); const remedy = require('remedy'); const ishtar = require('ishtar'); @@ -237,6 +238,10 @@ function cloudcmd(prefix, plugins) { prefix: prefix + '/salam', }), + nomine({ + prefix: prefix + '/rename', + }), + setUrl(prefix), logout, auth(),