index.cjs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. 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); } }
  4. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  5. 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); }
  6. Object.defineProperty(exports, '__esModule', {
  7. value: true
  8. });
  9. var RopeSequence = require('rope-sequence');
  10. var prosemirrorTransform = require('prosemirror-transform');
  11. var prosemirrorState = require('prosemirror-state');
  12. function _interopDefaultLegacy(e) {
  13. return e && _typeof(e) === 'object' && 'default' in e ? e : {
  14. 'default': e
  15. };
  16. }
  17. var RopeSequence__default = _interopDefaultLegacy(RopeSequence);
  18. var max_empty_items = 500;
  19. var Branch = function () {
  20. function Branch(items, eventCount) {
  21. _classCallCheck(this, Branch);
  22. this.items = items;
  23. this.eventCount = eventCount;
  24. }
  25. _createClass(Branch, [{
  26. key: "popEvent",
  27. value: function popEvent(state, preserveItems) {
  28. var _this = this;
  29. if (this.eventCount == 0) return null;
  30. var end = this.items.length;
  31. for (;; end--) {
  32. var next = this.items.get(end - 1);
  33. if (next.selection) {
  34. --end;
  35. break;
  36. }
  37. }
  38. var remap, mapFrom;
  39. if (preserveItems) {
  40. remap = this.remapping(end, this.items.length);
  41. mapFrom = remap.maps.length;
  42. }
  43. var transform = state.tr;
  44. var selection, remaining;
  45. var addAfter = [],
  46. addBefore = [];
  47. this.items.forEach(function (item, i) {
  48. if (!item.step) {
  49. if (!remap) {
  50. remap = _this.remapping(end, i + 1);
  51. mapFrom = remap.maps.length;
  52. }
  53. mapFrom--;
  54. addBefore.push(item);
  55. return;
  56. }
  57. if (remap) {
  58. addBefore.push(new Item(item.map));
  59. var step = item.step.map(remap.slice(mapFrom)),
  60. map;
  61. if (step && transform.maybeStep(step).doc) {
  62. map = transform.mapping.maps[transform.mapping.maps.length - 1];
  63. addAfter.push(new Item(map, undefined, undefined, addAfter.length + addBefore.length));
  64. }
  65. mapFrom--;
  66. if (map) remap.appendMap(map, mapFrom);
  67. } else {
  68. transform.maybeStep(item.step);
  69. }
  70. if (item.selection) {
  71. selection = remap ? item.selection.map(remap.slice(mapFrom)) : item.selection;
  72. remaining = new Branch(_this.items.slice(0, end).append(addBefore.reverse().concat(addAfter)), _this.eventCount - 1);
  73. return false;
  74. }
  75. }, this.items.length, 0);
  76. return {
  77. remaining: remaining,
  78. transform: transform,
  79. selection: selection
  80. };
  81. }
  82. }, {
  83. key: "addTransform",
  84. value: function addTransform(transform, selection, histOptions, preserveItems) {
  85. var newItems = [],
  86. eventCount = this.eventCount;
  87. var oldItems = this.items,
  88. lastItem = !preserveItems && oldItems.length ? oldItems.get(oldItems.length - 1) : null;
  89. for (var i = 0; i < transform.steps.length; i++) {
  90. var step = transform.steps[i].invert(transform.docs[i]);
  91. var item = new Item(transform.mapping.maps[i], step, selection),
  92. merged = void 0;
  93. if (merged = lastItem && lastItem.merge(item)) {
  94. item = merged;
  95. if (i) newItems.pop();else oldItems = oldItems.slice(0, oldItems.length - 1);
  96. }
  97. newItems.push(item);
  98. if (selection) {
  99. eventCount++;
  100. selection = undefined;
  101. }
  102. if (!preserveItems) lastItem = item;
  103. }
  104. var overflow = eventCount - histOptions.depth;
  105. if (overflow > DEPTH_OVERFLOW) {
  106. oldItems = cutOffEvents(oldItems, overflow);
  107. eventCount -= overflow;
  108. }
  109. return new Branch(oldItems.append(newItems), eventCount);
  110. }
  111. }, {
  112. key: "remapping",
  113. value: function remapping(from, to) {
  114. var maps = new prosemirrorTransform.Mapping();
  115. this.items.forEach(function (item, i) {
  116. var mirrorPos = item.mirrorOffset != null && i - item.mirrorOffset >= from ? maps.maps.length - item.mirrorOffset : undefined;
  117. maps.appendMap(item.map, mirrorPos);
  118. }, from, to);
  119. return maps;
  120. }
  121. }, {
  122. key: "addMaps",
  123. value: function addMaps(array) {
  124. if (this.eventCount == 0) return this;
  125. return new Branch(this.items.append(array.map(function (map) {
  126. return new Item(map);
  127. })), this.eventCount);
  128. }
  129. }, {
  130. key: "rebased",
  131. value: function rebased(rebasedTransform, rebasedCount) {
  132. if (!this.eventCount) return this;
  133. var rebasedItems = [],
  134. start = Math.max(0, this.items.length - rebasedCount);
  135. var mapping = rebasedTransform.mapping;
  136. var newUntil = rebasedTransform.steps.length;
  137. var eventCount = this.eventCount;
  138. this.items.forEach(function (item) {
  139. if (item.selection) eventCount--;
  140. }, start);
  141. var iRebased = rebasedCount;
  142. this.items.forEach(function (item) {
  143. var pos = mapping.getMirror(--iRebased);
  144. if (pos == null) return;
  145. newUntil = Math.min(newUntil, pos);
  146. var map = mapping.maps[pos];
  147. if (item.step) {
  148. var step = rebasedTransform.steps[pos].invert(rebasedTransform.docs[pos]);
  149. var selection = item.selection && item.selection.map(mapping.slice(iRebased + 1, pos));
  150. if (selection) eventCount++;
  151. rebasedItems.push(new Item(map, step, selection));
  152. } else {
  153. rebasedItems.push(new Item(map));
  154. }
  155. }, start);
  156. var newMaps = [];
  157. for (var i = rebasedCount; i < newUntil; i++) {
  158. newMaps.push(new Item(mapping.maps[i]));
  159. }
  160. var items = this.items.slice(0, start).append(newMaps).append(rebasedItems);
  161. var branch = new Branch(items, eventCount);
  162. if (branch.emptyItemCount() > max_empty_items) branch = branch.compress(this.items.length - rebasedItems.length);
  163. return branch;
  164. }
  165. }, {
  166. key: "emptyItemCount",
  167. value: function emptyItemCount() {
  168. var count = 0;
  169. this.items.forEach(function (item) {
  170. if (!item.step) count++;
  171. });
  172. return count;
  173. }
  174. }, {
  175. key: "compress",
  176. value: function compress() {
  177. var upto = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.items.length;
  178. var remap = this.remapping(0, upto),
  179. mapFrom = remap.maps.length;
  180. var items = [],
  181. events = 0;
  182. this.items.forEach(function (item, i) {
  183. if (i >= upto) {
  184. items.push(item);
  185. if (item.selection) events++;
  186. } else if (item.step) {
  187. var step = item.step.map(remap.slice(mapFrom)),
  188. map = step && step.getMap();
  189. mapFrom--;
  190. if (map) remap.appendMap(map, mapFrom);
  191. if (step) {
  192. var selection = item.selection && item.selection.map(remap.slice(mapFrom));
  193. if (selection) events++;
  194. var newItem = new Item(map.invert(), step, selection),
  195. merged,
  196. last = items.length - 1;
  197. if (merged = items.length && items[last].merge(newItem)) items[last] = merged;else items.push(newItem);
  198. }
  199. } else if (item.map) {
  200. mapFrom--;
  201. }
  202. }, this.items.length, 0);
  203. return new Branch(RopeSequence__default["default"].from(items.reverse()), events);
  204. }
  205. }]);
  206. return Branch;
  207. }();
  208. Branch.empty = new Branch(RopeSequence__default["default"].empty, 0);
  209. function cutOffEvents(items, n) {
  210. var cutPoint;
  211. items.forEach(function (item, i) {
  212. if (item.selection && n-- == 0) {
  213. cutPoint = i;
  214. return false;
  215. }
  216. });
  217. return items.slice(cutPoint);
  218. }
  219. var Item = function () {
  220. function Item(map, step, selection, mirrorOffset) {
  221. _classCallCheck(this, Item);
  222. this.map = map;
  223. this.step = step;
  224. this.selection = selection;
  225. this.mirrorOffset = mirrorOffset;
  226. }
  227. _createClass(Item, [{
  228. key: "merge",
  229. value: function merge(other) {
  230. if (this.step && other.step && !other.selection) {
  231. var step = other.step.merge(this.step);
  232. if (step) return new Item(step.getMap().invert(), step, this.selection);
  233. }
  234. }
  235. }]);
  236. return Item;
  237. }();
  238. var HistoryState = _createClass(function HistoryState(done, undone, prevRanges, prevTime, prevComposition) {
  239. _classCallCheck(this, HistoryState);
  240. this.done = done;
  241. this.undone = undone;
  242. this.prevRanges = prevRanges;
  243. this.prevTime = prevTime;
  244. this.prevComposition = prevComposition;
  245. });
  246. var DEPTH_OVERFLOW = 20;
  247. function applyTransaction(history, state, tr, options) {
  248. var historyTr = tr.getMeta(historyKey),
  249. rebased;
  250. if (historyTr) return historyTr.historyState;
  251. if (tr.getMeta(closeHistoryKey)) history = new HistoryState(history.done, history.undone, null, 0, -1);
  252. var appended = tr.getMeta("appendedTransaction");
  253. if (tr.steps.length == 0) {
  254. return history;
  255. } else if (appended && appended.getMeta(historyKey)) {
  256. if (appended.getMeta(historyKey).redo) return new HistoryState(history.done.addTransform(tr, undefined, options, mustPreserveItems(state)), history.undone, rangesFor(tr.mapping.maps[tr.steps.length - 1]), history.prevTime, history.prevComposition);else return new HistoryState(history.done, history.undone.addTransform(tr, undefined, options, mustPreserveItems(state)), null, history.prevTime, history.prevComposition);
  257. } else if (tr.getMeta("addToHistory") !== false && !(appended && appended.getMeta("addToHistory") === false)) {
  258. var composition = tr.getMeta("composition");
  259. var newGroup = history.prevTime == 0 || !appended && history.prevComposition != composition && (history.prevTime < (tr.time || 0) - options.newGroupDelay || !isAdjacentTo(tr, history.prevRanges));
  260. var prevRanges = appended ? mapRanges(history.prevRanges, tr.mapping) : rangesFor(tr.mapping.maps[tr.steps.length - 1]);
  261. return new HistoryState(history.done.addTransform(tr, newGroup ? state.selection.getBookmark() : undefined, options, mustPreserveItems(state)), Branch.empty, prevRanges, tr.time, composition == null ? history.prevComposition : composition);
  262. } else if (rebased = tr.getMeta("rebased")) {
  263. return new HistoryState(history.done.rebased(tr, rebased), history.undone.rebased(tr, rebased), mapRanges(history.prevRanges, tr.mapping), history.prevTime, history.prevComposition);
  264. } else {
  265. return new HistoryState(history.done.addMaps(tr.mapping.maps), history.undone.addMaps(tr.mapping.maps), mapRanges(history.prevRanges, tr.mapping), history.prevTime, history.prevComposition);
  266. }
  267. }
  268. function isAdjacentTo(transform, prevRanges) {
  269. if (!prevRanges) return false;
  270. if (!transform.docChanged) return true;
  271. var adjacent = false;
  272. transform.mapping.maps[0].forEach(function (start, end) {
  273. for (var i = 0; i < prevRanges.length; i += 2) {
  274. if (start <= prevRanges[i + 1] && end >= prevRanges[i]) adjacent = true;
  275. }
  276. });
  277. return adjacent;
  278. }
  279. function rangesFor(map) {
  280. var result = [];
  281. map.forEach(function (_from, _to, from, to) {
  282. return result.push(from, to);
  283. });
  284. return result;
  285. }
  286. function mapRanges(ranges, mapping) {
  287. if (!ranges) return null;
  288. var result = [];
  289. for (var i = 0; i < ranges.length; i += 2) {
  290. var from = mapping.map(ranges[i], 1),
  291. to = mapping.map(ranges[i + 1], -1);
  292. if (from <= to) result.push(from, to);
  293. }
  294. return result;
  295. }
  296. function histTransaction(history, state, dispatch, redo) {
  297. var preserveItems = mustPreserveItems(state);
  298. var histOptions = historyKey.get(state).spec.config;
  299. var pop = (redo ? history.undone : history.done).popEvent(state, preserveItems);
  300. if (!pop) return;
  301. var selection = pop.selection.resolve(pop.transform.doc);
  302. var added = (redo ? history.done : history.undone).addTransform(pop.transform, state.selection.getBookmark(), histOptions, preserveItems);
  303. var newHist = new HistoryState(redo ? added : pop.remaining, redo ? pop.remaining : added, null, 0, -1);
  304. dispatch(pop.transform.setSelection(selection).setMeta(historyKey, {
  305. redo: redo,
  306. historyState: newHist
  307. }).scrollIntoView());
  308. }
  309. var cachedPreserveItems = false,
  310. cachedPreserveItemsPlugins = null;
  311. function mustPreserveItems(state) {
  312. var plugins = state.plugins;
  313. if (cachedPreserveItemsPlugins != plugins) {
  314. cachedPreserveItems = false;
  315. cachedPreserveItemsPlugins = plugins;
  316. for (var i = 0; i < plugins.length; i++) {
  317. if (plugins[i].spec.historyPreserveItems) {
  318. cachedPreserveItems = true;
  319. break;
  320. }
  321. }
  322. }
  323. return cachedPreserveItems;
  324. }
  325. function closeHistory(tr) {
  326. return tr.setMeta(closeHistoryKey, true);
  327. }
  328. var historyKey = new prosemirrorState.PluginKey("history");
  329. var closeHistoryKey = new prosemirrorState.PluginKey("closeHistory");
  330. function history() {
  331. var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  332. config = {
  333. depth: config.depth || 100,
  334. newGroupDelay: config.newGroupDelay || 500
  335. };
  336. return new prosemirrorState.Plugin({
  337. key: historyKey,
  338. state: {
  339. init: function init() {
  340. return new HistoryState(Branch.empty, Branch.empty, null, 0, -1);
  341. },
  342. apply: function apply(tr, hist, state) {
  343. return applyTransaction(hist, state, tr, config);
  344. }
  345. },
  346. config: config,
  347. props: {
  348. handleDOMEvents: {
  349. beforeinput: function beforeinput(view, e) {
  350. var inputType = e.inputType;
  351. var command = inputType == "historyUndo" ? undo : inputType == "historyRedo" ? redo : null;
  352. if (!command) return false;
  353. e.preventDefault();
  354. return command(view.state, view.dispatch);
  355. }
  356. }
  357. }
  358. });
  359. }
  360. var undo = function undo(state, dispatch) {
  361. var hist = historyKey.getState(state);
  362. if (!hist || hist.done.eventCount == 0) return false;
  363. if (dispatch) histTransaction(hist, state, dispatch, false);
  364. return true;
  365. };
  366. var redo = function redo(state, dispatch) {
  367. var hist = historyKey.getState(state);
  368. if (!hist || hist.undone.eventCount == 0) return false;
  369. if (dispatch) histTransaction(hist, state, dispatch, true);
  370. return true;
  371. };
  372. function undoDepth(state) {
  373. var hist = historyKey.getState(state);
  374. return hist ? hist.done.eventCount : 0;
  375. }
  376. function redoDepth(state) {
  377. var hist = historyKey.getState(state);
  378. return hist ? hist.undone.eventCount : 0;
  379. }
  380. exports.closeHistory = closeHistory;
  381. exports.history = history;
  382. exports.redo = redo;
  383. exports.redoDepth = redoDepth;
  384. exports.undo = undo;
  385. exports.undoDepth = undoDepth;