From e2f3f832658e4b8a760cf083efc5c50b5e4af374 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 18 May 2017 14:49:00 +0300 Subject: [PATCH] feature(edit-file-vim) add ability to reuse edit-file: minimize file size --- client/modules/edit-file-vim.js | 183 ++------------------------------ client/modules/edit-file.js | 21 ++-- 2 files changed, 24 insertions(+), 180 deletions(-) diff --git a/client/modules/edit-file-vim.js b/client/modules/edit-file-vim.js index 6b598d57..0002fb8f 100644 --- a/client/modules/edit-file-vim.js +++ b/client/modules/edit-file-vim.js @@ -1,205 +1,40 @@ 'use strict'; -/* global CloudCmd, DOM, MenuIO */ - -const Format = require('format-io'); -const currify = require('currify/legacy'); -const squad = require('squad'); -const store = require('../../common/store'); +const exec = require('execon'); const Key = CloudCmd.Key; const Events = require('../dom/events'); -const call = currify((fn, callback) => { - fn(); - callback(); -}); - CloudCmd.EditFileVim = function EditFileVimProto(callback) { - const Info = DOM.CurrentInfo; - const Dialog = DOM.Dialog; const EditFileVim = this; - const config = CloudCmd.config; - let Menu; - - const exec = require('execon'); - const TITLE = 'Edit'; - const Images = DOM.Images; - - let MSG_CHANGED; const ConfigView = { bindKeys: false, beforeClose: () => { Events.rmKey(listener); - exec.ifExist(Menu, 'hide'); - isChanged(); } }; function init(callback) { - const editor = store(); - - const getMainEditor = () => CloudCmd.Edit.getEditor(); - const getEditor = squad(editor, getMainEditor); - const auth = squad(authCheck, editor); - const listeners = squad(setListeners, editor); - exec.series([ - CloudCmd.Edit, - call(getEditor), - call(auth), - call(listeners), - EditFileVim.show, - ], callback); - } - - function getName() { - const {name, isDir} = Info; - - if (isDir) - return `${name}.json`; - - return name; + CloudCmd.EditFile, + callback || EditFileVim.show, + ]); } this.show = () => { - Images.show.load(); + Events.addKey(listener); - Info.getData((error, data) => { - const path = Info.path; - const name = getName(); - - if (error) - return Images.hide(); - - setMsgChanged(name); - - Events.addKey(listener); - - CloudCmd.Edit - .show(ConfigView) - .getEditor() - .setValueFirst(path, data) - .setModeForPath(name) - .enableKey() - .setOptions({ - keyMap: 'vim' - }); - }); + CloudCmd.EditFile + .show(ConfigView) + .getEditor() + .setOption('keyMap', 'vim'); }; this.hide = () => { CloudCmd.Edit.hide(); }; - function setListeners(editor) { - const element = CloudCmd.Edit.getElement(); - - Events.addOnce('contextmenu', element, setMenu); - - editor.on('save', (value) => { - DOM.setCurrentSize(Format.size(value)); - }); - } - - function authCheck(spawn) { - if (!config('auth')) - return; - - spawn.emit('auth', config('username'), config('password')); - spawn.on('reject', () => { - Dialog.alert(TITLE, 'Wrong credentials!'); - }); - } - - function setMenu(event) { - const position = { - x: event.clientX, - y: event.clientY - }; - - event.preventDefault(); - - !Menu && DOM.loadRemote('menu', (error) => { - let noFocus; - const editor = CloudCmd.Edit.getEditor(); - const options = { - beforeShow: (params) => { - params.x -= 18; - params.y -= 27; - }, - - afterClick: () => { - !noFocus && editor.focus(); - } - }; - - const menuData = { - 'Save Ctrl+S' : () => { - editor.save(); - }, - 'Go To Line Ctrl+G' : () => { - noFocus = true; - editor.goToLine(); - }, - 'Cut Ctrl+X' : () => { - editor.cutToClipboard(); - }, - 'Copy Ctrl+C' : () => { - editor.copyToClipboard(); - }, - 'Paste Ctrl+V' : () => { - editor.pasteFromClipboard(); - }, - 'Delete Del' : () => { - editor.remove('right'); - }, - 'Select All Ctrl+A' : () => { - editor.selectAll(); - }, - 'Beautify Ctrl+B' : () => { - editor.beautify(); - }, - 'Minify Ctrl+M' : () => { - editor.minify(); - }, - 'Close Esc' : () => { - EditFileVim.hide(); - } - }; - - if (error) - return Dialog.alert(TITLE, error); - - if (Menu || !MenuIO) - return; - - const 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() { - const editor = CloudCmd.Edit.getEditor(); - const is = editor.isChanged(); - - if (!is) - return; - - const cancel = false; - Dialog.confirm(TITLE, MSG_CHANGED, {cancel}) - .then(() => { - editor.save(); - }); - } - function listener({keyCode, shiftKey}) { if (shiftKey && keyCode === Key.ESC) EditFileVim.hide(); diff --git a/client/modules/edit-file.js b/client/modules/edit-file.js index 97b68f25..8913c6a5 100644 --- a/client/modules/edit-file.js +++ b/client/modules/edit-file.js @@ -5,6 +5,7 @@ const Format = require('format-io'); const currify = require('currify/legacy'); const squad = require('squad'); +const exec = require('execon'); const store = require('../../common/store'); const call = currify((fn, callback) => { @@ -15,12 +16,11 @@ const call = currify((fn, callback) => { CloudCmd.EditFile = function EditFileProto(callback) { const Info = DOM.CurrentInfo; const Dialog = DOM.Dialog; - const EditFile = this; + const EditFile = exec.bind(); const config = CloudCmd.config; let Menu; - const exec = require('execon'); const TITLE = 'Edit'; const Images = DOM.Images; @@ -40,12 +40,14 @@ CloudCmd.EditFile = function EditFileProto(callback) { const auth = squad(authCheck, editor); const listeners = squad(setListeners, editor); + const show = callback ? exec : EditFile.show; + exec.series([ CloudCmd.Edit, call(getEditor), call(auth), call(listeners), - EditFile.show, + show, ], callback); } @@ -58,9 +60,13 @@ CloudCmd.EditFile = function EditFileProto(callback) { return name; } - this.show = () => { + EditFile.show = (options) => { + const config = Object.assign({}, ConfigView, options); + Images.show.load(); + CloudCmd.Edit.show(config); + Info.getData((error, data) => { const path = Info.path; const name = getName(); @@ -71,15 +77,16 @@ CloudCmd.EditFile = function EditFileProto(callback) { setMsgChanged(name); CloudCmd.Edit - .show(ConfigView) .getEditor() .setValueFirst(path, data) .setModeForPath(name) .enableKey(); }); + + return CloudCmd.Edit; }; - this.hide = () => { + EditFile.hide = () => { CloudCmd.Edit.hide(); }; @@ -191,5 +198,7 @@ CloudCmd.EditFile = function EditFileProto(callback) { } init(callback); + + return EditFile; };