@@ -7,6 +7,7 @@ import { config as toolsConfig } from './selection-tools'
7
7
import { clearOldAccessToken , getUserConfig , setAccessToken } from '../config/index.mjs'
8
8
import {
9
9
createElementAtPosition ,
10
+ getClientPosition ,
10
11
getPossibleElementByQuerySelector ,
11
12
initSession ,
12
13
isSafari ,
@@ -110,18 +111,27 @@ const deleteToolbar = () => {
110
111
async function prepareForSelectionTools ( ) {
111
112
document . addEventListener ( 'mouseup' , ( e ) => {
112
113
if ( toolbarContainer && toolbarContainer . contains ( e . target ) ) return
113
- if (
114
- toolbarContainer &&
114
+ const selectionElement =
115
115
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
119
118
120
119
deleteToolbar ( )
121
120
setTimeout ( ( ) => {
122
121
const selection = window . getSelection ( ) ?. toString ( )
123
122
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 )
125
135
toolbarContainer . className = 'chatgptbox-toolbar-container'
126
136
render (
127
137
< FloatingToolbar
@@ -168,8 +178,8 @@ async function prepareForSelectionToolsTouch() {
168
178
const selection = window . getSelection ( ) ?. toString ( )
169
179
if ( selection ) {
170
180
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 ,
173
183
)
174
184
toolbarContainer . className = 'chatgptbox-toolbar-container'
175
185
render (
0 commit comments