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 d5f12752fa0ad658dde1504e4defde5aad5fe5c1
parent cd882f397a28f497ea81b0557c5f5c1475078a6e
Author: Henry Unite <unitehenry@gmail.com>
Date:   Tue,  8 Sep 2020 13:29:08 -0400

clear format error handle

Diffstat:
Msrc/striveneditor.js | 69+++++++++++++++++++++++++++++++++++++++++----------------------------
1 file changed, 41 insertions(+), 28 deletions(-)

diff --git a/src/striveneditor.js b/src/striveneditor.js @@ -287,42 +287,55 @@ export default class StrivenEditor { } break; case 'removeFormat': + + const clear = () => { + const range = this.getRange(); + + const textNode = document.createTextNode( range.toString() ); + range.deleteContents(); + range.insertNode(textNode); + range.selectNodeContents(textNode); + + // Deactivate all toolbar options + this.toolbarOptions.forEach(o => + o.classList.remove('se-toolbar-option-active'), + ); + } - const range = this.getRange(); - - const { commonAncestorContainer } = range; - const { tagName } = commonAncestorContainer; - - if(commonAncestorContainer) { + try { + + const range = this.getRange(); + + const { commonAncestorContainer } = range; + const { tagName } = commonAncestorContainer; - if(tagName && (tagName === 'OL' || tagName === 'UL')) { - commonAncestorContainer.innerHTML = commonAncestorContainer.textContent; - return; + if(commonAncestorContainer) { + + if(tagName && (tagName === 'OL' || tagName === 'UL')) { + commonAncestorContainer.innerHTML = commonAncestorContainer.textContent; + return; + } + + const { parentElement } = commonAncestorContainer; + const formatTags = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6']; + if(formatTags.includes(parentElement.tagName)) { + parentElement.outerHTML = parentElement.textContent; + return; + } + } - const { parentElement } = commonAncestorContainer; - const formatTags = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6']; - if(formatTags.includes(parentElement.tagName)) { - parentElement.outerHTML = parentElement.textContent; - return; + const { previousElementSibling } = textNode; + if(previousElementSibling.tagName === 'OL' || previousElementSibling.tagName === 'UL') { + !previousElementSibling.textContent && previousElementSibling.remove(); } - } - - const textNode = document.createTextNode( range.toString() ); - range.deleteContents(); - range.insertNode(textNode); - range.selectNodeContents(textNode); - - const { previousElementSibling } = textNode; - if(previousElementSibling.tagName === 'OL' || previousElementSibling.tagName === 'UL') { - !previousElementSibling.textContent && previousElementSibling.remove(); + clear(); + + } catch (e) { + clear(); } - // Deactivate all toolbar options - this.toolbarOptions.forEach(o => - o.classList.remove('se-toolbar-option-active'), - ); break; case 'indent': setTimeout(() => this.setRange(this.range), 0);