From 91ada5ce7aa4be0276144b0efdf8edac696c9300 Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 29 Feb 2016 15:28:32 +0100 Subject: [PATCH] export Operation from chainpad. for use with OT --- www/common/chainpad.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/www/common/chainpad.js b/www/common/chainpad.js index cf6193df8..c5854c7cf 100644 --- a/www/common/chainpad.js +++ b/www/common/chainpad.js @@ -37,7 +37,7 @@ var check = Patch.check = function (patch, docLength_opt) { Common.assert(Array.isArray(patch.operations)); Common.assert(/^[0-9a-f]{64}$/.test(patch.parentHash)); for (var i = patch.operations.length - 1; i >= 0; i--) { - Operation.check(patch.operations[i], docLength_opt); // INTEREST + Operation.check(patch.operations[i], docLength_opt); if (i > 0) { Common.assert(!Operation.shouldMerge(patch.operations[i], patch.operations[i-1])); } @@ -211,7 +211,7 @@ var equals = Patch.equals = function (patchA, patchB) { return true; }; -var transform = Patch.transform = function (origToTransform, transformBy, doc, transformFunction) { // INTEREST +var transform = Patch.transform = function (origToTransform, transformBy, doc, transformFunction) { if (Common.PARANOIA) { check(origToTransform, doc.length); check(transformBy, doc.length); @@ -228,7 +228,7 @@ var transform = Patch.transform = function (origToTransform, transformBy, doc, t toTransform.operations[i] = Operation.transform(text, toTransform.operations[i], transformBy.operations[j], - transformFunction); // INTEREST + transformFunction); if (!toTransform.operations[i]) { break; } @@ -547,7 +547,7 @@ var hashOf = Message.hashOf = function (msg) { * along with this program. If not, see . */ var Common = require('./Common'); -var Operation = require('./Operation'); +var Operation = module.exports.Operation = require('./Operation'); var Patch = require('./Patch'); var Message = require('./Message'); var Sha = require('./SHA256'); @@ -697,14 +697,14 @@ var onPong = function (realtime, msg) { schedule(realtime, function () { sendPing(realtime); }, realtime.pingCycle); }; -var create = ChainPad.create = function (userName, authToken, channelId, initialState, config) { // INTEREST +var create = ChainPad.create = function (userName, authToken, channelId, initialState, config) { var realtime = { type: 'ChainPad', authDoc: '', - config: config || {}, // INTEREST + config: config || {}, userName: userName, authToken: authToken, @@ -884,7 +884,7 @@ var applyPatch = function (realtime, author, patch) { } else { realtime.uncommitted = Patch.transform( - realtime.uncommitted, patch, realtime.authDoc, realtime.config.transformFunction); // INTEREST + realtime.uncommitted, patch, realtime.authDoc, realtime.config.transformFunction); } realtime.uncommitted.parentHash = patch.inverseOf.parentHash; @@ -1163,7 +1163,6 @@ module.exports.create = function (userName, authToken, channelId, initialState, Common.assert(typeof(initialState) === 'string'); var realtime = ChainPad.create(userName, authToken, channelId, initialState, conf); return { - Operation: Operation, onPatch: enterChainPad(realtime, function (handler) { Common.assert(typeof(handler) === 'function'); realtime.patchHandlers.push(handler); @@ -1253,7 +1252,7 @@ var check = Operation.check = function (op, docLength_opt) { Common.assert(Common.isUint(op.toRemove)); Common.assert(typeof(op.toInsert) === 'string'); Common.assert(op.toRemove > 0 || op.toInsert.length > 0); - Common.assert(typeof(docLength_opt) !== 'number' || op.offset + op.toRemove <= docLength_opt); // INTEREST + Common.assert(typeof(docLength_opt) !== 'number' || op.offset + op.toRemove <= docLength_opt); }; var create = Operation.create = function (offset, toRemove, toInsert) { @@ -1444,7 +1443,7 @@ var rebase = Operation.rebase = function (oldOp, newOp) { * @param transformBy an existing operation which also has the same base. * @return toTransform *or* null if the result is a no-op. */ -var transform0 = Operation.transform0 = function (text, toTransform, transformBy) { // INTEREST +var transform0 = Operation.transform0 = function (text, toTransform, transformBy) { if (toTransform.offset > transformBy.offset) { if (toTransform.offset > transformBy.offset + transformBy.toRemove) { // simple rebase @@ -1477,14 +1476,14 @@ var transform0 = Operation.transform0 = function (text, toTransform, transformBy * @param transformBy an existing operation which also has the same base. * @return a modified clone of toTransform *or* toTransform itself if no change was made. */ -var transform = Operation.transform = function (text, toTransform, transformBy, transformFunction) { // INTEREST +var transform = Operation.transform = function (text, toTransform, transformBy, transformFunction) { if (Common.PARANOIA) { check(toTransform); check(transformBy); } - transformFunction = transformFunction || transform0; // INTEREST + transformFunction = transformFunction || transform0; toTransform = clone(toTransform); - var result = transformFunction(text, toTransform, transformBy); // INTEREST + var result = transformFunction(text, toTransform, transformBy); if (Common.PARANOIA && result) { check(result); } return result; };