feature(diff) add

This commit is contained in:
coderaiser 2013-11-12 10:21:39 +00:00
parent d5be099df5
commit 21b3756b6e
7 changed files with 2309 additions and 5 deletions

View file

@ -148,6 +148,7 @@ All main configuration could be done via [config.json](json/config.json "Config"
"apiURL" :"/api/v1",
"appCache" : false, /* cache files for offline use */
"analytics" : true, /* google analytics suport */
"diff" : false, /* when save - send patch not whole file */
"localStorage" : true, /* cache directory data */
"minify" : true /* minification of js,css,html and img */
"online" : true, /* load js files from cdn or local path */

View file

@ -2,6 +2,7 @@
<ul class="list" >
<li><span><input type="checkbox" id="appCache" {{ appCache }} ></input></span><label for="appCache"> App cache</label></li>
<li><span><input type="checkbox" id="analytics" {{ analytics }} ></input></span><label for="analytics"> Analytics</label></li>
<li><span><input type="checkbox" id="diff" {{ diff }} ></input></span><label for="diff"> Diff</label></li>
<li><span><input type="checkbox" id="localStorage" {{ localStorage }} ></input></span><label for="localStorage"> Local Storage</label></li>
<li><span><input type="checkbox" id="minify" {{ minify }} ></input></span><label for="minify"> Minify</label></li>
<li><span><input type="checkbox" id="online" {{ online }} ></input></span><label for="online"> Online</label></li>

View file

@ -1,4 +1,4 @@
var CloudCmd, Util, DOM, CloudFunc, JsDiff, ace;
var CloudCmd, Util, DOM, CloudFunc, ace, DiffProto, diff_match_patch;
(function(CloudCmd, Util, DOM, CloudFunc) {
'use strict';
@ -7,9 +7,11 @@ var CloudCmd, Util, DOM, CloudFunc, JsDiff, ace;
function EditProto(CloudCmd, Util, DOM, CloudFunc) {
var Name = 'Edit',
Loading = false,
DIR = CloudCmd.LIBDIRCLIENT + 'edit/',
DIR = CloudCmd.LIBDIRCLIENT + 'edit/',
LIBDIR = CloudCmd.LIBDIR,
Value,
Edit = this,
Diff,
Ace,
Msg,
Key = CloudCmd.Key,
@ -120,11 +122,46 @@ var CloudCmd, Util, DOM, CloudFunc, JsDiff, ace;
var lPath = DOM.getCurrentPath(),
lValue = Ace.getValue();
DOM.RESTful.save(lPath, lValue, Edit.showMessage);
CloudCmd.getConfig(function(config) {
var isDiff = config.diff;
Util.ifExec(!isDiff, function(patch) {
var query;
if (Util.isString(patch)) {
lValue = patch;
query = '?patch';
}
DOM.RESTful.save(lPath, lValue, Edit.showMessage, query);
}, function(callback) {
diff(lValue, function(diff) {
Util.exec(callback, diff);
});
});
});
}
});
}
function diff(pNewValue, pCallBack) {
var libs = [
LIBDIR + 'diff.js',
LIBDIR + 'diff/diff-match-patch.js'
];
DOM.anyLoadInParallel(libs, function() {
var patch;
if (!Diff)
Diff = new DiffProto(diff_match_patch);
patch = Diff.createPatch(Value, pNewValue);
Util.exec(pCallBack, patch);
});
}
function load(pCallBack) {
Util.time(Name + ' load');

21
lib/diff.js Normal file
View file

@ -0,0 +1,21 @@
(function (obj) {
obj.DiffProto = function(diffMatchPatch) {
var dmp = new diffMatchPatch();
this.createPatch = function(oldText, newText) {
var diff = dmp.diff_main(oldText, newText),
patchList = dmp.patch_make(oldText, newText, diff),
patch = dmp.patch_toText(patchList);
return patch;
};
this.applyPatch = function(oldText, patch) {
var patches = dmp.patch_fromText(patch),
results = dmp.patch_apply(patches, oldText);
return results;
};
};
})(this);

2193
lib/diff/diff-match-patch.js Normal file

File diff suppressed because it is too large Load diff

View file

@ -11,7 +11,7 @@
SLASH,
ISWIN32,
ext,
path, fs, zlib, url, pipe, CloudFunc,
path, fs, zlib, url, pipe, CloudFunc, diffPatch,
OK, FILE_NOT_FOUND, MOVED_PERMANENTLY,
REQUEST, RESPONSE,
@ -108,6 +108,9 @@
exports.auth = srvrequire('auth').auth,
exports.appcache = srvrequire('appcache'),
exports.dir = srvrequire('dir'),
diffPatch = librequire('diff/diff-match-patch').diff_match_patch,
exports.diff = new (librequire('diff').DiffProto)(diffPatch),
console.log(exports.diff);
exports.rest = srvrequire('rest').api,
exports.update = srvrequire('update'),
exports.ischanged = srvrequire('ischanged');

View file

@ -20,6 +20,7 @@
pipe = main.pipe,
CloudFunc = main.cloudfunc,
dir = main.dir,
diff = main.diff,
JSONDIR = main.JSONDIR,
OK = 200,
sendError = main.sendError,
@ -145,7 +146,54 @@
else
sendMsg(pParams, 'make dir', p.name);
});
else if(lQuery === 'patch')
getBody(p.request, function(pPatch) {
fs.readFile(p.name, Util.call(read, pParams));
function read(pParams) {
var lDiff, lStr, p, lData, lName,
lRet = main.checkCallBackParams(pParams) &&
main.checkParams(pParams.params);
if (lRet) {
p = pParams;
lName = p.params.name;
if (p.error)
main.sendError(p.params, p.error);
else {
lStr = p.data.toString();
lRet = Util.tryCatchLog(function() {
lDiff = diff.applyPatch(lStr, pPatch);
});
if (lDiff && !lRet)
fs.writeFile(lName, lDiff, Util.call(write, p.params));
else {
lName = path.basename(lName);
sendMsg(p.params, 'patch', lName, 'fail');
}
}
}
}
function write(pParams) {
var p, lName,
lRet = main.checkCallBackParams(pParams) &&
main.checkParams(pParams.params);
if (lRet) {
p = pParams;
if (p.error)
main.sendError(p.params, p.error);
else {
lName = path.basename(p.params.name);
sendMsg(p.params, 'patch', lName);
}
}
}
});
else
pipe.create({
read : p.request,