1
0
Fork 0
mirror of synced 2024-10-04 03:54:37 +13:00

Refactor clickoutside fix flashing side panel issues, but also fix annoying drag-outside closing actions

This commit is contained in:
Andrew Kingston 2024-05-02 16:46:58 +01:00
parent df25ff307c
commit ff23825241
3 changed files with 30 additions and 21 deletions

View file

@ -9,14 +9,13 @@ const conditionallyIgnoredClasses = [
".spectrum-Popover", ".spectrum-Popover",
] ]
let clickHandlers = [] let clickHandlers = []
let candidateTarget
/** /**
* Handle a body click event * Handle a body click event
*/ */
const handleClick = event => { const handleClick = event => {
// Treat right clicks (context menu events) as normal clicks console.log("CLOSE")
const eventType = event.type === "contextmenu" ? "click" : event.type
// Ignore click if this is an ignored class // Ignore click if this is an ignored class
if (event.target.closest('[data-ignore-click-outside="true"]')) { if (event.target.closest('[data-ignore-click-outside="true"]')) {
return return
@ -29,11 +28,6 @@ const handleClick = event => {
// Process handlers // Process handlers
clickHandlers.forEach(handler => { clickHandlers.forEach(handler => {
// Check that we're the right kind of click event
if (handler.allowedType && eventType !== handler.allowedType) {
return
}
// Check that the click isn't inside the target // Check that the click isn't inside the target
if (handler.element.contains(event.target)) { if (handler.element.contains(event.target)) {
return return
@ -51,17 +45,34 @@ const handleClick = event => {
handler.callback?.(event) handler.callback?.(event)
}) })
} }
document.documentElement.addEventListener("click", handleClick, true)
document.documentElement.addEventListener("mousedown", handleClick, true) const handleMouseUp = e => {
document.documentElement.addEventListener("contextmenu", handleClick, true) console.log("up")
if (candidateTarget === e.target) {
handleClick(e)
}
candidateTarget = null
}
const handleMouseDown = e => {
if (e.button !== 0) {
return
}
candidateTarget = e.target
document.removeEventListener("mouseup", handleMouseUp)
document.addEventListener("mouseup", handleMouseUp, true)
}
document.addEventListener("mousedown", handleMouseDown)
document.addEventListener("contextmenu", handleClick)
/** /**
* Adds or updates a click handler * Adds or updates a click handler
*/ */
const updateHandler = (id, element, anchor, callback, allowedType) => { const updateHandler = (id, element, anchor, callback) => {
let existingHandler = clickHandlers.find(x => x.id === id) let existingHandler = clickHandlers.find(x => x.id === id)
if (!existingHandler) { if (!existingHandler) {
clickHandlers.push({ id, element, anchor, callback, allowedType }) clickHandlers.push({ id, element, anchor, callback })
} else { } else {
existingHandler.callback = callback existingHandler.callback = callback
} }
@ -88,8 +99,7 @@ export default (element, opts) => {
const callback = const callback =
newOpts?.callback || (typeof newOpts === "function" ? newOpts : null) newOpts?.callback || (typeof newOpts === "function" ? newOpts : null)
const anchor = newOpts?.anchor || element const anchor = newOpts?.anchor || element
const allowedType = newOpts?.allowedType || "click" updateHandler(id, element, anchor, callback)
updateHandler(id, element, anchor, callback, allowedType)
} }
update(opts) update(opts)
return { return {

View file

@ -324,10 +324,7 @@
<div <div
id="side-panel-container" id="side-panel-container"
class:open={$sidePanelStore.open} class:open={$sidePanelStore.open}
use:clickOutside={{ use:clickOutside={autoCloseSidePanel ? sidePanelStore.actions.close : null}
callback: autoCloseSidePanel ? sidePanelStore.actions.close : null,
allowedType: "mousedown",
}}
class:builder={$builderStore.inBuilder} class:builder={$builderStore.inBuilder}
> >
<div class="side-panel-header"> <div class="side-panel-header">

View file

@ -121,8 +121,10 @@
const onContextMenu = e => { const onContextMenu = e => {
e.preventDefault() e.preventDefault()
setTimeout(() => {
ui.actions.blur() ui.actions.blur()
open = !open open = !open
}, 10)
} }
const sortAscending = () => { const sortAscending = () => {