2020-11-13 16:42:32 +01:00
|
|
|
<script>
|
2020-11-20 10:50:10 +01:00
|
|
|
import { getContext, setContext } from "svelte"
|
2021-01-27 16:52:12 +01:00
|
|
|
import { writable, get } from "svelte/store"
|
2020-11-18 20:18:18 +01:00
|
|
|
import * as ComponentLibrary from "@budibase/standard-components"
|
|
|
|
import Router from "./Router.svelte"
|
2021-01-21 11:41:30 +01:00
|
|
|
import { enrichProps, propsAreSame } from "../utils/componentProps"
|
2020-11-30 13:11:50 +01:00
|
|
|
import { bindingStore, builderStore } from "../store"
|
2021-01-27 16:52:12 +01:00
|
|
|
import { hashString } from "../utils/hash"
|
2020-11-13 16:42:32 +01:00
|
|
|
|
2020-11-17 13:08:24 +01:00
|
|
|
export let definition = {}
|
2020-11-13 16:42:32 +01:00
|
|
|
|
2021-01-21 12:31:45 +01:00
|
|
|
let enrichedProps
|
|
|
|
let componentProps
|
2020-11-13 16:42:32 +01:00
|
|
|
|
2021-01-27 16:52:12 +01:00
|
|
|
// Props are hashed when inside the builder preview and used as a key, so that
|
|
|
|
// components fully remount whenever any props change
|
|
|
|
let propsHash = 0
|
|
|
|
|
2021-01-06 11:13:30 +01:00
|
|
|
// Get contexts
|
2020-11-25 19:43:58 +01:00
|
|
|
const dataContext = getContext("data")
|
2020-11-18 20:18:18 +01:00
|
|
|
|
2020-11-25 10:50:51 +01:00
|
|
|
// Create component context
|
|
|
|
const componentStore = writable({})
|
|
|
|
setContext("component", componentStore)
|
2020-11-17 13:08:24 +01:00
|
|
|
|
2020-11-20 10:50:10 +01:00
|
|
|
// Extract component definition info
|
2020-11-25 10:50:51 +01:00
|
|
|
$: constructor = getComponentConstructor(definition._component)
|
2021-01-22 12:08:42 +01:00
|
|
|
$: children = definition._children || []
|
2020-11-24 12:02:10 +01:00
|
|
|
$: id = definition._id
|
2021-01-21 12:31:45 +01:00
|
|
|
$: enrichComponentProps(definition, $dataContext, $bindingStore)
|
2021-01-21 11:41:30 +01:00
|
|
|
$: updateProps(enrichedProps)
|
2021-01-06 11:13:30 +01:00
|
|
|
$: styles = definition._styles
|
|
|
|
|
2020-11-24 12:02:10 +01:00
|
|
|
// Update component context
|
2021-01-22 12:08:42 +01:00
|
|
|
$: componentStore.set({
|
|
|
|
id,
|
|
|
|
children: children.length,
|
2021-01-27 16:52:12 +01:00
|
|
|
styles: { ...styles, id },
|
2021-01-22 12:08:42 +01:00
|
|
|
})
|
2020-11-25 10:50:51 +01:00
|
|
|
|
2021-01-21 11:41:30 +01:00
|
|
|
// Updates the component props.
|
|
|
|
// Most props are deeply compared so that svelte will only trigger reactive
|
|
|
|
// statements on props that have actually changed.
|
|
|
|
const updateProps = props => {
|
2021-01-21 12:31:45 +01:00
|
|
|
if (!props) {
|
|
|
|
return
|
|
|
|
}
|
2021-01-27 16:52:12 +01:00
|
|
|
let propsChanged = false
|
2021-01-21 12:31:45 +01:00
|
|
|
if (!componentProps) {
|
|
|
|
componentProps = {}
|
2021-01-27 16:52:12 +01:00
|
|
|
propsChanged = true
|
2021-01-21 12:31:45 +01:00
|
|
|
}
|
2021-01-21 11:41:30 +01:00
|
|
|
Object.keys(props).forEach(key => {
|
|
|
|
if (!propsAreSame(props[key], componentProps[key])) {
|
2021-01-27 16:52:12 +01:00
|
|
|
propsChanged = true
|
2021-01-21 11:41:30 +01:00
|
|
|
componentProps[key] = props[key]
|
|
|
|
}
|
|
|
|
})
|
2021-01-27 16:52:12 +01:00
|
|
|
if (get(builderStore).inBuilder && propsChanged) {
|
|
|
|
propsHash = hashString(JSON.stringify(componentProps))
|
|
|
|
}
|
2021-01-21 11:41:30 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 10:50:51 +01:00
|
|
|
// Gets the component constructor for the specified component
|
|
|
|
const getComponentConstructor = component => {
|
|
|
|
const split = component?.split("/")
|
|
|
|
const name = split?.[split.length - 1]
|
2021-01-12 21:00:35 +01:00
|
|
|
if (name === "screenslot" && $builderStore.previewType !== "layout") {
|
|
|
|
return Router
|
|
|
|
}
|
|
|
|
return ComponentLibrary[name]
|
2020-11-25 10:50:51 +01:00
|
|
|
}
|
2020-11-30 13:11:50 +01:00
|
|
|
|
2021-01-20 14:49:41 +01:00
|
|
|
// Enriches any string component props using handlebars
|
|
|
|
const enrichComponentProps = async (definition, context, bindingStore) => {
|
|
|
|
enrichedProps = await enrichProps(definition, context, bindingStore)
|
|
|
|
}
|
2020-11-13 16:42:32 +01:00
|
|
|
</script>
|
|
|
|
|
2021-01-21 12:31:45 +01:00
|
|
|
{#if constructor && componentProps}
|
2021-01-27 16:52:12 +01:00
|
|
|
{#key propsHash}
|
|
|
|
<svelte:component this={constructor} {...componentProps}>
|
|
|
|
{#if children.length}
|
|
|
|
{#each children as child (child._id)}
|
|
|
|
<svelte:self definition={child} />
|
|
|
|
{/each}
|
|
|
|
{/if}
|
|
|
|
</svelte:component>
|
|
|
|
{/key}
|
2020-11-13 16:42:32 +01:00
|
|
|
{/if}
|