Update how grid DND styles are applied to remove flashing

This commit is contained in:
Andrew Kingston 2022-10-20 08:43:33 +01:00
parent 0c2e286ca7
commit 1508a5dc3b
4 changed files with 37 additions and 16 deletions

View File

@ -4574,7 +4574,12 @@
"size" "size"
], ],
"illegalChildren": ["grid", "section"], "illegalChildren": ["grid", "section"],
"allowedDirectChildren": [""] "allowedDirectChildren": [""],
"size": {
"width": 800,
"height": 400
},
"showEmptyState": false
}, },
"formblock": { "formblock": {
"name": "Form Block", "name": "Form Block",

View File

@ -91,6 +91,10 @@
let settingsDefinitionMap let settingsDefinitionMap
let missingRequiredSettings = false let missingRequiredSettings = false
// Temporary styles which can be added in the app preview for things like DND.
// We clear these whenever a new instance is received.
let ephemeralStyles
// Set up initial state for each new component instance // Set up initial state for each new component instance
$: initialise(instance) $: initialise(instance)
@ -181,6 +185,7 @@
normal: { normal: {
...instance._styles?.normal, ...instance._styles?.normal,
...additionalStyles, ...additionalStyles,
...ephemeralStyles,
}, },
custom: customCSS, custom: customCSS,
id, id,
@ -214,6 +219,7 @@
return return
} else { } else {
lastInstanceKey = instanceKey lastInstanceKey = instanceKey
ephemeralStyles = null
} }
// Pull definition and constructor // Pull definition and constructor
@ -515,6 +521,7 @@
getRawSettings: () => ({ ...staticSettings, ...dynamicSettings }), getRawSettings: () => ({ ...staticSettings, ...dynamicSettings }),
getDataContext: () => get(context), getDataContext: () => get(context),
reload: () => initialise(instance, true), reload: () => initialise(instance, true),
setEphemeralStyles: styles => (ephemeralStyles = styles),
}) })
} }
}) })

View File

@ -1,14 +1,18 @@
<script> <script>
import { onMount, onDestroy } from "svelte" import { onMount, onDestroy } from "svelte"
import { builderStore, componentStore, screenStore } from "stores" import { builderStore, componentStore } from "stores"
import { Utils } from "@budibase/frontend-core" import { Utils } from "@budibase/frontend-core"
import { findComponentById } from "utils/components.js"
let dragInfo let dragInfo
let gridStyles let gridStyles
$: dragNode = getDOMNode(dragInfo?.id) $: jsonStyles = JSON.stringify(gridStyles)
$: applyStyles(dragNode, gridStyles) $: id = dragInfo?.id
$: instance = componentStore.actions.getComponentInstance(id)
// We don't clear grid styles because that causes flashing, as the component
// will revert to its original position until the save completes.
$: gridStyles && instance?.setEphemeralStyles(gridStyles)
const isChildOfGrid = e => { const isChildOfGrid = e => {
return ( return (
@ -26,15 +30,6 @@
return [...component.children][0] return [...component.children][0]
} }
const applyStyles = (dragNode, gridStyles) => {
if (!dragNode || !gridStyles) {
return
}
Object.entries(gridStyles).forEach(([style, value]) => {
dragNode.style[style] = value
})
}
const processEvent = Utils.throttle((mouseX, mouseY) => { const processEvent = Utils.throttle((mouseX, mouseY) => {
if (!dragInfo?.grid) { if (!dragInfo?.grid) {
return return
@ -68,12 +63,15 @@
if (mode === "move") { if (mode === "move") {
deltaY = Math.min(Math.max(deltaY, rowDeltaMin), rowDeltaMax) deltaY = Math.min(Math.max(deltaY, rowDeltaMin), rowDeltaMax)
deltaX = Math.min(Math.max(deltaX, colDeltaMin), colDeltaMax) deltaX = Math.min(Math.max(deltaX, colDeltaMin), colDeltaMax)
gridStyles = { const newStyles = {
"grid-row-start": rowStart + deltaY, "grid-row-start": rowStart + deltaY,
"grid-row-end": rowEnd + deltaY, "grid-row-end": rowEnd + deltaY,
"grid-column-start": colStart + deltaX, "grid-column-start": colStart + deltaX,
"grid-column-end": colEnd + deltaX, "grid-column-end": colEnd + deltaX,
} }
if (JSON.stringify(newStyles) !== jsonStyles) {
gridStyles = newStyles
}
} else if (mode === "resize") { } else if (mode === "resize") {
let newStyles = {} let newStyles = {}
if (side === "right") { if (side === "right") {
@ -97,7 +95,9 @@
newStyles["grid-column-start"] = Math.min(colStart + deltaX, colEnd - 1) newStyles["grid-column-start"] = Math.min(colStart + deltaX, colEnd - 1)
newStyles["grid-row-start"] = Math.min(rowStart + deltaY, rowEnd - 1) newStyles["grid-row-start"] = Math.min(rowStart + deltaY, rowEnd - 1)
} }
gridStyles = newStyles if (JSON.stringify(newStyles) !== jsonStyles) {
gridStyles = newStyles
}
} }
}, 100) }, 100)

View File

@ -48,6 +48,9 @@ const createComponentStore = () => {
) )
const registerInstance = (id, instance) => { const registerInstance = (id, instance) => {
if (!id) {
return
}
store.update(state => { store.update(state => {
// If this is a custom component, flag it so we can reload this component // If this is a custom component, flag it so we can reload this component
// later if required // later if required
@ -67,6 +70,9 @@ const createComponentStore = () => {
} }
const unregisterInstance = id => { const unregisterInstance = id => {
if (!id) {
return
}
store.update(state => { store.update(state => {
// Remove from custom component map if required // Remove from custom component map if required
const component = state.mountedComponents[id]?.instance?.component const component = state.mountedComponents[id]?.instance?.component
@ -136,6 +142,9 @@ const createComponentStore = () => {
} }
const getComponentInstance = id => { const getComponentInstance = id => {
if (!id) {
return null
}
return get(store).mountedComponents[id] return get(store).mountedComponents[id]
} }