Skip to content

Commit

Permalink
Better popup positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
artemave committed Feb 2, 2024
1 parent 24bb442 commit 61e747c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist
node_modules
.DS_Store
.mise.toml
45 changes: 34 additions & 11 deletions contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,25 @@ function showPopup(e, content) {
$('body').append($popup)

$popup.on('transover-popup_content_updated', function() {
const pos = calculatePosition(e.clientX, e.clientY, $popup)
let textRect = {
top: e.clientY,
bottom: e.clientY,
left: e.clientX,
right: e.clientX,
}

const selection = window.getSelection()
if (selection.toString()) {
const selectionRect = selection.getRangeAt(0).getBoundingClientRect()
textRect = {
top: selectionRect.top,
bottom: selectionRect.bottom,
left: selectionRect.left,
right: selectionRect.right,
}
}
const pos = calculatePopupPosition(textRect, $popup)

$popup
.each(function() {
$(this.shadowRoot.querySelector('main')).hide()
Expand All @@ -88,23 +106,28 @@ function showPopup(e, content) {
$popup.attr({content, options: JSON.stringify(options)})
}

function calculatePosition(x, y, $popup) {
function calculatePopupPosition(textRect, $popup) {
const pos = {}
const margin = 5
const anchor = 10
const outerWidth = Number($popup.attr('outer-width'))
const outerHeight = Number($popup.attr('outer-height'))
const windowWidth = $(window).width()

// show popup to the right of the word if it fits into window this way
if (x + anchor + outerWidth + margin < $(window).width()) {
pos.x = x + anchor
if (textRect.right + anchor + outerWidth + margin < windowWidth) {
pos.x = textRect.right + anchor
}
// show popup to the left of the word if it fits into window this way
else if (x - anchor - outerWidth - margin > 0) {
pos.x = x - anchor - outerWidth
else if (textRect.left - anchor - outerWidth - margin > 0) {
pos.x = textRect.left - anchor - outerWidth
}
// align popup with selection start if it's too wide to be on the either sides
else if (textRect.left + anchor + outerWidth + margin < windowWidth) {
pos.x = textRect.left
}
// show popup at the very left if it is not wider than window
else if (outerWidth + margin*2 < $(window).width()) {
else if (outerWidth + margin*2 < windowWidth) {
pos.x = margin
}
// resize popup width to fit into window and position it the very left of the window
Expand All @@ -118,12 +141,12 @@ function calculatePosition(x, y, $popup) {
}

// show popup above the word if it fits into window this way
if (y - anchor - outerHeight - margin > 0) {
pos.y = y - anchor - outerHeight
if (textRect.top - anchor - outerHeight - margin > 0) {
pos.y = textRect.top - anchor - outerHeight
}
// show popup below the word if it fits into window this way
else if (y + anchor + outerHeight + margin < $(window).height()) {
pos.y = y + anchor
else if (textRect.bottom + anchor + outerHeight + margin < $(window).height()) {
pos.y = textRect.bottom + anchor
}
// show popup at the very top of the window
else {
Expand Down
3 changes: 2 additions & 1 deletion lib/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

main {
color: #333;
max-width: 800px;
font-family: Arial, sans-serif;
font-size: 14px;
font-weight: 300;
Expand Down Expand Up @@ -49,7 +50,7 @@

.pos_translation {
font-size: 1em;
line-height: 1.2em;
line-height: 1.3em;
}

.red {
Expand Down
2 changes: 1 addition & 1 deletion manifest_v2.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "TransOver",
"manifest_version": 2,
"version": "1.73",
"version": "1.74",
"icons": { "16": "to_16.png", "48": "to_48.png", "128": "to_128.png" },
"description": "Hover, click or select to translate (with text-to-speech). Translator.",
"background": {
Expand Down
2 changes: 1 addition & 1 deletion manifest_v3.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "TransOver",
"manifest_version": 3,
"version": "1.73",
"version": "1.74",
"icons": { "16": "to_16.png", "48": "to_48.png", "128": "to_128.png" },
"description": "Hover, click or select to translate (with text-to-speech). Translator.",
"background": {
Expand Down

0 comments on commit 61e747c

Please sign in to comment.