delta.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. var diff = require('fast-diff');
  2. var equal = require('deep-equal');
  3. var extend = require('extend');
  4. var op = require('./op');
  5. var NULL_CHARACTER = String.fromCharCode(0); // Placeholder char for embed in diff()
  6. var Delta = function (ops) {
  7. // Assume we are given a well formed ops
  8. if (Array.isArray(ops)) {
  9. this.ops = ops;
  10. } else if (ops != null && Array.isArray(ops.ops)) {
  11. this.ops = ops.ops;
  12. } else {
  13. this.ops = [];
  14. }
  15. };
  16. Delta.prototype.insert = function (text, attributes) {
  17. var newOp = {};
  18. if (text.length === 0) return this;
  19. newOp.insert = text;
  20. if (attributes != null && typeof attributes === 'object' && Object.keys(attributes).length > 0) {
  21. newOp.attributes = attributes;
  22. }
  23. return this.push(newOp);
  24. };
  25. Delta.prototype['delete'] = function (length) {
  26. if (length <= 0) return this;
  27. return this.push({ 'delete': length });
  28. };
  29. Delta.prototype.retain = function (length, attributes) {
  30. if (length <= 0) return this;
  31. var newOp = { retain: length };
  32. if (attributes != null && typeof attributes === 'object' && Object.keys(attributes).length > 0) {
  33. newOp.attributes = attributes;
  34. }
  35. return this.push(newOp);
  36. };
  37. Delta.prototype.push = function (newOp) {
  38. var index = this.ops.length;
  39. var lastOp = this.ops[index - 1];
  40. newOp = extend(true, {}, newOp);
  41. if (typeof lastOp === 'object') {
  42. if (typeof newOp['delete'] === 'number' && typeof lastOp['delete'] === 'number') {
  43. this.ops[index - 1] = { 'delete': lastOp['delete'] + newOp['delete'] };
  44. return this;
  45. }
  46. // Since it does not matter if we insert before or after deleting at the same index,
  47. // always prefer to insert first
  48. if (typeof lastOp['delete'] === 'number' && newOp.insert != null) {
  49. index -= 1;
  50. lastOp = this.ops[index - 1];
  51. if (typeof lastOp !== 'object') {
  52. this.ops.unshift(newOp);
  53. return this;
  54. }
  55. }
  56. if (equal(newOp.attributes, lastOp.attributes)) {
  57. if (typeof newOp.insert === 'string' && typeof lastOp.insert === 'string') {
  58. this.ops[index - 1] = { insert: lastOp.insert + newOp.insert };
  59. if (typeof newOp.attributes === 'object') this.ops[index - 1].attributes = newOp.attributes
  60. return this;
  61. } else if (typeof newOp.retain === 'number' && typeof lastOp.retain === 'number') {
  62. this.ops[index - 1] = { retain: lastOp.retain + newOp.retain };
  63. if (typeof newOp.attributes === 'object') this.ops[index - 1].attributes = newOp.attributes
  64. return this;
  65. }
  66. }
  67. }
  68. if (index === this.ops.length) {
  69. this.ops.push(newOp);
  70. } else {
  71. this.ops.splice(index, 0, newOp);
  72. }
  73. return this;
  74. };
  75. Delta.prototype.chop = function () {
  76. var lastOp = this.ops[this.ops.length - 1];
  77. if (lastOp && lastOp.retain && !lastOp.attributes) {
  78. this.ops.pop();
  79. }
  80. return this;
  81. };
  82. Delta.prototype.filter = function (predicate) {
  83. return this.ops.filter(predicate);
  84. };
  85. Delta.prototype.forEach = function (predicate) {
  86. this.ops.forEach(predicate);
  87. };
  88. Delta.prototype.map = function (predicate) {
  89. return this.ops.map(predicate);
  90. };
  91. Delta.prototype.partition = function (predicate) {
  92. var passed = [], failed = [];
  93. this.forEach(function(op) {
  94. var target = predicate(op) ? passed : failed;
  95. target.push(op);
  96. });
  97. return [passed, failed];
  98. };
  99. Delta.prototype.reduce = function (predicate, initial) {
  100. return this.ops.reduce(predicate, initial);
  101. };
  102. Delta.prototype.changeLength = function () {
  103. return this.reduce(function (length, elem) {
  104. if (elem.insert) {
  105. return length + op.length(elem);
  106. } else if (elem.delete) {
  107. return length - elem.delete;
  108. }
  109. return length;
  110. }, 0);
  111. };
  112. Delta.prototype.length = function () {
  113. return this.reduce(function (length, elem) {
  114. return length + op.length(elem);
  115. }, 0);
  116. };
  117. Delta.prototype.slice = function (start, end) {
  118. start = start || 0;
  119. if (typeof end !== 'number') end = Infinity;
  120. var ops = [];
  121. var iter = op.iterator(this.ops);
  122. var index = 0;
  123. while (index < end && iter.hasNext()) {
  124. var nextOp;
  125. if (index < start) {
  126. nextOp = iter.next(start - index);
  127. } else {
  128. nextOp = iter.next(end - index);
  129. ops.push(nextOp);
  130. }
  131. index += op.length(nextOp);
  132. }
  133. return new Delta(ops);
  134. };
  135. Delta.prototype.compose = function (other) {
  136. var thisIter = op.iterator(this.ops);
  137. var otherIter = op.iterator(other.ops);
  138. var ops = [];
  139. var firstOther = otherIter.peek();
  140. if (firstOther != null && typeof firstOther.retain === 'number' && firstOther.attributes == null) {
  141. var firstLeft = firstOther.retain;
  142. while (thisIter.peekType() === 'insert' && thisIter.peekLength() <= firstLeft) {
  143. firstLeft -= thisIter.peekLength();
  144. ops.push(thisIter.next());
  145. }
  146. if (firstOther.retain - firstLeft > 0) {
  147. otherIter.next(firstOther.retain - firstLeft);
  148. }
  149. }
  150. var delta = new Delta(ops);
  151. while (thisIter.hasNext() || otherIter.hasNext()) {
  152. if (otherIter.peekType() === 'insert') {
  153. delta.push(otherIter.next());
  154. } else if (thisIter.peekType() === 'delete') {
  155. delta.push(thisIter.next());
  156. } else {
  157. var length = Math.min(thisIter.peekLength(), otherIter.peekLength());
  158. var thisOp = thisIter.next(length);
  159. var otherOp = otherIter.next(length);
  160. if (typeof otherOp.retain === 'number') {
  161. var newOp = {};
  162. if (typeof thisOp.retain === 'number') {
  163. newOp.retain = length;
  164. } else {
  165. newOp.insert = thisOp.insert;
  166. }
  167. // Preserve null when composing with a retain, otherwise remove it for inserts
  168. var attributes = op.attributes.compose(thisOp.attributes, otherOp.attributes, typeof thisOp.retain === 'number');
  169. if (attributes) newOp.attributes = attributes;
  170. delta.push(newOp);
  171. // Optimization if rest of other is just retain
  172. if (!otherIter.hasNext() && equal(delta.ops[delta.ops.length - 1], newOp)) {
  173. var rest = new Delta(thisIter.rest());
  174. return delta.concat(rest).chop();
  175. }
  176. // Other op should be delete, we could be an insert or retain
  177. // Insert + delete cancels out
  178. } else if (typeof otherOp['delete'] === 'number' && typeof thisOp.retain === 'number') {
  179. delta.push(otherOp);
  180. }
  181. }
  182. }
  183. return delta.chop();
  184. };
  185. Delta.prototype.concat = function (other) {
  186. var delta = new Delta(this.ops.slice());
  187. if (other.ops.length > 0) {
  188. delta.push(other.ops[0]);
  189. delta.ops = delta.ops.concat(other.ops.slice(1));
  190. }
  191. return delta;
  192. };
  193. Delta.prototype.diff = function (other, index) {
  194. if (this.ops === other.ops) {
  195. return new Delta();
  196. }
  197. var strings = [this, other].map(function (delta) {
  198. return delta.map(function (op) {
  199. if (op.insert != null) {
  200. return typeof op.insert === 'string' ? op.insert : NULL_CHARACTER;
  201. }
  202. var prep = (delta === other) ? 'on' : 'with';
  203. throw new Error('diff() called ' + prep + ' non-document');
  204. }).join('');
  205. });
  206. var delta = new Delta();
  207. var diffResult = diff(strings[0], strings[1], index);
  208. var thisIter = op.iterator(this.ops);
  209. var otherIter = op.iterator(other.ops);
  210. diffResult.forEach(function (component) {
  211. var length = component[1].length;
  212. while (length > 0) {
  213. var opLength = 0;
  214. switch (component[0]) {
  215. case diff.INSERT:
  216. opLength = Math.min(otherIter.peekLength(), length);
  217. delta.push(otherIter.next(opLength));
  218. break;
  219. case diff.DELETE:
  220. opLength = Math.min(length, thisIter.peekLength());
  221. thisIter.next(opLength);
  222. delta['delete'](opLength);
  223. break;
  224. case diff.EQUAL:
  225. opLength = Math.min(thisIter.peekLength(), otherIter.peekLength(), length);
  226. var thisOp = thisIter.next(opLength);
  227. var otherOp = otherIter.next(opLength);
  228. if (equal(thisOp.insert, otherOp.insert)) {
  229. delta.retain(opLength, op.attributes.diff(thisOp.attributes, otherOp.attributes));
  230. } else {
  231. delta.push(otherOp)['delete'](opLength);
  232. }
  233. break;
  234. }
  235. length -= opLength;
  236. }
  237. });
  238. return delta.chop();
  239. };
  240. Delta.prototype.eachLine = function (predicate, newline) {
  241. newline = newline || '\n';
  242. var iter = op.iterator(this.ops);
  243. var line = new Delta();
  244. var i = 0;
  245. while (iter.hasNext()) {
  246. if (iter.peekType() !== 'insert') return;
  247. var thisOp = iter.peek();
  248. var start = op.length(thisOp) - iter.peekLength();
  249. var index = typeof thisOp.insert === 'string' ?
  250. thisOp.insert.indexOf(newline, start) - start : -1;
  251. if (index < 0) {
  252. line.push(iter.next());
  253. } else if (index > 0) {
  254. line.push(iter.next(index));
  255. } else {
  256. if (predicate(line, iter.next(1).attributes || {}, i) === false) {
  257. return;
  258. }
  259. i += 1;
  260. line = new Delta();
  261. }
  262. }
  263. if (line.length() > 0) {
  264. predicate(line, {}, i);
  265. }
  266. };
  267. Delta.prototype.transform = function (other, priority) {
  268. priority = !!priority;
  269. if (typeof other === 'number') {
  270. return this.transformPosition(other, priority);
  271. }
  272. var thisIter = op.iterator(this.ops);
  273. var otherIter = op.iterator(other.ops);
  274. var delta = new Delta();
  275. while (thisIter.hasNext() || otherIter.hasNext()) {
  276. if (thisIter.peekType() === 'insert' && (priority || otherIter.peekType() !== 'insert')) {
  277. delta.retain(op.length(thisIter.next()));
  278. } else if (otherIter.peekType() === 'insert') {
  279. delta.push(otherIter.next());
  280. } else {
  281. var length = Math.min(thisIter.peekLength(), otherIter.peekLength());
  282. var thisOp = thisIter.next(length);
  283. var otherOp = otherIter.next(length);
  284. if (thisOp['delete']) {
  285. // Our delete either makes their delete redundant or removes their retain
  286. continue;
  287. } else if (otherOp['delete']) {
  288. delta.push(otherOp);
  289. } else {
  290. // We retain either their retain or insert
  291. delta.retain(length, op.attributes.transform(thisOp.attributes, otherOp.attributes, priority));
  292. }
  293. }
  294. }
  295. return delta.chop();
  296. };
  297. Delta.prototype.transformPosition = function (index, priority) {
  298. priority = !!priority;
  299. var thisIter = op.iterator(this.ops);
  300. var offset = 0;
  301. while (thisIter.hasNext() && offset <= index) {
  302. var length = thisIter.peekLength();
  303. var nextType = thisIter.peekType();
  304. thisIter.next();
  305. if (nextType === 'delete') {
  306. index -= Math.min(length, index - offset);
  307. continue;
  308. } else if (nextType === 'insert' && (offset < index || !priority)) {
  309. index += length;
  310. }
  311. offset += length;
  312. }
  313. return index;
  314. };
  315. module.exports = Delta;