mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-19 01:17:18 +00:00
refactor(edit) add doDiff
This commit is contained in:
parent
70e2ee7214
commit
8f58774bcb
1 changed files with 37 additions and 39 deletions
|
|
@ -1,4 +1,4 @@
|
|||
var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch;
|
||||
var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch, Ace;
|
||||
(function(CloudCmd, Util, DOM, CloudFunc) {
|
||||
'use strict';
|
||||
|
||||
|
|
@ -14,7 +14,7 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch;
|
|||
Edit = this,
|
||||
Diff,
|
||||
Emmet,
|
||||
Ace,
|
||||
//Ace,
|
||||
Session,
|
||||
Modelist,
|
||||
Msg,
|
||||
|
|
@ -167,52 +167,50 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch;
|
|||
}
|
||||
|
||||
function save () {
|
||||
var path = Info.path,
|
||||
var path = Info.path,
|
||||
value = Ace.getValue();
|
||||
|
||||
CloudCmd.getConfig(function(config) {
|
||||
var isDiff = config.diff;
|
||||
|
||||
Util.ifExec(!isDiff, function(patch) {
|
||||
var query,
|
||||
MAX_SIZE = CloudFunc.MAX_FILE_SIZE,
|
||||
|
||||
length = value.length,
|
||||
|
||||
isLength = length > MAX_SIZE,
|
||||
isAllowed = DOM.Storage.isAllowed();
|
||||
var query = '',
|
||||
MAX_SIZE = CloudFunc.MAX_FILE_SIZE,
|
||||
patchLength = patch.length,
|
||||
length = Value.length,
|
||||
isLessMaxLength = length < MAX_SIZE,
|
||||
isLessLength = isLessMaxLength && patchLength < length,
|
||||
isStr = Util.isString(patch);
|
||||
|
||||
Value = value;
|
||||
Value = value;
|
||||
|
||||
Util.ifExec(isLength && !isAllowed, function() {
|
||||
if (!query)
|
||||
query = '';
|
||||
if (isStr && patch && isLessLength)
|
||||
query = '?patch';
|
||||
|
||||
DOM.RESTful.write(path + query, patch || Value, onSave);
|
||||
|
||||
}, Util.bind(doDiff, path));
|
||||
});
|
||||
}
|
||||
|
||||
function doDiff(path, callback) {
|
||||
var value = Ace.getValue();
|
||||
|
||||
diff(value, function(patch) {
|
||||
var isAllowed = DOM.Storage.isAllowed();
|
||||
|
||||
Util.ifExec(!isAllowed, callback, function(func) {
|
||||
DOM.checkStorageHash(path, function(error, equal) {
|
||||
if (!equal)
|
||||
patch = '';
|
||||
|
||||
DOM.RESTful.write(path + query, value, onSave);
|
||||
}, function(callback) {
|
||||
DOM.checkStorageHash(path, function(error, equal) {
|
||||
var isString = Util.isString(patch),
|
||||
patchLength = patch.length,
|
||||
lessLength = patchLength < length;
|
||||
|
||||
if (!error) {
|
||||
if (equal && isString && patchLength && lessLength) {
|
||||
value = patch;
|
||||
query = '?patch';
|
||||
}
|
||||
|
||||
callback();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}, function(callback) {
|
||||
diff(value, callback);
|
||||
func(patch);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function diff(pNewValue, pCallBack) {
|
||||
function diff(newValue, callback) {
|
||||
var libs = [
|
||||
LIBDIR + 'diff/diff-match-patch.js',
|
||||
LIBDIR + 'diff.js'
|
||||
|
|
@ -227,16 +225,16 @@ var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch;
|
|||
Diff = new DiffProto(diff_match_patch);
|
||||
|
||||
Util.ifExec(!isAllowed, function() {
|
||||
patch = Diff.createPatch(Value, pNewValue);
|
||||
Util.exec(pCallBack, patch);
|
||||
}, function(callback) {
|
||||
patch = Diff.createPatch(Value, newValue);
|
||||
Util.exec(callback, patch);
|
||||
}, function(func) {
|
||||
var path = Info.path;
|
||||
|
||||
DOM.getDataFromStorage(path, function(data) {
|
||||
if (data)
|
||||
Value = data;
|
||||
|
||||
callback();
|
||||
func();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue