Skip to content

Commit

Permalink
feature(edit-file) add
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Dec 28, 2016
1 parent 09e9757 commit 0126b72
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 126 deletions.
159 changes: 53 additions & 106 deletions client/edit-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,52 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format;
(function(CloudCmd, Util, DOM) {
'use strict';

CloudCmd.Edit = EditProto;

function EditProto(callback) {
var Name = 'Edit',
Loading = true,

Info = DOM.CurrentInfo,
Dialog = DOM.Dialog,

exec = Util.exec,

Menu,

EditorName = 'edward',
editor,
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,
Element,
ConfigView = {
beforeClose: function() {
exec.ifExist(Menu, 'hide');
isChanged(Edit.hide);
isChanged(EditFile.hide);
}
};

function init(callback) {
var element = createElement();

DOM.Events.addOnce('contextmenu', element, setMenu);
function init(callback) {
var editor;

exec.series([
CloudCmd.View,
CloudCmd.Edit,
getConfig,
function(callback) {
editor = CloudCmd.Edit.getEditor();
callback();
},
function(callback) {
authCheck(editor);
callback();
},

function(callback) {
CloudCmd.Edit
.create(element)
.show(callback);
setListeners(editor);
callback();
},
]);
}

function createElement() {
var element = DOM.load({
name : 'div',
style :
'width : 100%;' +
'height : 100%;' +
'font-family: "Droid Sans Mono";' +
'position : absolute;',
notAppend : true
});

return element;
function(callback) {
EditFile.show(callback);
},
], callback);
}

this.show = function() {
Images.show.load();

var editor = CloudCmd.Edit.getEditor();

Info.getData(function(error, data) {
var path = Info.path;
var isDir = Info.isDir;
Expand All @@ -82,32 +60,31 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format;
if (error)
return Images.hide();

editor.setValueFirst(path, data);

setMsgChanged(name);

editor
CloudCmd.Edit
.getEditor()
.setValueFirst(path, data)
.setModeForPath(name)
.setOption('fontSize', 16);

CloudCmd.View.show(Element, ConfigView);
CloudCmd.Edit.show(ConfigView);
});
};

this.hide = function() {
CloudCmd.View.hide();
CloudCmd.Edit.hide();
};

this.create = function(element) {
Element = element;
function setListeners(editor) {
var element = CloudCmd.Edit.getElement();

DOM.Events.addOnce('contextmenu', element, setMenu);

editor.on('save', function(value) {
var size = Format.size(value);
DOM.setCurrentSize(size);
DOM.setCurrentSize(Format.size(value));
});

return this;
};
}

function authCheck(spawn) {
DOM.Files.get('config', function(error, config) {
Expand All @@ -124,42 +101,6 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format;
});
}

function getConfig(callback) {
DOM.Files.get('config', function(error, config) {
if (error)
Dialog.alert(TITLE, error);
else if (config.editor)
EditorName = config.editor;

callback();
});
}

function loadFiles(element, callback) {
var prefix = CloudCmd.PREFIX,
prefixName = prefix + '/' + EditorName,
url = prefixName + '/' + EditorName + '.js';

Util.time(Name + ' load');

DOM.load.js(url, function() {
var word = window[EditorName],
options = {
maxSize : CloudFunc.MAX_FILE_SIZE,
prefix : prefixName,
socketPath : prefix
};

word(element, options, function(ed) {
Util.timeEnd(Name + ' load');
editor = ed;
Loading = false;

exec(callback);
});
});
}

function setMenu(event) {
var position = {
x: event.clientX,
Expand All @@ -169,18 +110,21 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format;
event.preventDefault();

!Menu && DOM.loadRemote('menu', function(error) {
var noFocus,
options = {
beforeShow: function(params) {
params.x -= 18;
params.y -= 27;
},

afterClick: function() {
!noFocus && editor.focus();
}
},
menuData = {
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();
},
Expand Down Expand Up @@ -210,7 +154,7 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format;
editor.minify();
},
'Close Esc' : function() {
Edit.hide();
EditFile.hide();
}
};

Expand All @@ -220,7 +164,9 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format;
if (Menu || !MenuIO)
return;

Menu = new MenuIO(Element, options, menuData);
var element = CloudCmd.Edit.getElement();

Menu = new MenuIO(element, options, menuData);
Menu.show(position.x, position.y);
});
}
Expand All @@ -230,6 +176,7 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format;
}

function isChanged() {
var editor = CloudCmd.Edit.getEditor();
var is = editor.isChanged();

is && Dialog.confirm(TITLE, MSG_CHANGED, {cancel: false})
Expand All @@ -239,7 +186,7 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format;
}

init(callback);
}
};

})(CloudCmd, Util, DOM, CloudFunc);

33 changes: 16 additions & 17 deletions client/edit.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format;
var CloudCmd, Util, DOM, CloudFunc;

(function(CloudCmd, Util, DOM) {
'use strict';

CloudCmd.Edit = EditProto;

function EditProto(callback) {
var Name = 'Edit',
Loading = true,

Dialog = DOM.Dialog,

var Name = 'Edit';
var Loading = true;

var Dialog = DOM.Dialog,
exec = Util.exec,
Edit = this,

Menu,
Element,

EditorName = 'edward',
EditorName = 'edward',
editor,

TITLE = 'Edit',
TITLE = 'Edit',

Element,
ConfigView = {
afterShow: function() {
editor
Expand All @@ -39,8 +35,7 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format;
function(callback) {
loadFiles(element, callback);
},
callback
]);
], callback);
}

function createElement() {
Expand All @@ -54,6 +49,8 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format;
notAppend: true
});

Element = element;

return element;
}

Expand All @@ -63,8 +60,6 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format;
}

function initConfig(config, options) {
var config = {};

Util.copyObj(config, ConfigView);

if (!options)
Expand All @@ -78,7 +73,7 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format;
config.afterShow = function() {
afterShow();
options.afterShow();
}
};
}

return config;
Expand All @@ -95,6 +90,10 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO, Format;
return editor;
};

this.getElement = function() {
return Element;
};

this.hide = function() {
CloudCmd.View.hide();
};
Expand Down
2 changes: 1 addition & 1 deletion client/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ var CloudCmd, Util, DOM;
break;

case Key.F4:
CloudCmd.Edit.show();
CloudCmd.EditFile.show();
event.preventDefault();
break;

Expand Down
2 changes: 1 addition & 1 deletion client/listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion json/modules.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[
"edit",
"edit-file",
"menu",
"view",
"help",
Expand All @@ -8,7 +9,7 @@
"contact",
"upload",
"operation",
"konsole",
"konsole",
"cloud", [{
"name": "remote",
"data": [{
Expand Down

0 comments on commit 0126b72

Please sign in to comment.