2
0

(logic) Add execute in parent window context for code block

This commit is contained in:
Baptiste Arnaud
2022-11-09 15:08:42 +01:00
parent de0158be24
commit b31b603cc7
18 changed files with 91 additions and 56 deletions

View File

@ -1,4 +1,4 @@
import { DataFromTypebot, IframeCallbacks, IframeParams } from '../types'
import { TypebotPostMessageData, IframeCallbacks, IframeParams } from '../types'
import './style.css'
export const createIframe = ({
@ -24,8 +24,8 @@ export const createIframe = ({
iframe.setAttribute('id', uniqueId)
if (backgroundColor) iframe.style.backgroundColor = backgroundColor
iframe.classList.add('typebot-iframe')
const { onNewVariableValue, onVideoPlayed } = iframeParams
listenForTypebotMessages({ onNewVariableValue, onVideoPlayed })
const { onNewVariableValue } = iframeParams
listenForTypebotMessages({ onNewVariableValue })
return iframe
}
@ -50,14 +50,17 @@ const parseStarterVariables = (starterVariables?: {
export const listenForTypebotMessages = (callbacks: IframeCallbacks) => {
window.addEventListener('message', (event) => {
const data = event.data as { from?: 'typebot' } & DataFromTypebot
const data = event.data as { from?: 'typebot' } & TypebotPostMessageData
if (data.from === 'typebot') processMessage(event.data, callbacks)
})
}
const processMessage = (data: DataFromTypebot, callbacks: IframeCallbacks) => {
const processMessage = (
data: TypebotPostMessageData,
callbacks: IframeCallbacks
) => {
if (data.redirectUrl) window.open(data.redirectUrl)
if (data.newVariableValue && callbacks.onNewVariableValue)
callbacks.onNewVariableValue(data.newVariableValue)
if (data.videoPlayed && callbacks.onVideoPlayed) callbacks.onVideoPlayed()
if (data.codeToExecute) data.codeToExecute()
}

View File

@ -8,7 +8,6 @@ export type IframeParams = {
export type IframeCallbacks = {
onNewVariableValue?: (v: Variable) => void
onVideoPlayed?: () => void
}
export type PopupParams = {
@ -52,10 +51,10 @@ export type Variable = {
value: string
}
export type DataFromTypebot = {
export type TypebotPostMessageData = {
redirectUrl?: string
newVariableValue?: Variable
videoPlayed?: boolean
codeToExecute?: Function
}
export const localStorageKeys = {