Skip to content

Commit d20e9c0

Browse files
author
Sebi Nemeth
committed
optional visibility chack
1 parent e4b0d4d commit d20e9c0

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

dist/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type LiveTranslatorPluginOptions = {
1010
persist?: boolean;
1111
root?: HTMLElement;
1212
refreshRate?: number;
13+
checkVisibility?: boolean;
1314
};
1415
export declare function encodeMessages(messagesObject: any): any;
1516
export declare const LiveTranslatorPlugin: {

dist/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,18 +259,18 @@ class LiveTranslatorManager {
259259
if (badges.length) {
260260
let position = { top: 0, left: 0 };
261261
try {
262-
let isVisible;
262+
let isVisible = !this._options.checkVisibility;
263263
if (node instanceof Text) {
264264
const clientRect = getBoundingClientRect(node);
265265
position.top = clientRect.top + window.scrollY;
266266
position.left = clientRect.left + window.screenX;
267-
isVisible = node.parentElement.contains(document.elementFromPoint(clientRect.left + clientRect.width / 2, clientRect.top + clientRect.height / 2));
267+
isVisible = isVisible || node.parentElement.contains(document.elementFromPoint(clientRect.left + clientRect.width / 2, clientRect.top + clientRect.height / 2));
268268
}
269269
else {
270270
const clientRect = node.getClientRects()[0];
271271
position.top = clientRect.top + clientRect.height - 10 + window.scrollY;
272272
position.left = clientRect.left + window.screenX;
273-
isVisible = node.contains(document.elementFromPoint(clientRect.left + clientRect.width / 2, clientRect.top + clientRect.height / 2));
273+
isVisible = isVisible || node.contains(document.elementFromPoint(clientRect.left + clientRect.width / 2, clientRect.top + clientRect.height / 2));
274274
}
275275
if (!isVisible) {
276276
continue;

src/demo/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ app.use(LiveTranslatorPlugin, {
1414
persist: true,
1515
root: document.getElementById('app'),
1616
refreshRate: 50,
17+
checkVisibility: true,
1718
})
1819

1920
app.mount('#app')

src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ type LiveTranslatorPluginOptions = {
8484
translationLink: (meta: TranslationMeta) => string
8585
persist?: boolean
8686
root?: HTMLElement
87-
refreshRate?: number
87+
refreshRate?: number,
88+
checkVisibility?: boolean,
8889
}
8990

9091
function deepForIn(object: Object, fn: (value: string, key: string) => void) {
@@ -305,19 +306,19 @@ class LiveTranslatorManager {
305306
if (badges.length) {
306307
let position = { top: 0, left: 0}
307308
try {
308-
let isVisible
309+
let isVisible = !this._options.checkVisibility
309310
if (node instanceof Text) {
310311
const clientRect = getBoundingClientRect(node)
311312
position.top = clientRect.top + window.scrollY
312313
position.left = clientRect.left + window.screenX
313-
isVisible = node.parentElement.contains(
314+
isVisible = isVisible || node.parentElement.contains(
314315
document.elementFromPoint(clientRect.left + clientRect.width/2, clientRect.top + clientRect.height/2)
315316
)
316317
} else {
317318
const clientRect = node.getClientRects()[0]
318319
position.top = clientRect.top + clientRect.height - 10 + window.scrollY
319320
position.left = clientRect.left + window.screenX
320-
isVisible = node.contains(
321+
isVisible = isVisible || node.contains(
321322
document.elementFromPoint(clientRect.left + clientRect.width/2, clientRect.top + clientRect.height/2)
322323
)
323324
}

0 commit comments

Comments
 (0)