striven-editor

Maintained and contributed to an open source WYSIWYG editor as an internship project that made it to production and was adopted by the company I was working at.
git clone https://unitehenry.com/git/striven-editor.git
Log | Files | Refs | README | LICENSE

commit 048938ab66fcf0b2ce1d38cb685300937dd6d842
parent 94dd2d3c97ea7c31ae802b67f0a5a4499190b756
Author: unitehenry <unitehenry@gmail.com>
Date:   Thu, 31 Oct 2019 11:55:51 -0400

reset editor toolbar state when content is removed

Diffstat:
Msrc/striveneditor.js | 31+++++++++++++++++++++----------
1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/src/striveneditor.js b/src/striveneditor.js @@ -253,10 +253,10 @@ export default class StrivenEditor { } // keep enabled options on initial selection - if(!this.body.innerHTML) { + if (!this.body.textContent) { this.toolbarOptions.forEach((opt) => { const path = opt.getElementsByTagName('path')[0]; - if(path.getAttribute('fill') === this.options.activeOptionColor) { + if (path.getAttribute('fill') === this.options.activeOptionColor) { document.execCommand(opt.id.split('-')[1], true); } }) @@ -521,13 +521,24 @@ export default class StrivenEditor { this.range = this.getRange(); - if (this.options.onEnter && e.key === 'Enter') { - - this.options.onEnter(e); - + switch (e.key) { + case 'Enter': + this.options.onEnter && this.options.onEnter(e); + break; + case 'Backspace': + // reset the toolbar state of the editor + if (!body.textContent) { + body.blur(); + setTimeout(() => { + body.focus(); + this.toolbarState(); + }, 100) + } + default: + break; } - body.innerHTML && this.toolbarState(); + body.textContent && this.toolbarState(); } const bodyMouseUp = body.onmouseup; @@ -539,13 +550,13 @@ export default class StrivenEditor { const bodyFocus = body.onfocus; body.onfocus = () => { this.setRange(); - body.innerHTML && this.toolbarState(); + body.textContent && this.toolbarState(); bodyFocus && bodyFocus(); } const bodyBlur = body.onblur; body.onblur = () => { - body.innerHTML && this.toolbarState(); + body.textContent && this.toolbarState(); bodyBlur && bodyBlur(); } @@ -1172,7 +1183,7 @@ export default class StrivenEditor { } setRange(range) { - if(range) { + if (range) { window.getSelection().removeAllRanges(); window.getSelection().addRange(range); } else {