forked from natuka/web.puabadge.com
535 lines
12 KiB
Vue
535 lines
12 KiB
Vue
<template>
|
|
<div class="composer">
|
|
<div v-if="editing" class="composer__edit-bar">
|
|
<span class="composer__edit-left">
|
|
<el-icon :size="14"><EditPen /></el-icon>
|
|
<span>{{ editingHint }}</span>
|
|
</span>
|
|
<button class="composer__exit" type="button" @click="emit('exit-edit')">
|
|
<el-icon :size="12"><Close /></el-icon>
|
|
<span>{{ exitEditLabel }}</span>
|
|
</button>
|
|
</div>
|
|
|
|
<p v-if="statusTip" class="composer__tip" :class="{ 'is-warn': tipWarn }">{{ statusTip }}</p>
|
|
|
|
<div ref="boxRef" class="composer__box" :class="{ 'is-editing': editing, 'is-disabled': disabled }">
|
|
<div v-if="previewUrl" ref="previewRef" class="composer__preview">
|
|
<img class="composer__preview-img" :src="previewUrl" alt="已选图片" />
|
|
<span v-if="inspecting" class="composer__preview-mask">识别中</span>
|
|
<button
|
|
v-else
|
|
class="composer__preview-close"
|
|
type="button"
|
|
aria-label="移除图片"
|
|
@click="emit('clear-preview')"
|
|
>
|
|
<el-icon :size="10" color="#fff"><Close /></el-icon>
|
|
</button>
|
|
</div>
|
|
|
|
<div v-if="titleSpec" class="composer__title" :class="{ 'is-invalid': titleInvalid }">
|
|
<label class="composer__title-label" :for="titleInputId">{{ titleSpec.label }}</label>
|
|
<div class="composer__title-field">
|
|
<input
|
|
:id="titleInputId"
|
|
class="composer__title-input"
|
|
type="text"
|
|
:value="productTitle"
|
|
:maxlength="titleSpec.max"
|
|
:placeholder="titleSpec.placeholder"
|
|
:disabled="disabled"
|
|
:aria-invalid="titleInvalid"
|
|
@input="onTitleInput"
|
|
/>
|
|
<span class="composer__title-count">{{ productTitleLength }} / {{ titleSpec.max }}</span>
|
|
</div>
|
|
<p v-if="titleInvalid" class="composer__title-error" role="alert">
|
|
请填写{{ titleSpec.label }}
|
|
</p>
|
|
</div>
|
|
|
|
<textarea
|
|
ref="textareaRef"
|
|
class="composer__input"
|
|
rows="2"
|
|
:value="modelValue"
|
|
:placeholder="resolvedPlaceholder"
|
|
:disabled="disabled"
|
|
@input="onInput"
|
|
@keydown="onKeydown"
|
|
/>
|
|
|
|
<div class="composer__toolbar">
|
|
<button
|
|
class="composer__upload"
|
|
type="button"
|
|
:aria-label="editing ? '编辑中不可更换照片' : '上传图片'"
|
|
:disabled="disabled || editing || inspecting"
|
|
@click="emit('upload')"
|
|
>
|
|
<el-icon v-if="inspecting" class="is-loading" :size="26"><Loading /></el-icon>
|
|
<el-icon v-else :size="28"><Picture /></el-icon>
|
|
</button>
|
|
|
|
<div class="composer__tags">
|
|
<span v-for="tag in tagItems" :key="tag.key" class="composer__tag" :title="tag.label">
|
|
<el-icon class="composer__tag-icon" :size="12"><component :is="tag.icon" /></el-icon>
|
|
<span class="composer__tag-text">{{ tag.label }}</span>
|
|
</span>
|
|
</div>
|
|
|
|
<button
|
|
class="composer__send"
|
|
type="button"
|
|
aria-label="发送"
|
|
:disabled="disabled || !canSend"
|
|
@click="emit('send')"
|
|
>
|
|
<el-icon :size="18"><Top /></el-icon>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { computed, ref } from 'vue'
|
|
import { Close, EditPen, Loading, Picture, Present, Star, Top, User } from '@element-plus/icons-vue'
|
|
import type { ProductTitleSpec } from '@/utils/jingdong-product-title'
|
|
|
|
export type ComposerTag = {
|
|
key: string
|
|
label: string
|
|
icon?: 'user' | 'star' | 'present'
|
|
}
|
|
|
|
const props = withDefaults(
|
|
defineProps<{
|
|
modelValue?: string
|
|
placeholder?: string
|
|
placeholderNeedPhoto?: string
|
|
placeholderEditing?: string
|
|
placeholderCartoon?: string
|
|
previewUrl?: string
|
|
tags?: ComposerTag[]
|
|
editing?: boolean
|
|
editingHint?: string
|
|
exitEditLabel?: string
|
|
disabled?: boolean
|
|
canSend?: boolean
|
|
/** 图片识别 / 主体识别进行中 */
|
|
inspecting?: boolean
|
|
/** 当前预览图是否为卡通效果图 */
|
|
cartoonEffect?: boolean
|
|
/** 底部状态提示 */
|
|
statusTip?: string
|
|
tipWarn?: boolean
|
|
/** 产品标题规则(有值时展示标题输入) */
|
|
titleSpec?: ProductTitleSpec | null
|
|
productTitle?: string
|
|
/** 标题未填时高亮错误 */
|
|
titleInvalid?: boolean
|
|
}>(),
|
|
{
|
|
modelValue: '',
|
|
placeholder: '',
|
|
placeholderNeedPhoto: '请先点击左侧按钮上传照片',
|
|
placeholderEditing: '说说想怎么改…',
|
|
placeholderCartoon: '已识别为效果图,可直接发送',
|
|
previewUrl: '',
|
|
tags: () => [],
|
|
editing: false,
|
|
editingHint: '',
|
|
exitEditLabel: '退出编辑',
|
|
disabled: false,
|
|
canSend: false,
|
|
inspecting: false,
|
|
cartoonEffect: false,
|
|
statusTip: '',
|
|
tipWarn: false,
|
|
titleSpec: null,
|
|
productTitle: '',
|
|
titleInvalid: false,
|
|
},
|
|
)
|
|
|
|
const emit = defineEmits<{
|
|
(e: 'update:modelValue', value: string): void
|
|
(e: 'update:productTitle', value: string): void
|
|
(e: 'upload'): void
|
|
(e: 'clear-preview'): void
|
|
(e: 'send'): void
|
|
(e: 'exit-edit'): void
|
|
}>()
|
|
|
|
const titleInputId = 'jd-composer-product-title'
|
|
const productTitleLength = computed(() => String(props.productTitle || '').length)
|
|
|
|
const iconMap = {
|
|
user: User,
|
|
star: Star,
|
|
present: Present,
|
|
}
|
|
|
|
const tagItems = computed(() =>
|
|
(props.tags || [])
|
|
.filter((tag) => tag && String(tag.label || '').trim())
|
|
.map((tag) => ({
|
|
key: tag.key || tag.label,
|
|
label: String(tag.label).trim(),
|
|
icon: iconMap[tag.icon || 'present'] || Present,
|
|
})),
|
|
)
|
|
|
|
const resolvedPlaceholder = computed(() => {
|
|
if (props.editing) return props.placeholderEditing || props.placeholder
|
|
if (!props.previewUrl) return props.placeholderNeedPhoto || props.placeholder
|
|
if (props.cartoonEffect) return props.placeholderCartoon || props.placeholder
|
|
return props.placeholder
|
|
})
|
|
|
|
const textareaRef = ref<HTMLTextAreaElement | null>(null)
|
|
const boxRef = ref<HTMLElement | null>(null)
|
|
const previewRef = ref<HTMLElement | null>(null)
|
|
|
|
function autosize() {
|
|
const el = textareaRef.value
|
|
if (!el) return
|
|
el.style.height = 'auto'
|
|
el.style.height = `${Math.min(el.scrollHeight, 96)}px`
|
|
}
|
|
|
|
function onInput(e: Event) {
|
|
const target = e.target as HTMLTextAreaElement
|
|
emit('update:modelValue', target.value)
|
|
autosize()
|
|
}
|
|
|
|
function onTitleInput(e: Event) {
|
|
const target = e.target as HTMLInputElement
|
|
emit('update:productTitle', target.value)
|
|
}
|
|
|
|
function onKeydown(e: KeyboardEvent) {
|
|
if (e.key !== 'Enter' || e.shiftKey) return
|
|
// 移动端/桌面:Enter 发送,Shift+Enter 换行
|
|
e.preventDefault()
|
|
if (!props.disabled && props.canSend) emit('send')
|
|
}
|
|
|
|
function focus() {
|
|
textareaRef.value?.focus()
|
|
}
|
|
|
|
function getPreviewEl() {
|
|
return previewRef.value
|
|
}
|
|
|
|
function getFlyTargetEl() {
|
|
return previewRef.value || boxRef.value
|
|
}
|
|
|
|
defineExpose({ focus, getPreviewEl, getFlyTargetEl })
|
|
</script>
|
|
|
|
<style scoped lang="scss">
|
|
.composer {
|
|
flex-shrink: 0;
|
|
padding: 8px 12px calc(10px + env(safe-area-inset-bottom));
|
|
background: transparent;
|
|
}
|
|
|
|
.composer__tip {
|
|
margin: 0 2px 8px;
|
|
padding: 0 4px;
|
|
font-size: 12px;
|
|
line-height: 1.4;
|
|
color: #888;
|
|
|
|
&.is-warn {
|
|
color: #c45c26;
|
|
}
|
|
}
|
|
|
|
.composer__edit-bar {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
gap: 8px;
|
|
margin-bottom: 8px;
|
|
padding: 8px 12px;
|
|
border-radius: 12px;
|
|
background: #eaf6ec;
|
|
color: #3a8e4d;
|
|
font-size: 12px;
|
|
}
|
|
|
|
.composer__edit-left {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 6px;
|
|
min-width: 0;
|
|
}
|
|
|
|
.composer__exit {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 3px;
|
|
flex-shrink: 0;
|
|
border: 0;
|
|
background: transparent;
|
|
color: #3a8e4d;
|
|
font-size: 12px;
|
|
cursor: pointer;
|
|
padding: 0;
|
|
}
|
|
|
|
.composer__box {
|
|
padding: 12px 12px 10px;
|
|
border-radius: 18px;
|
|
background: #ffffff;
|
|
border: 1px solid #ebebeb;
|
|
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.04);
|
|
transition: border-color 0.2s ease, box-shadow 0.2s ease;
|
|
|
|
&.is-editing {
|
|
border-color: rgba(58, 142, 77, 0.45);
|
|
box-shadow: 0 4px 16px rgba(58, 142, 77, 0.08);
|
|
}
|
|
|
|
&.is-disabled {
|
|
opacity: 0.92;
|
|
}
|
|
}
|
|
|
|
.composer__preview {
|
|
position: relative;
|
|
width: 56px;
|
|
height: 56px;
|
|
margin-bottom: 8px;
|
|
border-radius: 10px;
|
|
overflow: hidden;
|
|
background: #f0f0f0;
|
|
|
|
img {
|
|
width: 100%;
|
|
height: 100%;
|
|
object-fit: cover;
|
|
display: block;
|
|
}
|
|
}
|
|
|
|
.composer__preview-mask {
|
|
position: absolute;
|
|
inset: 0;
|
|
display: inline-flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
background: rgba(0, 0, 0, 0.45);
|
|
color: #fff;
|
|
font-size: 11px;
|
|
font-weight: 500;
|
|
}
|
|
|
|
.composer__preview-close {
|
|
position: absolute;
|
|
top: 2px;
|
|
right: 2px;
|
|
width: 18px;
|
|
height: 18px;
|
|
border: 0;
|
|
border-radius: 50%;
|
|
background: rgba(0, 0, 0, 0.55);
|
|
display: inline-flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
cursor: pointer;
|
|
padding: 0;
|
|
}
|
|
|
|
.composer__title {
|
|
margin: 0 0 10px;
|
|
padding: 8px 10px;
|
|
border-radius: 12px;
|
|
background: #f7f8f7;
|
|
border: 1px solid transparent;
|
|
|
|
&.is-invalid {
|
|
border-color: rgba(196, 92, 38, 0.45);
|
|
background: #fff8f4;
|
|
}
|
|
}
|
|
|
|
.composer__title-label {
|
|
display: block;
|
|
margin-bottom: 6px;
|
|
font-size: 12px;
|
|
font-weight: 600;
|
|
color: #3a8e4d;
|
|
}
|
|
|
|
.composer__title-field {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
}
|
|
|
|
.composer__title-input {
|
|
flex: 1;
|
|
min-width: 0;
|
|
height: 32px;
|
|
padding: 0 10px;
|
|
border: 1px solid #e6e6e6;
|
|
border-radius: 8px;
|
|
outline: none;
|
|
background: #fff;
|
|
font-size: 13px;
|
|
color: #1a1a1a;
|
|
font-family: inherit;
|
|
|
|
&::placeholder {
|
|
color: #b0b0b0;
|
|
}
|
|
|
|
&:disabled {
|
|
opacity: 0.6;
|
|
}
|
|
|
|
&:focus {
|
|
border-color: rgba(58, 142, 77, 0.55);
|
|
}
|
|
}
|
|
|
|
.composer__title-count {
|
|
flex-shrink: 0;
|
|
font-size: 11px;
|
|
color: #999;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.composer__title-error {
|
|
margin: 6px 0 0;
|
|
font-size: 12px;
|
|
line-height: 1.3;
|
|
color: #c45c26;
|
|
}
|
|
|
|
.composer__input {
|
|
display: block;
|
|
width: 100%;
|
|
min-height: 44px;
|
|
max-height: 96px;
|
|
margin: 0 0 10px;
|
|
padding: 0;
|
|
border: 0;
|
|
resize: none;
|
|
outline: none;
|
|
background: transparent;
|
|
font-size: 14px;
|
|
line-height: 1.5;
|
|
color: #1a1a1a;
|
|
font-family: inherit;
|
|
|
|
&::placeholder {
|
|
color: #b0b0b0;
|
|
}
|
|
|
|
&:disabled {
|
|
opacity: 0.6;
|
|
}
|
|
}
|
|
|
|
.composer__toolbar {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
}
|
|
|
|
.composer__upload {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
width: 44px;
|
|
height: 44px;
|
|
margin: -6px 0 -6px -6px;
|
|
padding: 0;
|
|
border: 0;
|
|
background: transparent;
|
|
color: #666;
|
|
cursor: pointer;
|
|
flex-shrink: 0;
|
|
|
|
&:disabled {
|
|
opacity: 0.4;
|
|
cursor: not-allowed;
|
|
}
|
|
|
|
.is-loading {
|
|
animation: jd-spin 0.9s linear infinite;
|
|
}
|
|
}
|
|
|
|
@keyframes jd-spin {
|
|
to {
|
|
transform: rotate(360deg);
|
|
}
|
|
}
|
|
|
|
.composer__tags {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 6px;
|
|
flex: 1;
|
|
min-width: 0;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.composer__tag {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 3px;
|
|
flex: 0 1 auto;
|
|
min-width: 0;
|
|
max-width: 88px;
|
|
height: 26px;
|
|
padding: 0 8px;
|
|
border-radius: 999px;
|
|
background: #f2f2f2;
|
|
color: #888;
|
|
font-size: 12px;
|
|
box-sizing: border-box;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.composer__tag-icon {
|
|
flex-shrink: 0;
|
|
}
|
|
|
|
.composer__tag-text {
|
|
min-width: 0;
|
|
overflow: hidden;
|
|
white-space: nowrap;
|
|
text-overflow: ellipsis;
|
|
}
|
|
|
|
.composer__send {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
width: 36px;
|
|
height: 36px;
|
|
flex-shrink: 0;
|
|
border: 0;
|
|
border-radius: 10px;
|
|
background: #3a8e4d;
|
|
color: #fff;
|
|
cursor: pointer;
|
|
|
|
&:disabled {
|
|
opacity: 0.4;
|
|
cursor: not-allowed;
|
|
}
|
|
}
|
|
</style>
|