Skip to content

Commit 7213972

Browse files
author
Sebi Nemeth
committed
add visibility check
1 parent 280901b commit 7213972

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

dist/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,19 +259,26 @@ class LiveTranslatorManager {
259259
if (badges.length) {
260260
let position = { top: 0, left: 0 };
261261
try {
262+
let isVisible;
262263
if (node instanceof Text) {
263264
const clientRect = getBoundingClientRect(node);
264265
position.top = clientRect.top + window.scrollY;
265266
position.left = clientRect.left + window.screenX;
267+
isVisible = node.parentElement.contains(document.elementFromPoint(clientRect.left + clientRect.width / 2, clientRect.top + clientRect.height / 2));
266268
}
267269
else {
268270
const clientRect = node.getClientRects()[0];
269271
position.top = clientRect.top + clientRect.height - 10 + window.scrollY;
270272
position.left = clientRect.left + window.screenX;
273+
isVisible = node.contains(document.elementFromPoint(clientRect.left + clientRect.width / 2, clientRect.top + clientRect.height / 2));
274+
}
275+
if (!isVisible) {
276+
continue;
271277
}
272278
}
273279
catch (error) {
274280
// console.warn('Could not get bounding box for', node);
281+
continue;
275282
}
276283
const container = document.createElement('span');
277284
container.classList.add('live-translator-badge-container');

src/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,17 +305,28 @@ class LiveTranslatorManager {
305305
if (badges.length) {
306306
let position = { top: 0, left: 0}
307307
try {
308+
let isVisible
308309
if (node instanceof Text) {
309310
const clientRect = getBoundingClientRect(node)
310311
position.top = clientRect.top + window.scrollY
311312
position.left = clientRect.left + window.screenX
313+
isVisible = node.parentElement.contains(
314+
document.elementFromPoint(clientRect.left + clientRect.width/2, clientRect.top + clientRect.height/2)
315+
)
312316
} else {
313317
const clientRect = node.getClientRects()[0]
314318
position.top = clientRect.top + clientRect.height - 10 + window.scrollY
315319
position.left = clientRect.left + window.screenX
320+
isVisible = node.contains(
321+
document.elementFromPoint(clientRect.left + clientRect.width/2, clientRect.top + clientRect.height/2)
322+
)
323+
}
324+
if (!isVisible) {
325+
continue
316326
}
317327
} catch (error) {
318328
// console.warn('Could not get bounding box for', node);
329+
continue
319330
}
320331
const container = document.createElement('span')
321332
container.classList.add('live-translator-badge-container')

0 commit comments

Comments
 (0)