code review, simplify iframeTemplate

This commit is contained in:
Martin McKeaveney 2021-11-10 12:12:33 +01:00
parent 753d2f51bc
commit b66d76f010
3 changed files with 12 additions and 21 deletions

View File

@ -95,12 +95,12 @@
const handlers = { const handlers = {
[MessageTypes.READY]: () => { [MessageTypes.READY]: () => {
// Initialise the app when mounted // Initialise the app when mounted
// Display preview immediately if the intelligent loading feature
// is not supported
if ($store.clientFeatures.messagePassing) { if ($store.clientFeatures.messagePassing) {
if (!loading) return if (!loading) return
} }
// Display preview immediately if the intelligent loading feature
// is not supported
if (!$store.clientFeatures.intelligentLoading) { if (!$store.clientFeatures.intelligentLoading) {
loading = false loading = false
} }
@ -119,9 +119,8 @@
} }
onMount(() => { onMount(() => {
if ($store.clientFeatures.messagePassing) { window.addEventListener("message", receiveMessage)
window.addEventListener("message", receiveMessage) if (!$store.clientFeatures.messagePassing) {
} else {
// Legacy - remove in later versions of BB // Legacy - remove in later versions of BB
iframe.contentWindow.addEventListener("ready", () => { iframe.contentWindow.addEventListener("ready", () => {
receiveMessage({ data: { type: MessageTypes.READY }}) receiveMessage({ data: { type: MessageTypes.READY }})
@ -132,15 +131,14 @@
// Add listener for events sent by client library in preview // Add listener for events sent by client library in preview
iframe.contentWindow.addEventListener("bb-event", handleBudibaseEvent) iframe.contentWindow.addEventListener("bb-event", handleBudibaseEvent)
iframe.contentWindow.addEventListener("keydown", handleKeydownEvent) iframe.contentWindow.addEventListener("keydown", handleKeydownEvent)
} }
}) })
// Remove all iframe event listeners on component destroy // Remove all iframe event listeners on component destroy
onDestroy(() => { onDestroy(() => {
if (iframe.contentWindow) { if (iframe.contentWindow) {
if ($store.clientFeatures.messagePassing) { window.removeEventListener("message", receiveMessage)
window.removeEventListener("message", receiveMessage) if (!$store.clientFeatures.messagePassing) {
} else {
// Legacy - remove in later versions of BB // Legacy - remove in later versions of BB
iframe.contentWindow.removeEventListener("bb-event", handleBudibaseEvent) iframe.contentWindow.removeEventListener("bb-event", handleBudibaseEvent)
iframe.contentWindow.removeEventListener("keydown", handleKeydownEvent) iframe.contentWindow.removeEventListener("keydown", handleKeydownEvent)

View File

@ -65,7 +65,6 @@ export default `
theme, theme,
customTheme, customTheme,
previewDevice, previewDevice,
messagePassing
} = parsed } = parsed
// Set some flags so the app knows we're in the builder // Set some flags so the app knows we're in the builder
@ -90,11 +89,7 @@ export default `
throw "The client library couldn't be loaded" throw "The client library couldn't be loaded"
} }
} catch (error) { } catch (error) {
if (messagePassing) { window.parent.postMessage({ type: "error", error })
window.parent.postMessage({ type: "error", error })
} else {
window.dispatchEvent(new CustomEvent("error", { detail: error }))
}
} }
} }
@ -104,7 +99,6 @@ export default `
}) })
window.parent.postMessage({ type: "ready" }) window.parent.postMessage({ type: "ready" })
window.dispatchEvent(new Event("ready"))
</script> </script>
</head> </head>
<body/> <body/>

View File

@ -81,13 +81,12 @@ export function createAuthStore() {
} }
async function setInitInfo(info) { async function setInitInfo(info) {
const response = await api.post(`/api/global/auth/init`, info) await api.post(`/api/global/auth/init`, info)
const json = await response.json()
auth.update(store => { auth.update(store => {
store.initInfo = json store.initInfo = info
return store return store
}) })
return json return info
} }
async function getInitInfo() { async function getInitInfo() {
@ -102,7 +101,7 @@ export function createAuthStore() {
return { return {
subscribe: store.subscribe, subscribe: store.subscribe,
setOrganisation: setOrganisation, setOrganisation,
getInitInfo, getInitInfo,
setInitInfo, setInitInfo,
checkQueryString: async () => { checkQueryString: async () => {