|
| 1 | +<!-- apps/kimi-web/src/components/chat/AttachmentChip.vue --> |
| 2 | +<!-- One attachment rendered as a pill chip — the SAME component for the |
| 3 | + composer's pending-attachment strip and for sent messages in the chat |
| 4 | + bubble. Context differences are props, not restyled variants: |
| 5 | + - composer: uploading spinner, error tint, remove button |
| 6 | + - bubble: plain chip, click opens preview / downloads |
| 7 | + Tile rule: images show a real thumbnail, videos a play glyph, files a |
| 8 | + neutral file icon with the extension badge next to the name. --> |
| 9 | +<script setup lang="ts"> |
| 10 | +import { computed } from 'vue'; |
| 11 | +import { useI18n } from 'vue-i18n'; |
| 12 | +import AuthMedia from './AuthMedia.vue'; |
| 13 | +import Icon from '../ui/Icon.vue'; |
| 14 | +import Spinner from '../ui/Spinner.vue'; |
| 15 | +import Tooltip from '../ui/Tooltip.vue'; |
| 16 | +import type { IconName } from '../../lib/icons'; |
| 17 | +
|
| 18 | +const props = withDefaults( |
| 19 | + defineProps<{ |
| 20 | + kind: 'image' | 'video' | 'file'; |
| 21 | + /** Undefined only for pasted media without a name — a generic label shows. */ |
| 22 | + name?: string; |
| 23 | + /** Thumbnail source for images (object URL or the authed file URL). */ |
| 24 | + url?: string; |
| 25 | + /** When present, AuthMedia fetches image bytes with auth. */ |
| 26 | + fileId?: string; |
| 27 | + mediaType?: string; |
| 28 | + size?: number; |
| 29 | + /** Composer: upload in flight — spinner replaces the ext badge. */ |
| 30 | + uploading?: boolean; |
| 31 | + /** Composer: upload failed — chip tinted, info icon replaces the badge. */ |
| 32 | + error?: boolean; |
| 33 | + /** Composer: show a remove button. */ |
| 34 | + removable?: boolean; |
| 35 | + /** Accessible label for the remove button. */ |
| 36 | + removeLabel?: string; |
| 37 | + }>(), |
| 38 | + { uploading: false, error: false, removable: false }, |
| 39 | +); |
| 40 | +
|
| 41 | +const emit = defineEmits<{ |
| 42 | + /** Primary action (preview media / download file) — the parent decides. */ |
| 43 | + activate: []; |
| 44 | + remove: []; |
| 45 | +}>(); |
| 46 | +
|
| 47 | +const { t } = useI18n(); |
| 48 | +
|
| 49 | +const ext = computed(() => { |
| 50 | + const fromName = props.name?.match(/\.([A-Za-z0-9]{1,8})$/)?.[1]; |
| 51 | + const e = fromName ?? props.mediaType?.split('/')[1]?.split('+')[0]; |
| 52 | + return e ? e.toUpperCase() : undefined; |
| 53 | +}); |
| 54 | +
|
| 55 | +const fileIcon = computed<IconName>(() => { |
| 56 | + const e = ext.value ?? ''; |
| 57 | + if (/^(txt|md|doc|docx|rtf|log)$/i.test(e)) return 'file-text'; |
| 58 | + return 'file'; |
| 59 | +}); |
| 60 | +
|
| 61 | +const displayName = computed(() => { |
| 62 | + if (props.name) return props.name; |
| 63 | + if (props.kind === 'image') return t('composer.attachmentImage'); |
| 64 | + if (props.kind === 'video') return t('composer.attachmentVideo'); |
| 65 | + return t('composer.attachmentFile'); |
| 66 | +}); |
| 67 | +
|
| 68 | +function formatSize(size: number): string { |
| 69 | + if (size < 1024) return `${size} B`; |
| 70 | + if (size < 1024 * 1024) return `${Math.round(size / 1024)} KB`; |
| 71 | + return `${(size / (1024 * 1024)).toFixed(1)} MB`; |
| 72 | +} |
| 73 | +
|
| 74 | +const title = computed(() => { |
| 75 | + const parts = [displayName.value]; |
| 76 | + if (props.size !== undefined) parts.push(formatSize(props.size)); |
| 77 | + return parts.join(' · '); |
| 78 | +}); |
| 79 | +</script> |
| 80 | + |
| 81 | +<template> |
| 82 | + <span |
| 83 | + class="att-chip" |
| 84 | + :class="{ 'is-error': error, uploading }" |
| 85 | + :title="title" |
| 86 | + :data-kind="kind" |
| 87 | + > |
| 88 | + <button type="button" class="att-activate" :aria-label="title" @click="emit('activate')"> |
| 89 | + <span class="att-tile"> |
| 90 | + <AuthMedia |
| 91 | + v-if="kind === 'image' && url" |
| 92 | + :url="url" |
| 93 | + kind="image" |
| 94 | + :alt="name" |
| 95 | + :file-id="fileId" |
| 96 | + media-class="att-thumb" |
| 97 | + /> |
| 98 | + <Icon v-else-if="kind === 'video'" name="play" size="sm" /> |
| 99 | + <Icon v-else-if="kind === 'image'" name="image" size="sm" /> |
| 100 | + <Icon v-else :name="fileIcon" size="sm" /> |
| 101 | + </span> |
| 102 | + <span class="att-name">{{ displayName }}</span> |
| 103 | + <Spinner v-if="uploading" size="sm" :label="t('composer.uploading')" /> |
| 104 | + <span v-else-if="error" class="att-err"><Icon name="info" size="sm" /></span> |
| 105 | + </button> |
| 106 | + <Tooltip v-if="removable" :text="removeLabel ?? t('composer.remove')"> |
| 107 | + <button type="button" class="att-rm" :aria-label="removeLabel ?? t('composer.remove')" @click="emit('remove')"> |
| 108 | + <Icon name="close" size="sm" /> |
| 109 | + </button> |
| 110 | + </Tooltip> |
| 111 | + </span> |
| 112 | +</template> |
| 113 | + |
| 114 | +<style scoped> |
| 115 | +.att-chip { |
| 116 | + display: inline-flex; |
| 117 | + align-items: center; |
| 118 | + gap: 6px; |
| 119 | + max-width: 220px; |
| 120 | + padding: 4px 9px 4px 5px; |
| 121 | + background: var(--color-bg); |
| 122 | + border: 1px solid var(--color-line); |
| 123 | + border-radius: 999px; |
| 124 | + font-size: var(--ui-font-size-sm); |
| 125 | + transition: border-color var(--duration-fast) ease; |
| 126 | +} |
| 127 | +.att-chip:hover { |
| 128 | + border-color: var(--color-line-strong); |
| 129 | +} |
| 130 | +.att-activate { |
| 131 | + display: inline-flex; |
| 132 | + align-items: center; |
| 133 | + gap: 6px; |
| 134 | + min-width: 0; |
| 135 | + padding: 0; |
| 136 | + border: none; |
| 137 | + background: transparent; |
| 138 | + color: inherit; |
| 139 | + font: inherit; |
| 140 | + cursor: pointer; |
| 141 | +} |
| 142 | +.att-activate:focus-visible { |
| 143 | + outline: none; |
| 144 | + box-shadow: var(--p-focus-ring); |
| 145 | + border-radius: 999px; |
| 146 | +} |
| 147 | +.att-tile { |
| 148 | + width: 20px; |
| 149 | + height: 20px; |
| 150 | + border-radius: 50%; |
| 151 | + flex: none; |
| 152 | + display: flex; |
| 153 | + align-items: center; |
| 154 | + justify-content: center; |
| 155 | + overflow: hidden; |
| 156 | + color: var(--color-text-muted); |
| 157 | + background: var(--color-surface-sunken); |
| 158 | +} |
| 159 | +.att-tile :deep(.att-thumb) { |
| 160 | + width: 100%; |
| 161 | + height: 100%; |
| 162 | + object-fit: cover; |
| 163 | + display: block; |
| 164 | +} |
| 165 | +.att-name { |
| 166 | + min-width: 0; |
| 167 | + overflow: hidden; |
| 168 | + text-overflow: ellipsis; |
| 169 | + white-space: nowrap; |
| 170 | + color: var(--color-text); |
| 171 | + font-weight: var(--weight-medium); |
| 172 | +} |
| 173 | +.att-chip.is-error { |
| 174 | + border-color: var(--color-danger-bd); |
| 175 | +} |
| 176 | +.att-chip.is-error .att-err { |
| 177 | + flex: none; |
| 178 | + display: flex; |
| 179 | + align-items: center; |
| 180 | + color: var(--color-danger); |
| 181 | +} |
| 182 | +.att-rm { |
| 183 | + flex: none; |
| 184 | + display: flex; |
| 185 | + align-items: center; |
| 186 | + justify-content: center; |
| 187 | + width: 18px; |
| 188 | + height: 18px; |
| 189 | + padding: 0; |
| 190 | + border: none; |
| 191 | + border-radius: 50%; |
| 192 | + background: transparent; |
| 193 | + color: var(--color-text-faint); |
| 194 | + cursor: pointer; |
| 195 | +} |
| 196 | +.att-rm:hover { |
| 197 | + background: var(--color-hover); |
| 198 | + color: var(--color-text); |
| 199 | +} |
| 200 | +.att-rm:focus-visible { |
| 201 | + outline: none; |
| 202 | + box-shadow: var(--p-focus-ring); |
| 203 | +} |
| 204 | +</style> |
0 commit comments