From f33ca4c9a5b7a618b54c25561ec98a358c2fa282 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 23 Jun 2024 23:10:26 -0700 Subject: [PATCH] refac --- src/lib/components/chat/Chat.svelte | 35 +++++++++++++++++++++++++++++ src/routes/+layout.svelte | 27 ---------------------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 8f8448847..621dc827e 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -127,6 +127,41 @@ } onMount(async () => { + window.addEventListener('message', async (event) => { + if (event.origin === window.origin) { + // Replace with your iframe's origin + console.log('Message received from iframe:', event.data); + if (event.data.type === 'input:prompt') { + console.log(event.data.text); + + const inputElement = document.getElementById('chat-textarea'); + + if (inputElement) { + prompt = event.data.text; + inputElement.focus(); + } + } + + if (event.data.type === 'action:submit') { + console.log(event.data.text); + + if (prompt !== '') { + await tick(); + submitPrompt(prompt); + } + } + + if (event.data.type === 'input:prompt:submit') { + console.log(event.data.text); + + if (prompt !== '') { + await tick(); + submitPrompt(event.data.text); + } + } + } + }); + if (!$chatId) { chatId.subscribe(async (value) => { if (!value) { diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 675ecc5dc..c4cbc9cfd 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -50,33 +50,6 @@ } }; - window.addEventListener('message', (event) => { - if (event.origin === window.origin) { - // Replace with your iframe's origin - console.log('Message received from iframe:', event.data); - if (event.data.type === 'input:prompt') { - console.log(event.data.text); - - const inputElement = document.getElementById('chat-textarea'); - - if (inputElement) { - inputElement.value = event.data.text; - inputElement.focus(); - } - } - - if (event.data.type === 'action:submit-prompt') { - console.log(event.data.text); - - const submitButtonElement = document.getElementById('send-message-button'); - - if (submitButtonElement) { - submitButtonElement.click(); - } - } - } - }); - window.addEventListener('resize', onResize); const setWakeLock = async () => {