Skip to content

Commit

Permalink
fix: floating tool created outside the window when it was on the edge (
Browse files Browse the repository at this point in the history
  • Loading branch information
josStorer committed Apr 21, 2023
1 parent 4d9479b commit da33ca7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
11 changes: 10 additions & 1 deletion src/components/FloatingToolbar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ function FloatingToolbar(props) {
const windowSize = useClampWindowSize([750, 1500], [0, Infinity])
const config = useConfig(() => {
setRender(true)
if (!triggered) props.container.style.position = 'absolute'
if (!triggered) {
props.container.style.position = 'absolute'
setTimeout(() => {
const left = Math.min(
Math.max(0, window.innerWidth - props.container.offsetWidth - 30),
Math.max(0, position.x),
)
props.container.style.left = left + 'px'
})
}
})

useEffect(() => {
Expand Down
35 changes: 15 additions & 20 deletions src/content-script/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ const deleteToolbar = () => {
toolbarContainer.remove()
}

const createSelectionTools = (toolbarContainer, selection) => {
toolbarContainer.className = 'chatgptbox-toolbar-container'
render(
<FloatingToolbar
session={initSession()}
selection={selection}
container={toolbarContainer}
dockable={true}
/>,
toolbarContainer,
)
}

async function prepareForSelectionTools() {
document.addEventListener('mouseup', (e) => {
if (toolbarContainer && toolbarContainer.contains(e.target)) return
Expand Down Expand Up @@ -154,16 +167,7 @@ async function prepareForSelectionTools() {
position = { x: e.pageX + 20, y: e.pageY + 20 }
}
toolbarContainer = createElementAtPosition(position.x, position.y)
toolbarContainer.className = 'chatgptbox-toolbar-container'
render(
<FloatingToolbar
session={initSession()}
selection={selection}
container={toolbarContainer}
dockable={true}
/>,
toolbarContainer,
)
createSelectionTools(toolbarContainer, selection)
}
})
})
Expand Down Expand Up @@ -207,16 +211,7 @@ async function prepareForSelectionToolsTouch() {
e.changedTouches[0].pageX + 20,
e.changedTouches[0].pageY + 20,
)
toolbarContainer.className = 'chatgptbox-toolbar-container'
render(
<FloatingToolbar
session={initSession()}
selection={selection}
container={toolbarContainer}
dockable={true}
/>,
toolbarContainer,
)
createSelectionTools(toolbarContainer, selection)
}
})
})
Expand Down

0 comments on commit da33ca7

Please sign in to comment.