From f2d1730139ade216359f86e147ccc4b45eee2e7d Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Tue, 11 Oct 2022 08:52:45 +0100 Subject: [PATCH] Improve column/row detection by appending an ephemeral child component and tune timings --- .../client/src/components/Component.svelte | 18 ++++++---------- .../src/components/preview/DNDHandler.svelte | 21 +++++++++++++++---- .../src/components/preview/Indicator.svelte | 4 ++-- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/packages/client/src/components/Component.svelte b/packages/client/src/components/Component.svelte index c3d6de5cae..9466bc461b 100644 --- a/packages/client/src/components/Component.svelte +++ b/packages/client/src/components/Component.svelte @@ -489,21 +489,15 @@ display: contents; } .component :global(> *) { - transition: padding 250ms ease, border 250ms ease; + transition: padding 260ms ease, border 260ms ease; } .component.explode :global(> *) { - padding: 16px !important; - gap: 16px !important; - border: 2px dashed var(--spectrum-global-color-gray-400) !important; + padding: 12px !important; + gap: 12px !important; + border: 2px dotted var(--spectrum-global-color-gray-400) !important; border-radius: 4px !important; } - .interactive :global(*:hover) { - cursor: pointer; - } - .draggable :global(*:hover) { - cursor: grab; - } - .editing :global(*:hover) { - cursor: auto; + .interactive :global(*) { + cursor: default; } diff --git a/packages/client/src/components/preview/DNDHandler.svelte b/packages/client/src/components/preview/DNDHandler.svelte index 8dd9a666bf..01929d58d8 100644 --- a/packages/client/src/components/preview/DNDHandler.svelte +++ b/packages/client/src/components/preview/DNDHandler.svelte @@ -102,14 +102,21 @@ return } + // Append an ephemeral div to allow us to determine layout if only one + // child exists + let ephemeralDiv + if (node.children.length === 1) { + ephemeralDiv = document.createElement("div") + ephemeralDiv.classList.add("placeholder") + node.appendChild(ephemeralDiv) + } + // We're now hovering over something which accepts children and is not // empty, so we need to work out where to inside the placeholder // Calculate the coordinates of various locations on each child. - // We include the placeholder component in this as it guarantees we have - // at least 2 child components, and therefore guarantee there is no - // ambiguity in the layout. const childCoords = [...(node.children || [])].map(node => { - const bounds = node.children[0].getBoundingClientRect() + const child = node.children?.[0] || node + const bounds = child.getBoundingClientRect() return { placeholder: node.classList.contains("placeholder"), centerX: bounds.left + bounds.width / 2, @@ -121,6 +128,12 @@ } }) + // Now that we've calculated the position of the children, we no longer need + // the ephemeral div + if (ephemeralDiv) { + node.removeChild(ephemeralDiv) + } + // Calculate the variance between each set of positions on the children const variances = Object.keys(childCoords[0]) .filter(x => x !== "placeholder") diff --git a/packages/client/src/components/preview/Indicator.svelte b/packages/client/src/components/preview/Indicator.svelte index 1cb669bdc4..0b5b95e00b 100644 --- a/packages/client/src/components/preview/Indicator.svelte +++ b/packages/client/src/components/preview/Indicator.svelte @@ -19,8 +19,8 @@