index.cjs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. 'use strict';
  2. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
  3. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  4. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  5. function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
  6. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
  7. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  8. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  9. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  10. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  11. function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
  12. Object.defineProperty(exports, '__esModule', {
  13. value: true
  14. });
  15. function tokens(frag, start, end, target) {
  16. for (var i = 0, off = 0; i < frag.childCount; i++) {
  17. var child = frag.child(i),
  18. endOff = off + child.nodeSize;
  19. var from = Math.max(off, start),
  20. to = Math.min(endOff, end);
  21. if (from < to) {
  22. if (child.isText) {
  23. for (var j = from; j < to; j++) {
  24. target.push(child.text.charCodeAt(j - off));
  25. }
  26. } else if (child.isLeaf) {
  27. target.push(child.type.name);
  28. } else {
  29. if (from == off) target.push(child.type.name);
  30. tokens(child.content, Math.max(off + 1, from) - off - 1, Math.min(endOff - 1, to) - off - 1, target);
  31. if (to == endOff) target.push(-1);
  32. }
  33. }
  34. off = endOff;
  35. }
  36. return target;
  37. }
  38. var MAX_DIFF_SIZE = 5000;
  39. function minUnchanged(sizeA, sizeB) {
  40. return Math.min(15, Math.max(2, Math.floor(Math.max(sizeA, sizeB) / 10)));
  41. }
  42. function computeDiff(fragA, fragB, range) {
  43. var tokA = tokens(fragA, range.fromA, range.toA, []);
  44. var tokB = tokens(fragB, range.fromB, range.toB, []);
  45. var start = 0,
  46. endA = tokA.length,
  47. endB = tokB.length;
  48. while (start < tokA.length && start < tokB.length && tokA[start] === tokB[start]) {
  49. start++;
  50. }
  51. if (start == tokA.length && start == tokB.length) return [];
  52. while (endA > start && endB > start && tokA[endA - 1] === tokB[endB - 1]) {
  53. endA--, endB--;
  54. }
  55. if (endA == start || endB == start || endA == endB && endA == start + 1) return [range.slice(start, endA, start, endB)];
  56. var lenA = endA - start,
  57. lenB = endB - start;
  58. var max = Math.min(MAX_DIFF_SIZE, lenA + lenB),
  59. off = max + 1;
  60. var history = [];
  61. var frontier = [];
  62. for (var len = off * 2, i = 0; i < len; i++) {
  63. frontier[i] = -1;
  64. }
  65. for (var size = 0; size <= max; size++) {
  66. for (var diag = -size; diag <= size; diag += 2) {
  67. var next = frontier[diag + 1 + max],
  68. prev = frontier[diag - 1 + max];
  69. var x = next < prev ? prev : next + 1,
  70. y = x + diag;
  71. while (x < lenA && y < lenB && tokA[start + x] === tokB[start + y]) {
  72. x++, y++;
  73. }
  74. frontier[diag + max] = x;
  75. if (x >= lenA && y >= lenB) {
  76. var _ret = function () {
  77. var diff = [],
  78. minSpan = minUnchanged(endA - start, endB - start);
  79. var fromA = -1,
  80. toA = -1,
  81. fromB = -1,
  82. toB = -1;
  83. var add = function add(fA, tA, fB, tB) {
  84. if (fromA > -1 && fromA < tA + minSpan) {
  85. fromA = fA;
  86. fromB = fB;
  87. } else {
  88. if (fromA > -1) diff.push(range.slice(fromA, toA, fromB, toB));
  89. fromA = fA;
  90. toA = tA;
  91. fromB = fB;
  92. toB = tB;
  93. }
  94. };
  95. for (var _i = size - 1; _i >= 0; _i--) {
  96. var _next = frontier[diag + 1 + max],
  97. _prev = frontier[diag - 1 + max];
  98. if (_next < _prev) {
  99. diag--;
  100. x = _prev + start;
  101. y = x + diag;
  102. add(x, x, y, y + 1);
  103. } else {
  104. diag++;
  105. x = _next + start;
  106. y = x + diag;
  107. add(x, x + 1, y, y);
  108. }
  109. frontier = history[_i >> 1];
  110. }
  111. if (fromA > -1) diff.push(range.slice(fromA, toA, fromB, toB));
  112. return {
  113. v: diff.reverse()
  114. };
  115. }();
  116. if (_typeof(_ret) === "object") return _ret.v;
  117. }
  118. }
  119. if (size % 2 == 0) history.push(frontier.slice());
  120. }
  121. return [range.slice(start, endA, start, endB)];
  122. }
  123. var Span = function () {
  124. function Span(length, data) {
  125. _classCallCheck(this, Span);
  126. this.length = length;
  127. this.data = data;
  128. }
  129. _createClass(Span, [{
  130. key: "cut",
  131. value: function cut(length) {
  132. return length == this.length ? this : new Span(length, this.data);
  133. }
  134. }], [{
  135. key: "slice",
  136. value: function slice(spans, from, to) {
  137. if (from == to) return Span.none;
  138. if (from == 0 && to == Span.len(spans)) return spans;
  139. var result = [];
  140. for (var i = 0, off = 0; off < to; i++) {
  141. var span = spans[i],
  142. end = off + span.length;
  143. var overlap = Math.min(to, end) - Math.max(from, off);
  144. if (overlap > 0) result.push(span.cut(overlap));
  145. off = end;
  146. }
  147. return result;
  148. }
  149. }, {
  150. key: "join",
  151. value: function join(a, b, combine) {
  152. if (a.length == 0) return b;
  153. if (b.length == 0) return a;
  154. var combined = combine(a[a.length - 1].data, b[0].data);
  155. if (combined == null) return a.concat(b);
  156. var result = a.slice(0, a.length - 1);
  157. result.push(new Span(a[a.length - 1].length + b[0].length, combined));
  158. for (var i = 1; i < b.length; i++) {
  159. result.push(b[i]);
  160. }
  161. return result;
  162. }
  163. }, {
  164. key: "len",
  165. value: function len(spans) {
  166. var len = 0;
  167. for (var i = 0; i < spans.length; i++) {
  168. len += spans[i].length;
  169. }
  170. return len;
  171. }
  172. }]);
  173. return Span;
  174. }();
  175. Span.none = [];
  176. var Change = function () {
  177. function Change(fromA, toA, fromB, toB, deleted, inserted) {
  178. _classCallCheck(this, Change);
  179. this.fromA = fromA;
  180. this.toA = toA;
  181. this.fromB = fromB;
  182. this.toB = toB;
  183. this.deleted = deleted;
  184. this.inserted = inserted;
  185. }
  186. _createClass(Change, [{
  187. key: "lenA",
  188. get: function get() {
  189. return this.toA - this.fromA;
  190. }
  191. }, {
  192. key: "lenB",
  193. get: function get() {
  194. return this.toB - this.fromB;
  195. }
  196. }, {
  197. key: "slice",
  198. value: function slice(startA, endA, startB, endB) {
  199. if (startA == 0 && startB == 0 && endA == this.toA - this.fromA && endB == this.toB - this.fromB) return this;
  200. return new Change(this.fromA + startA, this.fromA + endA, this.fromB + startB, this.fromB + endB, Span.slice(this.deleted, startA, endA), Span.slice(this.inserted, startB, endB));
  201. }
  202. }], [{
  203. key: "merge",
  204. value: function merge(x, y, combine) {
  205. if (x.length == 0) return y;
  206. if (y.length == 0) return x;
  207. var result = [];
  208. for (var iX = 0, iY = 0, curX = x[0], curY = y[0];;) {
  209. if (!curX && !curY) {
  210. return result;
  211. } else if (curX && (!curY || curX.toB < curY.fromA)) {
  212. var off = iY ? y[iY - 1].toB - y[iY - 1].toA : 0;
  213. result.push(off == 0 ? curX : new Change(curX.fromA, curX.toA, curX.fromB + off, curX.toB + off, curX.deleted, curX.inserted));
  214. curX = iX++ == x.length ? null : x[iX];
  215. } else if (curY && (!curX || curY.toA < curX.fromB)) {
  216. var _off = iX ? x[iX - 1].toB - x[iX - 1].toA : 0;
  217. result.push(_off == 0 ? curY : new Change(curY.fromA - _off, curY.toA - _off, curY.fromB, curY.toB, curY.deleted, curY.inserted));
  218. curY = iY++ == y.length ? null : y[iY];
  219. } else {
  220. var pos = Math.min(curX.fromB, curY.fromA);
  221. var fromA = Math.min(curX.fromA, curY.fromA - (iX ? x[iX - 1].toB - x[iX - 1].toA : 0)),
  222. toA = fromA;
  223. var fromB = Math.min(curY.fromB, curX.fromB + (iY ? y[iY - 1].toB - y[iY - 1].toA : 0)),
  224. toB = fromB;
  225. var deleted = Span.none,
  226. inserted = Span.none;
  227. var enteredX = false,
  228. enteredY = false;
  229. for (;;) {
  230. var nextX = !curX ? 2e8 : pos >= curX.fromB ? curX.toB : curX.fromB;
  231. var nextY = !curY ? 2e8 : pos >= curY.fromA ? curY.toA : curY.fromA;
  232. var next = Math.min(nextX, nextY);
  233. var inX = curX && pos >= curX.fromB,
  234. inY = curY && pos >= curY.fromA;
  235. if (!inX && !inY) break;
  236. if (inX && pos == curX.fromB && !enteredX) {
  237. deleted = Span.join(deleted, curX.deleted, combine);
  238. toA += curX.lenA;
  239. enteredX = true;
  240. }
  241. if (inX && !inY) {
  242. inserted = Span.join(inserted, Span.slice(curX.inserted, pos - curX.fromB, next - curX.fromB), combine);
  243. toB += next - pos;
  244. }
  245. if (inY && pos == curY.fromA && !enteredY) {
  246. inserted = Span.join(inserted, curY.inserted, combine);
  247. toB += curY.lenB;
  248. enteredY = true;
  249. }
  250. if (inY && !inX) {
  251. deleted = Span.join(deleted, Span.slice(curY.deleted, pos - curY.fromA, next - curY.fromA), combine);
  252. toA += next - pos;
  253. }
  254. if (inX && next == curX.toB) {
  255. curX = iX++ == x.length ? null : x[iX];
  256. enteredX = false;
  257. }
  258. if (inY && next == curY.toA) {
  259. curY = iY++ == y.length ? null : y[iY];
  260. enteredY = false;
  261. }
  262. pos = next;
  263. }
  264. if (fromA < toA || fromB < toB) result.push(new Change(fromA, toA, fromB, toB, deleted, inserted));
  265. }
  266. }
  267. }
  268. }]);
  269. return Change;
  270. }();
  271. var letter;
  272. try {
  273. letter = new RegExp("[\\p{Alphabetic}_]", "u");
  274. } catch (_) {}
  275. var nonASCIISingleCaseWordChar = /[\u00df\u0587\u0590-\u05f4\u0600-\u06ff\u3040-\u309f\u30a0-\u30ff\u3400-\u4db5\u4e00-\u9fcc\uac00-\ud7af]/;
  276. function isLetter(code) {
  277. if (code < 128) return code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 79 && code <= 122;
  278. var ch = String.fromCharCode(code);
  279. if (letter) return letter.test(ch);
  280. return ch.toUpperCase() != ch.toLowerCase() || nonASCIISingleCaseWordChar.test(ch);
  281. }
  282. function getText(frag, start, end) {
  283. var out = "";
  284. function convert(frag, start, end) {
  285. for (var i = 0, off = 0; i < frag.childCount; i++) {
  286. var child = frag.child(i),
  287. endOff = off + child.nodeSize;
  288. var from = Math.max(off, start),
  289. to = Math.min(endOff, end);
  290. if (from < to) {
  291. if (child.isText) {
  292. out += child.text.slice(Math.max(0, start - off), Math.min(child.text.length, end - off));
  293. } else if (child.isLeaf) {
  294. out += " ";
  295. } else {
  296. if (from == off) out += " ";
  297. convert(child.content, Math.max(0, from - off - 1), Math.min(child.content.size, end - off));
  298. if (to == endOff) out += " ";
  299. }
  300. }
  301. off = endOff;
  302. }
  303. }
  304. convert(frag, start, end);
  305. return out;
  306. }
  307. var MAX_SIMPLIFY_DISTANCE = 30;
  308. function simplifyChanges(changes, doc) {
  309. var result = [];
  310. for (var i = 0; i < changes.length; i++) {
  311. var end = changes[i].toB,
  312. start = i;
  313. while (i < changes.length - 1 && changes[i + 1].fromB <= end + MAX_SIMPLIFY_DISTANCE) {
  314. end = changes[++i].toB;
  315. }
  316. simplifyAdjacentChanges(changes, start, i + 1, doc, result);
  317. }
  318. return result;
  319. }
  320. function simplifyAdjacentChanges(changes, from, to, doc, target) {
  321. var start = Math.max(0, changes[from].fromB - MAX_SIMPLIFY_DISTANCE);
  322. var end = Math.min(doc.content.size, changes[to - 1].toB + MAX_SIMPLIFY_DISTANCE);
  323. var text = getText(doc.content, start, end);
  324. for (var i = from; i < to; i++) {
  325. var startI = i,
  326. last = changes[i],
  327. deleted = last.lenA,
  328. inserted = last.lenB;
  329. while (i < to - 1) {
  330. var next = changes[i + 1],
  331. boundary = false;
  332. var prevLetter = last.toB == end ? false : isLetter(text.charCodeAt(last.toB - 1 - start));
  333. for (var pos = last.toB; !boundary && pos < next.fromB; pos++) {
  334. var nextLetter = pos == end ? false : isLetter(text.charCodeAt(pos - start));
  335. if ((!prevLetter || !nextLetter) && pos != changes[startI].fromB) boundary = true;
  336. prevLetter = nextLetter;
  337. }
  338. if (boundary) break;
  339. deleted += next.lenA;
  340. inserted += next.lenB;
  341. last = next;
  342. i++;
  343. }
  344. if (inserted > 0 && deleted > 0 && !(inserted == 1 && deleted == 1)) {
  345. var _from = changes[startI].fromB,
  346. _to = changes[i].toB;
  347. if (_from < end && isLetter(text.charCodeAt(_from - start))) while (_from > start && isLetter(text.charCodeAt(_from - 1 - start))) {
  348. _from--;
  349. }
  350. if (_to > start && isLetter(text.charCodeAt(_to - 1 - start))) while (_to < end && isLetter(text.charCodeAt(_to - start))) {
  351. _to++;
  352. }
  353. var joined = fillChange(changes.slice(startI, i + 1), _from, _to);
  354. var _last = target.length ? target[target.length - 1] : null;
  355. if (_last && _last.toA == joined.fromA) target[target.length - 1] = new Change(_last.fromA, joined.toA, _last.fromB, joined.toB, _last.deleted.concat(joined.deleted), _last.inserted.concat(joined.inserted));else target.push(joined);
  356. } else {
  357. for (var j = startI; j <= i; j++) {
  358. target.push(changes[j]);
  359. }
  360. }
  361. }
  362. return changes;
  363. }
  364. function combine(a, b) {
  365. return a === b ? a : null;
  366. }
  367. function fillChange(changes, fromB, toB) {
  368. var fromA = changes[0].fromA - (changes[0].fromB - fromB);
  369. var last = changes[changes.length - 1];
  370. var toA = last.toA + (toB - last.toB);
  371. var deleted = Span.none,
  372. inserted = Span.none;
  373. var delData = (changes[0].deleted.length ? changes[0].deleted : changes[0].inserted)[0].data;
  374. var insData = (changes[0].inserted.length ? changes[0].inserted : changes[0].deleted)[0].data;
  375. for (var posA = fromA, posB = fromB, i = 0;; i++) {
  376. var next = i == changes.length ? null : changes[i];
  377. var endA = next ? next.fromA : toA,
  378. endB = next ? next.fromB : toB;
  379. if (endA > posA) deleted = Span.join(deleted, [new Span(endA - posA, delData)], combine);
  380. if (endB > posB) inserted = Span.join(inserted, [new Span(endB - posB, insData)], combine);
  381. if (!next) break;
  382. deleted = Span.join(deleted, next.deleted, combine);
  383. inserted = Span.join(inserted, next.inserted, combine);
  384. if (deleted.length) delData = deleted[deleted.length - 1].data;
  385. if (inserted.length) insData = inserted[inserted.length - 1].data;
  386. posA = next.toA;
  387. posB = next.toB;
  388. }
  389. return new Change(fromA, toA, fromB, toB, deleted, inserted);
  390. }
  391. var ChangeSet = function () {
  392. function ChangeSet(config, changes) {
  393. _classCallCheck(this, ChangeSet);
  394. this.config = config;
  395. this.changes = changes;
  396. }
  397. _createClass(ChangeSet, [{
  398. key: "addSteps",
  399. value: function addSteps(newDoc, maps, data) {
  400. var _this = this;
  401. var stepChanges = [];
  402. var _loop = function _loop(i) {
  403. var d = Array.isArray(data) ? data[i] : data;
  404. var off = 0;
  405. maps[i].forEach(function (fromA, toA, fromB, toB) {
  406. stepChanges.push(new Change(fromA + off, toA + off, fromB, toB, fromA == toA ? Span.none : [new Span(toA - fromA, d)], fromB == toB ? Span.none : [new Span(toB - fromB, d)]));
  407. off = toB - fromB - (toA - fromA);
  408. });
  409. };
  410. for (var i = 0; i < maps.length; i++) {
  411. _loop(i);
  412. }
  413. if (stepChanges.length == 0) return this;
  414. var newChanges = mergeAll(stepChanges, this.config.combine);
  415. var changes = Change.merge(this.changes, newChanges, this.config.combine);
  416. var updated = changes;
  417. var _loop2 = function _loop2(_i3) {
  418. var change = updated[_i3];
  419. if (change.fromA == change.toA || change.fromB == change.toB || !newChanges.some(function (r) {
  420. return r.toB > change.fromB && r.fromB < change.toB;
  421. })) {
  422. _i2 = _i3;
  423. return "continue";
  424. }
  425. var diff = computeDiff(_this.config.doc.content, newDoc.content, change);
  426. if (diff.length == 1 && diff[0].fromB == 0 && diff[0].toB == change.toB - change.fromB) {
  427. _i2 = _i3;
  428. return "continue";
  429. }
  430. if (updated == changes) updated = changes.slice();
  431. if (diff.length == 1) {
  432. updated[_i3] = diff[0];
  433. } else {
  434. var _updated;
  435. (_updated = updated).splice.apply(_updated, [_i3, 1].concat(_toConsumableArray(diff)));
  436. _i3 += diff.length - 1;
  437. }
  438. _i2 = _i3;
  439. };
  440. for (var _i2 = 0; _i2 < updated.length; _i2++) {
  441. var _ret2 = _loop2(_i2);
  442. if (_ret2 === "continue") continue;
  443. }
  444. return new ChangeSet(this.config, updated);
  445. }
  446. }, {
  447. key: "startDoc",
  448. get: function get() {
  449. return this.config.doc;
  450. }
  451. }, {
  452. key: "map",
  453. value: function map(f) {
  454. var mapSpan = function mapSpan(span) {
  455. var newData = f(span);
  456. return newData === span.data ? span : new Span(span.length, newData);
  457. };
  458. return new ChangeSet(this.config, this.changes.map(function (ch) {
  459. return new Change(ch.fromA, ch.toA, ch.fromB, ch.toB, ch.deleted.map(mapSpan), ch.inserted.map(mapSpan));
  460. }));
  461. }
  462. }, {
  463. key: "changedRange",
  464. value: function changedRange(b, maps) {
  465. if (b == this) return null;
  466. var touched = maps && touchedRange(maps);
  467. var moved = touched ? touched.toB - touched.fromB - (touched.toA - touched.fromA) : 0;
  468. function map(p) {
  469. return !touched || p <= touched.fromA ? p : p + moved;
  470. }
  471. var from = touched ? touched.fromB : 2e8,
  472. to = touched ? touched.toB : -2e8;
  473. function add(start) {
  474. var end = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : start;
  475. from = Math.min(start, from);
  476. to = Math.max(end, to);
  477. }
  478. var rA = this.changes,
  479. rB = b.changes;
  480. for (var iA = 0, iB = 0; iA < rA.length && iB < rB.length;) {
  481. var rangeA = rA[iA],
  482. rangeB = rB[iB];
  483. if (rangeA && rangeB && sameRanges(rangeA, rangeB, map)) {
  484. iA++;
  485. iB++;
  486. } else if (rangeB && (!rangeA || map(rangeA.fromB) >= rangeB.fromB)) {
  487. add(rangeB.fromB, rangeB.toB);
  488. iB++;
  489. } else {
  490. add(map(rangeA.fromB), map(rangeA.toB));
  491. iA++;
  492. }
  493. }
  494. return from <= to ? {
  495. from: from,
  496. to: to
  497. } : null;
  498. }
  499. }], [{
  500. key: "create",
  501. value: function create(doc) {
  502. var combine = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function (a, b) {
  503. return a === b ? a : null;
  504. };
  505. return new ChangeSet({
  506. combine: combine,
  507. doc: doc
  508. }, []);
  509. }
  510. }]);
  511. return ChangeSet;
  512. }();
  513. ChangeSet.computeDiff = computeDiff;
  514. function mergeAll(ranges, combine) {
  515. var start = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  516. var end = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ranges.length;
  517. if (end == start + 1) return [ranges[start]];
  518. var mid = start + end >> 1;
  519. return Change.merge(mergeAll(ranges, combine, start, mid), mergeAll(ranges, combine, mid, end), combine);
  520. }
  521. function endRange(maps) {
  522. var from = 2e8,
  523. to = -2e8;
  524. for (var i = 0; i < maps.length; i++) {
  525. var map = maps[i];
  526. if (from != 2e8) {
  527. from = map.map(from, -1);
  528. to = map.map(to, 1);
  529. }
  530. map.forEach(function (_s, _e, start, end) {
  531. from = Math.min(from, start);
  532. to = Math.max(to, end);
  533. });
  534. }
  535. return from == 2e8 ? null : {
  536. from: from,
  537. to: to
  538. };
  539. }
  540. function touchedRange(maps) {
  541. var b = endRange(maps);
  542. if (!b) return null;
  543. var a = endRange(maps.map(function (m) {
  544. return m.invert();
  545. }).reverse());
  546. return {
  547. fromA: a.from,
  548. toA: a.to,
  549. fromB: b.from,
  550. toB: b.to
  551. };
  552. }
  553. function sameRanges(a, b, map) {
  554. return map(a.fromB) == b.fromB && map(a.toB) == b.toB && sameSpans(a.deleted, b.deleted) && sameSpans(a.inserted, b.inserted);
  555. }
  556. function sameSpans(a, b) {
  557. if (a.length != b.length) return false;
  558. for (var i = 0; i < a.length; i++) {
  559. if (a[i].length != b[i].length || a[i].data !== b[i].data) return false;
  560. }
  561. return true;
  562. }
  563. exports.Change = Change;
  564. exports.ChangeSet = ChangeSet;
  565. exports.Span = Span;
  566. exports.simplifyChanges = simplifyChanges;