From d3c2c901b609e3dc5b49048d800909eea06c5111 Mon Sep 17 00:00:00 2001 From: ansuz Date: Sun, 17 Apr 2016 14:02:45 +0200 Subject: [PATCH] implement cursor transformation in a common library --- www/common/TextPatcher.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/www/common/TextPatcher.js b/www/common/TextPatcher.js index c554e7363..c5720a53d 100644 --- a/www/common/TextPatcher.js +++ b/www/common/TextPatcher.js @@ -85,6 +85,20 @@ var applyChange = function(ctx, oldval, newval, logging) { patch(ctx, op); }; +var transformCursor = function (cursor, op) { + var pos = op.offset; + var remove = op.toRemove; + var insert = op.toInsert.length; + if (typeof cursor === 'undefined') { return; } + if (typeof remove === 'number' && pos < cursor) { + cursor -= Math.min(remove, cursor - pos); + } + if (typeof insert === 'number' && pos < cursor) { + cursor += insert; + } + return cursor; +}; + var create = function(config) { var ctx = config.realtime; var logging = config.logging; @@ -116,6 +130,7 @@ return { diff: diff, // diff two strings patch: patch, // apply an operation to a chainpad's realtime facade log: log, // print the components of an operation + transformCursor: transformCursor, // transform the position of a cursor applyChange: applyChange // a convenient wrapper around diff/log/patch }; });