UNPKG

slate-history

Version:

An operation-based history implementation for Slate editors.

2 lines (1 loc) • 4.11 kB
!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";var n={isHistory:function(t){return e.isObject(t)&&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))}},r=new WeakMap,i=new WeakMap,o=new WeakMap,s=new WeakMap,a={isHistoryEditor:function(t){return n.isHistory(t.history)&&e.Editor.isEditor(t)},isMerging:function(t){return o.get(t)},isSplittingOnce:function(t){return s.get(t)},setSplittingOnce:function(t,e){s.set(t,e)},isSaving:function(t){return i.get(t)},redo:function(t){t.redo()},undo:function(t){t.undo()},withMerging:function(t,e){var n=a.isMerging(t);o.set(t,!0);try{e()}finally{o.set(t,n)}},withNewBatch:function(t,e){var n=a.isMerging(t),r=a.isSplittingOnce(t);o.set(t,!0),s.set(t,!0);try{e()}finally{o.set(t,n),r||s.delete(t)}},withoutMerging:function(t,e){var n=a.isMerging(t);o.set(t,!1);try{e()}finally{o.set(t,n)}},withoutSaving:function(t,e){var n=a.isSaving(t);i.set(t,!1);try{e()}finally{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,i=function(){};return{s:i,n:function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:i}}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 o,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,o=t},f:function(){try{s||null==n.return||n.return()}finally{if(a)throw o}}}}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=r,t.History=n,t.HistoryEditor=a,t.MERGING=o,t.SAVING=i,t.SPLITTING_ONCE=s,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 i=r[r.length-1];i.selectionBefore&&e.Transforms.setSelection(n,i.selectionBefore),a.withoutSaving(n,(function(){e.Editor.withoutNormalizing(n,(function(){var t,e=u(i.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",i)}},n.undo=function(){var t=n.history,r=t.undos;if(r.length>0){var i=r[r.length-1];a.withoutSaving(n,(function(){e.Editor.withoutNormalizing(n,(function(){var t,r=u(i.operations.map(e.Operation.inverse).reverse());try{for(r.s();!(t=r.n()).done;){var o=t.value;n.apply(o)}}catch(t){r.e(t)}finally{r.f()}i.selectionBefore&&e.Transforms.setSelection(n,i.selectionBefore)}))})),n.writeHistory("redos",i),t.undos.pop()}},n.apply=function(t){var e=n.operations,i=n.history,o=i.undos,s=o[o.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&&(!!e.includes(u)||l(t,u))),a.isSplittingOnce(n)&&(p=!1,a.setSplittingOnce(n,void 0)),s&&p)s.operations.push(t);else{var y={operations:[t],selectionBefore:n.selection};n.writeHistory("undos",y)}for(;o.length>100;)o.shift();i.redos=[]}r(t)},n.writeHistory=function(t,e){n.history[t].push(e)},n}}));