|
|
@ -51,19 +51,19 @@ define(function () {
|
|
|
|
* This algorithm is O(N). I suspect you could speed it up somehow using regular expressions.
|
|
|
|
* This algorithm is O(N). I suspect you could speed it up somehow using regular expressions.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
var applyChange = function(ctx, oldval, newval) {
|
|
|
|
var applyChange = function(ctx, oldval, newval) {
|
|
|
|
// Strings are immutable and have reference equality. I think this test is O(1), so its worth doing.
|
|
|
|
// Strings are immutable and have reference equality. I think this test is O(1), so its worth doing.
|
|
|
|
if (oldval === newval) return;
|
|
|
|
if (oldval === newval) return;
|
|
|
|
|
|
|
|
|
|
|
|
var commonStart = 0;
|
|
|
|
var commonStart = 0;
|
|
|
|
while (oldval.charAt(commonStart) === newval.charAt(commonStart)) {
|
|
|
|
while (oldval.charAt(commonStart) === newval.charAt(commonStart)) {
|
|
|
|
commonStart++;
|
|
|
|
commonStart++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var commonEnd = 0;
|
|
|
|
var commonEnd = 0;
|
|
|
|
while (oldval.charAt(oldval.length - 1 - commonEnd) === newval.charAt(newval.length - 1 - commonEnd) &&
|
|
|
|
while (oldval.charAt(oldval.length - 1 - commonEnd) === newval.charAt(newval.length - 1 - commonEnd) &&
|
|
|
|
commonEnd + commonStart < oldval.length && commonEnd + commonStart < newval.length) {
|
|
|
|
commonEnd + commonStart < oldval.length && commonEnd + commonStart < newval.length) {
|
|
|
|
commonEnd++;
|
|
|
|
commonEnd++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var bugz = {
|
|
|
|
var bugz = {
|
|
|
|
commonStart:commonStart,
|
|
|
|
commonStart:commonStart,
|
|
|
@ -71,14 +71,14 @@ var applyChange = function(ctx, oldval, newval) {
|
|
|
|
oldvalLength: oldval.length,
|
|
|
|
oldvalLength: oldval.length,
|
|
|
|
newvalLength: newval.length
|
|
|
|
newvalLength: newval.length
|
|
|
|
};
|
|
|
|
};
|
|
|
|
if (oldval.length !== commonStart + commonEnd) {
|
|
|
|
if (oldval.length !== commonStart + commonEnd) {
|
|
|
|
ctx.localChange && ctx.localChange(true);
|
|
|
|
if (ctx.localChange) { ctx.localChange(true); }
|
|
|
|
ctx.remove(commonStart, oldval.length - commonStart - commonEnd);
|
|
|
|
ctx.remove(commonStart, oldval.length - commonStart - commonEnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (newval.length !== commonStart + commonEnd) {
|
|
|
|
if (newval.length !== commonStart + commonEnd) {
|
|
|
|
ctx.localChange && ctx.localChange(true);
|
|
|
|
if (ctx.localChange) { ctx.localChange(true); }
|
|
|
|
ctx.insert(commonStart, newval.slice(commonStart, newval.length - commonEnd));
|
|
|
|
ctx.insert(commonStart, newval.slice(commonStart, newval.length - commonEnd));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -95,31 +95,32 @@ var cannonicalize = function (content) {
|
|
|
|
// specified.
|
|
|
|
// specified.
|
|
|
|
var attachTextarea = function(elem, ctx) {
|
|
|
|
var attachTextarea = function(elem, ctx) {
|
|
|
|
|
|
|
|
|
|
|
|
// initial state will always fail the !== check in genop.
|
|
|
|
// initial state will always fail the !== check in genop.
|
|
|
|
var content = {};
|
|
|
|
var content = {};
|
|
|
|
|
|
|
|
var newSelection;
|
|
|
|
// Replace the content of the text area with newText, and transform the
|
|
|
|
|
|
|
|
// current cursor by the specified function.
|
|
|
|
// Replace the content of the text area with newText, and transform the
|
|
|
|
var replaceText = function(newText, transformCursor) {
|
|
|
|
// current cursor by the specified function.
|
|
|
|
if (transformCursor) {
|
|
|
|
var replaceText = function(newText, transformCursor) {
|
|
|
|
var newSelection = [transformCursor(elem.selectionStart), transformCursor(elem.selectionEnd)];
|
|
|
|
if (transformCursor) {
|
|
|
|
}
|
|
|
|
newSelection = [transformCursor(elem.selectionStart), transformCursor(elem.selectionEnd)];
|
|
|
|
|
|
|
|
}
|
|
|
|
// Fixate the window's scroll while we set the element's value. Otherwise
|
|
|
|
|
|
|
|
// the browser scrolls to the element.
|
|
|
|
// Fixate the window's scroll while we set the element's value. Otherwise
|
|
|
|
var scrollTop = elem.scrollTop;
|
|
|
|
// the browser scrolls to the element.
|
|
|
|
elem.value = newText;
|
|
|
|
var scrollTop = elem.scrollTop;
|
|
|
|
content = elem.value; // Not done on one line so the browser can do newline conversion.
|
|
|
|
elem.value = newText;
|
|
|
|
if (elem.scrollTop !== scrollTop) elem.scrollTop = scrollTop;
|
|
|
|
content = elem.value; // Not done on one line so the browser can do newline conversion.
|
|
|
|
|
|
|
|
if (elem.scrollTop !== scrollTop) elem.scrollTop = scrollTop;
|
|
|
|
// Setting the selection moves the cursor. We'll just have to let your
|
|
|
|
|
|
|
|
// cursor drift if the element isn't active, though usually users don't
|
|
|
|
// Setting the selection moves the cursor. We'll just have to let your
|
|
|
|
// care.
|
|
|
|
// cursor drift if the element isn't active, though usually users don't
|
|
|
|
if (newSelection && window.document.activeElement === elem) {
|
|
|
|
// care.
|
|
|
|
elem.selectionStart = newSelection[0];
|
|
|
|
if (newSelection && window.document.activeElement === elem) {
|
|
|
|
elem.selectionEnd = newSelection[1];
|
|
|
|
elem.selectionStart = newSelection[0];
|
|
|
|
}
|
|
|
|
elem.selectionEnd = newSelection[1];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//replaceText(ctx.get());
|
|
|
|
//replaceText(ctx.get());
|
|
|
|
|
|
|
|
|
|
|
|