slate-history
Version:
An operation-based history implementation for Slate editors.
2 lines (1 loc) • 11.8 kB
JavaScript
!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 r={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))}},n=new WeakMap,o=new WeakMap,i=new WeakMap,a=new WeakMap,s={isHistoryEditor:function(t){return r.isHistory(t.history)&&e.Editor.isEditor(t)},isMerging:function(t){return i.get(t)},isSplittingOnce:function(t){return a.get(t)},setSplittingOnce:function(t,e){a.set(t,e)},isSaving:function(t){return o.get(t)},redo:function(t){t.redo()},undo:function(t){t.undo()},withMerging:function(t,e){var r=s.isMerging(t);i.set(t,!0);try{e()}finally{i.set(t,r)}},withNewBatch:function(t,e){var r=s.isMerging(t),n=s.isSplittingOnce(t);i.set(t,!0),a.set(t,!0);try{e()}finally{i.set(t,r),n||a.delete(t)}},withoutMerging:function(t,e){var r=s.isMerging(t);i.set(t,!1);try{e()}finally{i.set(t,r)}},withoutSaving:function(t,e){var r=s.isSaving(t);o.set(t,!1);try{e()}finally{o.set(t,r)}}};function f(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}function u(t,e){return t(e={exports:{}},e.exports),e.exports}var l=u((function(t){function e(r){return t.exports=e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},t.exports.__esModule=!0,t.exports.default=t.exports,e(r)}t.exports=e,t.exports.__esModule=!0,t.exports.default=t.exports}));f(l);var p=u((function(t){var e=l.default;t.exports=function(t,r){if("object"!=e(t)||!t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var o=n.call(t,r||"default");if("object"!=e(o))return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)},t.exports.__esModule=!0,t.exports.default=t.exports}));f(p);var c=u((function(t){var e=l.default;t.exports=function(t){var r=p(t,"string");return"symbol"==e(r)?r:r+""},t.exports.__esModule=!0,t.exports.default=t.exports}));f(c);var h=f(u((function(t){t.exports=function(t,e,r){return(e=c(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t},t.exports.__esModule=!0,t.exports.default=t.exports})));function y(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!r){if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return v(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?v(t,e):void 0}}(t))||e){r&&(t=r);var n=0,o=function(){};return{s:o,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},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,a=!0,s=!1;return{s:function(){r=r.call(t)},n:function(){var t=r.next();return a=t.done,t},e:function(t){s=!0,i=t},f:function(){try{a||null==r.return||r.return()}finally{if(s)throw i}}}}function v(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}function d(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function g(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?d(Object(r),!0).forEach((function(e){h(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):d(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}var m=function(t,r){return t.length===r.length+1&&e.Path.isParent(r,t)},x=function(t,r){return e.Path.transform(t,r)},b=function(t,r){return"remove_node"!==r.type&&"merge_node"!==r.type||!e.Path.equals(r.path,t)?"move_node"===r.type&&e.Path.equals(r.path,t)?e.Path.transform(t,{type:"insert_node",path:r.newPath,node:{text:""}}):x(t,r):t},w=function(t,r,n){var o=t.path,i=t.offset;if("set_selection"===r.type||"set_node"===r.type)return{path:o,offset:i};if(("insert_node"===r.type||"remove_node"===r.type||"split_node"===r.type||"merge_node"===r.type)&&m(r.path,o)){var a=r.path[r.path.length-1];if("insert_node"===r.type||"split_node"===r.type){var s="split_node"===r.type?a+1:a;(s<i||s===i&&"after"===n)&&i++}else if("remove_node"===r.type)a<i&&i--;else if(a<i)i--;else if(a===i)return null;var f=x(o,r);return f&&{path:f,offset:i}}if("move_node"===r.type){var u=m(r.path,o),l=e.Path.transform(r.path,r),p=null!==l&&m(l,o);if(u||p)return null}var c=e.Point.transform({path:o,offset:i},r,{affinity:"after"===n?"forward":"backward"});return c&&{path:c.path,offset:c.offset}},_=function(t,r,n){var o=O(t,r,n);return 1===(null==o?void 0:o.length)&&function(t,r){var n=t,o=r;return n.text===o.text&&n.offset===o.offset&&n.position===o.position&&(n.path===o.path||e.Path.equals(n.path,o.path))&&(n.newPath===o.newPath||e.Path.equals(n.newPath,o.newPath))}(t,o[0])?[t]:o},O=function(t,r,n){switch(t.type){case"set_selection":return[t];case"set_node":case"remove_node":var o=x(t.path,r);return o?[g(g({},t),{},{path:o})]:[];case"insert_node":if("insert_node"===r.type&&e.Path.equals(r.path,t.path)&&"before"===n)return[t];var i=b(t.path,r);return i?[g(g({},t),{},{path:i})]:[];case"move_node":var a=x(t.path,r);if(!a)return[];var s=b(t.newPath,r);return s?[g(g({},t),{},{path:a,newPath:s})]:null;case"insert_text":var f=w({path:t.path,offset:t.offset},r,n);return f?[g(g({},t),{},{path:f.path,offset:f.offset})]:[];case"remove_text":return function(t,r){var n=t.offset+t.text.length;if("set_selection"!==r.type&&e.Path.equals(r.path,t.path)){if("insert_text"===r.type&&r.offset>t.offset&&r.offset<n){var o=r.offset-t.offset;return[g(g({},t),{},{text:t.text.slice(0,o)}),g(g({},t),{},{offset:t.offset+r.text.length,text:t.text.slice(o)})]}if("remove_text"===r.type){for(var i=r.offset,a=r.offset+r.text.length,s="",f=t.offset;f<n;f++)(f<i||f>=a)&&(s+=t.text[f-t.offset]);if(!s)return[];var u=t.offset-Math.max(0,Math.min(t.offset,a)-i);return[g(g({},t),{},{offset:u,text:s})]}if("split_node"===r.type&&r.position>t.offset&&r.position<n){var l=r.position-t.offset;return[g(g({},t),{},{text:t.text.slice(0,l)}),g(g({},t),{},{path:e.Path.next(t.path),offset:0,text:t.text.slice(l)})]}}var p=w({path:t.path,offset:t.offset},r,"after");return p?[g(g({},t),{},{path:p.path,offset:p.offset})]:[]}(t,r);case"split_node":if(!x(t.path,r))return[];var u=w({path:t.path,offset:t.position},r,"before");return u?[g(g({},t),{},{path:u.path,position:u.offset})]:null;case"merge_node":var l=x(t.path,r);if(!l)return null;var p=w({path:e.Path.previous(t.path),offset:t.position},r,"after");return p&&e.Path.equals(p.path,e.Path.previous(l))?[g(g({},t),{},{path:l,position:p.offset})]:null}},P=function(t,e,r){if(0===t.length||0===e.length)return[t,e];if(t.length>1){var n=P([t[0]],e,r);if(!n)return null;var o=P(t.slice(1),n[1],r);return o?[n[0].concat(o[0]),o[1]]:null}if(e.length>1){var i=P(t,[e[0]],r);if(!i)return null;var a=P(i[0],e.slice(1),r);return a?[a[0],i[1].concat(a[1])]:null}var s=_(t[0],e[0],r),f=_(e[0],t[0],function(t){return"after"===t?"before":"after"}(r));return s&&f?[s,f]:null},S=function(t,r){var n,o=t,i=y(r);try{for(i.s();!(n=i.n()).done;){var a=n.value;if(!o)return null;o=e.Range.transform(o,a,{affinity:"inward"})}}catch(t){i.e(t)}finally{i.f()}return o},M=function(t,e){return t.length===e.length&&t.every((function(t,r){return t===e[r]}))},j=function(t,r){!function(t,e){for(var r=[e],n=t.length-1;n>=0&&r.length;n--){var o=t[n],i=P(o.operations,r,"after");if(!i||!i[0].length)return void t.splice(0,n+1);o.selectionBefore=S(o.selectionBefore,r),M(i[0],o.operations)||(o.operations=i[0]),r=i[1]}}(t.redos,r),function(t,r){for(var n=[r],o=t.length-1;o>=0&&n.length;o--){var i=t[o],a=i.operations.map(e.Operation.inverse).reverse(),s=P(a,n,"after");s?(n=s[1],s[0].length?(M(s[0],a)||(i.operations=s[0].map(e.Operation.inverse).reverse()),i.selectionBefore=S(i.selectionBefore,n)):t.splice(o,1)):(n=i.operations.concat(n),t.splice(o,1))}}(t.undos,r)};function A(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!r){if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return E(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?E(t,e):void 0}}(t))||e){r&&(t=r);var n=0,o=function(){};return{s:o,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},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,a=!0,s=!1;return{s:function(){r=r.call(t)},n:function(){var t=r.next();return a=t.done,t},e:function(t){s=!0,i=t},f:function(){try{a||null==r.return||r.return()}finally{if(s)throw i}}}}function E(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}var H=function(t,r){return!(!r||"insert_text"!==t.type||"insert_text"!==r.type||t.offset!==r.offset+r.text.length||!e.Path.equals(t.path,r.path))||!(!r||"remove_text"!==t.type||"remove_text"!==r.type||t.offset+t.text.length!==r.offset||!e.Path.equals(t.path,r.path))},B=function(t,e){return"set_selection"!==t.type};t.HISTORY=n,t.History=r,t.HistoryEditor=s,t.MERGING=i,t.SAVING=o,t.SPLITTING_ONCE=a,t.withHistory=function(t){var r=t,n=r.apply,o=r.normalize,i=r.setChildren,a=!0,f=!1;return r.history={undos:[],redos:[]},r.redo=function(){var t=r.history,n=t.redos;if(n.length>0){var o=n[n.length-1];o.selectionBefore&&e.Transforms.select(r,o.selectionBefore),f=!0;try{s.withoutSaving(r,(function(){e.Editor.withoutNormalizing(r,(function(){var t,e=A(o.operations);try{for(e.s();!(t=e.n()).done;){var n=t.value;r.apply(n)}}catch(t){e.e(t)}finally{e.f()}}))}))}finally{f=!1}t.redos.pop(),r.writeHistory("undos",o)}},r.undo=function(){var t=r.history,n=t.undos;if(n.length>0){var o=n[n.length-1];f=!0;try{s.withoutSaving(r,(function(){e.Editor.withoutNormalizing(r,(function(){var t,n=A(o.operations.map(e.Operation.inverse).reverse());try{for(n.s();!(t=n.n()).done;){var i=t.value;r.apply(i)}}catch(t){n.e(t)}finally{n.f()}o.selectionBefore&&e.Transforms.select(r,o.selectionBefore)}))}))}finally{f=!1}r.writeHistory("redos",o),t.undos.pop()}},r.apply=function(t){var e=r.operations,o=r.history,i=o.undos,u=i[i.length-1],l=u&&u.operations[u.operations.length-1],p=s.isSaving(r),c=s.isMerging(r);if(null==p&&(p=B(t)),p){if(null==c&&(c=null!=u&&(!!e.includes(l)||H(t,l))),!c&&!a&&u&&e.includes(l)&&(c=!0),s.isSplittingOnce(r)&&(c=!1,s.setSplittingOnce(r,void 0)),u&&c)u.operations.push(t);else{var h={operations:[t],selectionBefore:r.selection};r.writeHistory("undos",h)}for(;i.length>100;)i.shift();o.redos=[]}else f||"set_selection"===t.type||j(o,t);a=!1,n(t)},r.normalize=function(t){o(t),e.Editor.isNormalizing(r)&&(a=!0)},r.setChildren=function(t){var e=r.history.undos[r.history.undos.length-1],n=null==e?void 0:e.operations.length;s.withNewBatch(r,(function(){return i(t)}));var o=r.history.undos[r.history.undos.length-1];o===e&&(null==o?void 0:o.operations.length)===n||s.setSplittingOnce(r,!0)},r.writeHistory=function(t,e){r.history[t].push(e)},r}}));