Skip to content

Commit 3d3d54a

Browse files
authored
Merge pull request #4 from apicore-engineering/develop
Check visibility of element
2 parents 5ed1e49 + d20e9c0 commit 3d3d54a

File tree

8 files changed

+83
-4
lines changed

8 files changed

+83
-4
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: 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 = !this._options.checkVisibility;
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 = 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 = 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/demo/App.vue

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
<h3>Attribute</h3>
4848
<img class="image" src="https://source.unsplash.com/random/500x500" :alt="t('LTPlugin.Attrs.ImageAlt')"
4949
:title="t('LTPlugin.Attrs.ImageTitle')">
50+
51+
<div id="draggable" draggable @mousedown="dragStart" @mousemove="dragMove" @mouseup="dragEnd" ref="draggable" :title="t('LTPlugin.PositionAbsolute')">
52+
<h3>Absolute position</h3>
53+
{{ t('LTPlugin.Draggable') }}
54+
</div>
5055
</div>
5156
</div>
5257
</template>
@@ -66,6 +71,26 @@ const showMeta = computed(() => {
6671
return null
6772
})
6873
74+
const dragPos = ref<{ x: number, y: number } | null>(null)
75+
const draggable = ref<HTMLDivElement>()
76+
77+
function dragStart(e: MouseEvent) {
78+
dragPos.value = {
79+
x: e.offsetX,
80+
y: e.offsetY,
81+
}
82+
}
83+
84+
function dragMove(e: MouseEvent) {
85+
if (!dragPos.value) return
86+
draggable.value!.style.top = e.pageY - dragPos.value.y + 'px'
87+
draggable.value!.style.left = e.pageX - dragPos.value.x + 'px'
88+
}
89+
90+
function dragEnd() {
91+
dragPos.value = null
92+
}
93+
6994
</script>
7095

7196
<style lang="scss" scoped>
@@ -120,4 +145,32 @@ const showMeta = computed(() => {
120145
width: 80%;
121146
border-radius: 8px;
122147
}
148+
149+
#draggable {
150+
position: absolute;
151+
top: 8px;
152+
left: 8px;
153+
background: #eeeeeee2;
154+
width: 200px;
155+
height: 150px;
156+
display: flex;
157+
justify-content: center;
158+
align-items: center;
159+
border-radius: 8px;
160+
padding: 1rem;
161+
cursor: grab;
162+
flex-direction: column;
163+
164+
h3 {
165+
margin-top: 0;
166+
}
167+
168+
&:hover {
169+
outline: solid 2px #eeeeee;
170+
}
171+
172+
&:active {
173+
cursor: grabbing;
174+
}
175+
}
123176
</style>

src/demo/lang/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"PartOne": "Part one",
1212
"PartTwo": "Part two",
1313
"ListItemN": "Item {0}",
14-
"MultipleTitle": "Multiple strings inside one tag"
14+
"MultipleTitle": "Multiple strings inside one tag",
15+
"Draggable": "Drag me around to cover elements",
16+
"PositionAbsolute": "Position is absolute"
1517
}
1618
}

src/demo/lang/hu.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"PartOne": "Első rész",
1212
"PartTwo": "Második rész",
1313
"ListItemN": "Elem {0}",
14-
"MultipleTitle": "Több string egy tagen belül"
14+
"MultipleTitle": "Több string egy tagen belül",
15+
"Draggable": "Mozgass egy elem fölé, hogy eltakard azt",
16+
"PositionAbsolute": "'absolute' pozíció"
1517
}
1618
}

src/demo/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ app.use(LiveTranslatorPlugin, {
1313
},
1414
persist: true,
1515
root: document.getElementById('app'),
16-
refreshRate: 200,
16+
refreshRate: 50,
17+
checkVisibility: true,
1718
})
1819

1920
app.mount('#app')

src/demo/style.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ body {
2828
place-items: center;
2929
min-width: 320px;
3030
min-height: 100vh;
31+
overflow-x: hidden;
3132
}
3233

3334
h1 {

src/index.ts

Lines changed: 13 additions & 1 deletion
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,17 +306,28 @@ class LiveTranslatorManager {
305306
if (badges.length) {
306307
let position = { top: 0, left: 0}
307308
try {
309+
let isVisible = !this._options.checkVisibility
308310
if (node instanceof Text) {
309311
const clientRect = getBoundingClientRect(node)
310312
position.top = clientRect.top + window.scrollY
311313
position.left = clientRect.left + window.screenX
314+
isVisible = isVisible || node.parentElement.contains(
315+
document.elementFromPoint(clientRect.left + clientRect.width/2, clientRect.top + clientRect.height/2)
316+
)
312317
} else {
313318
const clientRect = node.getClientRects()[0]
314319
position.top = clientRect.top + clientRect.height - 10 + window.scrollY
315320
position.left = clientRect.left + window.screenX
321+
isVisible = isVisible || node.contains(
322+
document.elementFromPoint(clientRect.left + clientRect.width/2, clientRect.top + clientRect.height/2)
323+
)
324+
}
325+
if (!isVisible) {
326+
continue
316327
}
317328
} catch (error) {
318329
// console.warn('Could not get bounding box for', node);
330+
continue
319331
}
320332
const container = document.createElement('span')
321333
container.classList.add('live-translator-badge-container')

0 commit comments

Comments
 (0)