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 8d9f2f1e843c73a2a22169dded704269caf8e2d7
parent 85abe05c11947cfc1e7da7559edad0e500eeb90f
Author: unitehenry <unitehenry@gmail.com>
Date:   Thu, 31 Oct 2019 10:37:24 -0400

set range

Diffstat:
Msrc/striveneditor.js | 24+++++++++++++++---------
1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/src/striveneditor.js b/src/striveneditor.js @@ -178,8 +178,7 @@ export default class StrivenEditor { this.body.focus(); - window.getSelection().removeAllRanges(); - window.getSelection().addRange(this.range); + this.setRange(); const command = optionEl.id.split("-")[1]; @@ -529,8 +528,7 @@ export default class StrivenEditor { const bodyFocus = body.onfocus; body.onfocus = () => { - window.getSelection().removeAllRanges(); - window.getSelection().addRange(this.range); + this.setRange(); this.toolbarState(); bodyFocus && bodyFocus(); } @@ -580,8 +578,7 @@ export default class StrivenEditor { linkMenuButton.onclick = e => { const linkValue = linkMenuFormInput.value; - window.getSelection().removeAllRanges(); - window.getSelection().addRange(this.range); + this.setRange(); if (linkValue) { @@ -603,7 +600,7 @@ export default class StrivenEditor { } // insert link on no selection - if (this.browser.isFirefox && this.range.startOffset === this.range.endOffset) { + if (this.browser.isFirefox() && this.range.startOffset === this.range.endOffset) { const link = document.createElement('a'); link.href = linkValue; link.textContent = linkValue; @@ -702,8 +699,7 @@ export default class StrivenEditor { const widthValue = imageMenuWidthFormInput.value; if (linkValue) { - window.getSelection().removeAllRanges(); - window.getSelection().addRange(this.range); + this.setRange(); if (this.browser.isFirefox() || this.browser.isEdge()) { document.execCommand("insertImage", false, linkValue) } else { document.execCommand("insertImage", true, linkValue) } @@ -1165,6 +1161,16 @@ export default class StrivenEditor { this.body.innerHTML = html; } + setRange(range) { + if(range) { + window.getSelection().removeAllRanges(); + window.getSelection().addRange(range); + } else { + window.getSelection().removeAllRanges(); + window.getSelection().addRange(this.range); + } + } + clearContent() { this.body.innerHTML = ""; }