Skip to content

Commit

Permalink
add selectable elements
Browse files Browse the repository at this point in the history
  • Loading branch information
estevanmaito committed Mar 22, 2019
1 parent af661a9 commit 7f62098
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/sharect.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const Sharect = (function() {
let _arrowSize = 5
let _buttonMargin = 7 * 2
let _iconSize = 24 + _buttonMargin
let _selectableElements = ['body']

function createFacebookButton() {
const fbBtn = new Button(_facebookConfig.icon, function() {
Expand Down Expand Up @@ -132,19 +133,40 @@ const Sharect = (function() {
_text = _selection.toString()
}

function getClosestElement(element, ancestor) {
if (Element.prototype.closest) {
return element.closest(ancestor)
} else {
// IE 9+ polyfill
let el = element
do {
if (el.matches(ancestor)) return el
el = el.parentNode
} while (el !== null && el.nodeType === Node.ELEMENT_NODE)
return null
}
}

function isSelectableElement() {
let currentSelectedElement = window.getSelection().baseNode.parentNode
return _selectableElements.some(function(ancestor) {
return getClosestElement(currentSelectedElement, ancestor)
})
}

function attachEvents() {
window.addEventListener('mouseup', function() {
setTimeout(function mouseTimeout() {
if (hasTooltipDrawn()) {
if (hasSelection()) {
if (hasSelection() && isSelectableElement()) {
updateTextSelection()
moveTooltip()
return
} else {
document.querySelector('.sharect').remove()
}
}
if (hasSelection()) {
if (hasSelection() && isSelectableElement()) {
updateTextSelection()
drawTooltip()
}
Expand All @@ -164,6 +186,7 @@ const Sharect = (function() {
: options.twitterUsername
_backgroundColor = options.backgroundColor || _backgroundColor
_iconColor = options.iconColor || _iconColor
_selectableElements = options.selectableElements || _selectableElements
return this
}

Expand Down

0 comments on commit 7f62098

Please sign in to comment.