UNPKG

4.01 kBJavaScriptView Raw
1!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("slate")):"function"==typeof define&&define.amd?define(["exports","slate"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).SlateHistory={},t.Slate)}(this,(function(t,e){"use strict";
2/*!
3 * is-plain-object <https://github.com/jonschlinkert/is-plain-object>
4 *
5 * Copyright (c) 2014-2017, Jon Schlinkert.
6 * Released under the MIT License.
7 */function n(t){return"[object Object]"===Object.prototype.toString.call(t)}var r={isHistory:function(t){return!1!==n(r=t)&&(void 0===(o=r.constructor)||!1!==n(i=o.prototype)&&!1!==i.hasOwnProperty("isPrototypeOf"))&&Array.isArray(t.redos)&&Array.isArray(t.undos)&&(0===t.redos.length||e.Operation.isOperationList(t.redos[0].operations))&&(0===t.undos.length||e.Operation.isOperationList(t.undos[0].operations));var r,o,i}},o=new WeakMap,i=new WeakMap,s=new WeakMap,a={isHistoryEditor:function(t){return r.isHistory(t.history)&&e.Editor.isEditor(t)},isMerging:function(t){return s.get(t)},isSaving:function(t){return i.get(t)},redo:function(t){t.redo()},undo:function(t){t.undo()},withoutMerging:function(t,e){var n=a.isMerging(t);s.set(t,!1),e(),s.set(t,n)},withoutSaving:function(t,e){var n=a.isSaving(t);i.set(t,!1),e(),i.set(t,n)}};function u(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!n){if(Array.isArray(t)||(n=function(t,e){if(!t)return;if("string"==typeof t)return f(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);"Object"===n&&t.constructor&&(n=t.constructor.name);if("Map"===n||"Set"===n)return Array.from(t);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return f(t,e)}(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var r=0,o=function(){};return{s:o,n:function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:o}}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 i,s=!0,a=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return s=t.done,t},e:function(t){a=!0,i=t},f:function(){try{s||null==n.return||n.return()}finally{if(a)throw i}}}}function f(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}var l=function(t,n){return!(!n||"insert_text"!==t.type||"insert_text"!==n.type||t.offset!==n.offset+n.text.length||!e.Path.equals(t.path,n.path))||!(!n||"remove_text"!==t.type||"remove_text"!==n.type||t.offset+t.text.length!==n.offset||!e.Path.equals(t.path,n.path))},c=function(t,e){return"set_selection"!==t.type};t.HISTORY=o,t.History=r,t.HistoryEditor=a,t.MERGING=s,t.SAVING=i,t.withHistory=function(t){var n=t,r=n.apply;return n.history={undos:[],redos:[]},n.redo=function(){var t=n.history,r=t.redos;if(r.length>0){var o=r[r.length-1];o.selectionBefore&&e.Transforms.setSelection(n,o.selectionBefore),a.withoutSaving(n,(function(){e.Editor.withoutNormalizing(n,(function(){var t,e=u(o.operations);try{for(e.s();!(t=e.n()).done;){var r=t.value;n.apply(r)}}catch(t){e.e(t)}finally{e.f()}}))})),t.redos.pop(),n.writeHistory("undos",o)}},n.undo=function(){var t=n.history,r=t.undos;if(r.length>0){var o=r[r.length-1];a.withoutSaving(n,(function(){e.Editor.withoutNormalizing(n,(function(){var t,r=u(o.operations.map(e.Operation.inverse).reverse());try{for(r.s();!(t=r.n()).done;){var i=t.value;n.apply(i)}}catch(t){r.e(t)}finally{r.f()}o.selectionBefore&&e.Transforms.setSelection(n,o.selectionBefore)}))})),n.writeHistory("redos",o),t.undos.pop()}},n.apply=function(t){var e=n.operations,o=n.history,i=o.undos,s=i[i.length-1],u=s&&s.operations[s.operations.length-1],f=a.isSaving(n),p=a.isMerging(n);if(null==f&&(f=c(t)),f){if(null==p&&(p=null!=s&&(0!==e.length||l(t,u))),s&&p)s.operations.push(t);else{var y={operations:[t],selectionBefore:n.selection};n.writeHistory("undos",y)}for(;i.length>100;)i.shift();o.redos=[]}r(t)},n.writeHistory=function(t,e){n.history[t].push(e)},n}}));