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

pull/1/head
ansuz 9 years ago
commit 653ba33b65

@ -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"
} }
} }

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

@ -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);

@ -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) {
if (toTransform.offset > transformBy.offset) {
if (toTransform.offset > transformBy.offset + transformBy.toRemove) {
// simple rebase
toTransform.offset -= transformBy.toRemove;
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; /*
} FIXME NOT A REAL FUNCTION WONT WORK
if (toTransform.offset + toTransform.toRemove < transformBy.offset) { transformFunction: function (state0str, toTransform, transformBy) {
return toTransform; var state1A = JSON.parse(Operation.apply(state0str, transformBy));
} var state1B = JSON.parse(Operation.apply(state0str, toTransform));
toTransform.toRemove = transformBy.offset - toTransform.offset; var state0 = JSON.parse(state0str);
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)));

Loading…
Cancel
Save