Refactor clickoutside fix flashing side panel issues, but also fix annoying drag-outside closing actions
This commit is contained in:
parent
df25ff307c
commit
ff23825241
|
@ -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 {
|
||||||
|
|
|
@ -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">
|
||||||
|
|
|
@ -121,8 +121,10 @@
|
||||||
|
|
||||||
const onContextMenu = e => {
|
const onContextMenu = e => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
ui.actions.blur()
|
setTimeout(() => {
|
||||||
open = !open
|
ui.actions.blur()
|
||||||
|
open = !open
|
||||||
|
}, 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
const sortAscending = () => {
|
const sortAscending = () => {
|
||||||
|
|
Loading…
Reference in New Issue