|
| 1 | +function returnTooltipRevealTransform(endPosition = true, shouldShift = true) { |
| 2 | + const dxOffset = shouldShift ? "-50%" : "0"; |
| 3 | + const dyPercentOffset = configs.verticalLayoutTooltip ? 30 : 100; |
| 4 | + |
| 5 | + switch (configs.tooltipRevealEffect) { |
| 6 | + case "noTooltipEffect": |
| 7 | + return `translate(${dxOffset},0)`; |
| 8 | + case "moveUpTooltipEffect": |
| 9 | + return endPosition |
| 10 | + ? `translate(${dxOffset},0)` |
| 11 | + : `translate(${dxOffset}, ${dyPercentOffset}%)`; |
| 12 | + case "moveDownTooltipEffect": |
| 13 | + return endPosition |
| 14 | + ? `translate(${dxOffset},0)` |
| 15 | + : `translate(${dxOffset}, -${dyPercentOffset}%)`; |
| 16 | + case "scaleUpTooltipEffect": |
| 17 | + return endPosition |
| 18 | + ? `translate(${dxOffset},0) scale(1.0)` |
| 19 | + : `translate(${dxOffset},0) scale(0.0)`; |
| 20 | + case "scaleUpFromBottomTooltipEffect": |
| 21 | + return endPosition |
| 22 | + ? `translate(${dxOffset},0) scale(1.0)` |
| 23 | + : `translate(${dxOffset},0) scale(0.0)`; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +function onTooltipButtonClick(e, url, text) { |
| 28 | + try { |
| 29 | + const evt = e || window.event; |
| 30 | + |
| 31 | + if ("buttons" in evt) { |
| 32 | + if (evt.button == 0) { |
| 33 | + /// Left button click |
| 34 | + hideTooltip(); |
| 35 | + removeSelectionOnPage(); |
| 36 | + |
| 37 | + chrome.runtime.sendMessage({ |
| 38 | + type: "selecton-open-new-tab", |
| 39 | + url: url, |
| 40 | + focused: configs.leftClickBackgroundTab ? false : true, |
| 41 | + }); |
| 42 | + } else if (evt.button == 1) { |
| 43 | + /// Middle button click |
| 44 | + evt.preventDefault(); |
| 45 | + if (configs.middleClickHidesTooltip) { |
| 46 | + hideTooltip(); |
| 47 | + removeSelectionOnPage(); |
| 48 | + } |
| 49 | + |
| 50 | + chrome.runtime.sendMessage({ type: "selecton-open-new-tab", url: url, focused: false }); |
| 51 | + } |
| 52 | + } |
| 53 | + } catch (e) { |
| 54 | + window.open(url, "_blank"); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +function checkTooltipForCollidingWithSideEdges() { |
| 59 | + if (configs.debugMode) console.log("Checking Selecton tooltip for colliding with side edges..."); |
| 60 | + |
| 61 | + if (tooltip == null) return; |
| 62 | + |
| 63 | + let dx = tooltip.getBoundingClientRect().left; |
| 64 | + let tooltipWidth = 4.0; |
| 65 | + |
| 66 | + if (configs.verticalLayoutTooltip) { |
| 67 | + tooltipWidth = 140; |
| 68 | + } else { |
| 69 | + const tooltipButtons = tooltip.querySelectorAll(".selection-tooltip > .selection-popup-button"); |
| 70 | + for (let i = 0, l = tooltipButtons.length; i < l; i++) { |
| 71 | + tooltipWidth += tooltipButtons[i].offsetWidth; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// fix for collision detection not working with 'scale up' effects |
| 76 | + if ( |
| 77 | + configs.tooltipRevealEffect == "scaleUpTooltipEffect" || |
| 78 | + configs.tooltipRevealEffect == "scaleUpFromBottomTooltipEffect" |
| 79 | + ) { |
| 80 | + dx -= tooltipWidth / 2; |
| 81 | + } |
| 82 | + |
| 83 | + /// Tooltip is off-screen on the left |
| 84 | + if (dx < 0) { |
| 85 | + if (configs.debugMode) console.log("Tooltip is colliding with left edge. Fixing..."); |
| 86 | + |
| 87 | + tooltip.style.left = `${5 + tooltipWidth / 2}px`; |
| 88 | + |
| 89 | + /// Shift the arrow to match new position |
| 90 | + if (configs.showTooltipArrow && arrow) { |
| 91 | + const newLeftPercentForArrow = ((-dx + 5) / tooltipWidth) * 100; |
| 92 | + arrow.style.left = `${50 - newLeftPercentForArrow}%`; |
| 93 | + } |
| 94 | + } else { |
| 95 | + /// Check tooltip to be off-screen on the right |
| 96 | + const screenWidth = |
| 97 | + document.body.clientWidth || window.innerWidth || document.documentElement.clientWidth; |
| 98 | + |
| 99 | + const offscreenAmount = dx + tooltipWidth - screenWidth; |
| 100 | + |
| 101 | + /// Tooltip is off-screen on the right |
| 102 | + if (offscreenAmount > 0) { |
| 103 | + if (configs.debugMode) |
| 104 | + console.log(`Tooltip is colliding with right edge by ${offscreenAmount}px`); |
| 105 | + |
| 106 | + tooltip.style.left = "unset"; |
| 107 | + tooltip.style.right = `${5 - tooltipWidth / 2}px`; |
| 108 | + |
| 109 | + /// Shift the arrow to match new position |
| 110 | + if (configs.showTooltipArrow && arrow) { |
| 111 | + const newLeftPercentForArrow = ((offscreenAmount + 5) / tooltipWidth) * 100; |
| 112 | + arrow.style.left = `${50 + newLeftPercentForArrow}%`; |
| 113 | + } |
| 114 | + } else { |
| 115 | + if (configs.debugMode) console.log("Tooltip is not colliding with side edges"); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +function setBorderRadiusForSideButtons(parent, applyOnlyToButtons = true) { |
| 121 | + /// Set border radius for first and last buttons of horizontal tooltip |
| 122 | + const children = applyOnlyToButtons |
| 123 | + ? parent.querySelectorAll(".selection-tooltip > .selection-popup-button") |
| 124 | + : parent.children; |
| 125 | + const childrenLength = children.length; |
| 126 | + if (childrenLength == 1) { |
| 127 | + children[0].style.borderRadius = 0; |
| 128 | + } else { |
| 129 | + const revertedVerticalButtons = configs.verticalLayoutTooltip && tooltipOnBottom; |
| 130 | + children[0].style.borderRadius = revertedVerticalButtons |
| 131 | + ? lastButtonBorderRadius |
| 132 | + : firstButtonBorderRadius; |
| 133 | + children[childrenLength - 1].style.borderRadius = revertedVerticalButtons |
| 134 | + ? firstButtonBorderRadius |
| 135 | + : lastButtonBorderRadius; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +function addBasicTooltipButton(label, icon, onClick, isFirstButton = false, iconOpacity) { |
| 140 | + /// Used for basic button with action label + icon, when enabled |
| 141 | + const button = document.createElement("button"); |
| 142 | + button.classList.add("circular-button"); |
| 143 | + button.setAttribute( |
| 144 | + "class", |
| 145 | + isFirstButton || configs.showButtonBorders == false |
| 146 | + ? "selection-popup-button circular-button first-button" |
| 147 | + : "selection-popup-button circular-button first-button" |
| 148 | + ); |
| 149 | + |
| 150 | + const image = document.createElement("img"); |
| 151 | + image.src = chrome.runtime.getURL("../icons/button-icons/snipd2.png"); |
| 152 | + button.appendChild(image); |
| 153 | + console.log(image.src); |
| 154 | + |
| 155 | + button.onmousedown = onClick; |
| 156 | + |
| 157 | + const sidepanelButton = document.createElement("button"); |
| 158 | + sidepanelButton.classList.add("circular-button"); |
| 159 | + sidepanelButton.setAttribute( |
| 160 | + "class", |
| 161 | + isFirstButton || configs.showButtonBorders == false |
| 162 | + ? "selection-popup-button circular-button" |
| 163 | + : "selection-popup-button circular-button" |
| 164 | + ); |
| 165 | + const snipdLogoImage = document.createElement("img"); |
| 166 | + snipdLogoImage.src = chrome.runtime.getURL("../icons/button-icons/marker.svg"); |
| 167 | + sidepanelButton.appendChild(snipdLogoImage); |
| 168 | + |
| 169 | + sidepanelButton.onmousedown = () => { |
| 170 | + console.log("sidepanel clicked"); |
| 171 | + chrome.runtime.sendMessage({ type: "open-sidepanel" }); |
| 172 | + }; |
| 173 | + |
| 174 | + const centralPageButton = document.createElement("button"); |
| 175 | + centralPageButton.classList.add("circular-button"); |
| 176 | + centralPageButton.setAttribute( |
| 177 | + "class", |
| 178 | + isFirstButton || configs.showButtonBorders == false |
| 179 | + ? "selection-popup-button circular-button" |
| 180 | + : "selection-popup-button circular-button" |
| 181 | + ); |
| 182 | + const homeImage = document.createElement("img"); |
| 183 | + homeImage.src = chrome.runtime.getURL("../icons/button-icons/home.svg"); |
| 184 | + centralPageButton.appendChild(homeImage); |
| 185 | + |
| 186 | + centralPageButton.onmousedown = () => { |
| 187 | + chrome.runtime.sendMessage({ type: "open-central-page" }); |
| 188 | + |
| 189 | + } |
| 190 | + |
| 191 | + if (configs.reverseTooltipButtonsOrder && isFirstButton == false) |
| 192 | + tooltip.insertBefore(button, tooltip.children[1]); |
| 193 | + else tooltip.appendChild(button); |
| 194 | + |
| 195 | + const separator = document.createElement("div"); |
| 196 | + separator.classList.add("tooltip-separator"); |
| 197 | + separator.setAttribute("class", "tooltip-separator"); |
| 198 | + tooltip.appendChild(separator); |
| 199 | + |
| 200 | + tooltip.appendChild(sidepanelButton); |
| 201 | + tooltip.appendChild(centralPageButton); |
| 202 | + |
| 203 | + return button; |
| 204 | +} |
| 205 | + |
| 206 | +/// Hide tooltip when mouse moved far from text selection |
| 207 | +function mouseMoveToHideListener(mouseMoveEvent) { |
| 208 | + if (tooltipIsShown == false || configs.hideTooltipWhenCursorMovesAway == false) { |
| 209 | + window.removeEventListener("mousemove", mouseMoveToHideListener); |
| 210 | + return; |
| 211 | + } |
| 212 | + |
| 213 | + if ( |
| 214 | + Math.abs(mouseMoveEvent.clientX - lastMouseUpEvent.clientX) > this.window.screen.width / 4 || |
| 215 | + Math.abs(mouseMoveEvent.clientY - lastMouseUpEvent.clientY) > this.window.screen.height / 4 |
| 216 | + ) { |
| 217 | + window.removeEventListener("mousemove", mouseMoveToHideListener); |
| 218 | + |
| 219 | + try { |
| 220 | + hideTooltip(); |
| 221 | + hideDragHandles(); |
| 222 | + } catch (e) {} |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +/// Set tooltip styling to be on bottom of text selection |
| 227 | +function setTooltipOnBottom() { |
| 228 | + arrow.classList.add("arrow-on-bottom"); |
| 229 | + tooltipOnBottom = true; |
| 230 | + |
| 231 | + if (configs.showInfoPanel && infoPanel && infoPanel.isConnected) { |
| 232 | + const newInfoPanel = infoPanel.cloneNode(true); |
| 233 | + newInfoPanel.classList.add("info-panel-on-bottom"); |
| 234 | + tooltip.appendChild(newInfoPanel); |
| 235 | + try { |
| 236 | + infoPanel.remove(); |
| 237 | + tooltip.removeChild(infoPanel); |
| 238 | + } catch (e) {} |
| 239 | + infoPanel = newInfoPanel; |
| 240 | + } |
| 241 | +} |
| 242 | + |
| 243 | +/// Makes tooltip draggable by given element (for example, arrow) |
| 244 | +function makeTooltipElementDraggable(element, compensateTooltipHeight = true) { |
| 245 | + element.style.cursor = "grab"; |
| 246 | + element.onmousedown = function (e) { |
| 247 | + isDraggingTooltip = true; |
| 248 | + e.preventDefault(); |
| 249 | + if (configs.debugMode) console.log("Started dragging tooltip..."); |
| 250 | + |
| 251 | + tooltip.style.left = `0px`; |
| 252 | + tooltip.style.top = `0px`; |
| 253 | + tooltip.style.transition = `opacity ${configs.animationDuration}ms ease-in-out`; |
| 254 | + document.body.style.cursor = "grabbing"; |
| 255 | + |
| 256 | + // const tooltipOnBottom = arrow.classList.contains('arrow-on-bottom'); |
| 257 | + const tooltipHeightCompensation = tooltipOnBottom |
| 258 | + ? arrow.clientHeight / 3 |
| 259 | + : compensateTooltipHeight |
| 260 | + ? tooltip.clientHeight |
| 261 | + : 0; |
| 262 | + tooltip.style.transform = `translate(${e.clientX - tooltip.clientWidth / 2}px, ${ |
| 263 | + tooltipOnBottom |
| 264 | + ? e.clientY + tooltipHeightCompensation |
| 265 | + : e.clientY - tooltipHeightCompensation |
| 266 | + }px)`; |
| 267 | + tooltip.style.pointerEvents = "none"; |
| 268 | + |
| 269 | + document.onmousemove = function (e) { |
| 270 | + e.preventDefault(); |
| 271 | + |
| 272 | + /// Move main tooltip |
| 273 | + tooltip.style.transform = `translate(${e.clientX - tooltip.clientWidth / 2}px, ${ |
| 274 | + tooltipOnBottom |
| 275 | + ? e.clientY + tooltipHeightCompensation |
| 276 | + : e.clientY - tooltipHeightCompensation |
| 277 | + }px)`; |
| 278 | + }; |
| 279 | + |
| 280 | + document.onmouseup = function (e) { |
| 281 | + e.preventDefault(); |
| 282 | + document.onmousemove = null; |
| 283 | + document.onmouseup = null; |
| 284 | + isDraggingTooltip = false; |
| 285 | + document.body.style.cursor = "unset"; |
| 286 | + |
| 287 | + tooltip.style.left = `${e.clientX - tooltip.clientWidth / 2}px`; |
| 288 | + tooltip.style.top = `${ |
| 289 | + tooltipOnBottom |
| 290 | + ? e.clientY + tooltipHeightCompensation |
| 291 | + : e.clientY - tooltipHeightCompensation |
| 292 | + }px`; |
| 293 | + tooltip.style.transform = null; |
| 294 | + tooltip.style.pointerEvents = "auto"; |
| 295 | + |
| 296 | + if (configs.debugMode) console.log("Dragging tooltip finished"); |
| 297 | + }; |
| 298 | + |
| 299 | + if (configs.hideTooltipWhenCursorMovesAway) { |
| 300 | + window.removeEventListener("mousemove", mouseMoveToHideListener); |
| 301 | + } |
| 302 | + }; |
| 303 | +} |
0 commit comments