UNPKG

regular-redux-undo

Version:

the plugin of regular-redux to archieve undo and redo

2 lines (1 loc) 11.4 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.rex=t()}(this,function(){"use strict";function e(e){var t=_.call(e,C),n=e[C];try{e[C]=void 0;var r=!0}catch(e){}var i=A.call(e);return r&&(t?e[C]=n:delete e[C]),i}function t(e){return I.call(e)}function n(n){return null==n?void 0===n?T:M:N&&N in Object(n)?e(n):t(n)}function r(e){return null!=e&&"object"==(void 0===e?"undefined":m(e))}function i(e){if(!r(e)||n(e)!=R)return!1;var t=P(e);if(null===t)return!0;var i=F.call(t,"constructor")&&t.constructor;return"function"==typeof i&&i instanceof i&&z.call(i)==L}function o(e,t){return t={exports:{}},e(t,t.exports),t.exports}function u(e,t,n){function r(){h===d&&(h=d.slice())}function o(){return l}function c(e){if("function"!=typeof e)throw new Error("Expected listener to be a function.");var t=!0;return r(),h.push(e),function(){if(t){t=!1,r();var n=h.indexOf(e);h.splice(n,1)}}}function a(e){if(!i(e))throw new Error("Actions must be plain objects. Use custom middleware for async actions.");if(void 0===e.type)throw new Error('Actions may not have an undefined "type" property. Have you misspelled a constant?');if(p)throw new Error("Reducers may not dispatch actions.");try{p=!0,l=f(l,e)}finally{p=!1}for(var t=d=h,n=0;n<t.length;n++)(0,t[n])();return e}var s;if("function"==typeof t&&void 0===n&&(n=t,t=void 0),void 0!==n){if("function"!=typeof n)throw new Error("Expected the enhancer to be a function.");return n(u)(e,t)}if("function"!=typeof e)throw new Error("Expected the reducer to be a function.");var f=e,l=t,d=[],h=d,p=!1;return a({type:B.INIT}),s={dispatch:a,subscribe:c,getState:o,replaceReducer:function(e){if("function"!=typeof e)throw new Error("Expected the nextReducer to be a function.");f=e,a({type:B.INIT})}},s[K]=function(){var e,t=c;return e={subscribe:function(e){function n(){e.next&&e.next(o())}if("object"!==(void 0===e?"undefined":m(e)))throw new TypeError("Expected the observer to be an object.");return n(),{unsubscribe:t(n)}}},e[K]=function(){return this},e},s}function c(){for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];return 0===t.length?function(e){return e}:1===t.length?t[0]:t.reduce(function(e,t){return function(){return e(t.apply(void 0,arguments))}})}function a(){for(var e=arguments.length,t=Array(e),n=0;n<e;n++)t[n]=arguments[n];return function(e){return function(n,r,i){var o=e(n,r,i),u=o.dispatch,a=[],s={getState:o.getState,dispatch:function(e){return u(e)}};return a=t.map(function(e){return e(s)}),u=c.apply(void 0,a)(o.dispatch),G({},o,{dispatch:u})}}}function s(e){return Object.prototype.toString.call(e).slice(8,-1).toLowerCase()}function f(e){var t=s(e);return"array"===t?[].slice.call(e):"object"===t?Object.assign({},e):e}function l(e,t,n){if("object"!==s(e)||"object"!==s(t))return e;for(var r in t)(n||void 0===e[r]||null==e[r])&&(e[r]=t[r]);return e}function d(e){var t=s(e);return"object"===t?!Object.keys(e).length:"array"!==t||!e.length}function h(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=arguments[1];Object.keys(e).forEach(function(n){return t(e[n],n)})}function p(e,t){if(!e)throw new Error("[regular-redux] "+t)}function v(e,t){e&&console.error("[regular-redux] "+t)}function b(e,t,n){return function(r){return n?Q(e.current,r)!==Q(t.current,r):Q(e,r)!==Q(t,r)}}function y(e){var t=function(t,n){if(!t||!n)return t;var r=e.reducers[n.type];if(r){var i=r.path,o=r.handler,u=i.length?Q(t,i.join(".")):t,c=o(u,n.payload);if(c!==u&&c)return i.length?J(t,i.join("."),c):c}return t};return e.modifiers.slice().forEach(function(e){t=e(t)}),e.undoable?Z(W(e.name,t),!0):Z(t,!1)}function g(e,t){return function(n){return function(n){return function(r){var i={undo:e.undo.bind(e),redo:e.redo.bind(e),dispatch:e.dispatch.bind(e),getState:e.getState.bind(e),subscribe:e.subscribe.bind(e)};t(i,function(){return n(r)})}}}}var m="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},j=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},w=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),O=function(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t<e.length;t++)n[t]=e[t];return n}return Array.from(e)},S="object"==("undefined"==typeof global?"undefined":m(global))&&global&&global.Object===Object&&global,x="object"==("undefined"==typeof self?"undefined":m(self))&&self&&self.Object===Object&&self,k=(S||x||Function("return this")()).Symbol,E=Object.prototype,_=E.hasOwnProperty,A=E.toString,C=k?k.toStringTag:void 0,I=Object.prototype.toString,M="[object Null]",T="[object Undefined]",N=k?k.toStringTag:void 0,P=function(e,t){return function(n){return e(t(n))}}(Object.getPrototypeOf,Object),R="[object Object]",D=Function.prototype,U=Object.prototype,z=D.toString,F=U.hasOwnProperty,L=z.call(Object),q="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},H=o(function(e,t){Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e){var t,n=e.Symbol;return"function"==typeof n?n.observable?t=n.observable:(t=n("observable"),n.observable=t):t="@@observable",t}}),K=o(function(e,t){Object.defineProperty(t,"__esModule",{value:!0});var n,r=function(e){return e&&e.__esModule?e:{default:e}}(H);n="undefined"!=typeof self?self:"undefined"!=typeof window?window:void 0!==q?q:e;var i=(0,r.default)(n);t.default=i}),B={INIT:"@@redux/INIT"},G=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},J=function e(t,n,r){var i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{};!0===i&&(i={assign:!0});var o=i,u=o.assign;o.autoCreate;n=Array.isArray(n)?n:(""+n).split(".");var c=s(t),a=s(r),l=n.length;if(!l)return u||"object"!==c||"object"!==a?r:Object.assign({},t,r);var d=n.shift();return void 0===(t=f(t))&&i.autoCreate&&(t={}),1===l&&t[d]===r?t:(t[d]=e(t[d],n,r,i),t)},Q=function(e,t){return"string"==typeof t&&(t=t.split(".")),e&&t.some(function(t){if(null===(e=e[t])||void 0===e)return!0}),e},V=["ADD","UNDO","REDO","SEEK"],W=function e(t,n){var r={timeline:[],index:-1,size:arguments.length>2&&void 0!==arguments[2]?arguments[2]:1024},i={};return V.forEach(function(n){i[t+"_"+n]=e[n.toLowerCase()]}),function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:r,t=arguments[1];e.timeline||(r.current={},e=r);var o=i[t.type];if(o)return o(e,t.payload,t.replace);var u=n(e.current,t);return X(e,u,!1!==t.record,t)}},X=W.add=function(e,t,n,r){if(t===e.current||void 0===t||null===t)return e;var i={current:t};if(n){r.clean?e.timeline=[]:r.replace&&e.timeline.pop();var o=i.timeline=e.timeline.slice(e.index+1>=e.size?1:0,e.index+1);i.index=o.push(t)-1}return l(i,e)},Y=W.seek=function(e,t,n){var r=e.timeline,i=r.length-1;return t<0&&(t=0),t>i?(t=i,e):(n&&(e.timeline=r.slice(0,t+1)),t==e.index?e:l({index:t,current:r[t]},e))};W.undo=function(e,t,n){return Y(e,e.index-(t||1),n)},W.redo=function(e,t){return Y(e,e.index+(t||1))};var Z=function(e){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];return function(n,r){var i=e(n,r);return i&&(i.changed=b(i,n,t)),i}},$=function(){function e(t,n){j(this,e),this._children=Object.create(null),this._rawModule=t,this.path=n;var r=t.state||{};this.state="function"===s(r)?r():r}return w(e,[{key:"addChild",value:function(e,t){this._children[e]=t}},{key:"getChild",value:function(e){return this._children[e]}},{key:"removeChild",value:function(e){delete this._children[e]}},{key:"hasChild",value:function(){return!d(this._children)}},{key:"forEachModule",value:function(e){e(this),h(this._children,function(t){t.forEachModule(e)})}},{key:"checkState",value:function(){arguments.length>0&&void 0!==arguments[0]&&arguments[0]}},{key:"combineState",value:function(){var e=this;return h(this._children,function(t,n){e.state[n]=t.combineState()}),this.state}},{key:"reducers",get:function(){return this._rawModule.reducers||{}}}]),e}(),ee=function(){function e(t){j(this,e),this.root=Object.create(null),this.register([],t),this.root.checkState(),this.state=this.root.combineState()}return w(e,[{key:"register",value:function(e,t){var n=this,r=new $(t,e);0===e.length?this.root=r:this.get(e.slice(0,-1)).addChild(e[e.length-1],r),t.modules&&h(t.modules,function(t,r){n.register(e.concat(r),t)})}},{key:"unregister",value:function(e){var t=this.get(e.slice(0,-1)),n=e[e.length-1];t.removeChild(n)}},{key:"get",value:function(e){return e.reduce(function(e,t){return e.getChild(t)},this.root)}}]),e}();return{Store:function(){function e(){var t=this,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};j(this,e);var r=n.name,i=void 0===r?"rex":r,o=n.size,c=void 0===o?1024:o,s=n.undoable,f=void 0===s||s,l=n.modifiers,d=void 0===l?[]:l,v=n.middlewares,b=void 0===v?[]:v;this.name=i,this.size=c,this.modifiers=d,this.middlewares=b,this.undoable=!!f,this._modules=new ee(n),this.reducers=Object.create(null),this._modules.root.forEachModule(function(e){h(e.reducers,function(n,r){p(!t.reducers[r],"name of reducer must be unique, there is already a reducer named '"+r+"' exist"),t.reducers[r]={path:e.path.slice(),handler:n}})}),this.reducers["@replace/state"]={path:[],handler:function(e,t){return t.state}};(b=b.map(function(e){return g(t,e)})).unshift(function(e){return function(e){return function(n){return"function"==typeof n?n(t.dispatch.bind(t)):e(n)}}}),this.store=u(y(this),{},a.apply(void 0,O(b)))}return w(e,[{key:"replaceState",value:function(e,t){t=Object.assign({},t,{state:e}),this.store.dispatch({type:"@replace/state",payload:t})}},{key:"undo",value:function(){if(!this.canUndo())return!1;this.store.dispatch({type:this.name+"_UNDO"})}},{key:"canUndo",value:function(){var e=this.store.getState(),t=e.timeline,n=e.index;return this.undoable&&0!==n&&t.length}},{key:"redo",value:function(){if(!this.canRedo())return!1;this.store.dispatch({type:this.name+"_REDO"})}},{key:"canRedo",value:function(){var e=this.store.getState(),t=e.timeline,n=e.index;return this.undoable&&n!==t.length-1&&t.length}},{key:"subscribe",value:function(e){return this.store.subscribe(e)}},{key:"getState",value:function(){return this.undoable?this.store.getState().current:this.store.getState()}},{key:"dispatch",value:function(e,t){if("undo"===e)return this.undoable?this.undo():v(!0,"can not undo because of the config.undoable is false");if("redo"===e)return this.undoable?this.redo():v(!0,"can not redo because of the config.undoable is false");if("@init/state"===e)return this.replaceState(this._modules.state,{clean:!0});if("function"==typeof e)return this.store.dispatch(e);if(p("string"==typeof e,"the type of a reducer must be a string."),this.reducers[e]){var n={type:e,payload:t};this.store.dispatch(n)}else v(!0,"the reducer "+e+" is not fount in reducers list.")}},{key:"state",get:function(){return this.getState()},set:function(e){}}]),e}(),set:J,get:Q,splice:function(e,t){for(var n=Q(e,t).slice(),r=arguments.length,i=Array(r>2?r-2:0),o=2;o<r;o++)i[o-2]=arguments[o];return n.splice.apply(n,i),J(e,t,n,!0)}}});