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

@ -8,6 +8,7 @@ import React, {
useEffect,
useState,
} from 'react'
import { sendEventToParent } from 'services/chat'
import { safeStringify } from 'services/variable'
export type LinkedTypebot = Pick<
@ -75,6 +76,15 @@ export const TypebotContext = ({
const updateVariableValue = (variableId: string, value: unknown) => {
const formattedValue = safeStringify(value)
sendEventToParent({
newVariableValue: {
name:
typebot.variables.find((variable) => variable.id === variableId)
?.name ?? '',
value: formattedValue ?? '',
},
})
setLocalTypebot((typebot) => ({
...typebot,
variables: typebot.variables.map((v) =>

View File

@ -7,6 +7,7 @@ import {
TypingEmulation,
} from 'models'
import { isBubbleBlock, isInputBlock } from 'utils'
import type { TypebotPostMessageData } from 'typebot-js'
export const computeTypingTimeout = (
bubbleContent: string,
@ -31,3 +32,17 @@ export const getLastChatBlockType = (
) as (BubbleBlock | InputBlock)[]
return displayedBlocks.pop()?.type
}
export const sendEventToParent = (data: TypebotPostMessageData) => {
try {
window.top?.postMessage(
{
from: 'typebot',
...data,
},
'*'
)
} catch (error) {
console.error(error)
}
}

View File

@ -19,7 +19,8 @@ import {
VariableWithUnknowValue,
} from 'models'
import { byId, isDefined, isNotDefined, sendRequest } from 'utils'
import { sanitizeUrl } from './utils'
import { sendEventToParent } from './chat'
import { isEmbedded, sanitizeUrl } from './utils'
import { parseCorrectValueType, parseVariables } from './variable'
type EdgeId = string
@ -153,14 +154,7 @@ const executeRedirect = (
try {
window.open(formattedUrl)
} catch (err) {
//Can't access to parent window
window.top?.postMessage(
{
from: 'typebot',
redirectUrl: formattedUrl,
},
'*'
)
sendEventToParent({ redirectUrl: formattedUrl })
}
} else {
window.open(formattedUrl, block.options.isNewTab ? '_blank' : '_self')
@ -173,15 +167,25 @@ const executeCode = async (
{ typebot: { variables } }: LogicContext
) => {
if (!block.options.content) return
const func = Function(
...variables.map((v) => v.id),
parseVariables(variables, { fieldToParse: 'id' })(block.options.content)
)
try {
await func(...variables.map((v) => parseCorrectValueType(v.value)))
} catch (err) {
console.error(err)
console.log('isEmbedded', isEmbedded)
if (block.options.shouldExecuteInParentContext && isEmbedded) {
const func = Function(
...variables.map((v) => v.id),
parseVariables(variables)(block.options.content)
)
sendEventToParent({ codeToExecute: func })
} else {
const func = Function(
...variables.map((v) => v.id),
parseVariables(variables, { fieldToParse: 'id' })(block.options.content)
)
try {
await func(...variables.map((v) => parseCorrectValueType(v.value)))
} catch (err) {
console.error(err)
}
}
return block.outgoingEdgeId
}

View File

@ -9,3 +9,8 @@ export const sanitizeUrl = (url: string): string =>
export const isMobile =
typeof window !== 'undefined' &&
window.matchMedia('only screen and (max-width: 760px)').matches
export const isEmbedded =
typeof window !== 'undefined' &&
window.parent &&
window.location !== window.top?.location