editorjs-undo
Advanced tools
Comparing version 2.0.0-rc.1 to 2.0.0-rc.2
# Changelog | ||
## 2.0.0-rc.2 (28.02.2022) | ||
* Optimizes the observer, saving with the EditorJS API only when the content changes. | ||
## 2.0.0-rc.1 (24.02.2022) | ||
@@ -4,0 +7,0 @@ * Set the caret feature to the undo and redo actions. |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Undo=e():t.Undo=e()}(self,(function(){return(()=>{var t={571:function(t,e){var n,i;n=function(t){"use strict";var e=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}(),n=function(){function t(e){(function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")})(this,t),this.target=e,this.isContentEditable=e&&e.contentEditable}return e(t,[{key:"getPos",value:function(){if(document.activeElement!==this.target)return-1;if(this.isContentEditable){this.target.focus();var t=document.getSelection().getRangeAt(0),e=t.cloneRange();return e.selectNodeContents(this.target),e.setEnd(t.endContainer,t.endOffset),e.toString().length}return this.target.selectionStart}},{key:"setPos",value:function(t){if(this.isContentEditable){if(t>=0){var e=window.getSelection(),n=this.createRange(this.target,{count:t});n&&(n.collapse(!1),e.removeAllRanges(),e.addRange(n))}}else this.target.setSelectionRange(t,t)}},{key:"createRange",value:function(t,e,n){if(n||((n=document.createRange()).selectNode(t),n.setStart(t,0)),0===e.count)n.setEnd(t,e.count);else if(t&&e.count>0)if(t.nodeType===Node.TEXT_NODE)t.textContent.length<e.count?e.count-=t.textContent.length:(n.setEnd(t,e.count),e.count=0);else for(var i=0;i<t.childNodes.length&&(n=this.createRange(t.childNodes[i],e,n),0!==e.count);i++);return n}}]),t}();t.exports=n},void 0===(i=n.apply(e,[t]))||(t.exports=i)},334:(t,e,n)=>{t.exports=n(571)}},e={};function n(i){var s=e[i];if(void 0!==s)return s.exports;var o=e[i]={exports:{}};return t[i].call(o.exports,o,o.exports,n),o.exports}n.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return n.d(e,{a:e}),e},n.d=(t,e)=>{for(var i in e)n.o(e,i)&&!n.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);var i={};return(()=>{"use strict";n.d(i,{default:()=>a});var t=n(334),e=n.n(t);function s(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}var o=function(){function t(e,n){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.holder=n,this.observer=null,this.debounceTimer=200,this.mutationDebouncer=this.debounce((function(){e()}),this.debounceTimer)}var e,n;return e=t,n=[{key:"setMutationObserver",value:function(){var t=this,e=this.holder;this.observer=new MutationObserver((function(e){t.mutationHandler(e)})),this.observer.observe(e,{childList:!0,attributes:!0,subtree:!0,characterData:!0,characterDataOldValue:!0})}},{key:"mutationHandler",value:function(t){var e=this,n=!1;t.forEach((function(t){switch(t.type){case"childList":t.target===e.holder?e.onDestroy():n=!0;break;case"characterData":n=!0;break;case"attributes":t.target.classList.contains("ce-block")||(n=!0)}})),n&&this.mutationDebouncer()}},{key:"debounce",value:function(t,e){var n,i=this;return function(){for(var s=arguments.length,o=new Array(s),r=0;r<s;r++)o[r]=arguments[r];var a=i;clearTimeout(n),n=setTimeout((function(){return t.apply(a,o)}),e)}}},{key:"onDestroy",value:function(){var t=new CustomEvent("destroy");document.dispatchEvent(t),this.observer.disconnect()}}],n&&s(e.prototype,n),t}();function r(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}var a=function(){function t(e){var n=this,i=e.editor,s=e.config,r=void 0===s?{}:s,a=e.onUpdate,c=e.maxLength;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t);var l={maxLength:30,onUpdate:function(){},config:{shortcuts:{undo:"CMD+Z",redo:"CMD+Y"}}},u=i.blocks,h=i.caret,d=i.configuration,f=d.holder,v=l.config.shortcuts,p=r.shortcuts,k=void 0===p?v:p;this.holder="string"==typeof f?document.getElementById(f):f,this.editor=i,this.blocks=u,this.caret=h,this.shouldSaveHistory=!0,this.readOnly=d.readOnly,this.maxLength=c||l.maxLength,this.onUpdate=a||l.onUpdate,this.config={shortcuts:k},new o((function(){return n.registerChange()}),this.holder).setMutationObserver(),this.setEventListeners(),this.initialItem=null,this.clear()}var n,i,s;return n=t,s=[{key:"isReadOnlySupported",get:function(){return!0}}],(i=[{key:"truncate",value:function(t,e){for(;t.length>e;)t.shift()}},{key:"initialize",value:function(t){var e="blocks"in t?t.blocks:t,n={index:e.length-1,state:e};this.stack[0]=n,this.initialItem=n}},{key:"clear",value:function(){this.stack=this.initialItem?[this.initialItem]:[{index:0,state:[{type:"paragraph",data:{text:""}}]}],this.position=0,this.onUpdate()}},{key:"setReadOnly",value:function(){var t=document.querySelector(".ce-toolbox");this.readOnly=!t}},{key:"registerChange",value:function(){var t=this;this.setReadOnly(),this.readOnly||(this.editor&&this.editor.save&&this.shouldSaveHistory&&this.editor.save().then((function(e){t.editorDidUpdate(e.blocks)&&t.save(e.blocks)})),this.shouldSaveHistory=!0)}},{key:"editorDidUpdate",value:function(t){var e=this.stack[this.position].state;return!!t.length&&(t.length!==e.length||JSON.stringify(e)!==JSON.stringify(t))}},{key:"save",value:function(t){this.position>=this.maxLength&&this.truncate(this.stack,this.maxLength),this.position=Math.min(this.position,this.stack.length-1),this.stack=this.stack.slice(0,this.position+1);var e=this.blocks.getCurrentBlockIndex(),n="paragraph"===t[e].type||"header"===t[e].type?this.getCaretIndex(e):null;this.stack.push({index:e,state:t,caretIndex:n}),this.position+=1,this.onUpdate()}},{key:"getCaretIndex",value:function(t){var n=document.getElementsByClassName("ce-block__content");return new(e())(n[t].firstChild).getPos()}},{key:"insertDeletedBlock",value:function(t,e,n){for(var i=0;i<t.length;i+=1)if(!e[i]||t[i].id!==e[i].id){this.blocks.insert(t[i].type,t[i].data,{},i,!0),this.caret.setToBlock(n,"end");break}}},{key:"blockWasDropped",value:function(t,e){return t.length===e.length&&t.some((function(t,n){return t.id!==e[n].id}))}},{key:"blockWasSkipped",value:function(t,e,n,i){return t<e||n.length!==i.length}},{key:"blockWasDeleted",value:function(t,e){return t.length>e.length}},{key:"undo",value:function(){var t=this;if(this.canUndo()){this.shouldSaveHistory=!1;var e=this.stack[this.position-=1].index,n=this.stack[this.position],i=n.state,s=n.caretIndex,o=this.stack[this.position+1].index,r=this.stack[this.position+1].state;if(this.onUpdate(),i[e]||(e-=1,this.stack[this.position].index=e),this.blockWasDeleted(i,r))return void this.insertDeletedBlock(i,r,e);if(this.blockWasSkipped(e,o,i,r)&&0!==this.position)return this.blocks.delete(),void this.caret.setToBlock(e,"end");if(this.blockWasDropped(i,r)&&0!==this.position)return void this.blocks.render({blocks:i}).then((function(){return t.caret.setToBlock(e,"end")}));var a=this.blocks.getBlockByIndex(e).id;this.blocks.update(a,i[e].data),this.setCaretIndex(e,s)}}},{key:"setCaretIndex",value:function(t,n){if(n){var i=document.getElementsByClassName("ce-block__content");new(e())(i[t].firstChild).setPos(n)}else this.caret.setToBlock(t,"end")}},{key:"redo",value:function(){var t=this;if(this.canRedo()){this.shouldSaveHistory=!1;var e=this.stack[this.position+=1],n=e.index,i=e.state,s=e.caretIndex,o=this.stack[this.position-1].index,r=this.stack[this.position-1].state;if(this.blockWasDeleted(r,i))return this.blocks.delete(),void this.caret.setToBlock(n,"end");if(this.blockWasSkipped(o,n,i,r))return this.blocks.insert(i[n].type,i[n].data,{},n,!0),void this.caret.setToBlock(n,"end");if(this.blockWasDropped(i,r)&&1!==this.position)return void this.blocks.render({blocks:i}).then((function(){return t.caret.setToBlock(n,"end")}));this.onUpdate();var a=(this.blocks.getBlockByIndex(n)||i[n]).id;this.blocks.update(a,i[n].data),this.setCaretIndex(n,s)}}},{key:"canUndo",value:function(){return!this.readOnly&&this.position>0}},{key:"canRedo",value:function(){return!this.readOnly&&this.position<this.count()}},{key:"count",value:function(){return this.stack.length-1}},{key:"parseKeys",value:function(t){var e={CMD:/(Mac)/i.test(navigator.platform)?"metaKey":"ctrlKey",ALT:"altKey",SHIFT:"shiftKey"},n=t.slice(0,-1).map((function(t){return e[t]})),i=n.includes("shiftKey")?t[t.length-1].toUpperCase():t[t.length-1].toLowerCase();return n.push(i),n}},{key:"setEventListeners",value:function(){var t=this,e=this.holder,n=this.config.shortcuts,i=n.undo,s=n.redo,o=i.replace(/ /g,"").split("+"),r=s.replace(/ /g,"").split("+"),a=this.parseKeys(o),c=this.parseKeys(r),l=function(t,e){return!(2!==e.length||!t[e[0]]||t.key!==e[1])||!(3!==e.length||!t[e[0]]||!t[e[1]]||t.key!==e[2])},u=function(e){l(e,a)&&(e.preventDefault(),t.undo())},h=function(e){l(e,c)&&(e.preventDefault(),t.redo())};e.addEventListener("keydown",u),e.addEventListener("keydown",h),e.addEventListener("destroy",(function(){e.removeEventListener("keydown",u),e.removeEventListener("keydown",h)}))}}])&&r(n.prototype,i),s&&r(n,s),t}()})(),i.default})()})); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Undo=e():t.Undo=e()}(self,(function(){return(()=>{var t={571:function(t,e){var n,i;n=function(t){"use strict";var e=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}(),n=function(){function t(e){(function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")})(this,t),this.target=e,this.isContentEditable=e&&e.contentEditable}return e(t,[{key:"getPos",value:function(){if(document.activeElement!==this.target)return-1;if(this.isContentEditable){this.target.focus();var t=document.getSelection().getRangeAt(0),e=t.cloneRange();return e.selectNodeContents(this.target),e.setEnd(t.endContainer,t.endOffset),e.toString().length}return this.target.selectionStart}},{key:"setPos",value:function(t){if(this.isContentEditable){if(t>=0){var e=window.getSelection(),n=this.createRange(this.target,{count:t});n&&(n.collapse(!1),e.removeAllRanges(),e.addRange(n))}}else this.target.setSelectionRange(t,t)}},{key:"createRange",value:function(t,e,n){if(n||((n=document.createRange()).selectNode(t),n.setStart(t,0)),0===e.count)n.setEnd(t,e.count);else if(t&&e.count>0)if(t.nodeType===Node.TEXT_NODE)t.textContent.length<e.count?e.count-=t.textContent.length:(n.setEnd(t,e.count),e.count=0);else for(var i=0;i<t.childNodes.length&&(n=this.createRange(t.childNodes[i],e,n),0!==e.count);i++);return n}}]),t}();t.exports=n},void 0===(i=n.apply(e,[t]))||(t.exports=i)},334:(t,e,n)=>{t.exports=n(571)}},e={};function n(i){var o=e[i];if(void 0!==o)return o.exports;var s=e[i]={exports:{}};return t[i].call(s.exports,s,s.exports,n),s.exports}n.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return n.d(e,{a:e}),e},n.d=(t,e)=>{for(var i in e)n.o(e,i)&&!n.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);var i={};return(()=>{"use strict";n.d(i,{default:()=>a});var t=n(334),e=n.n(t);function o(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}var s=function(){function t(e,n){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.holder=n,this.observer=null,this.debounceTimer=200,this.mutationDebouncer=this.debounce((function(){e()}),this.debounceTimer)}var e,n;return e=t,n=[{key:"setMutationObserver",value:function(){var t=this,e=this.holder.querySelector(".codex-editor__redactor");this.observer=new MutationObserver((function(e){t.mutationHandler(e)})),this.observer.observe(e,{childList:!0,attributes:!0,subtree:!0,characterData:!0,characterDataOldValue:!0})}},{key:"mutationHandler",value:function(t){var e=this,n=!1;t.forEach((function(t){switch(t.type){case"childList":t.target===e.holder?e.onDestroy():n=!0;break;case"characterData":n=!0;break;case"attributes":t.target.classList.contains("ce-block")||(n=!0)}})),n&&this.mutationDebouncer()}},{key:"debounce",value:function(t,e){var n,i=this;return function(){for(var o=arguments.length,s=new Array(o),r=0;r<o;r++)s[r]=arguments[r];var a=i;clearTimeout(n),n=setTimeout((function(){return t.apply(a,s)}),e)}}},{key:"onDestroy",value:function(){var t=new CustomEvent("destroy");document.dispatchEvent(t),this.observer.disconnect()}}],n&&o(e.prototype,n),t}();function r(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}var a=function(){function t(e){var n=this,i=e.editor,o=e.config,r=void 0===o?{}:o,a=e.onUpdate,c=e.maxLength;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t);var l={maxLength:30,onUpdate:function(){},config:{shortcuts:{undo:"CMD+Z",redo:"CMD+Y"}}},u=i.blocks,h=i.caret,d=i.configuration,f=d.holder,v=l.config.shortcuts,p=r.shortcuts,k=void 0===p?v:p;this.holder="string"==typeof f?document.getElementById(f):f,this.editor=i,this.blocks=u,this.caret=h,this.shouldSaveHistory=!0,this.readOnly=d.readOnly,this.maxLength=c||l.maxLength,this.onUpdate=a||l.onUpdate,this.config={shortcuts:k},new s((function(){return n.registerChange()}),this.holder).setMutationObserver(),this.setEventListeners(),this.initialItem=null,this.clear()}var n,i,o;return n=t,o=[{key:"isReadOnlySupported",get:function(){return!0}}],(i=[{key:"truncate",value:function(t,e){for(;t.length>e;)t.shift()}},{key:"initialize",value:function(t){var e="blocks"in t?t.blocks:t,n={index:e.length-1,state:e};this.stack[0]=n,this.initialItem=n}},{key:"clear",value:function(){this.stack=this.initialItem?[this.initialItem]:[{index:0,state:[{type:"paragraph",data:{text:""}}]}],this.position=0,this.onUpdate()}},{key:"setReadOnly",value:function(){var t=document.querySelector(".ce-toolbox");this.readOnly=!t}},{key:"registerChange",value:function(){var t=this;this.setReadOnly(),this.readOnly||(this.editor&&this.editor.save&&this.shouldSaveHistory&&this.editor.save().then((function(e){t.editorDidUpdate(e.blocks)&&t.save(e.blocks)})),this.shouldSaveHistory=!0)}},{key:"editorDidUpdate",value:function(t){var e=this.stack[this.position].state;return!!t.length&&(t.length!==e.length||JSON.stringify(e)!==JSON.stringify(t))}},{key:"save",value:function(t){this.position>=this.maxLength&&this.truncate(this.stack,this.maxLength),this.position=Math.min(this.position,this.stack.length-1),this.stack=this.stack.slice(0,this.position+1);var e=this.blocks.getCurrentBlockIndex(),n="paragraph"===t[e].type||"header"===t[e].type?this.getCaretIndex(e):null;this.stack.push({index:e,state:t,caretIndex:n}),this.position+=1,this.onUpdate()}},{key:"getCaretIndex",value:function(t){var n=document.getElementsByClassName("ce-block__content");return new(e())(n[t].firstChild).getPos()}},{key:"insertDeletedBlock",value:function(t,e,n){for(var i=0;i<t.length;i+=1)if(!e[i]||t[i].id!==e[i].id){this.blocks.insert(t[i].type,t[i].data,{},i,!0),this.caret.setToBlock(n,"end");break}}},{key:"blockWasDropped",value:function(t,e){return t.length===e.length&&t.some((function(t,n){return t.id!==e[n].id}))}},{key:"blockWasSkipped",value:function(t,e,n,i){return t<e||n.length!==i.length}},{key:"blockWasDeleted",value:function(t,e){return t.length>e.length}},{key:"undo",value:function(){var t=this;if(this.canUndo()){this.shouldSaveHistory=!1;var e=this.stack[this.position-=1].index,n=this.stack[this.position],i=n.state,o=n.caretIndex,s=this.stack[this.position+1].index,r=this.stack[this.position+1].state;if(this.onUpdate(),i[e]||(e-=1,this.stack[this.position].index=e),this.blockWasDeleted(i,r))return void this.insertDeletedBlock(i,r,e);if(this.blockWasSkipped(e,s,i,r)&&0!==this.position)return this.blocks.delete(),void this.caret.setToBlock(e,"end");if(this.blockWasDropped(i,r)&&0!==this.position)return void this.blocks.render({blocks:i}).then((function(){return t.caret.setToBlock(e,"end")}));var a=this.blocks.getBlockByIndex(e).id;this.blocks.update(a,i[e].data),this.setCaretIndex(e,o)}}},{key:"setCaretIndex",value:function(t,n){if(n){var i=document.getElementsByClassName("ce-block__content");new(e())(i[t].firstChild).setPos(n)}else this.caret.setToBlock(t,"end")}},{key:"redo",value:function(){var t=this;if(this.canRedo()){this.shouldSaveHistory=!1;var e=this.stack[this.position+=1],n=e.index,i=e.state,o=e.caretIndex,s=this.stack[this.position-1].index,r=this.stack[this.position-1].state;if(this.blockWasDeleted(r,i))return this.blocks.delete(),void this.caret.setToBlock(n,"end");if(this.blockWasSkipped(s,n,i,r))return this.blocks.insert(i[n].type,i[n].data,{},n,!0),void this.caret.setToBlock(n,"end");if(this.blockWasDropped(i,r)&&1!==this.position)return void this.blocks.render({blocks:i}).then((function(){return t.caret.setToBlock(n,"end")}));this.onUpdate();var a=(this.blocks.getBlockByIndex(n)||i[n]).id;this.blocks.update(a,i[n].data),this.setCaretIndex(n,o)}}},{key:"canUndo",value:function(){return!this.readOnly&&this.position>0}},{key:"canRedo",value:function(){return!this.readOnly&&this.position<this.count()}},{key:"count",value:function(){return this.stack.length-1}},{key:"parseKeys",value:function(t){var e={CMD:/(Mac)/i.test(navigator.platform)?"metaKey":"ctrlKey",ALT:"altKey",SHIFT:"shiftKey"},n=t.slice(0,-1).map((function(t){return e[t]})),i=n.includes("shiftKey")?t[t.length-1].toUpperCase():t[t.length-1].toLowerCase();return n.push(i),n}},{key:"setEventListeners",value:function(){var t=this,e=this.holder,n=this.config.shortcuts,i=n.undo,o=n.redo,s=i.replace(/ /g,"").split("+"),r=o.replace(/ /g,"").split("+"),a=this.parseKeys(s),c=this.parseKeys(r),l=function(t,e){return!(2!==e.length||!t[e[0]]||t.key!==e[1])||!(3!==e.length||!t[e[0]]||!t[e[1]]||t.key!==e[2])},u=function(e){l(e,a)&&(e.preventDefault(),t.undo())},h=function(e){l(e,c)&&(e.preventDefault(),t.redo())};e.addEventListener("keydown",u),e.addEventListener("keydown",h),e.addEventListener("destroy",(function(){e.removeEventListener("keydown",u),e.removeEventListener("keydown",h)}))}}])&&r(n.prototype,i),o&&r(n,o),t}()})(),i.default})()})); |
{ | ||
"name": "editorjs-undo", | ||
"version": "2.0.0-rc.1", | ||
"version": "2.0.0-rc.2", | ||
"keywords": [ | ||
@@ -5,0 +5,0 @@ "undo", |
@@ -14,3 +14,3 @@ ![](https://badgen.net/badge/Editor.js/v2.0/blue) | ||
This new version 2.0.0-rc.0 introduces breaking changes. Remember that this is a beta version. | ||
This new version 2.0.0-rc.2 introduces breaking changes. Remember that this is a beta version. | ||
@@ -23,2 +23,5 @@ Please, report bugs or behavior issues :). | ||
* Solves issues in documents with several images, for example the blink with undo/redo. | ||
* Sets the caret in the respective position when the user is typing inside an existing text. | ||
* Adds support to ReadOnly toggle. | ||
* Optimizes the observer, saving with the EditorJS API only when the content changes. | ||
@@ -28,3 +31,3 @@ Get the package | ||
```shell | ||
$ npm i --save-dev editorjs-undo@2.0.0-rc.0 | ||
$ npm i --save-dev editorjs-undo@2.0.0-rc.2 | ||
``` | ||
@@ -31,0 +34,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29193
180