2021-11-02 09:45:27 +01:00
|
|
|
<script>
|
2022-11-15 13:58:52 +01:00
|
|
|
import { getContext, onDestroy } from "svelte"
|
2021-11-02 09:45:27 +01:00
|
|
|
import { generate } from "shortid"
|
2022-08-23 16:53:28 +02:00
|
|
|
import { builderStore } from "../stores/builder.js"
|
2021-11-02 09:45:27 +01:00
|
|
|
import Component from "components/Component.svelte"
|
|
|
|
|
|
|
|
export let type
|
|
|
|
export let props
|
|
|
|
export let styles
|
2021-11-08 15:35:58 +01:00
|
|
|
export let context
|
2022-11-17 15:20:18 +01:00
|
|
|
export let name
|
2022-06-30 20:31:25 +02:00
|
|
|
export let order = 0
|
2022-08-24 09:37:53 +02:00
|
|
|
export let containsSlot = false
|
2021-11-02 09:58:50 +01:00
|
|
|
|
|
|
|
// ID is only exposed as a prop so that it can be bound to from parent
|
|
|
|
// block components
|
2021-11-02 09:45:27 +01:00
|
|
|
export let id
|
|
|
|
|
2022-06-30 20:31:25 +02:00
|
|
|
const component = getContext("component")
|
2021-11-02 09:45:27 +01:00
|
|
|
const block = getContext("block")
|
|
|
|
const rand = generate()
|
|
|
|
|
2021-11-02 09:58:50 +01:00
|
|
|
// Create a fake component instance so that we can use the core Component
|
|
|
|
// to render this part of the block, taking advantage of binding enrichment
|
2021-11-08 15:35:58 +01:00
|
|
|
$: id = `${block.id}-${context ?? rand}`
|
2023-07-19 18:11:15 +02:00
|
|
|
$: parentId = $component?.id
|
|
|
|
$: inBuilder = $builderStore.inBuilder
|
2021-11-02 13:58:38 +01:00
|
|
|
$: instance = {
|
2023-07-30 14:07:14 +02:00
|
|
|
_component: getComponent(type),
|
2021-11-02 13:58:38 +01:00
|
|
|
_id: id,
|
2023-07-30 14:07:14 +02:00
|
|
|
_instanceName: getInstanceName(name, type),
|
2022-08-24 10:44:33 +02:00
|
|
|
_styles: {
|
|
|
|
...styles,
|
|
|
|
normal: styles?.normal || {},
|
|
|
|
},
|
2022-08-24 09:37:53 +02:00
|
|
|
_containsSlot: containsSlot,
|
2021-11-02 13:58:38 +01:00
|
|
|
...props,
|
2021-11-02 09:45:27 +01:00
|
|
|
}
|
2022-08-23 16:53:28 +02:00
|
|
|
|
|
|
|
// Register this block component if we're inside the builder so it can be
|
|
|
|
// ejected later
|
|
|
|
$: {
|
2023-07-19 18:11:15 +02:00
|
|
|
if (inBuilder) {
|
|
|
|
block.registerComponent(id, parentId, order ?? 0, instance)
|
2022-08-23 16:53:28 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-15 13:58:52 +01:00
|
|
|
|
2023-07-30 14:07:14 +02:00
|
|
|
const getComponent = type => {
|
|
|
|
if (!type) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
if (type.startsWith("plugin/")) {
|
|
|
|
return type
|
|
|
|
} else {
|
|
|
|
return `@budibase/standard-components/${type}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const getInstanceName = (name, type) => {
|
|
|
|
if (name) {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
if (!type) {
|
|
|
|
return "New component"
|
|
|
|
}
|
|
|
|
if (type.startsWith("plugin/")) {
|
|
|
|
type = type.split("plugin/")[1]
|
|
|
|
}
|
|
|
|
return type[0].toUpperCase() + type.slice(1)
|
|
|
|
}
|
|
|
|
|
2022-11-15 13:58:52 +01:00
|
|
|
onDestroy(() => {
|
2023-07-19 18:11:15 +02:00
|
|
|
if (inBuilder) {
|
|
|
|
block.unregisterComponent(id, parentId)
|
2022-11-15 13:58:52 +01:00
|
|
|
}
|
|
|
|
})
|
2021-11-02 09:45:27 +01:00
|
|
|
</script>
|
|
|
|
|
2021-11-12 16:19:25 +01:00
|
|
|
<Component {instance} isBlock>
|
2021-11-02 09:45:27 +01:00
|
|
|
<slot />
|
|
|
|
</Component>
|