From de183d5c78dfd009ea2fb1b76789c0ecf11ea99f Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Tue, 30 Jul 2024 16:48:54 +0100 Subject: [PATCH] Refactor and tidy up --- packages/client/manifest.json | 7 +++++++ .../client/src/components/Component.svelte | 19 ++++++++++++++++++ .../app/container/GridContainer.svelte | 7 +++++++ .../src/components/preview/DNDHandler.svelte | 12 ++--------- .../preview/DNDPlaceholderOverlay.svelte | 4 ++-- .../components/preview/GridDNDHandler.svelte | 15 ++------------ .../components/preview/IndicatorSet.svelte | 15 +++++++------- .../src/components/preview/SettingsBar.svelte | 11 ++++++---- packages/client/src/utils/domDebounce.js | 14 ------------- packages/client/src/utils/grid.js | 20 +++++++++++++++++++ 10 files changed, 74 insertions(+), 50 deletions(-) delete mode 100644 packages/client/src/utils/domDebounce.js create mode 100644 packages/client/src/utils/grid.js diff --git a/packages/client/manifest.json b/packages/client/manifest.json index f282cf17a4..8231c0005f 100644 --- a/packages/client/manifest.json +++ b/packages/client/manifest.json @@ -117,6 +117,9 @@ "width": 400, "height": 200 }, + "grid": { + "fill": true + }, "styles": ["padding", "size", "background", "border", "shadow"], "settings": [ { @@ -561,6 +564,10 @@ "width": 105, "height": 32 }, + "grid": { + "hAlign": "center", + "vAlign": "center" + }, "settings": [ { "type": "text", diff --git a/packages/client/src/components/Component.svelte b/packages/client/src/components/Component.svelte index eaab3c56a9..340cc6e577 100644 --- a/packages/client/src/components/Component.svelte +++ b/packages/client/src/components/Component.svelte @@ -198,7 +198,9 @@ $: darkMode = !currentTheme?.includes("light") // Build meta styles and stringify to apply to the wrapper node + $: definitionMetaStyles = getDefinitonMetaStyles(definition) $: metaStyles = { + ...definitionMetaStyles, ...instance._styles?.meta, ...ephemeralStyles, } @@ -618,6 +620,22 @@ builderStore.actions.selectComponent(id) } + // Util to generate meta styles based on component definition + const alignToStyleMap = { + start: "flex-start", + center: "center", + end: "flex-end", + stretch: "stretch", + } + const getDefinitonMetaStyles = definition => { + const gridHAlign = definition.grid?.hAlign || "stretch" + const gridVAlign = definition.grid?.vAlign || "center" + return { + ["align-items"]: alignToStyleMap[gridHAlign], + ["justify-content"]: alignToStyleMap[gridVAlign], + } + } + onMount(() => { // Register this component instance for external access if ($appStore.isDevApp) { @@ -661,6 +679,7 @@ class:parent={hasChildren} class:block={isBlock} class:error={errorState} + class:fill={definition.grid?.fill} data-id={id} data-name={name} data-icon={icon} diff --git a/packages/client/src/components/app/container/GridContainer.svelte b/packages/client/src/components/app/container/GridContainer.svelte index 50f48b6dc1..6ee761a681 100644 --- a/packages/client/src/components/app/container/GridContainer.svelte +++ b/packages/client/src/components/app/container/GridContainer.svelte @@ -123,4 +123,11 @@ --row-start: var(--grid-mobile-row-start, var(--grid-desktop-row-start, 1)); --row-end: var(--grid-mobile-row-end, var(--grid-desktop-row-end, 2)); } + + /* Handle grid children which need to fill the outer component wrapper */ + .grid :global(> .component.fill > *) { + width: 100%; + height: 100%; + flex: 1 1 0; + } diff --git a/packages/client/src/components/preview/DNDHandler.svelte b/packages/client/src/components/preview/DNDHandler.svelte index 767efc9e3a..bdd538748b 100644 --- a/packages/client/src/components/preview/DNDHandler.svelte +++ b/packages/client/src/components/preview/DNDHandler.svelte @@ -13,6 +13,7 @@ import { Utils } from "@budibase/frontend-core" import { findComponentById } from "utils/components.js" import { DNDPlaceholderID } from "constants" + import { isGridEvent } from "utils/grid" const ThrottleRate = 130 @@ -25,15 +26,6 @@ // Local flag for whether we are awaiting an async drop event let dropping = false - // Util to check if a DND event originates from a grid (or inside a grid). - // This is important as we do not handle grid DND in this handler. - const isGridEvent = e => { - return e.target - ?.closest?.(".component") - ?.parentNode?.closest?.(".component") - ?.childNodes[0]?.classList.contains("grid") - } - // Util to get the inner DOM node by a component ID const getDOMNode = id => { return document.getElementsByClassName(`${id}-dom`)[0] @@ -267,7 +259,7 @@ // Check if we're adding a new component rather than moving one if (source.newComponentType) { dropping = true - await builderStore.actions.dropNewComponent( + builderStore.actions.dropNewComponent( source.newComponentType, drop.parent, drop.index diff --git a/packages/client/src/components/preview/DNDPlaceholderOverlay.svelte b/packages/client/src/components/preview/DNDPlaceholderOverlay.svelte index 0be7faff1b..0ad4280e07 100644 --- a/packages/client/src/components/preview/DNDPlaceholderOverlay.svelte +++ b/packages/client/src/components/preview/DNDPlaceholderOverlay.svelte @@ -1,7 +1,7 @@ diff --git a/packages/client/src/components/preview/SettingsBar.svelte b/packages/client/src/components/preview/SettingsBar.svelte index c5109c6bca..e80cb0eaca 100644 --- a/packages/client/src/components/preview/SettingsBar.svelte +++ b/packages/client/src/components/preview/SettingsBar.svelte @@ -4,7 +4,8 @@ import SettingsColorPicker from "./SettingsColorPicker.svelte" import SettingsPicker from "./SettingsPicker.svelte" import { builderStore, componentStore, dndIsDragging } from "stores" - import { domDebounce } from "utils/domDebounce" + import { Utils } from "@budibase/frontend-core" + import { isGridChild } from "utils/grid" const verticalOffset = 36 const horizontalOffset = 2 @@ -49,8 +50,10 @@ return } const id = $builderStore.selectedComponentId - const parent = document.getElementsByClassName(id)?.[0] - const element = parent?.children?.[0] + let element = document.getElementsByClassName(id)?.[0] + if (!isGridChild(element)) { + element = element?.children?.[0] + } // The settings bar is higher in the dom tree than the selection indicators // as we want to be able to render the settings bar wider than the screen, @@ -111,7 +114,7 @@ measured = true } } - const debouncedUpdate = domDebounce(updatePosition) + const debouncedUpdate = Utils.domDebounce(updatePosition) onMount(() => { debouncedUpdate() diff --git a/packages/client/src/utils/domDebounce.js b/packages/client/src/utils/domDebounce.js deleted file mode 100644 index b15d2698b4..0000000000 --- a/packages/client/src/utils/domDebounce.js +++ /dev/null @@ -1,14 +0,0 @@ -export const domDebounce = (callback, extractParams = x => x) => { - let active = false - let lastParams - return (...params) => { - lastParams = extractParams(...params) - if (!active) { - active = true - requestAnimationFrame(() => { - callback(lastParams) - active = false - }) - } - } -} diff --git a/packages/client/src/utils/grid.js b/packages/client/src/utils/grid.js new file mode 100644 index 0000000000..03c48505ce --- /dev/null +++ b/packages/client/src/utils/grid.js @@ -0,0 +1,20 @@ +export const isGridEvent = e => { + return ( + e.target + .closest?.(".component") + ?.parentNode.closest(".component") + ?.childNodes[0]?.classList?.contains("grid") || + e.target.classList.contains("anchor") + ) +} + +export const isGridChild = node => { + return node + ?.closest(".component") + ?.parentNode.closest(".component") + ?.childNodes[0]?.classList?.contains("grid") +} + +export const getGridParentID = node => { + return node?.closest(".grid")?.parentNode.dataset.id +}