index.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var isPlainObject = require('is-plain-object');
  4. var slate = require('slate');
  5. var History = {
  6. /**
  7. * Check if a value is a `History` object.
  8. */
  9. isHistory: function isHistory(value) {
  10. return isPlainObject.isPlainObject(value) && Array.isArray(value.redos) && Array.isArray(value.undos) && (value.redos.length === 0 || slate.Operation.isOperationList(value.redos[0])) && (value.undos.length === 0 || slate.Operation.isOperationList(value.undos[0]));
  11. }
  12. };
  13. /**
  14. * Weakmaps for attaching state to the editor.
  15. */
  16. var HISTORY = new WeakMap();
  17. var SAVING = new WeakMap();
  18. var MERGING = new WeakMap();
  19. var HistoryEditor = {
  20. /**
  21. * Check if a value is a `HistoryEditor` object.
  22. */
  23. isHistoryEditor: function isHistoryEditor(value) {
  24. return History.isHistory(value.history) && slate.Editor.isEditor(value);
  25. },
  26. /**
  27. * Get the merge flag's current value.
  28. */
  29. isMerging: function isMerging(editor) {
  30. return MERGING.get(editor);
  31. },
  32. /**
  33. * Get the saving flag's current value.
  34. */
  35. isSaving: function isSaving(editor) {
  36. return SAVING.get(editor);
  37. },
  38. /**
  39. * Redo to the previous saved state.
  40. */
  41. redo: function redo(editor) {
  42. editor.redo();
  43. },
  44. /**
  45. * Undo to the previous saved state.
  46. */
  47. undo: function undo(editor) {
  48. editor.undo();
  49. },
  50. /**
  51. * Apply a series of changes inside a synchronous `fn`, without merging any of
  52. * the new operations into previous save point in the history.
  53. */
  54. withoutMerging: function withoutMerging(editor, fn) {
  55. var prev = HistoryEditor.isMerging(editor);
  56. MERGING.set(editor, false);
  57. fn();
  58. MERGING.set(editor, prev);
  59. },
  60. /**
  61. * Apply a series of changes inside a synchronous `fn`, without saving any of
  62. * their operations into the history.
  63. */
  64. withoutSaving: function withoutSaving(editor, fn) {
  65. var prev = HistoryEditor.isSaving(editor);
  66. SAVING.set(editor, false);
  67. fn();
  68. SAVING.set(editor, prev);
  69. }
  70. };
  71. function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
  72. 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); }
  73. 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; }
  74. /**
  75. * The `withHistory` plugin keeps track of the operation history of a Slate
  76. * editor as operations are applied to it, using undo and redo stacks.
  77. *
  78. * If you are using TypeScript, you must extend Slate's CustomTypes to use
  79. * this plugin.
  80. *
  81. * See https://docs.slatejs.org/concepts/11-typescript to learn how.
  82. */
  83. var withHistory = function withHistory(editor) {
  84. var e = editor;
  85. var apply = e.apply;
  86. e.history = {
  87. undos: [],
  88. redos: []
  89. };
  90. e.redo = function () {
  91. var history = e.history;
  92. var redos = history.redos;
  93. if (redos.length > 0) {
  94. var batch = redos[redos.length - 1];
  95. HistoryEditor.withoutSaving(e, function () {
  96. slate.Editor.withoutNormalizing(e, function () {
  97. var _iterator = _createForOfIteratorHelper(batch),
  98. _step;
  99. try {
  100. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  101. var op = _step.value;
  102. e.apply(op);
  103. }
  104. } catch (err) {
  105. _iterator.e(err);
  106. } finally {
  107. _iterator.f();
  108. }
  109. });
  110. });
  111. history.redos.pop();
  112. history.undos.push(batch);
  113. }
  114. };
  115. e.undo = function () {
  116. var history = e.history;
  117. var undos = history.undos;
  118. if (undos.length > 0) {
  119. var batch = undos[undos.length - 1];
  120. HistoryEditor.withoutSaving(e, function () {
  121. slate.Editor.withoutNormalizing(e, function () {
  122. var inverseOps = batch.map(slate.Operation.inverse).reverse();
  123. var _iterator2 = _createForOfIteratorHelper(inverseOps),
  124. _step2;
  125. try {
  126. for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
  127. var op = _step2.value;
  128. e.apply(op);
  129. }
  130. } catch (err) {
  131. _iterator2.e(err);
  132. } finally {
  133. _iterator2.f();
  134. }
  135. });
  136. });
  137. history.redos.push(batch);
  138. history.undos.pop();
  139. }
  140. };
  141. e.apply = function (op) {
  142. var operations = e.operations,
  143. history = e.history;
  144. var undos = history.undos;
  145. var lastBatch = undos[undos.length - 1];
  146. var lastOp = lastBatch && lastBatch[lastBatch.length - 1];
  147. var overwrite = shouldOverwrite(op, lastOp);
  148. var save = HistoryEditor.isSaving(e);
  149. var merge = HistoryEditor.isMerging(e);
  150. if (save == null) {
  151. save = shouldSave(op);
  152. }
  153. if (save) {
  154. if (merge == null) {
  155. if (lastBatch == null) {
  156. merge = false;
  157. } else if (operations.length !== 0) {
  158. merge = true;
  159. } else {
  160. merge = shouldMerge(op, lastOp) || overwrite;
  161. }
  162. }
  163. if (lastBatch && merge) {
  164. if (overwrite) {
  165. lastBatch.pop();
  166. }
  167. lastBatch.push(op);
  168. } else {
  169. var batch = [op];
  170. undos.push(batch);
  171. }
  172. while (undos.length > 100) {
  173. undos.shift();
  174. }
  175. if (shouldClear(op)) {
  176. history.redos = [];
  177. }
  178. }
  179. apply(op);
  180. };
  181. return e;
  182. };
  183. /**
  184. * Check whether to merge an operation into the previous operation.
  185. */
  186. var shouldMerge = function shouldMerge(op, prev) {
  187. if (op.type === 'set_selection') {
  188. return true;
  189. }
  190. if (prev && op.type === 'insert_text' && prev.type === 'insert_text' && op.offset === prev.offset + prev.text.length && slate.Path.equals(op.path, prev.path)) {
  191. return true;
  192. }
  193. if (prev && op.type === 'remove_text' && prev.type === 'remove_text' && op.offset + op.text.length === prev.offset && slate.Path.equals(op.path, prev.path)) {
  194. return true;
  195. }
  196. return false;
  197. };
  198. /**
  199. * Check whether an operation needs to be saved to the history.
  200. */
  201. var shouldSave = function shouldSave(op, prev) {
  202. if (op.type === 'set_selection' && (op.properties == null || op.newProperties == null)) {
  203. return false;
  204. }
  205. return true;
  206. };
  207. /**
  208. * Check whether an operation should overwrite the previous one.
  209. */
  210. var shouldOverwrite = function shouldOverwrite(op, prev) {
  211. if (prev && op.type === 'set_selection' && prev.type === 'set_selection') {
  212. return true;
  213. }
  214. return false;
  215. };
  216. /**
  217. * Check whether an operation should clear the redos stack.
  218. */
  219. var shouldClear = function shouldClear(op) {
  220. if (op.type === 'set_selection') {
  221. return false;
  222. }
  223. return true;
  224. };
  225. exports.HISTORY = HISTORY;
  226. exports.History = History;
  227. exports.HistoryEditor = HistoryEditor;
  228. exports.MERGING = MERGING;
  229. exports.SAVING = SAVING;
  230. exports.withHistory = withHistory;
  231. //# sourceMappingURL=index.js.map