summaryrefslogtreecommitdiff
path: root/lib/ot/simple-text-operation.js
blob: 6db296e02446538ecb235ce573dd9639248e58a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
if (typeof ot === 'undefined') {
  // Export for browsers
  var ot = {};
}

ot.SimpleTextOperation = (function (global) {

  var TextOperation = global.ot ? global.ot.TextOperation : require('./text-operation');

  function SimpleTextOperation () {}


  // Insert the string `str` at the zero-based `position` in the document.
  function Insert (str, position) {
    if (!this || this.constructor !== SimpleTextOperation) {
      // => function was called without 'new'
      return new Insert(str, position);
    }
    this.str = str;
    this.position = position;
  }

  Insert.prototype = new SimpleTextOperation();
  SimpleTextOperation.Insert = Insert;

  Insert.prototype.toString = function () {
    return 'Insert(' + JSON.stringify(this.str) + ', ' + this.position + ')';
  };

  Insert.prototype.equals = function (other) {
    return other instanceof Insert &&
      this.str === other.str &&
      this.position === other.position;
  };

  Insert.prototype.apply = function (doc) {
    return doc.slice(0, this.position) + this.str + doc.slice(this.position);
  };


  // Delete `count` many characters at the zero-based `position` in the document.
  function Delete (count, position) {
    if (!this || this.constructor !== SimpleTextOperation) {
      return new Delete(count, position);
    }
    this.count = count;
    this.position = position;
  }

  Delete.prototype = new SimpleTextOperation();
  SimpleTextOperation.Delete = Delete;

  Delete.prototype.toString = function () {
    return 'Delete(' + this.count + ', ' + this.position + ')';
  };

  Delete.prototype.equals = function (other) {
    return other instanceof Delete &&
      this.count === other.count &&
      this.position === other.position;
  };

  Delete.prototype.apply = function (doc) {
    return doc.slice(0, this.position) + doc.slice(this.position + this.count);
  };


  // An operation that does nothing. This is needed for the result of the
  // transformation of two deletions of the same character.
  function Noop () {
    if (!this || this.constructor !== SimpleTextOperation) { return new Noop(); }
  }

  Noop.prototype = new SimpleTextOperation();
  SimpleTextOperation.Noop = Noop;

  Noop.prototype.toString = function () {
    return 'Noop()';
  };

  Noop.prototype.equals = function (other) { return other instanceof Noop; };

  Noop.prototype.apply = function (doc) { return doc; };

  var noop = new Noop();


  SimpleTextOperation.transform = function (a, b) {
    if (a instanceof Noop || b instanceof Noop) { return [a, b]; }

    if (a instanceof Insert && b instanceof Insert) {
      if (a.position < b.position || (a.position === b.position && a.str < b.str)) {
        return [a, new Insert(b.str, b.position + a.str.length)];
      }
      if (a.position > b.position || (a.position === b.position && a.str > b.str)) {
        return [new Insert(a.str, a.position + b.str.length), b];
      }
      return [noop, noop];
    }

    if (a instanceof Insert && b instanceof Delete) {
      if (a.position <= b.position) {
        return [a, new Delete(b.count, b.position + a.str.length)];
      }
      if (a.position >= b.position + b.count) {
        return [new Insert(a.str, a.position - b.count), b];
      }
      // Here, we have to delete the inserted string of operation a.
      // That doesn't preserve the intention of operation a, but it's the only
      // thing we can do to get a valid transform function.
      return [noop, new Delete(b.count + a.str.length, b.position)];
    }

    if (a instanceof Delete && b instanceof Insert) {
      if (a.position >= b.position) {
        return [new Delete(a.count, a.position + b.str.length), b];
      }
      if (a.position + a.count <= b.position) {
        return [a, new Insert(b.str, b.position - a.count)];
      }
      // Same problem as above. We have to delete the string that was inserted
      // in operation b.
      return [new Delete(a.count + b.str.length, a.position), noop];
    }

    if (a instanceof Delete && b instanceof Delete) {
      if (a.position === b.position) {
        if (a.count === b.count) {
          return [noop, noop];
        } else if (a.count < b.count) {
          return [noop, new Delete(b.count - a.count, b.position)];
        }
        return [new Delete(a.count - b.count, a.position), noop];
      }
      if (a.position < b.position) {
        if (a.position + a.count <= b.position) {
          return [a, new Delete(b.count, b.position - a.count)];
        }
        if (a.position + a.count >= b.position + b.count) {
          return [new Delete(a.count - b.count, a.position), noop];
        }
        return [
          new Delete(b.position - a.position, a.position),
          new Delete(b.position + b.count - (a.position + a.count), a.position)
        ];
      }
      if (a.position > b.position) {
        if (a.position >= b.position + b.count) {
          return [new Delete(a.count, a.position - b.count), b];
        }
        if (a.position + a.count <= b.position + b.count) {
          return [noop, new Delete(b.count - a.count, b.position)];
        }
        return [
          new Delete(a.position + a.count - (b.position + b.count), b.position),
          new Delete(a.position - b.position, b.position)
        ];
      }
    }
  };

  // Convert a normal, composable `TextOperation` into an array of
  // `SimpleTextOperation`s.
  SimpleTextOperation.fromTextOperation = function (operation) {
    var simpleOperations = [];
    var index = 0;
    for (var i = 0; i < operation.ops.length; i++) {
      var op = operation.ops[i];
      if (TextOperation.isRetain(op)) {
        index += op;
      } else if (TextOperation.isInsert(op)) {
        simpleOperations.push(new Insert(op, index));
        index += op.length;
      } else {
        simpleOperations.push(new Delete(Math.abs(op), index));
      }
    }
    return simpleOperations;
  };


  return SimpleTextOperation;
})(this);

// Export for CommonJS
if (typeof module === 'object') {
  module.exports = ot.SimpleTextOperation;
}