Pass preview type to client app so it can decide whether components are selectable

This commit is contained in:
Andrew Kingston 2021-01-06 10:11:56 +00:00
parent 0e6c3d1aff
commit c101715c0e
3 changed files with 29 additions and 20 deletions

View File

@ -1,6 +1,6 @@
<script> <script>
import { onMount } from "svelte" import { onMount } from "svelte"
import { store, currentAsset } from "builderStore" import { store, currentAsset, selectedComponent } from "builderStore"
import iframeTemplate from "./iframeTemplate" import iframeTemplate from "./iframeTemplate"
import { Screen } from "builderStore/store/screenTemplates/utils/Screen" import { Screen } from "builderStore/store/screenTemplates/utils/Screen"
import { FrontendTypes } from "../../../constants" import { FrontendTypes } from "../../../constants"
@ -33,6 +33,7 @@
layout, layout,
screen, screen,
selectedComponentId, selectedComponentId,
previewType: $store.currentFrontEndType,
} }
// Saving pages and screens to the DB causes them to have _revs. // Saving pages and screens to the DB causes them to have _revs.
@ -54,17 +55,18 @@
// Refresh the preview when required // Refresh the preview when required
$: refreshContent(strippedJson) $: refreshContent(strippedJson)
// Initialise the app when mounted
onMount(() => { onMount(() => {
// Initialise the app when mounted
iframe.contentWindow.addEventListener( iframe.contentWindow.addEventListener(
"bb-ready", "bb-ready",
() => { () => refreshContent(strippedJson),
refreshContent(strippedJson) { once: true }
},
{
once: true,
}
) )
// Add listener to select components
iframe.contentWindow.addEventListener("bb-select-component", data => {
store.actions.components.select({ _id: data.detail })
})
}) })
</script> </script>

View File

@ -10,6 +10,9 @@
*, *:before, *:after { *, *:before, *:after {
box-sizing: border-box; box-sizing: border-box;
} }
* {
pointer-events: none;
}
</style> </style>
<script src='/assets/budibase-client.js'></script> <script src='/assets/budibase-client.js'></script>
<script> <script>
@ -19,7 +22,7 @@
} }
// Extract data from message // Extract data from message
const { selectedComponentId, layout, screen } = JSON.parse(event.data) const { selectedComponentId, layout, screen, previewType } = JSON.parse(event.data)
// Set some flags so the app knows we're in the builder // Set some flags so the app knows we're in the builder
window["##BUDIBASE_IN_BUILDER##"] = true window["##BUDIBASE_IN_BUILDER##"] = true
@ -27,6 +30,7 @@
window["##BUDIBASE_PREVIEW_SCREEN##"] = screen window["##BUDIBASE_PREVIEW_SCREEN##"] = screen
window["##BUDIBASE_SELECTED_COMPONENT_ID##"] = selectedComponentId window["##BUDIBASE_SELECTED_COMPONENT_ID##"] = selectedComponentId
window["##BUDIBASE_PREVIEW_ID##"] = Math.random() window["##BUDIBASE_PREVIEW_ID##"] = Math.random()
window["##BUDIBASE_PREVIEW_TYPE##"] = previewType
// Initialise app // Initialise app
if (window.loadBudibase) { if (window.loadBudibase) {
@ -34,15 +38,6 @@
} }
} }
// Ignore clicks
["click", "mousedown"].forEach(type => {
document.addEventListener(type, function(e) {
e.preventDefault()
e.stopPropagation()
return false
}, true)
})
window.addEventListener("message", receiveMessage) window.addEventListener("message", receiveMessage)
window.dispatchEvent(new Event("bb-ready")) window.dispatchEvent(new Event("bb-ready"))
</script> </script>

View File

@ -7,8 +7,20 @@ const createBuilderStore = () => {
screen: null, screen: null,
selectedComponentId: null, selectedComponentId: null,
previewId: null, previewId: null,
previewType: null,
}
const store = writable(initialState)
const actions = {
selectComponent: id => {
window.dispatchEvent(
new CustomEvent("bb-select-component", { detail: id })
)
},
}
return {
...store,
actions,
} }
return writable(initialState)
} }
export const builderStore = createBuilderStore() export const builderStore = createBuilderStore()