1
0
Fork 0
mirror of synced 2024-09-20 11:27:56 +12:00

More improvements

This commit is contained in:
Andrew Kingston 2024-08-02 16:44:18 +01:00
parent 0ba00a5117
commit 1bce9855d0
No known key found for this signature in database
4 changed files with 38 additions and 74 deletions

View file

@ -5,8 +5,8 @@
import { import {
isGridEvent, isGridEvent,
getGridParentID, getGridParentID,
gridCSSVars, GridParams,
GridVars, getGridVar,
} from "utils/grid" } from "utils/grid"
// Smallest possible 1x1 transparent GIF // Smallest possible 1x1 transparent GIF
@ -19,11 +19,12 @@
let id let id
// Grid CSS variables // Grid CSS variables
$: device = $builderStore.previewDevice
$: vars = { $: vars = {
colStart: $gridCSSVars[GridVars.ColStart], colStart: getGridVar(device, GridParams.ColStart),
colEnd: $gridCSSVars[GridVars.ColEnd], colEnd: getGridVar(device, GridParams.ColEnd),
rowStart: $gridCSSVars[GridVars.RowStart], rowStart: getGridVar(device, GridParams.RowStart),
rowEnd: $gridCSSVars[GridVars.RowEnd], rowEnd: getGridVar(device, GridParams.RowEnd),
} }
// Some memoisation of primitive types for performance // Some memoisation of primitive types for performance

View file

@ -1,17 +1,28 @@
<script> <script>
import { Icon } from "@budibase/bbui" import { Icon } from "@budibase/bbui"
import { builderStore } from "stores" import { builderStore } from "stores"
import { getGridVarValue } from "utils/grid"
export let style export let style
export let value export let value
export let icon export let icon
export let title export let title
export let gridStyles
export let componentId export let componentId
$: currentValue = getGridVarValue(gridStyles, style) // Needs to update in real time
let currentValue
$: fetchCurrentValue(componentId, style)
$: active = currentValue === value $: active = currentValue === value
const fetchCurrentValue = (id, style) => {
const node = document.getElementsByClassName(`${id}-dom`)[0]?.parentNode
if (!node) {
return null
}
const styles = getComputedStyle(node)
currentValue = styles.getPropertyValue(style)
}
</script> </script>
<!-- svelte-ignore a11y-no-static-element-interactions --> <!-- svelte-ignore a11y-no-static-element-interactions -->

View file

