2020-11-13 16:42:32 +01:00
|
|
|
<script>
|
2020-11-20 10:50:10 +01:00
|
|
|
import { getContext, setContext } from "svelte"
|
2020-11-24 10:31:54 +01:00
|
|
|
import { writable } from "svelte/store"
|
2020-11-18 20:18:18 +01:00
|
|
|
import * as ComponentLibrary from "@budibase/standard-components"
|
|
|
|
import Router from "./Router.svelte"
|
2020-11-25 10:50:51 +01:00
|
|
|
import { enrichProps } from "../utils/componentProps"
|
2020-11-30 13:11:50 +01:00
|
|
|
import { bindingStore, builderStore } from "../store"
|
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
|
|
|
|
2020-11-25 10:50:51 +01:00
|
|
|
// Get local data binding context
|
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)
|
2020-11-24 10:31:54 +01:00
|
|
|
$: children = definition._children
|
2020-11-24 12:02:10 +01:00
|
|
|
$: id = definition._id
|
2020-11-25 19:43:58 +01:00
|
|
|
$: enrichedProps = enrichProps(definition, $dataContext, $bindingStore)
|
2020-11-20 10:50:10 +01:00
|
|
|
|
2020-11-24 12:02:10 +01:00
|
|
|
// Update component context
|
|
|
|
// ID is duplicated inside style so that the "styleable" helper can set
|
|
|
|
// an ID data tag for unique reference to components
|
2020-11-25 10:50:51 +01:00
|
|
|
$: componentStore.set({ id, styles: { ...definition._styles, id } })
|
|
|
|
|
|
|
|
// Gets the component constructor for the specified component
|
|
|
|
const getComponentConstructor = component => {
|
|
|
|
const split = component?.split("/")
|
|
|
|
const name = split?.[split.length - 1]
|
|
|
|
return name === "screenslot" ? Router : ComponentLibrary[name]
|
|
|
|
}
|
2020-11-30 13:11:50 +01:00
|
|
|
|
|
|
|
// Returns a unique key to let svelte know when to remount components.
|
|
|
|
// If a component is selected we want to remount it every time any props
|
|
|
|
// change.
|
|
|
|
const getChildKey = childId => {
|
|
|
|
const selected = childId === $builderStore.selectedComponentId
|
|
|
|
return selected ? `${childId}-${$builderStore.previewId}` : childId
|
|
|
|
}
|
2020-11-13 16:42:32 +01:00
|
|
|
</script>
|
|
|
|
|
2020-11-17 13:08:24 +01:00
|
|
|
{#if constructor}
|
2020-11-20 10:50:10 +01:00
|
|
|
<svelte:component this={constructor} {...enrichedProps}>
|
2020-11-17 13:08:24 +01:00
|
|
|
{#if children && children.length}
|
2020-11-30 13:11:50 +01:00
|
|
|
{#each children as child (getChildKey(child._id))}
|
2020-11-17 13:08:24 +01:00
|
|
|
<svelte:self definition={child} />
|
|
|
|
{/each}
|
|
|
|
{/if}
|
|
|
|
</svelte:component>
|
2020-11-13 16:42:32 +01:00
|
|
|
{/if}
|