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 9465f4a58751fe666ac0026025181824ddc27517
parent 6866b0d930da1ae8632249f8a1aa8ddc9a3731ec
Author: Wagner Souza <wagner.souza@accenture.com>
Date:   Mon, 21 Oct 2019 10:17:20 -0300

Improves browser detection by checking UA

Diffstat:
Msrc/striveneditor.js | 89++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 66 insertions(+), 23 deletions(-)

diff --git a/src/striveneditor.js b/src/striveneditor.js @@ -183,13 +183,13 @@ export default class StrivenEditor { switch (command) { case "insertOrderedList": - if (this.isFirefox) { + if (this.browser.isFirefox()) { document.execCommand("indent"); document.execCommand(command); indents(); } - else if (this.isEdge) { + else if (this.browser.isEdge()) { document.execCommand(command); [...document.querySelectorAll('ol')].forEach(ol => ol.style.marginLeft = "40px"); } @@ -199,13 +199,13 @@ export default class StrivenEditor { } break; case "insertUnorderedList": - if (this.isFirefox) { + if (this.browser.isFirefox()) { document.execCommand("indent"); document.execCommand(command); indents(); } - else if (this.isEdge) { + else if (this.browser.isEdge()) { document.execCommand(command); [...document.querySelectorAll('ul')].forEach(ul => ul.style.marginLeft = "40px"); } @@ -245,7 +245,7 @@ export default class StrivenEditor { } break; default: - if (this.isFirefox || this.isEdge) { + if (this.browser.isFirefox() || this.browser.isEdge()) { document.execCommand(command); (command === 'indent') && indents(); } @@ -743,7 +743,7 @@ export default class StrivenEditor { window.getSelection().removeAllRanges(); window.getSelection().addRange(this.range); - if (this.isFirefox || this.isEdge) { + if (this.browser.isFirefox() || this.browser.isEdge()) { document.execCommand("createLink", false, linkValue) } else { @@ -761,7 +761,7 @@ export default class StrivenEditor { } const bodyLinks = this.body.querySelectorAll("a"); - [...bodyLinks].forEach(link => !this.isFirefox && (link.contentEditable = 'false')); + [...bodyLinks].forEach(link => !this.browser.isFirefox() && (link.contentEditable = 'false')); linkMenuFormInput.value = ""; this.closeLinkMenu(); @@ -858,7 +858,7 @@ export default class StrivenEditor { window.getSelection().removeAllRanges(); window.getSelection().addRange(this.range); - if (this.isFirefox || this.isEdge) { document.execCommand("insertImage", false, linkValue) } + if (this.browser.isFirefox() || this.browser.isEdge()) { document.execCommand("insertImage", false, linkValue) } else { document.execCommand("insertImage", true, linkValue) } let insertedImage = [...this.body.querySelectorAll(`img`)].filter(img => img.src === linkValue); @@ -1295,28 +1295,71 @@ export default class StrivenEditor { }) } + /** + * This method is used to detect the user browser and environment + */ establishBrowser() { - // Change this to user agent + const userAgent = (navigator && navigator.userAgent || '').toLowerCase() + const vendor = (navigator && navigator.vendor || '').toLowerCase() + + const comparator = { + '<': function(a, b) { return a < b; }, + '<=': function(a, b) { return a <= b; }, + '>': function(a, b) { return a > b; }, + '>=': function(a, b) { return a >= b; } + }; + + const compareVersion = (version, range) => { + const str = (range + ''); + const n = +(str.match(/\d+/) || NaN); + const op = str.match(/^[<>]=?|/)[0]; + return comparator[op] ? comparator[op](version, n) : (version == n || n !== n); + }; - // Opera 8.0+ - this.isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0; + this.browser = { + userAgent, + vendor, + }; + + // detect opera + this.browser.isOpera = function isOpera(range) { + const match = userAgent.match(/(?:^opera.+?version|opr)\/(\d+)/); + return match !== null && compareVersion(match[1], range); + } - // Firefox 1.0+ - this.isFirefox = typeof InstallTrigger !== 'undefined'; + // detect chrome + this.browser.isChrome = function isChrome(range) { + const match = /google inc/.test(vendor) ? userAgent.match(/(?:chrome|crios)\/(\d+)/) : null; + return match !== null && !this.isOpera() && compareVersion(match[1], range); + } - // Safari 3.0+ "[object HTMLElementConstructor]" - this.isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && safari.pushNotification)); + // detect firefox + this.browser.isFirefox = function isFirefox(range) { + const match = userAgent.match(/(?:firefox|fxios)\/(\d+)/); + return match !== null && compareVersion(match[1], range); + }; - // Internet Explorer 6-11 - this.isIE = /*@cc_on!@*/false || !!document.documentMode; + // detect safari + this.browser.isSafari = function isSafari(range) { + const match = userAgent.match(/version\/(\d+).+?safari/); + return match !== null && compareVersion(match[1], range); + }; - // Edge 20+ - this.isEdge = !this.isIE && !!window.StyleMedia; + // detect internet explorer + this.browser.isIE = function isIE(range) { + const match = userAgent.match(/(?:msie |trident.+?; rv:)(\d+)/); + return match !== null && compareVersion(match[1], range); + }; - // Chrome 1 - 71 - this.isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime); + // detect edge + this.browser.isEdge = function isEdge(range) { + const match = userAgent.match(/edge\/(\d+)/); + return match !== null && compareVersion(match[1], range); + }; - // Blink engine detection - this.isBlink = (this.isChrome || this.isOpera) && !!window.CSS; + // detect blink engine + this.browser.isBlink = function isBlink() { + return (this.isChrome() || this.isOpera()) && !!window.CSS; + }; } } \ No newline at end of file