From ff23825241f1ccd18ebcabbe81855a5ebf1ae2eb Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 2 May 2024 16:46:58 +0100 Subject: [PATCH] Refactor clickoutside fix flashing side panel issues, but also fix annoying drag-outside closing actions --- packages/bbui/src/Actions/click_outside.js | 40 ++++++++++++------- .../client/src/components/app/Layout.svelte | 5 +-- .../components/grid/cells/HeaderCell.svelte | 6 ++- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/packages/bbui/src/Actions/click_outside.js b/packages/bbui/src/Actions/click_outside.js index 535473f395..1a8baa1bfc 100644 --- a/packages/bbui/src/Actions/click_outside.js +++ b/packages/bbui/src/Actions/click_outside.js @@ -9,14 +9,13 @@ const conditionallyIgnoredClasses = [ ".spectrum-Popover", ] let clickHandlers = [] +let candidateTarget /** * Handle a body click event */ const handleClick = event => { - // Treat right clicks (context menu events) as normal clicks - const eventType = event.type === "contextmenu" ? "click" : event.type - + console.log("CLOSE") // Ignore click if this is an ignored class if (event.target.closest('[data-ignore-click-outside="true"]')) { return @@ -29,11 +28,6 @@ const handleClick = event => { // Process handlers 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 if (handler.element.contains(event.target)) { return @@ -51,17 +45,34 @@ const handleClick = event => { handler.callback?.(event) }) } -document.documentElement.addEventListener("click", handleClick, true) -document.documentElement.addEventListener("mousedown", handleClick, true) -document.documentElement.addEventListener("contextmenu", handleClick, true) + +const handleMouseUp = e => { + 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 */ -const updateHandler = (id, element, anchor, callback, allowedType) => { +const updateHandler = (id, element, anchor, callback) => { let existingHandler = clickHandlers.find(x => x.id === id) if (!existingHandler) { - clickHandlers.push({ id, element, anchor, callback, allowedType }) + clickHandlers.push({ id, element, anchor, callback }) } else { existingHandler.callback = callback } @@ -88,8 +99,7 @@ export default (element, opts) => { const callback = newOpts?.callback || (typeof newOpts === "function" ? newOpts : null) const anchor = newOpts?.anchor || element - const allowedType = newOpts?.allowedType || "click" - updateHandler(id, element, anchor, callback, allowedType) + updateHandler(id, element, anchor, callback) } update(opts) return { diff --git a/packages/client/src/components/app/Layout.svelte b/packages/client/src/components/app/Layout.svelte index eb1e0022cc..72da3e9012 100644 --- a/packages/client/src/components/app/Layout.svelte +++ b/packages/client/src/components/app/Layout.svelte @@ -324,10 +324,7 @@
diff --git a/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte b/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte index af1f1b6b78..1b40966e0d 100644 --- a/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte +++ b/packages/frontend-core/src/components/grid/cells/HeaderCell.svelte @@ -121,8 +121,10 @@ const onContextMenu = e => { e.preventDefault() - ui.actions.blur() - open = !open + setTimeout(() => { + ui.actions.blur() + open = !open + }, 10) } const sortAscending = () => {