Skip to content

Commit

Permalink
fix: fix the issue where the floating window disappears when releasin…
Browse files Browse the repository at this point in the history
…g the mouse outside the window
  • Loading branch information
josStorer committed Mar 13, 2023
1 parent 6a31266 commit 0eae222
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/content-script/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,35 @@ let toolbarContainer
async function prepareForSelectionTools() {
document.addEventListener('mouseup', (e) => {
if (toolbarContainer && toolbarContainer.contains(e.target)) return
if (
toolbarContainer &&
window.getSelection()?.rangeCount > 0 &&
toolbarContainer.contains(window.getSelection()?.getRangeAt(0).endContainer.parentElement)
)
return

if (toolbarContainer) toolbarContainer.remove()
setTimeout(() => {
const selection = window.getSelection()?.toString()
if (selection) {
toolbarContainer = createElementAtPosition(e.pageX + 15, e.pageY - 15)
render(<FloatingToolbar selection={selection} />, toolbarContainer)
const position = { x: e.pageX + 15, y: e.pageY - 15 }
toolbarContainer = createElementAtPosition(position.x, position.y)
toolbarContainer.className = 'toolbar-container'
render(
<FloatingToolbar
selection={selection}
position={position}
container={toolbarContainer}
/>,
toolbarContainer,
)
}
})
})
document.addEventListener('mousedown', (e) => {
if (toolbarContainer && toolbarContainer.contains(e.target)) return

if (toolbarContainer) toolbarContainer.remove()
document.querySelectorAll('.toolbar-container').forEach((e) => e.remove())
})
document.addEventListener('keydown', (e) => {
if (
Expand Down

0 comments on commit 0eae222

Please sign in to comment.