slate-history.js 9.5 KB

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