Skip to content

Commit da33ca7

Browse files
committed
fix: floating tool created outside the window when it was on the edge (#222)
1 parent 4d9479b commit da33ca7

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

src/components/FloatingToolbar/index.jsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,16 @@ function FloatingToolbar(props) {
2323
const windowSize = useClampWindowSize([750, 1500], [0, Infinity])
2424
const config = useConfig(() => {
2525
setRender(true)
26-
if (!triggered) props.container.style.position = 'absolute'
26+
if (!triggered) {
27+
props.container.style.position = 'absolute'
28+
setTimeout(() => {
29+
const left = Math.min(
30+
Math.max(0, window.innerWidth - props.container.offsetWidth - 30),
31+
Math.max(0, position.x),
32+
)
33+
props.container.style.left = left + 'px'
34+
})
35+
}
2736
})
2837

2938
useEffect(() => {

src/content-script/index.jsx

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,19 @@ const deleteToolbar = () => {
126126
toolbarContainer.remove()
127127
}
128128

129+
const createSelectionTools = (toolbarContainer, selection) => {
130+
toolbarContainer.className = 'chatgptbox-toolbar-container'
131+
render(
132+
<FloatingToolbar
133+
session={initSession()}
134+
selection={selection}
135+
container={toolbarContainer}
136+
dockable={true}
137+
/>,
138+
toolbarContainer,
139+
)
140+
}
141+
129142
async function prepareForSelectionTools() {
130143
document.addEventListener('mouseup', (e) => {
131144
if (toolbarContainer && toolbarContainer.contains(e.target)) return
@@ -154,16 +167,7 @@ async function prepareForSelectionTools() {
154167
position = { x: e.pageX + 20, y: e.pageY + 20 }
155168
}
156169
toolbarContainer = createElementAtPosition(position.x, position.y)
157-
toolbarContainer.className = 'chatgptbox-toolbar-container'
158-
render(
159-
<FloatingToolbar
160-
session={initSession()}
161-
selection={selection}
162-
container={toolbarContainer}
163-
dockable={true}
164-
/>,
165-
toolbarContainer,
166-
)
170+
createSelectionTools(toolbarContainer, selection)
167171
}
168172
})
169173
})
@@ -207,16 +211,7 @@ async function prepareForSelectionToolsTouch() {
207211
e.changedTouches[0].pageX + 20,
208212
e.changedTouches[0].pageY + 20,
209213
)
210-
toolbarContainer.className = 'chatgptbox-toolbar-container'
211-
render(
212-
<FloatingToolbar
213-
session={initSession()}
214-
selection={selection}
215-
container={toolbarContainer}
216-
dockable={true}
217-
/>,
218-
toolbarContainer,
219-
)
214+
createSelectionTools(toolbarContainer, selection)
220215
}
221216
})
222217
})

0 commit comments

Comments
 (0)