From 79993bafda569da99081566e365eb0b9e0edf04e Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 11 Jun 2021 08:05:49 +0100 Subject: [PATCH] Add placeholders and empty states automatically as required to any empty client components --- .../client/src/components/ClientApp.svelte | 2 +- .../client/src/components/Component.svelte | 55 +++++++++++++------ packages/client/src/components/Screen.svelte | 2 +- .../components/preview/HoverIndicator.svelte | 4 +- packages/client/src/store/builder.js | 4 +- packages/client/src/utils/styleable.js | 5 ++ .../standard-components/src/Container.svelte | 22 +------- .../src/DataProvider.svelte | 8 ++- .../src/Placeholder.svelte | 21 +++++++ .../standard-components/src/Repeater.svelte | 39 ++++--------- .../src/StackedList.svelte | 2 +- .../src/charts/ApexChart.svelte | 8 +-- packages/standard-components/src/helpers.js | 24 -------- packages/standard-components/src/index.js | 4 ++ 14 files changed, 96 insertions(+), 104 deletions(-) create mode 100644 packages/standard-components/src/Placeholder.svelte diff --git a/packages/client/src/components/ClientApp.svelte b/packages/client/src/components/ClientApp.svelte index a8a5c1515c..684d5ed61c 100644 --- a/packages/client/src/components/ClientApp.svelte +++ b/packages/client/src/components/ClientApp.svelte @@ -65,7 +65,7 @@ >
- +
diff --git a/packages/client/src/components/Component.svelte b/packages/client/src/components/Component.svelte index 061b954342..b327232b21 100644 --- a/packages/client/src/components/Component.svelte +++ b/packages/client/src/components/Component.svelte @@ -6,8 +6,9 @@ import { enrichProps, propsAreSame } from "../utils/componentProps" import { builderStore } from "../store" import { hashString } from "../utils/hash" + import Manifest from "@budibase/standard-components/manifest.json" - export let definition = {} + export let instance = {} // Props that will be passed to the component instance let componentProps @@ -28,26 +29,29 @@ const componentStore = writable({}) setContext("component", componentStore) - // Extract component definition info - $: constructor = getComponentConstructor(definition._component) - $: children = definition._children || [] - $: id = definition._id - $: name = definition._instanceName - $: updateComponentProps(definition, $context) - $: styles = definition._styles - $: transition = definition._transition + // Extract component instance info + $: constructor = getComponentConstructor(instance._component) + $: definition = getComponentDefinition(instance._component) + $: children = instance._children || [] + $: id = instance._id + $: name = instance._instanceName + $: empty = + !children.length && definition?.hasChildren && $builderStore.inBuilder + $: updateComponentProps(instance, $context) $: selected = $builderStore.inBuilder && - $builderStore.selectedComponentId === definition._id + $builderStore.selectedComponentId === instance._id // Update component context $: componentStore.set({ id, children: children.length, - styles: { ...styles, id }, - transition, + styles: { ...instance._styles, id, empty }, + empty, + transition: instance._transition, selected, props: componentProps, + name, }) // Gets the component constructor for the specified component @@ -60,14 +64,20 @@ return ComponentLibrary[name] } + const getComponentDefinition = component => { + const prefix = "@budibase/standard-components/" + const type = component?.replace(prefix, "") + return type ? Manifest[type] : null + } + // Enriches any string component props using handlebars - const updateComponentProps = (definition, context) => { + const updateComponentProps = (instance, context) => { // Record the timestamp so we can reference it after enrichment latestUpdateTime = Date.now() const enrichmentTime = latestUpdateTime // Enrich props with context - const enrichedProps = enrichProps(definition, context) + const enrichedProps = enrichProps(instance, context) // Abandon this update if a newer update has started if (enrichmentTime !== latestUpdateTime) { @@ -100,14 +110,21 @@ } -
+
{#key propsHash} {#if constructor && componentProps} {#if children.length} {#each children as child (child._id)} - + {/each} + {:else if empty} +
{name}
{/if}
{/if} @@ -115,7 +132,11 @@
diff --git a/packages/client/src/components/Screen.svelte b/packages/client/src/components/Screen.svelte index f97fcb52af..b1efbf875b 100644 --- a/packages/client/src/components/Screen.svelte +++ b/packages/client/src/components/Screen.svelte @@ -22,6 +22,6 @@ {#key screenDefinition?._id} - + {/key} diff --git a/packages/client/src/components/preview/HoverIndicator.svelte b/packages/client/src/components/preview/HoverIndicator.svelte index 7dd1c78dae..43c22725b5 100644 --- a/packages/client/src/components/preview/HoverIndicator.svelte +++ b/packages/client/src/components/preview/HoverIndicator.svelte @@ -20,12 +20,12 @@ onMount(() => { document.addEventListener("mouseover", onMouseOver) - document.addEventListener("mouseleave", onMouseLeave) + window.addEventListener("mouseleave", onMouseLeave) }) onDestroy(() => { document.removeEventListener("mouseover", onMouseOver) - document.removeEventListener("mouseleave", onMouseLeave) + window.removeEventListener("mouseleave", onMouseLeave) }) diff --git a/packages/client/src/store/builder.js b/packages/client/src/store/builder.js index afaa9ee3ec..57b6b289f0 100644 --- a/packages/client/src/store/builder.js +++ b/packages/client/src/store/builder.js @@ -1,5 +1,5 @@ import { writable, derived } from "svelte/store" -import manifest from "@budibase/standard-components/manifest.json" +import Manifest from "@budibase/standard-components/manifest.json" const dispatchEvent = (type, data) => { window.dispatchEvent( @@ -46,7 +46,7 @@ const createBuilderStore = () => { const component = findComponentById(asset?.props, selectedComponentId) const prefix = "@budibase/standard-components/" const type = component?._component?.replace(prefix, "") - const definition = type ? manifest[type] : null + const definition = type ? Manifest[type] : null return { ...$state, selectedComponent: component, diff --git a/packages/client/src/utils/styleable.js b/packages/client/src/utils/styleable.js index 4ea92a3acd..30af3413bf 100644 --- a/packages/client/src/utils/styleable.js +++ b/packages/client/src/utils/styleable.js @@ -35,6 +35,11 @@ export const styleable = (node, styles = {}) => { // Applies a style string to a DOM node const applyStyles = styleString => { + // Apply empty border if required + if (newStyles.empty) { + styleString += "border: 2px dashed rgba(0, 0, 0, 0.25);" + } + node.style = styleString node.dataset.componentId = componentId } diff --git a/packages/standard-components/src/Container.svelte b/packages/standard-components/src/Container.svelte index 3288f60e7f..cb4a982643 100644 --- a/packages/standard-components/src/Container.svelte +++ b/packages/standard-components/src/Container.svelte @@ -9,8 +9,6 @@ export let vAlign export let size - let element - $: directionClass = direction ? `valid-container direction-${direction}` : "" $: hAlignClass = hAlign ? `hAlign-${hAlign}` : "" $: vAlignClass = vAlign ? `vAlign-${vAlign}` : "" @@ -19,23 +17,13 @@
- {#if !$component.children && $builderStore.inBuilder} -
Add some content
- {:else} - - {/if} +
diff --git a/packages/standard-components/src/DataProvider.svelte b/packages/standard-components/src/DataProvider.svelte index 7b759b5348..2a51c3ef2e 100644 --- a/packages/standard-components/src/DataProvider.svelte +++ b/packages/standard-components/src/DataProvider.svelte @@ -7,6 +7,8 @@ luceneSort, luceneLimit, } from "./lucene" + import Placeholder from "./Placeholder.svelte" + import Container from "./Container.svelte" export let dataSource export let filter @@ -231,7 +233,11 @@
{:else} - + {#if !$component.children} + + {:else} + + {/if} {#if paginate && internalTable}