@ -7,7 +7,7 @@
import { builderStore, componentStore, dndIsDragging } from "stores" import { builderStore, componentStore, dndIsDragging } from "stores"
import { Utils } from "@budibase/frontend-core" import { Utils } from "@budibase/frontend-core"
import { findComponentParent } from "utils/components" import { findComponentParent } from "utils/components"
import { gridCSSVars, GridVars } from "utils/grid" import { getGridVar, GridParams } from "utils/grid"
const verticalOffset = 36 const verticalOffset = 36
const horizontalOffset = 2 const horizontalOffset = 2
@ -44,9 +44,9 @@
insideGrid && insideGrid &&
(definition?.grid?.hAlign !== "stretch" || (definition?.grid?.hAlign !== "stretch" ||
definition?.grid?.vAlign !== "stretch") definition?.grid?.vAlign !== "stretch")
$: gridHAlignVar = $gridCSSVars[GridVars.HAlign] $: device = $builderStore.previewDevice
$: gridVAlignVar = $gridCSSVars[GridVars.VAlign] $: gridHAlignVar = getGridVar(device, GridParams.HAlign)
$: gridStyles = $state?.styles $: gridVAlignVar = getGridVar(device, GridParams.VAlign)
const getBarSettings = definition => { const getBarSettings = definition => {
let allSettings = [] let allSettings = []
@ -156,7 +156,6 @@
value="start" value="start"
icon="AlignLeft" icon="AlignLeft"
title="Align left" title="Align left"
{gridStyles}
{componentId} {componentId}
/> />
<GridStylesButton <GridStylesButton
@ -164,7 +163,6 @@
value="center" value="center"
icon="AlignCenter" icon="AlignCenter"
title="Align center" title="Align center"
{gridStyles}
{componentId} {componentId}
/> />
<GridStylesButton <GridStylesButton
@ -172,7 +170,6 @@
value="end" value="end"
icon="AlignRight" icon="AlignRight"
title="Align right" title="Align right"
{gridStyles}
{componentId} {componentId}
/> />
<GridStylesButton <GridStylesButton
@ -180,7 +177,6 @@
value="stretch" value="stretch"
icon="MoveLeftRight" icon="MoveLeftRight"
title="Stretch horizontally" title="Stretch horizontally"
{gridStyles}
{componentId} {componentId}
/> />
<div class="divider" /> <div class="divider" />
@ -189,7 +185,6 @@
value="start" value="start"
icon="AlignTop" icon="AlignTop"
title="Align top" title="Align top"
{gridStyles}
{componentId} {componentId}
/> />
<GridStylesButton <GridStylesButton
@ -197,7 +192,6 @@
value="center" value="center"
icon="AlignMiddle" icon="AlignMiddle"
title="Align middle" title="Align middle"
{gridStyles}
{componentId} {componentId}
/> />
<GridStylesButton <GridStylesButton
@ -205,7 +199,6 @@
value="end" value="end"
icon="AlignBottom" icon="AlignBottom"
title="Align bottom" title="Align bottom"
{gridStyles}
{componentId} {componentId}
/> />
<GridStylesButton <GridStylesButton
@ -213,7 +206,6 @@
value="stretch" value="stretch"
icon="MoveUpDown" icon="MoveUpDown"
title="Stretch vertically" title="Stretch vertically"
{gridStyles}
{componentId} {componentId}
/> />
<div class="divider" /> <div class="divider" />

View file

@ -1,5 +1,4 @@
import { builderStore } from "stores" import { builderStore } from "stores"
import { derived } from "svelte/store"
import { buildStyleString } from "utils/styleable.js" import { buildStyleString } from "utils/styleable.js"
/** /**
@ -11,10 +10,13 @@ import { buildStyleString } from "utils/styleable.js"
* *
* Component definitions define their default layout preference via the * Component definitions define their default layout preference via the
* `grid.hAlign` and `grid.vAlign` keys in the manifest. * `grid.hAlign` and `grid.vAlign` keys in the manifest.
*
* We also apply grid-[mobile/desktop]-grow CSS classes to component wrapper
* DOM nodes to use later in selectors, to control the sizing of children.
*/ */
// Enum representing the different CSS variables we use for grid metadata // Enum representing the different CSS variables we use for grid metadata
export const GridVars = { export const GridParams = {
HAlign: "h-align", HAlign: "h-align",
VAlign: "v-align", VAlign: "v-align",
ColStart: "col-start", ColStart: "col-start",
@ -35,38 +37,8 @@ const Devices = {
Mobile: "mobile", Mobile: "mobile",
} }
// A derived map of all CSS variables for the current device
const previewDevice = derived(builderStore, $store => $store.previewDevice)
export const gridCSSVars = derived(previewDevice, $device => {
const device = $device === Devices.Mobile ? Devices.Mobile : Devices.Desktop
let vars = {}
for (let type of Object.values(GridVars)) {
vars[type] = `--grid-${device}-${type}`
}
return vars
})
// Builds a CSS variable name for a certain piece of grid metadata // Builds a CSS variable name for a certain piece of grid metadata
export const getGridCSSVar = (device, type) => `--grid-${device}-${type}` export const getGridVar = (device, param) => `--grid-${device}-${param}`
// Generates the CSS variable for a certain grid param suffix, for the other
// device variant than the one included in this variable
export const getOtherDeviceGridVar = cssVar => {
if (cssVar.includes(Devices.Desktop)) {
return cssVar.replace(Devices.Desktop, Devices.Mobile)
} else {
return cssVar.replace(Devices.Mobile, Devices.Desktop)
}
}
// Gets the default value for a certain grid CSS variable
export const getDefaultGridVarValue = cssVar => {
if (cssVar.includes("align")) {
return cssVar.includes("-h-") ? "stretch" : "center"
} else {
return cssVar.endsWith("-start") ? 1 : 2
}
}
// Determines whether a JS event originated from immediately within a grid // Determines whether a JS event originated from immediately within a grid
export const isGridEvent = e => { export const isGridEvent = e => {
@ -92,32 +64,18 @@ export const getGridParentID = node => {
return node?.parentNode?.closest(".grid")?.parentNode.dataset.id return node?.parentNode?.closest(".grid")?.parentNode.dataset.id
} }
// Gets the current value of a certain grid CSS variable for a component
export const getGridVarValue = (styles, variable) => {
// Try the desired variable
let val = styles?.variables?.[variable]
// Otherwise try the other device variables
if (!val) {
val = styles?.[getOtherDeviceGridVar(variable)]
}
// Otherwise use the default
return val ? val : getDefaultGridVarValue(variable)
}
// Svelte action to apply required class names and styles to our component // Svelte action to apply required class names and styles to our component
// wrappers // wrappers
export const gridLayout = (node, metadata) => { export const gridLayout = (node, metadata) => {
let selectComponent let selectComponent
// Applies the required listeners, CSS and classes to a component DOM node
const applyMetadata = metadata => { const applyMetadata = metadata => {
const { id, styles, interactive, errored, definition } = metadata const { id, styles, interactive, errored, definition } = metadata
consol.log(styles)
// Callback to select the component when clicking on the wrapper // Callback to select the component when clicking on the wrapper
selectComponent = e => { selectComponent = e => {
e.preventDefault() e.stopPropagation()
builderStore.actions.selectComponent(id) builderStore.actions.selectComponent(id)
} }
@ -145,13 +103,14 @@ export const gridLayout = (node, metadata) => {
node.style = buildStyleString(vars) node.style = buildStyleString(vars)
// Toggle classes to specify whether our children should fill // Toggle classes to specify whether our children should fill
const desktopVar = getGridCSSVar(Devices.Desktop, GridVars.VAlign)
const mobileVar = getGridCSSVar(Devices.Mobile, GridVars.VAlign)
node.classList.toggle( node.classList.toggle(
GridClasses.DesktopFill, GridClasses.DesktopFill,
vars[desktopVar] === "stretch" vars[getGridVar(Devices.Desktop, GridParams.VAlign)] === "stretch"
)
node.classList.toggle(
GridClasses.MobileFill,
vars[getGridVar(Devices.Mobile, GridParams.VAlign)] === "stretch"
) )
node.classList.toggle(GridClasses.MobileFill, vars[mobileVar] === "stretch")
// Add a listener to select this node on click // Add a listener to select this node on click
if (interactive) { if (interactive) {
@ -159,6 +118,7 @@ export const gridLayout = (node, metadata) => {
} }
} }
// Removes the previously set up listeners
const removeListeners = () => { const removeListeners = () => {
node.removeEventListener("click", selectComponent) node.removeEventListener("click", selectComponent)
} }