From 2a2d9812b229da47a7abd81a97c820c999b198e7 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Mon, 8 Jun 2020 21:13:19 +0100 Subject: [PATCH] app preview improvements --- .../AppPreview/CurrentItemPreview.svelte | 124 ++++++++++-------- .../AppPreview/iframeTemplate.js | 59 ++++++--- 2 files changed, 106 insertions(+), 77 deletions(-) diff --git a/packages/builder/src/components/userInterface/AppPreview/CurrentItemPreview.svelte b/packages/builder/src/components/userInterface/AppPreview/CurrentItemPreview.svelte index a7288c8c91..538e8c0769 100644 --- a/packages/builder/src/components/userInterface/AppPreview/CurrentItemPreview.svelte +++ b/packages/builder/src/components/userInterface/AppPreview/CurrentItemPreview.svelte @@ -21,14 +21,48 @@ return componentName || "element" } - $: iframe && - console.log( - iframe.contentDocument.head.insertAdjacentHTML( - "beforeend", - `<\style>` - ) - ) + const screenPlaceholder = { + name: "Screen Placeholder", + route: "*", + props: { + _component: "@budibase/standard-components/container", + type: "div", + _children: [ + { + _component: "@budibase/standard-components/container", + _styles: { normal: {}, hover: {}, active: {}, selected: {} }, + _id: "__screenslot__text", + _code: "", + className: "", + onLoad: [], + type: "div", + _children: [ + { + _component: "@budibase/standard-components/text", + _styles: { + normal: {}, + hover: {}, + active: {}, + selected: {}, + }, + _id: "__screenslot__text_2", + _code: "", + text: "content", + font: "", + color: "", + textAlign: "inline", + verticalAlign: "inline", + formattingTag: "none", + }, + ], + }, + ], + }, + } + + $: hasComponent = !!$store.currentPreviewItem + $: { styles = "" // Apply the CSS from the currently selected page and its screens @@ -52,49 +86,12 @@ $: frontendDefinition = { appId: $store.appId, libraries: $store.libraries, - page: $store.currentPreviewItem, - screens: screensExist - ? $store.currentPreviewItem._screens - : [ - { - name: "Screen Placeholder", - route: "*", - props: { - _component: "@budibase/standard-components/container", - type: "div", - _children: [ - { - _component: "@budibase/standard-components/container", - _styles: { normal: {}, hover: {}, active: {}, selected: {} }, - _id: "__screenslot__text", - _code: "", - className: "", - onLoad: [], - type: "div", - _children: [ - { - _component: "@budibase/standard-components/text", - _styles: { - normal: {}, - hover: {}, - active: {}, - selected: {}, - }, - _id: "__screenslot__text_2", - _code: "", - text: "content", - font: "", - color: "", - textAlign: "inline", - verticalAlign: "inline", - formattingTag: "none", - }, - ], - }, - ], - }, - }, - ], + page: $store.pages[$store.currentPageName], + screens: [ + $store.currentFrontEndType === "page" + ? screenPlaceholder + : $store.currentPreviewItem, + ], appRootPath: "", } @@ -103,6 +100,25 @@ $: selectedComponentId = $store.currentComponentInfo ? $store.currentComponentInfo._id : "" + + const refreshContent = () => { + iframe.contentWindow.postMessage(JSON.stringify({ + styles, + stylesheetLinks, + selectedComponentType, + selectedComponentId, + frontendDefinition, + })) + } + let iframeLoaded = false + $: if(iframe && !iframeLoaded) { + iframe.contentWindow.addEventListener("bb-ready", refreshContent) + iframeLoaded = true + } + + $: if(iframe && frontendDefinition) { + refreshContent() + }
@@ -111,13 +127,7 @@ style="height: 100%; width: 100%" title="componentPreview" bind:this={iframe} - srcdoc={iframeTemplate({ - styles, - stylesheetLinks, - selectedComponentType, - selectedComponentId, - frontendDefinition: JSON.stringify(frontendDefinition), - })} /> + srcdoc={iframeTemplate} /> {/if}
diff --git a/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.js b/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.js index 462becb32f..c7b7aeebcc 100644 --- a/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.js +++ b/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.js @@ -1,20 +1,6 @@ -export default ({ - styles, - stylesheetLinks, - selectedComponentType, - selectedComponentId, - frontendDefinition, -}) => ` +export default ` - ${stylesheetLinks} -