From 8f58774bcb5c7cd4cf332277d5b3d0e749a7885c Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 25 Mar 2014 09:22:28 -0400 Subject: [PATCH] refactor(edit) add doDiff --- lib/client/edit.js | 76 ++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/lib/client/edit.js b/lib/client/edit.js index 2fa082f3..7db84c95 100644 --- a/lib/client/edit.js +++ b/lib/client/edit.js @@ -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(); }); }); });