From 509bfbe7497a17d7a1519fce483e6d5ae39ae2f4 Mon Sep 17 00:00:00 2001 From: Juan Manuel Giraldo <84339390+juanmagiraldor@users.noreply.github.com> Date: Tue, 17 Oct 2023 12:06:10 -0500 Subject: [PATCH] Solve undo with custom blocks (#230) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Solve undo with custom blocks Co-authored-by: David Roldán * Solve github endpoint Co-authored-by: David Roldán * Implement defaultBlock in initialData Co-authored-by: Edwin Steven Guayacan --------- Co-authored-by: David Roldán Co-authored-by: Mario Rodriguez <64106728+MarioRodriguezS@users.noreply.github.com> Co-authored-by: Edwin Steven Guayacan --- .github/workflows/codeql.yml | 1 + dist/bundle.js | 2 +- src/index.js | 9 +++++---- test/fixtures/editor.js | 4 +++- test/undo.test.js | 2 +- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index accc179..5b0db6b 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -47,6 +47,7 @@ jobs: egress-policy: block allowed-endpoints: > *.github.com:443 + github.com:443 *.githubapp.com:443 *.githubusercontent.com:443 diff --git a/dist/bundle.js b/dist/bundle.js index 10c81ce..42eb213 100644 --- a/dist/bundle.js +++ b/dist/bundle.js @@ -1 +1 @@ -!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Undo=t():e.Undo=t()}(self,(()=>(()=>{var e={571:function(e,t){var n,i;n=function(e){"use strict";var t=function(){function e(e,t){for(var n=0;n=0){var t=window.getSelection(),n=this.createRange(this.target,{count:e});n&&(n.collapse(!1),t.removeAllRanges(),t.addRange(n))}}else this.target.setSelectionRange(e,e)}},{key:"createRange",value:function(e,t,n){if(n||((n=document.createRange()).selectNode(e),n.setStart(e,0)),0===t.count)n.setEnd(e,t.count);else if(e&&t.count>0)if(e.nodeType===Node.TEXT_NODE)e.textContent.length{e.exports=n(571)}},t={};function n(i){var o=t[i];if(void 0!==o)return o.exports;var r=t[i]={exports:{}};return e[i].call(r.exports,r,r.exports,n),r.exports}n.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return n.d(t,{a:t}),t},n.d=(e,t)=>{for(var i in t)n.o(t,i)&&!n.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:t[i]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var i={};return(()=>{"use strict";n.d(i,{default:()=>u});var e=n(334),t=n.n(e);function o(e,t){for(var n=0;nt;)e.shift()}},{key:"initialize",value:function(e){var t="blocks"in e?e.blocks:e,n={index:t.length-1,state:t};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 e=this.holder.querySelector(".ce-toolbox");this.readOnly=!e}},{key:"registerChange",value:function(){var e=this;this.setReadOnly(),this.readOnly||(this.editor&&this.editor.save&&this.shouldSaveHistory&&this.editor.save().then((function(t){e.editorDidUpdate(t.blocks)&&e.save(t.blocks)})),this.shouldSaveHistory=!0)}},{key:"editorDidUpdate",value:function(e){var t=this.stack[this.position].state;return!!e.length&&(e.length!==t.length||JSON.stringify(t)!==JSON.stringify(e))}},{key:"save",value:function(e){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 t=this.blocks.getCurrentBlockIndex(),n=this.blocks.getBlocksCount(),i=t;e[t]||(i-=n-e.length);var o=!e[i]||"paragraph"!==e[i].type&&"header"!==e[i].type?null:this.getCaretIndex(t);this.stack.push({index:i,state:e,caretIndex:o}),this.position+=1,this.onUpdate()}},{key:"getCaretIndex",value:function(e){var n=this.holder.getElementsByClassName("ce-block__content");return new(t())(n[e].firstChild).getPos()}},{key:"insertDeletedBlock",value:function(e,t,n){for(var i=0;it.length}},{key:"undo",value:function(){var e=this;if(this.canUndo()){var t=this.stack[this.position],n=t.index,i=t.state;this.position-=1,this.shouldSaveHistory=!1;var o=this.stack[this.position].index,r=this.stack[this.position],s=r.state,a=r.caretIndex;this.onUpdate();var c=this.blocks.getBlocksCount();if(s[o]||(o-=1,this.stack[this.position].index=o),this.blockWasDeleted(s,i))this.insertDeletedBlock(s,i,o);else if(this.blockWasSkipped(o,n,s,i))this.blocks.delete(),this.caret.setToBlock(o,"end");else if(c>s.length)this.blocks.render({blocks:s}).then((function(){e.editor.blocks.insert("paragraph",{text:""}),e.caret.setToLastBlock("end")}));else if(this.blockWasDropped(s,i))this.blocks.render({blocks:s}).then((function(){return e.caret.setToBlock(o,"end")}));else if(this.contentChangedInNoFocusBlock(o,n)){var l=this.blocks.getBlockByIndex(n).id;this.blocks.update(l,s[n].data),this.setCaretIndex(o,a)}var u=this.blocks.getBlockByIndex(o);u&&(this.blocks.update(u.id,s[o].data),this.setCaretIndex(o,a))}}},{key:"setCaretIndex",value:function(e,n){if(n&&-1!==n){var i=this.holder.getElementsByClassName("ce-block__content");new(t())(i[e].firstChild).setPos(n)}else this.caret.setToBlock(e,"end")}},{key:"insertBlock",value:function(e,t){this.blocks.insert(e[t].type,e[t].data,{},t,!0)}},{key:"insertSkippedBlocks",value:function(e,t){for(var n=e;n0}},{key:"canRedo",value:function(){return!this.readOnly&&this.position(()=>{var e={571:function(e,t){var n,i;n=function(e){"use strict";var t=function(){function e(e,t){for(var n=0;n=0){var t=window.getSelection(),n=this.createRange(this.target,{count:e});n&&(n.collapse(!1),t.removeAllRanges(),t.addRange(n))}}else this.target.setSelectionRange(e,e)}},{key:"createRange",value:function(e,t,n){if(n||((n=document.createRange()).selectNode(e),n.setStart(e,0)),0===t.count)n.setEnd(e,t.count);else if(e&&t.count>0)if(e.nodeType===Node.TEXT_NODE)e.textContent.length{e.exports=n(571)}},t={};function n(i){var o=t[i];if(void 0!==o)return o.exports;var r=t[i]={exports:{}};return e[i].call(r.exports,r,r.exports,n),r.exports}n.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return n.d(t,{a:t}),t},n.d=(e,t)=>{for(var i in t)n.o(t,i)&&!n.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:t[i]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var i={};return(()=>{"use strict";n.d(i,{default:()=>u});var e=n(334),t=n.n(e);function o(e,t){for(var n=0;nt;)e.shift()}},{key:"initialize",value:function(e){var t="blocks"in e?e.blocks:e,n={index:t.length-1,state:t};this.stack[0]=n,this.initialItem=n}},{key:"clear",value:function(){this.stack=this.initialItem?[this.initialItem]:[{index:0,state:[{type:this.defaultBlock,data:{}}]}],this.position=0,this.onUpdate()}},{key:"setReadOnly",value:function(){var e=this.holder.querySelector(".ce-toolbox");this.readOnly=!e}},{key:"registerChange",value:function(){var e=this;this.setReadOnly(),this.readOnly||(this.editor&&this.editor.save&&this.shouldSaveHistory&&this.editor.save().then((function(t){e.editorDidUpdate(t.blocks)&&e.save(t.blocks)})),this.shouldSaveHistory=!0)}},{key:"editorDidUpdate",value:function(e){var t=this.stack[this.position].state;return!!e.length&&(e.length!==t.length||JSON.stringify(t)!==JSON.stringify(e))}},{key:"save",value:function(e){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 t=this.blocks.getCurrentBlockIndex(),n=this.blocks.getBlocksCount(),i=t;e[t]||(i-=n-e.length);var o=!e[i]||"paragraph"!==e[i].type&&"header"!==e[i].type?null:this.getCaretIndex(t);this.stack.push({index:i,state:e,caretIndex:o}),this.position+=1,this.onUpdate()}},{key:"getCaretIndex",value:function(e){var n=this.holder.getElementsByClassName("ce-block__content");return new(t())(n[e].firstChild).getPos()}},{key:"insertDeletedBlock",value:function(e,t,n){for(var i=0;it.length}},{key:"undo",value:function(){var e=this;if(this.canUndo()){var t=this.stack[this.position],n=t.index,i=t.state;this.position-=1,this.shouldSaveHistory=!1;var o=this.stack[this.position].index,r=this.stack[this.position],s=r.state,a=r.caretIndex;this.onUpdate();var c=this.blocks.getBlocksCount();if(s[o]||(o-=1,this.stack[this.position].index=o),this.blockWasDeleted(s,i))this.insertDeletedBlock(s,i,o);else if(this.blockWasSkipped(o,n,s,i))this.blocks.delete(),this.caret.setToBlock(o,"end");else if(c>s.length)this.blocks.render({blocks:s}).then((function(){e.editor.blocks.insert(e.defaultBlock,{}),e.caret.setToLastBlock("end")}));else if(this.blockWasDropped(s,i))this.blocks.render({blocks:s}).then((function(){return e.caret.setToBlock(o,"end")}));else if(this.contentChangedInNoFocusBlock(o,n)){var l=this.blocks.getBlockByIndex(n).id;this.blocks.update(l,s[n].data),this.setCaretIndex(o,a)}var u=this.blocks.getBlockByIndex(o);u&&(this.blocks.update(u.id,s[o].data),this.setCaretIndex(o,a))}}},{key:"setCaretIndex",value:function(e,n){if(n&&-1!==n){var i=this.holder.getElementsByClassName("ce-block__content");new(t())(i[e].firstChild).setPos(n)}else this.caret.setToBlock(e,"end")}},{key:"insertBlock",value:function(e,t){this.blocks.insert(e[t].type,e[t].data,{},t,!0)}},{key:"insertSkippedBlocks",value:function(e,t){for(var n=e;n0}},{key:"canRedo",value:function(){return!this.readOnly&&this.position { - this.editor.blocks.insert("paragraph", {text: ""}) - this.caret.setToLastBlock("end") ; + this.editor.blocks.insert(this.defaultBlock, {}); + this.caret.setToLastBlock("end"); }); } else if (this.blockWasDropped(state, nextState)) { this.blocks diff --git a/test/fixtures/editor.js b/test/fixtures/editor.js index 647b3b0..ab809f7 100644 --- a/test/fixtures/editor.js +++ b/test/fixtures/editor.js @@ -17,7 +17,9 @@ const editor = { caret: { setToBlock() {}, }, - configuration: {}, + configuration: { + defaultBlock: 'paragraph', + }, }; const readOnlyEditor = { diff --git a/test/undo.test.js b/test/undo.test.js index 6881770..e760ea4 100644 --- a/test/undo.test.js +++ b/test/undo.test.js @@ -64,7 +64,7 @@ describe('Undo', () => { undo = new Undo({ editor }); }); - const intialStackData = { index: 0, state: [{ type: 'paragraph', data: { text: '' } }] }; + const intialStackData = { index: 0, state: [{ type: 'paragraph', data: {} }] }; it('is unable to perform an undo operation in an empty stack', () => { expect(undo.canUndo()).toBe(false);