Skip to content

Instantly share code, notes, and snippets.

@aldendaniels
Last active January 2, 2016 21:59
Show Gist options
  • Save aldendaniels/8367109 to your computer and use it in GitHub Desktop.
Save aldendaniels/8367109 to your computer and use it in GitHub Desktop.
Ace document.applyDelta function that splits 'remove' deltas.
this.applyDelta = function(delta) {
// Split large insert deltas. This is necessary because:
// 1. We need to support splicing delta lines into the document via $lines.splice.apply(...)
// 2. fn.apply() doesn't work for a large number of params. The mallest threshold is on safari 0xFFFF.
if (delta.lines.length > 65000)
{
// Get split deltas.
var deltas = [];
var fnPush = (delta.action == 'insert' ? Array.prototype.push : Array.prototype.unshift); // Fire delete actions in reverse.
while (delta.lines.length > 65000)
{
// Get split deltas.
var lines = delta.lines.splice(0, 65000);
lines.push('');
fnPush.apply(deltas, [{
action: delta.action,
lines: lines,
range: new Range(delta.range.start.row, delta.range.start.column,
delta.range.start.row + 65000, 0)
}]);
// Update remaining delta.
delta.range.start.row += 65000;
delta.range.start.column = 0;
}
fnPush.apply(deltas, [delta]);
// Apply split deltas.
for (var i in deltas)
{
applyDelta(this.$lines, deltas[i]);
this._emit("change", { data: deltas[i]});
}
}
else
{
applyDelta(this.$lines, delta);
this._emit("change", { data: delta });
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment