-
-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webui): support chat-panel component
- Loading branch information
Showing
7 changed files
with
109 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,26 @@ | ||
<template> | ||
<k-card class="sandbox"> | ||
<div class="history" ref="panel"> | ||
<p v-for="({ username, content }, index) in messages" :key="index"> | ||
{{ username }}: {{ content }} | ||
<k-chat-panel class="sandbox" :messages="messages"> | ||
<template #default="{ username, content }"> | ||
<p> | ||
{{ username }}: <k-message :content="content"/> | ||
</p> | ||
</div> | ||
<k-input v-model="text" @enter="onEnter"></k-input> | ||
</k-card> | ||
</template> | ||
</k-chat-panel> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { receive, storage, send } from '~/client' | ||
import { ref, nextTick } from 'vue' | ||
import { receive, storage } from '~/client' | ||
interface Message { | ||
username: string | ||
content: string | ||
} | ||
const text = ref('') | ||
const panel = ref<Element>(null) | ||
const messages = storage.create<Message[]>('chat', []) | ||
function addMessage(body: Message) { | ||
receive('chat', (body) => { | ||
messages.value.push(body) | ||
const { scrollTop, clientHeight, scrollHeight } = panel.value | ||
if (Math.abs(scrollTop + clientHeight - scrollHeight) < 1) { | ||
nextTick(scrollToBottom) | ||
} | ||
} | ||
function scrollToBottom() { | ||
panel.value.scrollTop = panel.value.scrollHeight - panel.value.clientHeight | ||
} | ||
receive('message', addMessage) | ||
function onEnter() { | ||
if (!text.value) return | ||
// addMessage('user', text.value) | ||
// const { token, id } = user.value | ||
// send('chat', { token, id, content: text.value }) | ||
// text.value = '' | ||
} | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<template> | ||
<k-card class="k-chat-panel"> | ||
<div class="k-chat-panel-body" ref="body"> | ||
<template v-for="(message, index) in messages" :key="index"> | ||
<slot v-bind="message"/> | ||
</template> | ||
</div> | ||
<k-input v-model="text" @enter="onEnter" @paste="onPaste"></k-input> | ||
</k-card> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { ref, watch, defineProps, onMounted, nextTick, defineEmit } from 'vue' | ||
import { segment } from '~/client' | ||
const emit = defineEmit(['enter']) | ||
const props = defineProps<{ messages: any[], pinned?: boolean }>() | ||
const text = ref('') | ||
const body = ref<Element>(null) | ||
onMounted(scrollToBottom) | ||
function scrollToBottom() { | ||
body.value.scrollTop = body.value.scrollHeight - body.value.clientHeight | ||
} | ||
watch(props.messages, () => { | ||
const { scrollTop, clientHeight, scrollHeight } = body.value | ||
if (!props.pinned || Math.abs(scrollTop + clientHeight - scrollHeight) < 1) { | ||
nextTick(scrollToBottom) | ||
} | ||
}) | ||
function onEnter() { | ||
if (!text.value) return | ||
emit('enter', text.value) | ||
text.value = '' | ||
} | ||
async function onPaste(event: ClipboardEvent) { | ||
const item = event.clipboardData.items[0] | ||
if (item.kind === 'file') { | ||
event.preventDefault() | ||
const file = item.getAsFile() | ||
const reader = new FileReader() | ||
reader.addEventListener('load', function () { | ||
emit('enter', segment.image('base64://' + reader.result.slice(22))) | ||
}, false) | ||
reader.readAsDataURL(file) | ||
} | ||
} | ||
</script> | ||
|
||
<style lang="scss"> | ||
.k-chat-panel { | ||
height: 100%; | ||
position: relative; | ||
.k-chat-panel-body { | ||
position: absolute; | ||
top: 2rem; | ||
left: 2rem; | ||
right: 2rem; | ||
bottom: 6rem; | ||
overflow-x: visible; | ||
overflow-y: auto; | ||
} | ||
.k-input { | ||
position: absolute; | ||
bottom: 2rem; | ||
left: 2rem; | ||
right: 2rem; | ||
} | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters