Skip to content

Commit ca32f87

Browse files
committed
style: adjust the position of the floating selectionTool to avoid occlusion (#58, #40)
1 parent 5e14639 commit ca32f87

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/content-script/index.jsx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { config as toolsConfig } from './selection-tools'
77
import { clearOldAccessToken, getUserConfig, setAccessToken } from '../config/index.mjs'
88
import {
99
createElementAtPosition,
10+
getClientPosition,
1011
getPossibleElementByQuerySelector,
1112
initSession,
1213
isSafari,
@@ -110,18 +111,27 @@ const deleteToolbar = () => {
110111
async function prepareForSelectionTools() {
111112
document.addEventListener('mouseup', (e) => {
112113
if (toolbarContainer && toolbarContainer.contains(e.target)) return
113-
if (
114-
toolbarContainer &&
114+
const selectionElement =
115115
window.getSelection()?.rangeCount > 0 &&
116-
toolbarContainer.contains(window.getSelection()?.getRangeAt(0).endContainer.parentElement)
117-
)
118-
return
116+
window.getSelection()?.getRangeAt(0).endContainer.parentElement
117+
if (toolbarContainer && selectionElement && toolbarContainer.contains(selectionElement)) return
119118

120119
deleteToolbar()
121120
setTimeout(() => {
122121
const selection = window.getSelection()?.toString()
123122
if (selection) {
124-
toolbarContainer = createElementAtPosition(e.pageX + 15, e.pageY - 15)
123+
const inputElement = selectionElement.querySelector('input, textarea')
124+
let position
125+
if (inputElement) {
126+
position = getClientPosition(inputElement)
127+
position = {
128+
x: position.x + window.scrollX + inputElement.offsetWidth + 50,
129+
y: e.pageY + 30,
130+
}
131+
} else {
132+
position = { x: e.pageX + 20, y: e.pageY + 20 }
133+
}
134+
toolbarContainer = createElementAtPosition(position.x, position.y)
125135
toolbarContainer.className = 'chatgptbox-toolbar-container'
126136
render(
127137
<FloatingToolbar
@@ -168,8 +178,8 @@ async function prepareForSelectionToolsTouch() {
168178
const selection = window.getSelection()?.toString()
169179
if (selection) {
170180
toolbarContainer = createElementAtPosition(
171-
e.changedTouches[0].pageX + 15,
172-
e.changedTouches[0].pageY - 15,
181+
e.changedTouches[0].pageX + 20,
182+
e.changedTouches[0].pageY + 20,
173183
)
174184
toolbarContainer.className = 'chatgptbox-toolbar-container'
175185
render(

0 commit comments

Comments
 (0)