added message draft persistence and send button
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
<template>
|
||||
<textarea ref="textareaRef" v-model="content" @input="resize" @keydown="handleKeydown" rows="1"
|
||||
:placeholder="$t('chat-input-placeholder')"></textarea>
|
||||
<div class="container-div">
|
||||
<textarea ref="textareaRef" v-model="content" @input="handleInput" @keydown="handleKeydown" rows="1"
|
||||
:placeholder="$t('chat-input-placeholder')"></textarea>
|
||||
|
||||
<button class="send-btn" @click="submit" :disabled="!content.trim()">
|
||||
<i class="fas fa-paper-plane"></i>
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, nextTick } from 'vue'
|
||||
import { ref, nextTick, onMounted } from 'vue'
|
||||
import { saveMessageDraft, getMessageDraft } from '../store'
|
||||
|
||||
const content = ref('')
|
||||
const textareaRef = ref<HTMLTextAreaElement | null>(null)
|
||||
@@ -17,10 +24,24 @@ defineExpose({
|
||||
}
|
||||
})
|
||||
|
||||
onMounted(async () => {
|
||||
const saved = await getMessageDraft()
|
||||
if (saved) {
|
||||
content.value = saved
|
||||
resize()
|
||||
}
|
||||
})
|
||||
|
||||
function handleInput() {
|
||||
resize()
|
||||
saveMessageDraft(content.value)
|
||||
}
|
||||
|
||||
function submit() {
|
||||
if (!content.value.trim()) return
|
||||
emit('send', content.value)
|
||||
content.value = ''
|
||||
saveMessageDraft('')
|
||||
resize()
|
||||
}
|
||||
|
||||
@@ -43,6 +64,9 @@ function handleKeydown(e: KeyboardEvent) {
|
||||
textarea.selectionStart = textarea.selectionEnd = start + 1
|
||||
})
|
||||
resize()
|
||||
|
||||
saveMessageDraft(content.value)
|
||||
|
||||
e.preventDefault()
|
||||
} else if (!e.shiftKey && !e.altKey && e.key === 'Enter') {
|
||||
submit()
|
||||
@@ -52,6 +76,13 @@ function handleKeydown(e: KeyboardEvent) {
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.container-div {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
display: flex;
|
||||
align-items: flex-end;
|
||||
}
|
||||
|
||||
textarea {
|
||||
width: 100%;
|
||||
resize: none;
|
||||
@@ -64,5 +95,26 @@ textarea {
|
||||
border: none;
|
||||
color: inherit;
|
||||
outline: none;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.send-btn {
|
||||
margin-right: 0.8rem;
|
||||
background: transparent;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
color: var(--text);
|
||||
padding: 0;
|
||||
transition: color 0.2s;
|
||||
font-size: 1.2rem;
|
||||
}
|
||||
|
||||
.send-btn:hover:not(:disabled) {
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
.send-btn:disabled {
|
||||
cursor: not-allowed;
|
||||
opacity: 0.5;
|
||||
}
|
||||
</style>
|
||||
|
||||
13
src/store.ts
13
src/store.ts
@@ -223,3 +223,16 @@ export async function getCompactLayoutPreference(): Promise<boolean> {
|
||||
const s = await getStore();
|
||||
return (await s.get<boolean>('compact_layout')) ?? false;
|
||||
}
|
||||
|
||||
// ==== Message draft ====
|
||||
|
||||
export async function saveMessageDraft(text: string) {
|
||||
const s = await getStore()
|
||||
await s.set('message_draft', text)
|
||||
await s.save()
|
||||
}
|
||||
|
||||
export async function getMessageDraft(): Promise<string> {
|
||||
const s = await getStore()
|
||||
return (await s.get<string>('message_draft')) ?? ''
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user