From 7541ce91ddb591c35aa7c0142f95c6abded6fd31 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Mon, 8 Jun 2020 21:13:19 +0100 Subject: [PATCH 1/3] 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} - From 5799fbf23fc1d61d22f7ab60e0bd6cb74f8b4f81 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Tue, 9 Jun 2020 06:22:00 +0100 Subject: [PATCH 2/3] code review: add listner once --- .../userInterface/AppPreview/CurrentItemPreview.svelte | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/builder/src/components/userInterface/AppPreview/CurrentItemPreview.svelte b/packages/builder/src/components/userInterface/AppPreview/CurrentItemPreview.svelte index 538e8c0769..53450c2200 100644 --- a/packages/builder/src/components/userInterface/AppPreview/CurrentItemPreview.svelte +++ b/packages/builder/src/components/userInterface/AppPreview/CurrentItemPreview.svelte @@ -110,11 +110,8 @@ frontendDefinition, })) } - let iframeLoaded = false - $: if(iframe && !iframeLoaded) { - iframe.contentWindow.addEventListener("bb-ready", refreshContent) - iframeLoaded = true - } + + $: if(iframe) iframe.contentWindow.addEventListener("bb-ready", refreshContent, { once: true }) $: if(iframe && frontendDefinition) { refreshContent() From 1c1447011be5f7d4a3c1fd1688f9210dcb1479f6 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Tue, 9 Jun 2020 11:44:33 +0100 Subject: [PATCH 3/3] preventDefault on clicks, in preview --- .../components/userInterface/AppPreview/iframeTemplate.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.js b/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.js index c7b7aeebcc..3d305f1c1d 100644 --- a/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.js +++ b/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.js @@ -53,6 +53,11 @@ export default ` let styles let selectedComponentStyle + document.addEventListener("click", function(e) { + e.preventDefault() + e.stopPropagation() + return false; + }, true) import('/_builder/budibase-client.esm.mjs') .then(module => {