Merge branch 'vdom' of github.com:xwiki-labs/cryptpad into demoEffect

This commit is contained in:
ansuz 2016-02-11 13:15:57 +01:00
commit 653ba33b65
4 changed files with 74 additions and 122 deletions

View File

@ -4,6 +4,8 @@
"version": "0.1.0", "version": "0.1.0",
"dependencies": { "dependencies": {
"express": "~4.10.1", "express": "~4.10.1",
"ws": "^1.0.1" "ws": "^1.0.1",
"level": "~1.4.0",
"nthen": "~0.1.0"
} }
} }

View File

@ -1,77 +1,48 @@
var level=require("level"); var Level = require("level");
var nThen = require('nthen');
module.exports.create = function(conf,cb){ var getIndex = function(db, cName, cb) {
console.log("Loading leveldb"); db.get(cName+'=>index', function(e, out){
if (e) { throw e; }
var db=level(conf.levelPath||'./test.level.db'), cb(parseInt(out));
indices={}, });
Channel={}; };
var makeChannel=function(cName){ var insert = function (db, channelName, content, cb) {
Channel[cName]={ var index;
lastModified:0, nThen(function (waitFor) {
messages:[], getIndex(db, channelName, waitFor(function (i) { index = i; }));
}; }).nThen(function (waitFor) {
}, db.put(channelName+'=>index', ''+index, waitFor(function (e) { if (e) { throw e; } }));
makeIndex=function(cName){ }).nThen(function (waitFor) {
// initializing to negative one means we can increment on inserts db.put(channelName+'=>'+index, content, waitFor(function (e) { if (e) { throw e; } }));
// so we always start from zero. }).nThen(cb);
indices[cName]=-1; };
},
loadIndex=function(cName, out){ var getMessages = function (db, channelName, msgHandler) {
indices[cName]=parseInt(out); var index;
typeof indices[cName] !== 'number' && nThen(function (waitFor) {
console.error("FOUND A NON-NUMERIC INDEX for channel: %s", cName); getIndex(db, channelName, waitFor(function (i) { index = i; }));
}, }).nThen(function (waitFor) {
getIndex=function(cName,f){ var again = function (i) {
if(typeof indices[cName] !== 'undefined'){ db.get(channelName + '=>' + i, waitFor(function (e, out) {
f(indices[cName]); if (e) { throw e; }
}else{ msgHandler(out);
// get and increment the channelIndex if (i < index) { again(i+1); }
db.get(cName+'=>index',function(e,out){ }));
if(e){ };
// it doesn't exist, so initialize it again(0);
makeIndex(cName); });
}else{ };
// it exists. parse it
loadIndex(cName,out); module.exports.create = function (conf, cb) {
} var db = Level(conf.levelPath || './test.level.db');
f(indices[cName]); cb({
}); message: function (channelName, content, cb) {
} insert(db, channelName, content, cb);
}; },
getMessages: function (channelName, msgHandler) {
cb({ getMessages(db, channelName, msgHandler);
message: function(cName,content,cb){ }
getIndex(cName,function(index){
var index = ++indices[cName];
db.put(cName+'=>index', ''+index,function(e){
if(e) console.error(e);
});
db.put(cName+'=>'+index, content, function(err){
if(err){
console.log(err);
}
cb();
});
});
},
getMessages: function(cName, cb){
/* get all messages relating to a channel */
getIndex(cName, function(index){
var last = index,
i = 0,
next = function () {
db.get(cName+'=>'+i, function (e,out) {
if(e) return console.error(e);
cb(out);
if (++i <= last) {
next();
}
});
};
next();
});
},
}); });
}; };

View File

@ -372,7 +372,7 @@ var random = Patch.random = function (doc, opCount) {
var PARANOIA = module.exports.PARANOIA = false; var PARANOIA = module.exports.PARANOIA = false;
/* throw errors over non-compliant messages which would otherwise be treated as invalid */ /* throw errors over non-compliant messages which would otherwise be treated as invalid */
var TESTING = module.exports.TESTING = true; var TESTING = module.exports.TESTING = false;
var assert = module.exports.assert = function (expr) { var assert = module.exports.assert = function (expr) {
if (!expr) { throw new Error("Failed assertion"); } if (!expr) { throw new Error("Failed assertion"); }
@ -1163,6 +1163,7 @@ module.exports.create = function (userName, authToken, channelId, initialState,
Common.assert(typeof(initialState) === 'string'); Common.assert(typeof(initialState) === 'string');
var realtime = ChainPad.create(userName, authToken, channelId, initialState, conf); var realtime = ChainPad.create(userName, authToken, channelId, initialState, conf);
return { return {
Operation: Operation,
onPatch: enterChainPad(realtime, function (handler) { onPatch: enterChainPad(realtime, function (handler) {
Common.assert(typeof(handler) === 'function'); Common.assert(typeof(handler) === 'function');
realtime.patchHandlers.push(handler); realtime.patchHandlers.push(handler);

View File

@ -6,12 +6,9 @@ define([
'/common/convert.js', '/common/convert.js',
'/common/toolbar.js', '/common/toolbar.js',
'/common/cursor.js', '/common/cursor.js',
'/common/Operation.js',
'/bower_components/jquery/dist/jquery.min.js', '/bower_components/jquery/dist/jquery.min.js',
'/customize/pad.js' '/customize/pad.js'
], function (Config, Messages, Crypto, realtimeInput, Convert, Toolbar, Cursor, Operation) { ], function (Config, Messages, Crypto, realtimeInput, Convert, Toolbar, Cursor) {
window.Operation = Operation;
var $ = window.jQuery; var $ = window.jQuery;
var ifrw = $('#pad-iframe')[0].contentWindow; var ifrw = $('#pad-iframe')[0].contentWindow;
window.Ckeditor = ifrw.CKEDITOR; window.Ckeditor = ifrw.CKEDITOR;
@ -107,53 +104,34 @@ define([
onInit: onInit, onInit: onInit,
transformFunction : function (text, toTransform, transformBy) { transformFunction : function (text, toTransform, transformBy) {
/* FIXME
operational transform on json shouldn't be in all editors
just those transmitting/expecting JSON
*/
false && console.log({
text: text,
toTransform: toTransform,
transformBy: transformBy
});
var resultOp = ChainPad.Operation.transform0(text, toTransform, transformBy);
var text2 = ChainPad.Operation.apply(transformBy, text);
var text3 = ChainPad.Operation.apply(resultOp, text2);
try { JSON.parse(text3); return resultOp; } catch (e) { console.log(e); }
// returning **null** breaks out of the loop // returning **null** breaks out of the loop
// which transforms conflicting operations // which transforms conflicting operations
// in theory this should prevent us from producing bad JSON // in theory this should prevent us from producing bad JSON
return null; return null;
}, }
// OT
/* /*
transformFunction: function (text, toTransform, transformBy) { FIXME NOT A REAL FUNCTION WONT WORK
if (toTransform.offset > transformBy.offset) { transformFunction: function (state0str, toTransform, transformBy) {
if (toTransform.offset > transformBy.offset + transformBy.toRemove) { var state1A = JSON.parse(Operation.apply(state0str, transformBy));
// simple rebase var state1B = JSON.parse(Operation.apply(state0str, toTransform));
toTransform.offset -= transformBy.toRemove; var state0 = JSON.parse(state0str);
toTransform.offset += transformBy.toInsert.length;
// TODO check that this is using the correct parameters
// TODO get the actual library
// TODO functionize this because it's repeated
var temp = Operation.apply(text, toTransform)
try {
JSON.parse(temp);
} catch (err) {
console.error(err.stack);
return null;
} }
return toTransform; */
}
// goto the end, anything you deleted that they also deleted should be skipped.
var newOffset = transformBy.offset + transformBy.toInsert.length;
toTransform.toRemove = 0; //-= (newOffset - toTransform.offset);
if (toTransform.toRemove < 0) { toTransform.toRemove = 0; }
toTransform.offset = newOffset;
if (toTransform.toInsert.length === 0 && toTransform.toRemove === 0) {
return null;
}
return toTransform;
}
if (toTransform.offset + toTransform.toRemove < transformBy.offset) {
return toTransform;
}
toTransform.toRemove = transformBy.offset - toTransform.offset;
if (toTransform.toInsert.length === 0 && toTransform.toRemove === 0) {
return null;
}
return toTransform;
} */
}); });
$textarea.val(JSON.stringify(Convert.dom.to.hjson(inner))); $textarea.val(JSON.stringify(Convert.dom.to.hjson(inner)));