- threads: reply counts, ChatThreadPane with replies, live replies and thread mute - channel pins in ChatPinsPanel; a refused pin hides the action for the session - reactions, attachments with upload progress and bearer downloads, inline edits, the delete dialog (moderators also in channels) - link cards with the registerEmbedRenderer registry; linkify for escaped text - slash commands: the command picker over chatbots, dispatch with sender-only answers - slots conversation-actions (DMs only) and sidebar-footer - chat-tokens.css and chat-base.css, EN and PL copy, README
281 lines
12 KiB
Vue
281 lines
12 KiB
Vue
<script lang="ts">
|
|
import { ref } from 'vue'
|
|
|
|
/** On touch (hover: none) one message at a time shows its action bar. */
|
|
const actionsOpenId = ref<number | null>(null)
|
|
</script>
|
|
|
|
<script setup lang="ts">
|
|
// One message, left-aligned with the author's initials. The body goes
|
|
// through renderChatText only (escape-first inline markdown, autolinks and
|
|
// member mentions); it is the only HTML binding of the layer. Names, file
|
|
// names and every other field are text bindings.
|
|
//
|
|
// The action bar offers a reaction, a reply in a thread (outside the thread
|
|
// pane), pin or unpin (channels, while the host allows it), Edit on your own
|
|
// text messages and Delete where the store's canDelete allows it. Edits
|
|
// happen inline; deletes are confirmed by the list's ChatDeleteDialog.
|
|
// The first URLs of the text (up to limits.max_embeds) show as link cards:
|
|
// a host's registered renderer, else ChatLinkEmbed.
|
|
import type { ChatAttachment, ChatMember, ChatMessage } from '../../lib/types'
|
|
import { shortName } from '../../lib/chatFormat'
|
|
import { mentionsHandle, renderChatText } from '../../lib/chatText'
|
|
import { fmtDateTime, fmtSize, fmtTime } from '../../lib/format'
|
|
import { embedRendererFor, messageUrls } from '../../lib/linkEmbeds'
|
|
import { useMediaQuery } from '../../lib/media'
|
|
import ChatLinkEmbed from './LinkEmbed.vue'
|
|
import ChatReactionBar from './ReactionBar.vue'
|
|
|
|
const props = defineProps<{ message: ChatMessage; followUp?: boolean; author?: ChatMember | null; meId: number | null; inThread?: boolean }>()
|
|
const emit = defineEmits<{ delete: [message: ChatMessage, opener: HTMLElement | null]; thread: [message: ChatMessage] }>()
|
|
|
|
const { t } = useI18n()
|
|
const chat = useChat()
|
|
const touch = useMediaQuery('(hover: none)')
|
|
const root = ref<HTMLElement | null>(null)
|
|
const bar = ref<InstanceType<typeof ChatReactionBar> | null>(null)
|
|
const reactBtn = ref<HTMLButtonElement | null>(null)
|
|
const editBtn = ref<HTMLButtonElement | null>(null)
|
|
const deleteBtn = ref<HTMLButtonElement | null>(null)
|
|
const editInput = ref<HTMLTextAreaElement | null>(null)
|
|
|
|
const mine = computed(() => props.meId !== null && props.message.sender_id === props.meId)
|
|
const name = computed(() => shortName(props.author, props.message.sender_name, t('chat.someone')))
|
|
const time = computed(() => fmtTime(props.message.created_at))
|
|
const timeTitle = computed(() => fmtDateTime(props.message.created_at))
|
|
const isBot = computed(() => !!props.author?.is_bot)
|
|
/** Someone else's message that mentions you gets the accent marker. */
|
|
const mentionsMe = computed(() => !mine.value && mentionsHandle(props.message.content, chat.myHandle))
|
|
const editedTitle = computed(() => (props.message.edited_at ? t('chat.msg.editedAt', { at: fmtDateTime(props.message.edited_at) }) : ''))
|
|
const deleted = computed(() => !!props.message.deleted_at)
|
|
const rendered = computed(() => renderChatText(props.message.content, chat.membersByHandle))
|
|
const hasReactions = computed(() => (props.message.reactions ?? []).some((r) => r.count > 0))
|
|
/** The bar shows for an add started from the action bar even without reactions. */
|
|
const picking = ref(false)
|
|
const actionsOpen = computed(() => actionsOpenId.value === props.message.id)
|
|
|
|
const canReact = computed(() => !!chat.allowedReactions?.length)
|
|
const canEdit = computed(() => chat.canEdit(props.message))
|
|
const canDelete = computed(() => chat.canDelete(props.message))
|
|
const canThread = computed(() => !props.inThread && !props.message.root_id)
|
|
const canPin = computed(() => !props.message.root_id && chat.pinAllowed && chat.isChannel(props.message.conversation_id))
|
|
const pinned = computed(() => chat.isPinned(props.message))
|
|
const replyCount = computed(() => props.message.thread_summary?.reply_count ?? 0)
|
|
|
|
/** Link cards: the first URLs of the text, at most limits.max_embeds. */
|
|
const embeds = computed(() => {
|
|
const max = chat.limits?.max_embeds ?? 1
|
|
if (deleted.value || max <= 0) return []
|
|
return messageUrls(props.message.content)
|
|
.slice(0, max)
|
|
.map((url) => ({ url, component: embedRendererFor(url) ?? ChatLinkEmbed }))
|
|
})
|
|
|
|
// Attachments download through a bearer fetch; never shown inline.
|
|
const attachments = computed(() => props.message.attachments ?? [])
|
|
const downloading = ref<number | null>(null)
|
|
|
|
async function download(a: ChatAttachment) {
|
|
if (downloading.value !== null) return
|
|
downloading.value = a.id
|
|
try {
|
|
await chat.download(props.message, a)
|
|
} finally {
|
|
downloading.value = null
|
|
}
|
|
}
|
|
|
|
// Inline edit: only the text; attachments stay as they are.
|
|
const editing = ref(false)
|
|
const editText = ref('')
|
|
const saving = ref(false)
|
|
const editError = ref('')
|
|
const LINE = 24
|
|
|
|
function grow() {
|
|
const el = editInput.value
|
|
if (!el) return
|
|
el.style.height = 'auto'
|
|
const cs = getComputedStyle(el)
|
|
const max = LINE * 8 + parseFloat(cs.paddingTop) + parseFloat(cs.paddingBottom)
|
|
el.style.height = `${Math.min(el.scrollHeight, max)}px`
|
|
el.style.overflowY = el.scrollHeight > max ? 'auto' : 'hidden'
|
|
}
|
|
|
|
async function startEdit() {
|
|
if (!canEdit.value) return
|
|
editText.value = props.message.content ?? ''
|
|
editError.value = ''
|
|
editing.value = true
|
|
actionsOpenId.value = null
|
|
await nextTick()
|
|
const el = editInput.value
|
|
if (!el) return
|
|
grow()
|
|
el.focus()
|
|
el.setSelectionRange(el.value.length, el.value.length)
|
|
}
|
|
|
|
function endEdit() {
|
|
editing.value = false
|
|
editError.value = ''
|
|
void nextTick(() => editBtn.value?.focus())
|
|
}
|
|
|
|
async function saveEdit() {
|
|
if (saving.value) return
|
|
saving.value = true
|
|
editError.value = ''
|
|
const failed = await chat.edit(props.message, editText.value)
|
|
saving.value = false
|
|
if (failed) {
|
|
editError.value = t(`chat.${failed}`)
|
|
return
|
|
}
|
|
endEdit()
|
|
}
|
|
|
|
function onEditKeydown(e: KeyboardEvent) {
|
|
if (e.isComposing) return
|
|
if (e.key === 'Escape') {
|
|
e.preventDefault()
|
|
e.stopPropagation()
|
|
if (!saving.value) endEdit()
|
|
return
|
|
}
|
|
if (e.key === 'Enter' && !e.shiftKey) {
|
|
e.preventDefault()
|
|
void saveEdit()
|
|
}
|
|
}
|
|
|
|
watch(editText, () => void nextTick(grow))
|
|
watch(deleted, (d) => {
|
|
if (d) editing.value = false
|
|
})
|
|
|
|
function askDelete() {
|
|
if (canDelete.value) emit('delete', props.message, deleteBtn.value)
|
|
}
|
|
|
|
async function addReaction() {
|
|
if (!canReact.value) return
|
|
picking.value = true
|
|
await nextTick()
|
|
bar.value?.openPicker(reactBtn.value)
|
|
}
|
|
|
|
function onPickerClosed() {
|
|
picking.value = false
|
|
}
|
|
|
|
/** Touch: a tap on the message toggles its action bar; links and buttons keep their own tap. */
|
|
function onTap(e: MouseEvent) {
|
|
if (!touch.value) return
|
|
const target = e.target as HTMLElement | null
|
|
if (target?.closest('a, button, textarea, input, .chat-msg-actions')) return
|
|
actionsOpenId.value = actionsOpen.value ? null : props.message.id
|
|
}
|
|
|
|
function onOutside(e: PointerEvent) {
|
|
if (root.value && e.target instanceof Node && root.value.contains(e.target)) return
|
|
if (actionsOpen.value) actionsOpenId.value = null
|
|
}
|
|
|
|
watch(actionsOpen, (open) => {
|
|
if (open) document.addEventListener('pointerdown', onOutside, true)
|
|
else document.removeEventListener('pointerdown', onOutside, true)
|
|
})
|
|
|
|
onBeforeUnmount(() => {
|
|
document.removeEventListener('pointerdown', onOutside, true)
|
|
if (actionsOpen.value) actionsOpenId.value = null
|
|
})
|
|
</script>
|
|
|
|
<template>
|
|
<article
|
|
ref="root"
|
|
class="chat-msg"
|
|
:class="{ 'is-follow': followUp, 'is-actions-open': actionsOpen, 'is-mention': mentionsMe, 'is-pinned': pinned }"
|
|
:data-message-id="message.id"
|
|
@click="onTap"
|
|
>
|
|
<div class="chat-msg-gutter">
|
|
<ChatAvatar v-if="!followUp" :user-id="message.sender_id" :member="author" :fallback="message.sender_name" :me="mine" />
|
|
<time v-else class="chat-msg-time" :datetime="message.created_at" :title="timeTitle">{{ time }}</time>
|
|
</div>
|
|
<div class="chat-msg-content">
|
|
<div v-if="!followUp" class="chat-msg-head">
|
|
<span class="chat-msg-name">{{ name }}</span>
|
|
<span v-if="isBot" class="chat-tag">{{ t('chat.msg.bot') }}</span>
|
|
<span v-else-if="mine" class="chat-tag chat-tag--me">{{ t('chat.msg.you') }}</span>
|
|
<time class="chat-msg-time" :datetime="message.created_at" :title="timeTitle">{{ time }}</time>
|
|
<span v-if="message.edited_at" class="chat-msg-edited" :title="editedTitle">{{ t('chat.msg.edited') }}</span>
|
|
<span v-if="pinned" class="chat-tag chat-tag--pin"><ChatIcon name="pin" />{{ t('chat.pins.pinned') }}</span>
|
|
</div>
|
|
<span v-else class="chat-sr">{{ name }}, {{ time }}:</span>
|
|
<p v-if="deleted" class="chat-msg-deleted">{{ t('chat.msg.deleted') }}</p>
|
|
<form v-else-if="editing" class="chat-msg-edit" @submit.prevent="saveEdit">
|
|
<textarea ref="editInput" v-model="editText" rows="1" :aria-label="t('chat.msg.editLabel')" :readonly="saving" @keydown="onEditKeydown"></textarea>
|
|
<p v-if="editError" class="chat-error" role="alert">{{ editError }}</p>
|
|
<div class="chat-btn-row">
|
|
<button class="chat-btn chat-btn--primary chat-btn--small" type="submit" :aria-busy="saving ? 'true' : undefined">{{ t('chat.msg.save') }}</button>
|
|
<button class="chat-btn chat-btn--ghost chat-btn--small" type="button" :disabled="saving" @click="endEdit">{{ t('chat.msg.cancel') }}</button>
|
|
</div>
|
|
</form>
|
|
<template v-else>
|
|
<div v-if="message.content" class="chat-msg-body" v-html="rendered"></div>
|
|
<span v-if="followUp && message.edited_at" class="chat-msg-edited" :title="editedTitle">{{ t('chat.msg.edited') }}</span>
|
|
</template>
|
|
<ul v-if="!deleted && attachments.length" class="chat-msg-files">
|
|
<li v-for="a in attachments" :key="a.id">
|
|
<button
|
|
class="chat-msg-file"
|
|
type="button"
|
|
:aria-label="t('chat.msg.download', { name: a.filename, size: fmtSize(a.size) })"
|
|
:aria-busy="downloading === a.id ? 'true' : undefined"
|
|
@click="download(a)"
|
|
>
|
|
<ChatIcon name="file" />
|
|
<span class="chat-msg-file-name">{{ a.filename }}</span>
|
|
<span class="chat-msg-file-size">{{ fmtSize(a.size) }}</span>
|
|
</button>
|
|
</li>
|
|
</ul>
|
|
<div v-if="embeds.length && !editing" class="chat-msg-embeds">
|
|
<component :is="e.component" v-for="e in embeds" :key="e.url.href" :url="e.url" />
|
|
</div>
|
|
<ChatReactionBar v-if="!deleted && (hasReactions || picking)" ref="bar" :message="message" @closed="onPickerClosed" />
|
|
<button v-if="canThread && replyCount > 0" class="chat-thread-link" type="button" @click="emit('thread', message)">
|
|
<ChatIcon name="thread" />{{ t('chat.thread.replies', { n: replyCount }, replyCount) }}
|
|
</button>
|
|
</div>
|
|
<div v-if="!deleted && !editing" class="chat-msg-actions" role="toolbar" :aria-label="t('chat.msg.actions')">
|
|
<button ref="reactBtn" class="chat-msg-action" type="button" :aria-label="t('chat.reactions.add')" :title="t('chat.reactions.add')" :disabled="!canReact" @click="addReaction">
|
|
<ChatIcon name="smile" />
|
|
</button>
|
|
<button v-if="canThread" class="chat-msg-action" type="button" :aria-label="t('chat.thread.reply')" :title="t('chat.thread.reply')" @click="emit('thread', message)">
|
|
<ChatIcon name="thread" />
|
|
</button>
|
|
<button
|
|
v-if="canPin"
|
|
class="chat-msg-action"
|
|
type="button"
|
|
:aria-label="pinned ? t('chat.pins.unpin') : t('chat.pins.pin')"
|
|
:title="pinned ? t('chat.pins.unpin') : t('chat.pins.pin')"
|
|
:aria-pressed="pinned ? 'true' : 'false'"
|
|
@click="chat.togglePin(message)"
|
|
>
|
|
<ChatIcon name="pin" />
|
|
</button>
|
|
<button v-if="canEdit" ref="editBtn" class="chat-msg-action" type="button" :aria-label="t('chat.msg.edit')" :title="t('chat.msg.edit')" @click="startEdit">
|
|
<ChatIcon name="pencil" />
|
|
</button>
|
|
<button v-if="canDelete" ref="deleteBtn" class="chat-msg-action chat-msg-action--danger" type="button" :aria-label="t('chat.msg.delete')" :title="t('chat.msg.delete')" @click="askDelete">
|
|
<ChatIcon name="trash" />
|
|
</button>
|
|
</div>
|
|
</article>
|
|
</template>
|