simplified the actions again to fix some bugs

This commit is contained in:
Samuel Andert 2023-07-22 19:05:44 +02:00
parent 68708b65d8
commit 4d5b634e55
3 changed files with 19 additions and 12 deletions

View File

@ -1,20 +1,16 @@
<script> <script>
import { createMessage } from '$lib/services/messages'; import { createMessage } from '$lib/services/messages';
import { getContextStore } from '$lib/stores/contextStore.ts';
const store = getContextStore('MessageInput'); export function sendMessage(text) {
$: newMessageText = $store.newMessageText; // bind newMessageText from the store if (text && text.trim() !== '') {
function handleSend() {
if (newMessageText.trim() !== '') {
const message = { const message = {
text: newMessageText, text: text,
sender: 'user', sender: 'user',
type: 'chat' type: 'chat'
}; };
const appCommandPattern = /@app:(\w+)/; const appCommandPattern = /@app:(\w+)/;
const match = newMessageText.match(appCommandPattern); const match = text.match(appCommandPattern);
if (match && match[1]) { if (match && match[1]) {
message.composite = { message.composite = {
layout: '', layout: '',
@ -30,8 +26,12 @@
createMessage(message); createMessage(message);
} }
} }
function handleSend() {
sendMessage(text);
}
</script> </script>
<button class="px-4 py-2 text-white bg-blue-500 rounded hover:bg-blue-600" on:click={handleSend}> <button class="px-4 py-2 text-white bg-blue-500 rounded hover:bg-blue-600" on:click={handleSend}>
Send Message Send
</button> </button>

View File

@ -1,12 +1,18 @@
<script> <script>
import { getContextStore } from '$lib/stores/contextStore.ts'; import { getContextStore } from '$lib/stores/contextStore.ts';
import ClearMessages from './ClearMessages.svelte';
import SendMessage from './SendMessage.svelte';
const store = getContextStore('MessageInput'); const store = getContextStore('MessageInput');
let newMessageText = ''; let newMessageText = '';
let sendFunction; // To hold the sendMessage function from SendMessage component
function handleKeyDown(event) { function handleKeyDown(event) {
if (event.key === 'Enter') { if (event.key === 'Enter') {
sendFunction(newMessageText);
newMessageText = ''; newMessageText = '';
store.set({ newMessageText }); // Update the store directly
event.preventDefault(); event.preventDefault();
} }
} }
@ -23,4 +29,6 @@
bind:value={newMessageText} bind:value={newMessageText}
on:keydown={handleKeyDown} on:keydown={handleKeyDown}
/> />
<SendMessage bind:sendMessage={sendFunction} />
<ClearMessages />
</footer> </footer>

View File

@ -25,10 +25,9 @@
}, },
{ {
id: 3, id: 3,
componentName: 'MessageInput', componentName: 'Terminal',
props: {}, props: {},
slot: 'footer', slot: 'footer'
actions: ['ClearMessages', 'SendMessage']
} }
] ]
}; };