2011-12-04 16:33:56 +01:00
|
|
|
/**
|
|
|
|
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
|
|
|
* This helps other people to understand this code better and helps them to improve it.
|
|
|
|
* TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
|
|
|
|
*/
|
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2009 Google Inc.
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function makeChangesetTracker(scheduler, apool, aceCallbacksProvider)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
|
|
|
|
// latest official text from server
|
|
|
|
var baseAText = Changeset.makeAText("\n");
|
|
|
|
// changes applied to baseText that have been submitted
|
|
|
|
var submittedChangeset = null;
|
|
|
|
// changes applied to submittedChangeset since it was prepared
|
|
|
|
var userChangeset = Changeset.identity(1);
|
|
|
|
// is the changesetTracker enabled
|
|
|
|
var tracking = false;
|
|
|
|
// stack state flag so that when we change the rep we don't
|
|
|
|
// handle the notification recursively. When setting, always
|
|
|
|
// unset in a "finally" block. When set to true, the setter
|
|
|
|
// takes change of userChangeset.
|
|
|
|
var applyingNonUserChanges = false;
|
|
|
|
|
|
|
|
var changeCallback = null;
|
|
|
|
|
|
|
|
var changeCallbackTimeout = null;
|
2011-07-07 19:59:34 +02:00
|
|
|
|
|
|
|
function setChangeCallbackTimeout()
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
// can call this multiple times per call-stack, because
|
|
|
|
// we only schedule a call to changeCallback if it exists
|
|
|
|
// and if there isn't a timeout already scheduled.
|
2011-07-07 19:59:34 +02:00
|
|
|
if (changeCallback && changeCallbackTimeout === null)
|
|
|
|
{
|
|
|
|
changeCallbackTimeout = scheduler.setTimeout(function()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
changeCallback();
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
changeCallbackTimeout = null;
|
|
|
|
}
|
2011-03-26 14:10:41 +01:00
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var self;
|
|
|
|
return self = {
|
2011-07-07 19:59:34 +02:00
|
|
|
isTracking: function()
|
|
|
|
{
|
|
|
|
return tracking;
|
|
|
|
},
|
|
|
|
setBaseText: function(text)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
self.setBaseAttributedText(Changeset.makeAText(text), null);
|
|
|
|
},
|
2011-07-07 19:59:34 +02:00
|
|
|
setBaseAttributedText: function(atext, apoolJsonObj)
|
|
|
|
{
|
|
|
|
aceCallbacksProvider.withCallbacks("setBaseText", function(callbacks)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
tracking = true;
|
|
|
|
baseAText = Changeset.cloneAText(atext);
|
2011-07-07 19:59:34 +02:00
|
|
|
if (apoolJsonObj)
|
|
|
|
{
|
|
|
|
var wireApool = (new AttribPool()).fromJsonable(apoolJsonObj);
|
|
|
|
baseAText.attribs = Changeset.moveOpsToNewPool(baseAText.attribs, wireApool, apool);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
submittedChangeset = null;
|
|
|
|
userChangeset = Changeset.identity(atext.text.length);
|
|
|
|
applyingNonUserChanges = true;
|
2011-07-07 19:59:34 +02:00
|
|
|
try
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
callbacks.setDocumentAttributedText(atext);
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
applyingNonUserChanges = false;
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2011-07-07 19:59:34 +02:00
|
|
|
composeUserChangeset: function(c)
|
|
|
|
{
|
|
|
|
if (!tracking) return;
|
2011-03-26 14:10:41 +01:00
|
|
|
if (applyingNonUserChanges) return;
|
|
|
|
if (Changeset.isIdentity(c)) return;
|
|
|
|
userChangeset = Changeset.compose(userChangeset, c, apool);
|
|
|
|
|
|
|
|
setChangeCallbackTimeout();
|
|
|
|
},
|
2011-07-07 19:59:34 +02:00
|
|
|
applyChangesToBase: function(c, optAuthor, apoolJsonObj)
|
|
|
|
{
|
|
|
|
if (!tracking) return;
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
aceCallbacksProvider.withCallbacks("applyChangesToBase", function(callbacks)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
if (apoolJsonObj)
|
|
|
|
{
|
|
|
|
var wireApool = (new AttribPool()).fromJsonable(apoolJsonObj);
|
|
|
|
c = Changeset.moveOpsToNewPool(c, wireApool, apool);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
baseAText = Changeset.applyToAText(c, baseAText, apool);
|
|
|
|
|
|
|
|
var c2 = c;
|
2011-07-07 19:59:34 +02:00
|
|
|
if (submittedChangeset)
|
|
|
|
{
|
|
|
|
var oldSubmittedChangeset = submittedChangeset;
|
|
|
|
submittedChangeset = Changeset.follow(c, oldSubmittedChangeset, false, apool);
|
|
|
|
c2 = Changeset.follow(oldSubmittedChangeset, c, true, apool);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var preferInsertingAfterUserChanges = true;
|
|
|
|
var oldUserChangeset = userChangeset;
|
|
|
|
userChangeset = Changeset.follow(c2, oldUserChangeset, preferInsertingAfterUserChanges, apool);
|
2011-07-07 19:59:34 +02:00
|
|
|
var postChange = Changeset.follow(oldUserChangeset, c2, !preferInsertingAfterUserChanges, apool);
|
2011-03-26 14:10:41 +01:00
|
|
|
|
|
|
|
var preferInsertionAfterCaret = true; //(optAuthor && optAuthor > thisAuthor);
|
|
|
|
applyingNonUserChanges = true;
|
2011-07-07 19:59:34 +02:00
|
|
|
try
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
callbacks.applyChangesetToDocument(postChange, preferInsertionAfterCaret);
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
applyingNonUserChanges = false;
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2011-07-07 19:59:34 +02:00
|
|
|
prepareUserChangeset: function()
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
// If there are user changes to submit, 'changeset' will be the
|
|
|
|
// changeset, else it will be null.
|
|
|
|
var toSubmit;
|
2011-07-07 19:59:34 +02:00
|
|
|
if (submittedChangeset)
|
|
|
|
{
|
|
|
|
// submission must have been canceled, prepare new changeset
|
|
|
|
// that includes old submittedChangeset
|
|
|
|
toSubmit = Changeset.compose(submittedChangeset, userChangeset, apool);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (Changeset.isIdentity(userChangeset)) toSubmit = null;
|
|
|
|
else toSubmit = userChangeset;
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var cs = null;
|
2011-07-07 19:59:34 +02:00
|
|
|
if (toSubmit)
|
|
|
|
{
|
|
|
|
submittedChangeset = toSubmit;
|
|
|
|
userChangeset = Changeset.identity(Changeset.newLen(toSubmit));
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
cs = toSubmit;
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
var wireApool = null;
|
2011-07-07 19:59:34 +02:00
|
|
|
if (cs)
|
|
|
|
{
|
|
|
|
var forWire = Changeset.prepareForWire(cs, apool);
|
|
|
|
wireApool = forWire.pool.toJsonable();
|
|
|
|
cs = forWire.translated;
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
var data = {
|
|
|
|
changeset: cs,
|
|
|
|
apool: wireApool
|
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
return data;
|
|
|
|
},
|
2011-07-07 19:59:34 +02:00
|
|
|
applyPreparedChangesetToBase: function()
|
|
|
|
{
|
|
|
|
if (!submittedChangeset)
|
|
|
|
{
|
|
|
|
// violation of protocol; use prepareUserChangeset first
|
|
|
|
throw new Error("applySubmittedChangesToBase: no submitted changes to apply");
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
//bumpDebug("applying committed changeset: "+submittedChangeset.encodeToString(false));
|
|
|
|
baseAText = Changeset.applyToAText(submittedChangeset, baseAText, apool);
|
|
|
|
submittedChangeset = null;
|
|
|
|
},
|
2011-07-07 19:59:34 +02:00
|
|
|
setUserChangeNotificationCallback: function(callback)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
changeCallback = callback;
|
|
|
|
},
|
2011-07-07 19:59:34 +02:00
|
|
|
hasUncommittedChanges: function()
|
|
|
|
{
|
|
|
|
return !!(submittedChangeset || (!Changeset.isIdentity(userChangeset)));
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|