Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

405 Zeilen
16 KiB

  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. var fast_diff_1 = __importDefault(require("fast-diff"));
  6. var lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
  7. var lodash_isequal_1 = __importDefault(require("lodash.isequal"));
  8. var AttributeMap_1 = __importDefault(require("./AttributeMap"));
  9. var Op_1 = __importDefault(require("./Op"));
  10. var NULL_CHARACTER = String.fromCharCode(0); // Placeholder char for embed in diff()
  11. var Delta = /** @class */ (function () {
  12. function Delta(ops) {
  13. // Assume we are given a well formed ops
  14. if (Array.isArray(ops)) {
  15. this.ops = ops;
  16. }
  17. else if (ops != null && Array.isArray(ops.ops)) {
  18. this.ops = ops.ops;
  19. }
  20. else {
  21. this.ops = [];
  22. }
  23. }
  24. Delta.prototype.insert = function (arg, attributes) {
  25. var newOp = {};
  26. if (typeof arg === 'string' && arg.length === 0) {
  27. return this;
  28. }
  29. newOp.insert = arg;
  30. if (attributes != null &&
  31. typeof attributes === 'object' &&
  32. Object.keys(attributes).length > 0) {
  33. newOp.attributes = attributes;
  34. }
  35. return this.push(newOp);
  36. };
  37. Delta.prototype.delete = function (length) {
  38. if (length <= 0) {
  39. return this;
  40. }
  41. return this.push({ delete: length });
  42. };
  43. Delta.prototype.retain = function (length, attributes) {
  44. if (length <= 0) {
  45. return this;
  46. }
  47. var newOp = { retain: length };
  48. if (attributes != null &&
  49. typeof attributes === 'object' &&
  50. Object.keys(attributes).length > 0) {
  51. newOp.attributes = attributes;
  52. }
  53. return this.push(newOp);
  54. };
  55. Delta.prototype.push = function (newOp) {
  56. var index = this.ops.length;
  57. var lastOp = this.ops[index - 1];
  58. newOp = lodash_clonedeep_1.default(newOp);
  59. if (typeof lastOp === 'object') {
  60. if (typeof newOp.delete === 'number' &&
  61. typeof lastOp.delete === 'number') {
  62. this.ops[index - 1] = { delete: lastOp.delete + newOp.delete };
  63. return this;
  64. }
  65. // Since it does not matter if we insert before or after deleting at the same index,
  66. // always prefer to insert first
  67. if (typeof lastOp.delete === 'number' && newOp.insert != null) {
  68. index -= 1;
  69. lastOp = this.ops[index - 1];
  70. if (typeof lastOp !== 'object') {
  71. this.ops.unshift(newOp);
  72. return this;
  73. }
  74. }
  75. if (lodash_isequal_1.default(newOp.attributes, lastOp.attributes)) {
  76. if (typeof newOp.insert === 'string' &&
  77. typeof lastOp.insert === 'string') {
  78. this.ops[index - 1] = { insert: lastOp.insert + newOp.insert };
  79. if (typeof newOp.attributes === 'object') {
  80. this.ops[index - 1].attributes = newOp.attributes;
  81. }
  82. return this;
  83. }
  84. else if (typeof newOp.retain === 'number' &&
  85. typeof lastOp.retain === 'number') {
  86. this.ops[index - 1] = { retain: lastOp.retain + newOp.retain };
  87. if (typeof newOp.attributes === 'object') {
  88. this.ops[index - 1].attributes = newOp.attributes;
  89. }
  90. return this;
  91. }
  92. }
  93. }
  94. if (index === this.ops.length) {
  95. this.ops.push(newOp);
  96. }
  97. else {
  98. this.ops.splice(index, 0, newOp);
  99. }
  100. return this;
  101. };
  102. Delta.prototype.chop = function () {
  103. var lastOp = this.ops[this.ops.length - 1];
  104. if (lastOp && lastOp.retain && !lastOp.attributes) {
  105. this.ops.pop();
  106. }
  107. return this;
  108. };
  109. Delta.prototype.filter = function (predicate) {
  110. return this.ops.filter(predicate);
  111. };
  112. Delta.prototype.forEach = function (predicate) {
  113. this.ops.forEach(predicate);
  114. };
  115. Delta.prototype.map = function (predicate) {
  116. return this.ops.map(predicate);
  117. };
  118. Delta.prototype.partition = function (predicate) {
  119. var passed = [];
  120. var failed = [];
  121. this.forEach(function (op) {
  122. var target = predicate(op) ? passed : failed;
  123. target.push(op);
  124. });
  125. return [passed, failed];
  126. };
  127. Delta.prototype.reduce = function (predicate, initialValue) {
  128. return this.ops.reduce(predicate, initialValue);
  129. };
  130. Delta.prototype.changeLength = function () {
  131. return this.reduce(function (length, elem) {
  132. if (elem.insert) {
  133. return length + Op_1.default.length(elem);
  134. }
  135. else if (elem.delete) {
  136. return length - elem.delete;
  137. }
  138. return length;
  139. }, 0);
  140. };
  141. Delta.prototype.length = function () {
  142. return this.reduce(function (length, elem) {
  143. return length + Op_1.default.length(elem);
  144. }, 0);
  145. };
  146. Delta.prototype.slice = function (start, end) {
  147. if (start === void 0) { start = 0; }
  148. if (end === void 0) { end = Infinity; }
  149. var ops = [];
  150. var iter = Op_1.default.iterator(this.ops);
  151. var index = 0;
  152. while (index < end && iter.hasNext()) {
  153. var nextOp = void 0;
  154. if (index < start) {
  155. nextOp = iter.next(start - index);
  156. }
  157. else {
  158. nextOp = iter.next(end - index);
  159. ops.push(nextOp);
  160. }
  161. index += Op_1.default.length(nextOp);
  162. }
  163. return new Delta(ops);
  164. };
  165. Delta.prototype.compose = function (other) {
  166. var thisIter = Op_1.default.iterator(this.ops);
  167. var otherIter = Op_1.default.iterator(other.ops);
  168. var ops = [];
  169. var firstOther = otherIter.peek();
  170. if (firstOther != null &&
  171. typeof firstOther.retain === 'number' &&
  172. firstOther.attributes == null) {
  173. var firstLeft = firstOther.retain;
  174. while (thisIter.peekType() === 'insert' &&
  175. thisIter.peekLength() <= firstLeft) {
  176. firstLeft -= thisIter.peekLength();
  177. ops.push(thisIter.next());
  178. }
  179. if (firstOther.retain - firstLeft > 0) {
  180. otherIter.next(firstOther.retain - firstLeft);
  181. }
  182. }
  183. var delta = new Delta(ops);
  184. while (thisIter.hasNext() || otherIter.hasNext()) {
  185. if (otherIter.peekType() === 'insert') {
  186. delta.push(otherIter.next());
  187. }
  188. else if (thisIter.peekType() === 'delete') {
  189. delta.push(thisIter.next());
  190. }
  191. else {
  192. var length_1 = Math.min(thisIter.peekLength(), otherIter.peekLength());
  193. var thisOp = thisIter.next(length_1);
  194. var otherOp = otherIter.next(length_1);
  195. if (typeof otherOp.retain === 'number') {
  196. var newOp = {};
  197. if (typeof thisOp.retain === 'number') {
  198. newOp.retain = length_1;
  199. }
  200. else {
  201. newOp.insert = thisOp.insert;
  202. }
  203. // Preserve null when composing with a retain, otherwise remove it for inserts
  204. var attributes = AttributeMap_1.default.compose(thisOp.attributes, otherOp.attributes, typeof thisOp.retain === 'number');
  205. if (attributes) {
  206. newOp.attributes = attributes;
  207. }
  208. delta.push(newOp);
  209. // Optimization if rest of other is just retain
  210. if (!otherIter.hasNext() &&
  211. lodash_isequal_1.default(delta.ops[delta.ops.length - 1], newOp)) {
  212. var rest = new Delta(thisIter.rest());
  213. return delta.concat(rest).chop();
  214. }
  215. // Other op should be delete, we could be an insert or retain
  216. // Insert + delete cancels out
  217. }
  218. else if (typeof otherOp.delete === 'number' &&
  219. typeof thisOp.retain === 'number') {
  220. delta.push(otherOp);
  221. }
  222. }
  223. }
  224. return delta.chop();
  225. };
  226. Delta.prototype.concat = function (other) {
  227. var delta = new Delta(this.ops.slice());
  228. if (other.ops.length > 0) {
  229. delta.push(other.ops[0]);
  230. delta.ops = delta.ops.concat(other.ops.slice(1));
  231. }
  232. return delta;
  233. };
  234. Delta.prototype.diff = function (other, cursor) {
  235. if (this.ops === other.ops) {
  236. return new Delta();
  237. }
  238. var strings = [this, other].map(function (delta) {
  239. return delta
  240. .map(function (op) {
  241. if (op.insert != null) {
  242. return typeof op.insert === 'string' ? op.insert : NULL_CHARACTER;
  243. }
  244. var prep = delta === other ? 'on' : 'with';
  245. throw new Error('diff() called ' + prep + ' non-document');
  246. })
  247. .join('');
  248. });
  249. var retDelta = new Delta();
  250. var diffResult = fast_diff_1.default(strings[0], strings[1], cursor);
  251. var thisIter = Op_1.default.iterator(this.ops);
  252. var otherIter = Op_1.default.iterator(other.ops);
  253. diffResult.forEach(function (component) {
  254. var length = component[1].length;
  255. while (length > 0) {
  256. var opLength = 0;
  257. switch (component[0]) {
  258. case fast_diff_1.default.INSERT:
  259. opLength = Math.min(otherIter.peekLength(), length);
  260. retDelta.push(otherIter.next(opLength));
  261. break;
  262. case fast_diff_1.default.DELETE:
  263. opLength = Math.min(length, thisIter.peekLength());
  264. thisIter.next(opLength);
  265. retDelta.delete(opLength);
  266. break;
  267. case fast_diff_1.default.EQUAL:
  268. opLength = Math.min(thisIter.peekLength(), otherIter.peekLength(), length);
  269. var thisOp = thisIter.next(opLength);
  270. var otherOp = otherIter.next(opLength);
  271. if (lodash_isequal_1.default(thisOp.insert, otherOp.insert)) {
  272. retDelta.retain(opLength, AttributeMap_1.default.diff(thisOp.attributes, otherOp.attributes));
  273. }
  274. else {
  275. retDelta.push(otherOp).delete(opLength);
  276. }
  277. break;
  278. }
  279. length -= opLength;
  280. }
  281. });
  282. return retDelta.chop();
  283. };
  284. Delta.prototype.eachLine = function (predicate, newline) {
  285. if (newline === void 0) { newline = '\n'; }
  286. var iter = Op_1.default.iterator(this.ops);
  287. var line = new Delta();
  288. var i = 0;
  289. while (iter.hasNext()) {
  290. if (iter.peekType() !== 'insert') {
  291. return;
  292. }
  293. var thisOp = iter.peek();
  294. var start = Op_1.default.length(thisOp) - iter.peekLength();
  295. var index = typeof thisOp.insert === 'string'
  296. ? thisOp.insert.indexOf(newline, start) - start
  297. : -1;
  298. if (index < 0) {
  299. line.push(iter.next());
  300. }
  301. else if (index > 0) {
  302. line.push(iter.next(index));
  303. }
  304. else {
  305. if (predicate(line, iter.next(1).attributes || {}, i) === false) {
  306. return;
  307. }
  308. i += 1;
  309. line = new Delta();
  310. }
  311. }
  312. if (line.length() > 0) {
  313. predicate(line, {}, i);
  314. }
  315. };
  316. Delta.prototype.invert = function (base) {
  317. var inverted = new Delta();
  318. this.reduce(function (baseIndex, op) {
  319. if (op.insert) {
  320. inverted.delete(Op_1.default.length(op));
  321. }
  322. else if (op.retain && op.attributes == null) {
  323. inverted.retain(op.retain);
  324. return baseIndex + op.retain;
  325. }
  326. else if (op.delete || (op.retain && op.attributes)) {
  327. var length_2 = (op.delete || op.retain);
  328. var slice = base.slice(baseIndex, baseIndex + length_2);
  329. slice.forEach(function (baseOp) {
  330. if (op.delete) {
  331. inverted.push(baseOp);
  332. }
  333. else if (op.retain && op.attributes) {
  334. inverted.retain(Op_1.default.length(baseOp), AttributeMap_1.default.invert(op.attributes, baseOp.attributes));
  335. }
  336. });
  337. return baseIndex + length_2;
  338. }
  339. return baseIndex;
  340. }, 0);
  341. return inverted.chop();
  342. };
  343. Delta.prototype.transform = function (arg, priority) {
  344. if (priority === void 0) { priority = false; }
  345. priority = !!priority;
  346. if (typeof arg === 'number') {
  347. return this.transformPosition(arg, priority);
  348. }
  349. var other = arg;
  350. var thisIter = Op_1.default.iterator(this.ops);
  351. var otherIter = Op_1.default.iterator(other.ops);
  352. var delta = new Delta();
  353. while (thisIter.hasNext() || otherIter.hasNext()) {
  354. if (thisIter.peekType() === 'insert' &&
  355. (priority || otherIter.peekType() !== 'insert')) {
  356. delta.retain(Op_1.default.length(thisIter.next()));
  357. }
  358. else if (otherIter.peekType() === 'insert') {
  359. delta.push(otherIter.next());
  360. }
  361. else {
  362. var length_3 = Math.min(thisIter.peekLength(), otherIter.peekLength());
  363. var thisOp = thisIter.next(length_3);
  364. var otherOp = otherIter.next(length_3);
  365. if (thisOp.delete) {
  366. // Our delete either makes their delete redundant or removes their retain
  367. continue;
  368. }
  369. else if (otherOp.delete) {
  370. delta.push(otherOp);
  371. }
  372. else {
  373. // We retain either their retain or insert
  374. delta.retain(length_3, AttributeMap_1.default.transform(thisOp.attributes, otherOp.attributes, priority));
  375. }
  376. }
  377. }
  378. return delta.chop();
  379. };
  380. Delta.prototype.transformPosition = function (index, priority) {
  381. if (priority === void 0) { priority = false; }
  382. priority = !!priority;
  383. var thisIter = Op_1.default.iterator(this.ops);
  384. var offset = 0;
  385. while (thisIter.hasNext() && offset <= index) {
  386. var length_4 = thisIter.peekLength();
  387. var nextType = thisIter.peekType();
  388. thisIter.next();
  389. if (nextType === 'delete') {
  390. index -= Math.min(length_4, index - offset);
  391. continue;
  392. }
  393. else if (nextType === 'insert' && (offset < index || !priority)) {
  394. index += length_4;
  395. }
  396. offset += length_4;
  397. }
  398. return index;
  399. };
  400. Delta.Op = Op_1.default;
  401. Delta.AttributeMap = AttributeMap_1.default;
  402. return Delta;
  403. }());
  404. module.exports = Delta;
  405. //# sourceMappingURL=Delta.js.map