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.
|
|
|
|
*/
|
|
|
|
|
2012-01-16 05:16:11 +01:00
|
|
|
var Changeset = require('/easysync2').Changeset;
|
|
|
|
var extend = require('/ace2_common').extend;
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2012-01-16 02:23:48 +01:00
|
|
|
var undoModule = (function()
|
2011-07-07 19:59:34 +02:00
|
|
|
{
|
|
|
|
var stack = (function()
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var stackElements = [];
|
|
|
|
// two types of stackElements:
|
|
|
|
// 1) { elementType: UNDOABLE_EVENT, eventType: "anything", [backset: <changeset>,]
|
|
|
|
// [selStart: <char number>, selEnd: <char number>, selFocusAtStart: <boolean>] }
|
|
|
|
// 2) { elementType: EXTERNAL_CHANGE, changeset: <changeset> }
|
|
|
|
// invariant: no two consecutive EXTERNAL_CHANGEs
|
|
|
|
var numUndoableEvents = 0;
|
|
|
|
|
|
|
|
var UNDOABLE_EVENT = "undoableEvent";
|
|
|
|
var EXTERNAL_CHANGE = "externalChange";
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function clearStack()
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
stackElements.length = 0;
|
2011-07-07 19:59:34 +02:00
|
|
|
stackElements.push(
|
|
|
|
{
|
|
|
|
elementType: UNDOABLE_EVENT,
|
|
|
|
eventType: "bottom"
|
|
|
|
});
|
2011-03-26 14:10:41 +01:00
|
|
|
numUndoableEvents = 1;
|
|
|
|
}
|
|
|
|
clearStack();
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function pushEvent(event)
|
|
|
|
{
|
|
|
|
var e = extend(
|
|
|
|
{}, event);
|
2011-03-26 14:10:41 +01:00
|
|
|
e.elementType = UNDOABLE_EVENT;
|
|
|
|
stackElements.push(e);
|
|
|
|
numUndoableEvents++;
|
|
|
|
//dmesg("pushEvent backset: "+event.backset);
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function pushExternalChange(cs)
|
|
|
|
{
|
|
|
|
var idx = stackElements.length - 1;
|
|
|
|
if (stackElements[idx].elementType == EXTERNAL_CHANGE)
|
|
|
|
{
|
|
|
|
stackElements[idx].changeset = Changeset.compose(stackElements[idx].changeset, cs, getAPool());
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
stackElements.push(
|
|
|
|
{
|
|
|
|
elementType: EXTERNAL_CHANGE,
|
|
|
|
changeset: cs
|
|
|
|
});
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function _exposeEvent(nthFromTop)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
// precond: 0 <= nthFromTop < numUndoableEvents
|
|
|
|
var targetIndex = stackElements.length - 1 - nthFromTop;
|
|
|
|
var idx = stackElements.length - 1;
|
2011-07-07 19:59:34 +02:00
|
|
|
while (idx > targetIndex || stackElements[idx].elementType == EXTERNAL_CHANGE)
|
|
|
|
{
|
|
|
|
if (stackElements[idx].elementType == EXTERNAL_CHANGE)
|
|
|
|
{
|
|
|
|
var ex = stackElements[idx];
|
|
|
|
var un = stackElements[idx - 1];
|
|
|
|
if (un.backset)
|
|
|
|
{
|
|
|
|
var excs = ex.changeset;
|
|
|
|
var unbs = un.backset;
|
|
|
|
un.backset = Changeset.follow(excs, un.backset, false, getAPool());
|
|
|
|
ex.changeset = Changeset.follow(unbs, ex.changeset, true, getAPool());
|
|
|
|
if ((typeof un.selStart) == "number")
|
|
|
|
{
|
|
|
|
var newSel = Changeset.characterRangeFollow(excs, un.selStart, un.selEnd);
|
|
|
|
un.selStart = newSel[0];
|
|
|
|
un.selEnd = newSel[1];
|
|
|
|
if (un.selStart == un.selEnd)
|
|
|
|
{
|
|
|
|
un.selFocusAtStart = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stackElements[idx - 1] = ex;
|
|
|
|
stackElements[idx] = un;
|
|
|
|
if (idx >= 2 && stackElements[idx - 2].elementType == EXTERNAL_CHANGE)
|
|
|
|
{
|
|
|
|
ex.changeset = Changeset.compose(stackElements[idx - 2].changeset, ex.changeset, getAPool());
|
|
|
|
stackElements.splice(idx - 2, 1);
|
|
|
|
idx--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
idx--;
|
|
|
|
}
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function getNthFromTop(n)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
// precond: 0 <= n < numEvents()
|
|
|
|
_exposeEvent(n);
|
|
|
|
return stackElements[stackElements.length - 1 - n];
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function numEvents()
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
return numUndoableEvents;
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function popEvent()
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
// precond: numEvents() > 0
|
|
|
|
_exposeEvent(0);
|
|
|
|
numUndoableEvents--;
|
|
|
|
return stackElements.pop();
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
return {
|
|
|
|
numEvents: numEvents,
|
|
|
|
popEvent: popEvent,
|
|
|
|
pushEvent: pushEvent,
|
|
|
|
pushExternalChange: pushExternalChange,
|
|
|
|
clearStack: clearStack,
|
|
|
|
getNthFromTop: getNthFromTop
|
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
})();
|
|
|
|
|
|
|
|
// invariant: stack always has at least one undoable event
|
|
|
|
var undoPtr = 0; // zero-index from top of stack, 0 == top
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function clearHistory()
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
stack.clearStack();
|
|
|
|
undoPtr = 0;
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function _charOccurrences(str, c)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var i = 0;
|
|
|
|
var count = 0;
|
2011-07-07 19:59:34 +02:00
|
|
|
while (i >= 0 && i < str.length)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
i = str.indexOf(c, i);
|
2011-07-07 19:59:34 +02:00
|
|
|
if (i >= 0)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
i++;
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function _opcodeOccurrences(cs, opcode)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
return _charOccurrences(Changeset.unpack(cs).ops, opcode);
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function _mergeChangesets(cs1, cs2)
|
|
|
|
{
|
|
|
|
if (!cs1) return cs2;
|
|
|
|
if (!cs2) return cs1;
|
2011-03-26 14:10:41 +01:00
|
|
|
|
|
|
|
// Rough heuristic for whether changesets should be considered one action:
|
|
|
|
// each does exactly one insertion, no dels, and the composition does also; or
|
|
|
|
// each does exactly one deletion, no ins, and the composition does also.
|
|
|
|
// A little weird in that it won't merge "make bold" with "insert char"
|
|
|
|
// but will merge "make bold and insert char" with "insert char",
|
|
|
|
// though that isn't expected to come up.
|
|
|
|
var plusCount1 = _opcodeOccurrences(cs1, '+');
|
|
|
|
var plusCount2 = _opcodeOccurrences(cs2, '+');
|
|
|
|
var minusCount1 = _opcodeOccurrences(cs1, '-');
|
|
|
|
var minusCount2 = _opcodeOccurrences(cs2, '-');
|
2011-07-07 19:59:34 +02:00
|
|
|
if (plusCount1 == 1 && plusCount2 == 1 && minusCount1 == 0 && minusCount2 == 0)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var merge = Changeset.compose(cs1, cs2, getAPool());
|
|
|
|
var plusCount3 = _opcodeOccurrences(merge, '+');
|
|
|
|
var minusCount3 = _opcodeOccurrences(merge, '-');
|
2011-07-07 19:59:34 +02:00
|
|
|
if (plusCount3 == 1 && minusCount3 == 0)
|
|
|
|
{
|
|
|
|
return merge;
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
else if (plusCount1 == 0 && plusCount2 == 0 && minusCount1 == 1 && minusCount2 == 1)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var merge = Changeset.compose(cs1, cs2, getAPool());
|
|
|
|
var plusCount3 = _opcodeOccurrences(merge, '+');
|
|
|
|
var minusCount3 = _opcodeOccurrences(merge, '-');
|
2011-07-07 19:59:34 +02:00
|
|
|
if (plusCount3 == 0 && minusCount3 == 1)
|
|
|
|
{
|
|
|
|
return merge;
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function reportEvent(event)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var topEvent = stack.getNthFromTop(0);
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function applySelectionToTop()
|
|
|
|
{
|
|
|
|
if ((typeof event.selStart) == "number")
|
|
|
|
{
|
|
|
|
topEvent.selStart = event.selStart;
|
|
|
|
topEvent.selEnd = event.selEnd;
|
|
|
|
topEvent.selFocusAtStart = event.selFocusAtStart;
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
if ((!event.backset) || Changeset.isIdentity(event.backset))
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
applySelectionToTop();
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
else
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var merged = false;
|
2011-07-07 19:59:34 +02:00
|
|
|
if (topEvent.eventType == event.eventType)
|
|
|
|
{
|
|
|
|
var merge = _mergeChangesets(event.backset, topEvent.backset);
|
|
|
|
if (merge)
|
|
|
|
{
|
|
|
|
topEvent.backset = merge;
|
|
|
|
//dmesg("reportEvent merge: "+merge);
|
|
|
|
applySelectionToTop();
|
|
|
|
merged = true;
|
|
|
|
}
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
if (!merged)
|
|
|
|
{
|
|
|
|
stack.pushEvent(event);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
undoPtr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function reportExternalChange(changeset)
|
|
|
|
{
|
|
|
|
if (changeset && !Changeset.isIdentity(changeset))
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
stack.pushExternalChange(changeset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function _getSelectionInfo(event)
|
|
|
|
{
|
|
|
|
if ((typeof event.selStart) != "number")
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
return null;
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
selStart: event.selStart,
|
|
|
|
selEnd: event.selEnd,
|
|
|
|
selFocusAtStart: event.selFocusAtStart
|
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For "undo" and "redo", the change event must be returned
|
|
|
|
// by eventFunc and NOT reported through the normal mechanism.
|
|
|
|
// "eventFunc" should take a changeset and an optional selection info object,
|
|
|
|
// or can be called with no arguments to mean that no undo is possible.
|
|
|
|
// "eventFunc" will be called exactly once.
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function performUndo(eventFunc)
|
|
|
|
{
|
|
|
|
if (undoPtr < stack.numEvents() - 1)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var backsetEvent = stack.getNthFromTop(undoPtr);
|
2011-07-07 19:59:34 +02:00
|
|
|
var selectionEvent = stack.getNthFromTop(undoPtr + 1);
|
2011-03-26 14:10:41 +01:00
|
|
|
var undoEvent = eventFunc(backsetEvent.backset, _getSelectionInfo(selectionEvent));
|
|
|
|
stack.pushEvent(undoEvent);
|
|
|
|
undoPtr += 2;
|
|
|
|
}
|
|
|
|
else eventFunc();
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function performRedo(eventFunc)
|
|
|
|
{
|
|
|
|
if (undoPtr >= 2)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var backsetEvent = stack.getNthFromTop(0);
|
|
|
|
var selectionEvent = stack.getNthFromTop(1);
|
|
|
|
eventFunc(backsetEvent.backset, _getSelectionInfo(selectionEvent));
|
|
|
|
stack.popEvent();
|
|
|
|
undoPtr -= 2;
|
|
|
|
}
|
|
|
|
else eventFunc();
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function getAPool()
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
return undoModule.apool;
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
return {
|
|
|
|
clearHistory: clearHistory,
|
|
|
|
reportEvent: reportEvent,
|
|
|
|
reportExternalChange: reportExternalChange,
|
|
|
|
performUndo: performUndo,
|
|
|
|
performRedo: performRedo,
|
|
|
|
enabled: true,
|
|
|
|
apool: null
|
|
|
|
}; // apool is filled in by caller
|
|
|
|
})();
|
2012-01-16 02:23:48 +01:00
|
|
|
|
|
|
|
if (typeof exports !== 'undefined') {
|
|
|
|
exports.undoModule = undoModule;
|
|
|
|
}
|