1
0
Fork 0
mirror of synced 2024-09-30 09:07:25 +13:00
budibase/packages/builder/src/components/userInterface/AppPreview/CurrentItemPreview.svelte

223 lines
5.8 KiB
Svelte
Raw Normal View History

2019-08-20 08:18:23 +12:00
<script>
import { store, backendUiStore } from "builderStore"
2020-02-12 05:36:16 +13:00
import { map, join } from "lodash/fp"
2020-05-07 21:53:34 +12:00
import iframeTemplate from "./iframeTemplate"
import { pipe } from "../../../helpers"
2019-09-03 21:42:19 +12:00
2020-02-12 05:36:16 +13:00
let iframe
let styles = ""
2020-02-12 05:36:16 +13:00
function transform_component(comp) {
const props = comp.props || comp
if (props && props._children && props._children.length) {
props._children = props._children.map(transform_component)
}
2020-02-12 05:36:16 +13:00
return props
}
2020-10-28 04:28:13 +13:00
const getComponentTypeName = component => {
2020-05-26 02:23:56 +12:00
let [componentName] = component._component.match(/[a-z]*$/)
return componentName || "element"
}
const screenPlaceholder = {
2020-06-09 08:13:19 +12:00
name: "Screen Placeholder",
route: "*",
props: {
2020-07-09 03:31:26 +12:00
_id: "screenslot-placeholder",
_component: "@budibase/standard-components/container",
_styles: {
normal: {
flex: "1 1 auto",
},
2020-07-09 03:31:26 +12:00
hover: {},
active: {},
selected: {},
2020-07-01 20:49:09 +12:00
},
2020-07-08 08:29:20 +12:00
_code: "",
className: "",
onLoad: [],
type: "div",
_children: [
2020-06-09 08:13:19 +12:00
{
2020-07-09 03:31:26 +12:00
_id: "51a1b494-0fa4-49c3-90cc-c2a6c7a3f888",
_component: "@budibase/standard-components/container",
_styles: {
normal: {
display: "flex",
"flex-direction": "column",
"align-items": "center",
flex: "1 1 auto",
2020-07-09 03:31:26 +12:00
},
hover: {},
active: {},
selected: {},
},
2020-07-09 03:31:26 +12:00
_code: "",
className: "",
onLoad: [],
type: "div",
_instanceId: "inst_40d9036_4c81114e2bf145ab8721978c66e09a10",
_instanceName: "Container",
_children: [
{
_id: "90a52cd0-f215-46c1-b29b-e28f9e7edf72",
_component: "@budibase/standard-components/heading",
_styles: {
normal: {
width: "500px",
padding: "8px",
},
hover: {},
active: {},
selected: {},
},
2020-07-09 03:31:26 +12:00
_code: "",
className: "",
text: "Screen Slot",
2020-07-09 03:31:26 +12:00
type: "h1",
_instanceId: "inst_40d9036_4c81114e2bf145ab8721978c66e09a10",
_instanceName: "Heading",
_children: [],
2020-07-01 20:49:09 +12:00
},
2020-07-09 03:31:26 +12:00
{
_id: "71a3da65-72c6-4c43-8c6a-49871c07b77d",
_component: "@budibase/standard-components/text",
_styles: {
normal: {
"max-width": "",
"text-align": "left",
width: "500px",
padding: "8px",
},
hover: {},
active: {},
selected: {},
2020-06-09 08:13:19 +12:00
},
2020-07-09 03:31:26 +12:00
_code: "",
text:
"The screens that you create will be displayed inside this box.",
type: "none",
_instanceId: "inst_40d9036_4c81114e2bf145ab8721978c66e09a10",
_instanceName: "Text",
2020-07-08 08:29:20 +12:00
},
2020-07-09 03:31:26 +12:00
{
_id: "8af80374-460d-497b-a5d8-7dd2ec4a7bbc",
_component: "@budibase/standard-components/text",
_styles: {
normal: {
"max-width": "",
"text-align": "left",
width: "500px",
padding: "8px",
},
hover: {},
active: {},
selected: {},
},
2020-07-09 03:31:26 +12:00
_code: "",
text:
"This box is just a placeholder, to show you the position of screens.",
type: "none",
_instanceId: "inst_40d9036_4c81114e2bf145ab8721978c66e09a10",
_instanceName: "Text",
},
2020-07-09 03:31:26 +12:00
],
},
2020-06-09 08:13:19 +12:00
],
2020-07-08 08:29:20 +12:00
_instanceName: "Content Placeholder",
2020-06-09 08:13:19 +12:00
},
}
2020-02-12 05:36:16 +13:00
$: hasComponent = !!$store.currentPreviewItem
$: {
2020-05-13 01:44:30 +12:00
styles = ""
2020-05-07 21:53:34 +12:00
// Apply the CSS from the currently selected page and its screens
const currentPage = $store.pages[$store.currentPageName]
styles += currentPage._css
for (let screen of currentPage._screens) {
2020-05-07 21:53:34 +12:00
styles += screen._css
}
styles = styles
}
2019-08-20 08:18:23 +12:00
2020-05-26 21:19:04 +12:00
$: stylesheetLinks = pipe($store.pages.stylesheets, [
2020-10-28 04:28:13 +13:00
map(s => `<link rel="stylesheet" href="${s}"/>`),
2020-05-26 21:19:04 +12:00
join("\n"),
])
2020-02-11 05:58:20 +13:00
2020-05-07 21:53:34 +12:00
$: screensExist =
$store.currentPreviewItem._screens &&
$store.currentPreviewItem._screens.length > 0
2020-04-07 04:06:04 +12:00
2020-02-12 05:36:16 +13:00
$: frontendDefinition = {
2020-05-06 23:17:15 +12:00
appId: $store.appId,
libraries: $store.libraries,
2020-06-09 08:13:19 +12:00
page: $store.pages[$store.currentPageName],
screens: [
$store.currentFrontEndType === "page"
? screenPlaceholder
: $store.currentPreviewItem,
2020-06-09 08:13:19 +12:00
],
2020-02-12 05:36:16 +13:00
}
2020-05-26 02:23:56 +12:00
$: selectedComponentType = getComponentTypeName($store.currentComponentInfo)
2020-05-07 21:53:34 +12:00
$: selectedComponentId = $store.currentComponentInfo
? $store.currentComponentInfo._id
: ""
2020-06-09 08:13:19 +12:00
const refreshContent = () => {
iframe.contentWindow.postMessage(
JSON.stringify({
styles,
stylesheetLinks,
selectedComponentType,
selectedComponentId,
frontendDefinition,
appId: $store.appId,
2020-06-19 03:59:31 +12:00
instanceId: $backendUiStore.selectedDatabase._id,
})
)
2020-06-09 08:13:19 +12:00
}
2020-06-09 17:22:00 +12:00
$: if (iframe)
iframe.contentWindow.addEventListener("bb-ready", refreshContent, {
once: true,
})
2020-06-09 08:13:19 +12:00
$: if (iframe && frontendDefinition) {
refreshContent()
}
2020-02-12 05:36:16 +13:00
</script>
<div class="component-container">
{#if hasComponent && $store.currentPreviewItem}
<iframe
style="height: 100%; width: 100%"
title="componentPreview"
bind:this={iframe}
2020-06-09 08:13:19 +12:00
srcdoc={iframeTemplate} />
2020-02-12 05:36:16 +13:00
{/if}
2019-08-20 08:18:23 +12:00
</div>
<style>
2020-02-12 05:36:16 +13:00
.component-container {
grid-row-start: middle;
grid-column-start: middle;
position: relative;
overflow: hidden;
margin: auto;
height: 100%;
2020-02-12 05:36:16 +13:00
}
2020-02-12 05:36:16 +13:00
.component-container iframe {
border: 0;
left: 0;
top: 0;
width: 100%;
}
2020-02-19 04:53:22 +13:00
</style>