add attachment-selector
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
# AttachmentSelector 附件选择器
|
||||
|
||||
用于展示、预览和选择图片、视频及其他附件。组件只管理界面状态;上传、删除保存和业务分类由调用方处理。
|
||||
|
||||
## 基础用法
|
||||
|
||||
```vue
|
||||
<attachment-selector
|
||||
:items="attachmentItems"
|
||||
@select="handleSelect"
|
||||
@update:items="handleItemsUpdate"
|
||||
/>
|
||||
```
|
||||
|
||||
`select` 事件携带文件、原生输入框和选择来源。调用方完成上传后,将附件列表写回 `items`。组件不会发起网络请求,也不会替调用方保存删除操作。
|
||||
|
||||
`select-start` 在打开文件选择器之前触发,携带 `file`、`photo` 或 `video` 来源,可用于在用户选完文件前设置业务分类。
|
||||
|
||||
## 附件条目
|
||||
|
||||
`AttachmentSelectorItem` 至少包含 `url`。`previewUrl` 可指定原始预览地址,`thumbnailUrl` 可指定缩略图,`mimeType` 和 `name` 用于识别及展示文件。未知业务字段会随条目更新一并保留。
|
||||
|
||||
图片和视频由组件预览。其他文件显示文件卡片,并通过 `open` 事件交由调用方打开。
|
||||
|
||||
## 自定义操作
|
||||
|
||||
- 默认操作包含“拍摄”菜单(拍照片、录视频)和“选择”按钮。
|
||||
- `capture-actions` 插槽可在默认拍摄菜单中追加业务操作,作用域提供 `capturePhoto` 方法。调用该方法会按拍照片流程打开相机。
|
||||
- `add-actions` 插槽可替换整个默认操作区域,并获得 `selectFiles`、`capturePhoto`、`captureVideo` 方法。只需增加拍摄菜单项时,优先使用 `capture-actions`。
|
||||
- `item-label` 插槽可展示业务分类或其他标签。
|
||||
- `item-actions` 插槽可替换条目角标操作。
|
||||
- `viewer-actions` 插槽可在预览器中加入业务操作,作用域包含当前条目、原列表索引和关闭预览的 `close` 方法。
|
||||
|
||||
## API
|
||||
|
||||
<api-table name="AttachmentSelector" />
|
||||
@@ -1,6 +1,6 @@
|
||||
# 组件总览
|
||||
|
||||
共 16 个组件,分为布局、编辑器、交互与 AI 对话四类。
|
||||
共 17 个组件,分为布局、编辑器、交互与 AI 对话四类。
|
||||
|
||||
## 布局组件
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
| --- | --- |
|
||||
| [BottomPopup](/components/bottom-popup) | 从底部升起的操作面板,可带标题与左右按钮。 |
|
||||
| [DurationPicker](/components/duration-picker) | 按时长值选择并回填,单位与候选项由组件内部推导。 |
|
||||
| [AttachmentSelector](/components/attachment-selector) | 选择并预览图片、视频及其他附件,上传和业务数据处理由调用方负责。 |
|
||||
|
||||
## AI 对话组件
|
||||
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ layout: home
|
||||
hero:
|
||||
name: timi-tdesign-mobile
|
||||
text: 移动端 TDesign 组件库
|
||||
tagline: 15 个业务组件,配套可交互演示与由源码自动生成的 API 文档
|
||||
tagline: 16 个业务组件,配套可交互演示与由源码自动生成的 API 文档
|
||||
actions:
|
||||
- theme: brand
|
||||
text: 快速开始
|
||||
|
||||
+2
-3
@@ -35,7 +35,7 @@
|
||||
"node": ">=16.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"tdesign-mobile-vue": "^1.13.2",
|
||||
"tdesign-mobile-vue": "^1.16.2",
|
||||
"timi-web": "link:E:/WebProject/timi-web"
|
||||
},
|
||||
"peerDependencies": {
|
||||
@@ -63,11 +63,10 @@
|
||||
"unplugin-auto-import": "^19.3.0",
|
||||
"unplugin-vue-components": "^28.7.0",
|
||||
"vite-plugin-dts": "^4.5.4",
|
||||
"vite-plugin-vue-setup-extend": "^0.4.0",
|
||||
"@vue/tsconfig": "^0.7.0",
|
||||
"vue": "3.5.26",
|
||||
"vue-router": "4.5.0",
|
||||
"vite": "7.1.11",
|
||||
"vite": "8.0.16",
|
||||
"vue-tsc": "^2.2.12"
|
||||
}
|
||||
}
|
||||
|
||||
Generated
+394
-336
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,206 @@
|
||||
<template>
|
||||
<div class="add-trigger">
|
||||
<slot
|
||||
name="actions"
|
||||
:select-files="selectFiles"
|
||||
:capture-photo="capturePhoto"
|
||||
:capture-video="captureVideo"
|
||||
>
|
||||
<t-popover
|
||||
v-model:visible="capturePopoverVisible"
|
||||
placement="left"
|
||||
theme="light"
|
||||
>
|
||||
<t-button
|
||||
class="capture"
|
||||
theme="primary"
|
||||
variant="dashed"
|
||||
:loading="busy"
|
||||
:disabled="busy"
|
||||
>
|
||||
<template #icon>
|
||||
<t-icon name="camera" />
|
||||
</template>
|
||||
拍摄
|
||||
</t-button>
|
||||
<template #content>
|
||||
<div class="add-actions">
|
||||
<t-button
|
||||
variant="text"
|
||||
theme="primary"
|
||||
class="add-action"
|
||||
:disabled="busy"
|
||||
@click="capturePhoto"
|
||||
>
|
||||
<template #icon>
|
||||
<t-icon name="camera" />
|
||||
</template>
|
||||
拍照片
|
||||
</t-button>
|
||||
<t-button
|
||||
variant="text"
|
||||
theme="primary"
|
||||
class="add-action"
|
||||
:disabled="busy"
|
||||
@click="captureVideo"
|
||||
>
|
||||
<template #icon>
|
||||
<t-icon name="video-camera-1" />
|
||||
</template>
|
||||
录视频
|
||||
</t-button>
|
||||
<slot name="capture-actions" :capture-photo="capturePhoto" />
|
||||
</div>
|
||||
</template>
|
||||
</t-popover>
|
||||
<t-button
|
||||
class="file"
|
||||
theme="primary"
|
||||
variant="dashed"
|
||||
:loading="busy"
|
||||
:disabled="busy"
|
||||
@click="selectFiles"
|
||||
>
|
||||
<template #icon>
|
||||
<t-icon name="file-attachment" />
|
||||
</template>
|
||||
选择
|
||||
</t-button>
|
||||
</slot>
|
||||
<input
|
||||
ref="fileInput"
|
||||
class="file-input"
|
||||
type="file"
|
||||
accept="*/*"
|
||||
multiple
|
||||
@change="handleFileChange($event, 'file')"
|
||||
/>
|
||||
<input
|
||||
ref="photoInput"
|
||||
class="file-input"
|
||||
type="file"
|
||||
accept="image/*"
|
||||
capture="environment"
|
||||
@change="handleFileChange($event, 'photo')"
|
||||
/>
|
||||
<input
|
||||
ref="videoInput"
|
||||
class="file-input"
|
||||
type="file"
|
||||
accept="video/*"
|
||||
capture="environment"
|
||||
@change="handleFileChange($event, 'video')"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import type { AttachmentSelectorSelection } from "./types";
|
||||
|
||||
interface Props {
|
||||
busy: boolean;
|
||||
}
|
||||
|
||||
const props = defineProps<Props>();
|
||||
const emit = defineEmits<{
|
||||
select: [value: AttachmentSelectorSelection];
|
||||
"select-start": [source: AttachmentSelectorSelection["source"]];
|
||||
}>();
|
||||
|
||||
const capturePopoverVisible = ref(false);
|
||||
const fileInput = ref<HTMLInputElement>();
|
||||
const photoInput = ref<HTMLInputElement>();
|
||||
const videoInput = ref<HTMLInputElement>();
|
||||
|
||||
function selectFiles(): void {
|
||||
openFilePicker(fileInput.value, "file");
|
||||
}
|
||||
|
||||
function capturePhoto(): void {
|
||||
capturePopoverVisible.value = false;
|
||||
openFilePicker(photoInput.value, "photo");
|
||||
}
|
||||
|
||||
function captureVideo(): void {
|
||||
capturePopoverVisible.value = false;
|
||||
openFilePicker(videoInput.value, "video");
|
||||
}
|
||||
|
||||
function openFilePicker(input: HTMLInputElement | undefined, source: AttachmentSelectorSelection["source"]): void {
|
||||
if (!input || props.busy) {
|
||||
return;
|
||||
}
|
||||
input.value = "";
|
||||
emit("select-start", source);
|
||||
input.click();
|
||||
}
|
||||
|
||||
function handleFileChange(event: Event, source: AttachmentSelectorSelection["source"]): void {
|
||||
const input = event.target as HTMLInputElement;
|
||||
const files = Array.from(input.files || []);
|
||||
if (files.length) {
|
||||
emit("select", { files, input, source });
|
||||
}
|
||||
input.value = "";
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.add-trigger {
|
||||
--td-popover-padding: 0 .5rem;
|
||||
gap: .375rem;
|
||||
width: 5.5rem;
|
||||
height: 5.5rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
||||
:deep(.t-popover__content) {
|
||||
border-radius: .875rem;
|
||||
}
|
||||
|
||||
.capture,
|
||||
.file {
|
||||
--td-button-border-radius: .875rem;
|
||||
|
||||
width: 5.5rem;
|
||||
height: 2.5625rem;
|
||||
min-height: 0;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.add-actions {
|
||||
gap: .25rem;
|
||||
display: grid;
|
||||
padding: .5rem 0;
|
||||
|
||||
:deep(.t-button) {
|
||||
--td-button-medium-height: 32px;
|
||||
--td-button-medium-padding-horizontal: 10px;
|
||||
|
||||
gap: .5rem;
|
||||
width: 100%;
|
||||
position: relative;
|
||||
font-weight: unset;
|
||||
justify-content: start;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.file-input {
|
||||
display: none;
|
||||
}
|
||||
|
||||
:global(html.theme-dark) {
|
||||
.capture,
|
||||
.file {
|
||||
background: rgba(37, 99, 235, .16);
|
||||
border-color: rgba(96, 165, 250, .28);
|
||||
|
||||
span {
|
||||
color: #93c5fd;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,175 @@
|
||||
<template>
|
||||
<div class="item">
|
||||
<t-button variant="text" class="preview-trigger" @click="emit('preview')">
|
||||
<div v-if="mediaKind === 'video'" class="video-thumbnail">
|
||||
<t-image
|
||||
v-if="thumbnailUrl"
|
||||
class="image"
|
||||
:src="thumbnailUrl"
|
||||
:alt="getMediaName(item)"
|
||||
fit="cover"
|
||||
/>
|
||||
<video
|
||||
v-else
|
||||
:src="previewUrl"
|
||||
muted
|
||||
playsinline
|
||||
preload="metadata"
|
||||
/>
|
||||
<t-icon class="play" name="play-circle-filled" size="2rem" />
|
||||
</div>
|
||||
<div v-else-if="mediaKind === 'file'" class="file-thumbnail">
|
||||
<t-icon name="file-attachment" size="2rem" />
|
||||
<span v-text="getMediaName(item)" />
|
||||
</div>
|
||||
<t-image
|
||||
v-else
|
||||
class="image"
|
||||
:src="thumbnailUrl"
|
||||
:alt="getMediaName(item)"
|
||||
fit="cover"
|
||||
/>
|
||||
</t-button>
|
||||
<div v-if="$slots.label || item.name" class="item-label">
|
||||
<slot name="label" :item="item">
|
||||
<span v-text="getMediaName(item)" />
|
||||
</slot>
|
||||
</div>
|
||||
<div v-if="$slots.actions || removable" class="item-actions">
|
||||
<slot name="actions" :item="item" :remove="removeItem">
|
||||
<t-button
|
||||
v-if="removable"
|
||||
theme="danger"
|
||||
class="remove"
|
||||
shape="circle"
|
||||
size="extra-small"
|
||||
:disabled="busy"
|
||||
@click="removeItem"
|
||||
>
|
||||
<t-icon name="close" size="1rem" />
|
||||
</t-button>
|
||||
</slot>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import type { AttachmentSelectorItem } from "./types";
|
||||
import { getMediaKind, getMediaName, getMediaPreviewURL, getMediaThumbnailURL } from "./utils";
|
||||
|
||||
interface Props {
|
||||
item: AttachmentSelectorItem;
|
||||
busy: boolean;
|
||||
removable: boolean;
|
||||
}
|
||||
|
||||
const props = defineProps<Props>();
|
||||
const emit = defineEmits<{
|
||||
preview: [];
|
||||
remove: [];
|
||||
}>();
|
||||
|
||||
const mediaKind = computed(() => getMediaKind(props.item));
|
||||
const previewUrl = computed(() => getMediaPreviewURL(props.item));
|
||||
const thumbnailUrl = computed(() => getMediaThumbnailURL(props.item));
|
||||
|
||||
function removeItem(): void {
|
||||
if (!props.busy) {
|
||||
emit("remove");
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.item {
|
||||
width: 5rem;
|
||||
height: 5rem;
|
||||
flex: 0 0 5rem;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
border-radius: 1rem;
|
||||
background: var(--td-bg-color-secondarycontainer, #f3f3f3);
|
||||
|
||||
.preview-trigger {
|
||||
padding: 0;
|
||||
border: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
display: block;
|
||||
cursor: pointer;
|
||||
background: transparent;
|
||||
|
||||
.image {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.video-thumbnail,
|
||||
.file-thumbnail {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.video-thumbnail {
|
||||
background: #0f172a;
|
||||
|
||||
video {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.play {
|
||||
inset: 50% auto auto 50%;
|
||||
opacity: .7;
|
||||
position: absolute;
|
||||
transform: translate(-50%, -50%);
|
||||
}
|
||||
}
|
||||
|
||||
.file-thumbnail {
|
||||
gap: .25rem;
|
||||
padding: .375rem;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
flex-direction: column;
|
||||
color: var(--td-text-color-secondary, #777);
|
||||
|
||||
span {
|
||||
width: 100%;
|
||||
overflow: hidden;
|
||||
font-size: .625rem;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.item-label {
|
||||
width: 100%;
|
||||
bottom: 0;
|
||||
padding: .125rem .5rem;
|
||||
position: absolute;
|
||||
overflow: hidden;
|
||||
font-size: .75rem;
|
||||
text-align: center;
|
||||
line-height: 1.2;
|
||||
border-radius: 0 0 1rem 1rem;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.item-actions {
|
||||
top: .375rem;
|
||||
right: .375rem;
|
||||
position: absolute;
|
||||
|
||||
.remove {
|
||||
:deep(.t-button__content) {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,145 @@
|
||||
<template>
|
||||
<teleport to="body">
|
||||
<t-image-viewer
|
||||
v-model:visible="viewerVisible"
|
||||
:images="images"
|
||||
:style="{ zIndex: props.viewerZIndex }"
|
||||
show-index
|
||||
@close="handleClose"
|
||||
@index-change="handleIndexChange"
|
||||
>
|
||||
<template #image="params">
|
||||
<div
|
||||
v-if="isVideoViewerItem(params.index)"
|
||||
:class="[params.className, 'viewer-video-wrap']"
|
||||
:style="params.style"
|
||||
@click.stop
|
||||
@pointerdown.stop
|
||||
@pointermove.stop
|
||||
@pointerup.stop
|
||||
@touchstart.stop
|
||||
@touchmove.stop
|
||||
@touchend.stop
|
||||
>
|
||||
<video
|
||||
:src="getViewerMediaURL(params.index)"
|
||||
class="viewer-video"
|
||||
controls
|
||||
playsinline
|
||||
preload="metadata"
|
||||
@loadedmetadata="handleViewerVideoLoaded($event, params)"
|
||||
@transitionstart="params.onTransitionstart"
|
||||
@transitionend="params.onTransitionend"
|
||||
/>
|
||||
</div>
|
||||
<img
|
||||
v-else
|
||||
:src="params.src"
|
||||
:class="params.className"
|
||||
:style="params.style"
|
||||
:alt="getViewerAlt(params.index)"
|
||||
@load="params.onLoad"
|
||||
@transitionstart="params.onTransitionstart"
|
||||
@transitionend="params.onTransitionend"
|
||||
/>
|
||||
</template>
|
||||
<template #delete-btn>
|
||||
<div v-if="$slots.actions" class="viewer-actions" @click.stop>
|
||||
<slot name="actions" :item="currentMediaItem" :index="currentSourceIndex" :close="handleClose" />
|
||||
</div>
|
||||
</template>
|
||||
</t-image-viewer>
|
||||
</teleport>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import type { ImageSlotParams } from "tdesign-mobile-vue";
|
||||
import type { AttachmentSelectorViewerItem } from "./types";
|
||||
import { getMediaKind, getMediaName, getMediaPreviewURL } from "./utils";
|
||||
|
||||
interface Props {
|
||||
images: string[];
|
||||
items: readonly AttachmentSelectorViewerItem[];
|
||||
visible: boolean;
|
||||
viewerZIndex?: number;
|
||||
}
|
||||
|
||||
const props = defineProps<Props>();
|
||||
const emit = defineEmits<{
|
||||
"update:visible": [value: boolean];
|
||||
}>();
|
||||
|
||||
const viewerVisible = computed({
|
||||
get: () => props.visible,
|
||||
set: value => emit("update:visible", value)
|
||||
});
|
||||
const viewerIndex = ref(0);
|
||||
const currentMediaItem = computed(() => props.items[viewerIndex.value]);
|
||||
const currentSourceIndex = computed(() => currentMediaItem.value?.sourceIndex ?? -1);
|
||||
|
||||
watch(() => props.visible, value => {
|
||||
if (value) {
|
||||
viewerIndex.value = 0;
|
||||
}
|
||||
});
|
||||
|
||||
function handleClose(): void {
|
||||
emit("update:visible", false);
|
||||
}
|
||||
|
||||
function handleIndexChange(index: number): void {
|
||||
viewerIndex.value = index;
|
||||
}
|
||||
|
||||
function isVideoViewerItem(index?: number): boolean {
|
||||
const item = getViewerItem(index);
|
||||
return !!item && getMediaKind(item) === "video";
|
||||
}
|
||||
|
||||
function getViewerItem(index?: number): AttachmentSelectorViewerItem | undefined {
|
||||
return index === undefined ? undefined : props.items[index];
|
||||
}
|
||||
|
||||
function getViewerMediaURL(index?: number): string {
|
||||
const item = getViewerItem(index);
|
||||
return item ? getMediaPreviewURL(item) : "";
|
||||
}
|
||||
|
||||
function getViewerAlt(index?: number): string {
|
||||
const item = getViewerItem(index);
|
||||
return item ? getMediaName(item) : "";
|
||||
}
|
||||
|
||||
function handleViewerVideoLoaded(event: Event, params: ImageSlotParams): void {
|
||||
const video = event.currentTarget as HTMLVideoElement;
|
||||
if (0 < video.videoHeight) {
|
||||
video.height = video.videoHeight;
|
||||
}
|
||||
params.onLoad?.(event);
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.viewer-actions {
|
||||
color: #fff;
|
||||
margin-left: -1rem;
|
||||
}
|
||||
|
||||
.viewer-video-wrap {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
max-height: 100%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.viewer-video {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
max-height: 100%;
|
||||
background: #0f172a;
|
||||
object-fit: contain;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,12 @@
|
||||
import view from "./index.vue";
|
||||
import { WebToolkit } from "timi-web";
|
||||
|
||||
export type {
|
||||
AttachmentSelectorItem,
|
||||
AttachmentSelectorProps,
|
||||
AttachmentSelectorSelection,
|
||||
AttachmentSelectorViewerItem
|
||||
} from "./types";
|
||||
|
||||
export const AttachmentSelector = WebToolkit.withInstall(view);
|
||||
export default AttachmentSelector;
|
||||
@@ -0,0 +1,164 @@
|
||||
<template>
|
||||
<div v-bind="$attrs" class="attachment-selector">
|
||||
<div class="label" :class="{ required }">
|
||||
<slot name="title">
|
||||
<span v-text="title" />
|
||||
</slot>
|
||||
</div>
|
||||
<div class="media-shell" :class="{ readonly }">
|
||||
<div class="items">
|
||||
<center-tips v-if="!props.items.length" v-text="emptyText" />
|
||||
<attachment-selector-item
|
||||
v-for="(item, index) in props.items"
|
||||
:key="item.id ?? item.url + '-' + index"
|
||||
:item="item"
|
||||
:busy="isBusy"
|
||||
:removable="!readonly"
|
||||
@preview="openMedia(index)"
|
||||
@remove="removeMedia(index)"
|
||||
>
|
||||
<template v-if="$slots['item-label']" #label>
|
||||
<slot name="item-label" :item="item" :index="index" />
|
||||
</template>
|
||||
<template v-if="$slots['item-actions']" #actions="slotProps">
|
||||
<slot name="item-actions" :item="item" :index="index" :remove="slotProps.remove" />
|
||||
</template>
|
||||
</attachment-selector-item>
|
||||
</div>
|
||||
<attachment-selector-add
|
||||
v-if="!readonly"
|
||||
:busy="isBusy"
|
||||
@select="emit('select', $event)"
|
||||
@select-start="emit('select-start', $event)"
|
||||
>
|
||||
<template v-if="$slots['add-actions']" #actions="slotProps">
|
||||
<slot name="add-actions" v-bind="slotProps" />
|
||||
</template>
|
||||
<template v-if="$slots['capture-actions']" #capture-actions="slotProps">
|
||||
<slot name="capture-actions" v-bind="slotProps" />
|
||||
</template>
|
||||
</attachment-selector-add>
|
||||
</div>
|
||||
<attachment-selector-viewer
|
||||
v-model:visible="mediaViewerVisible"
|
||||
:images="mediaViewerImages"
|
||||
:items="mediaViewerItems"
|
||||
:viewer-z-index="viewerZIndex"
|
||||
>
|
||||
<template v-if="$slots['viewer-actions']" #actions="slotProps">
|
||||
<slot name="viewer-actions" v-bind="slotProps" />
|
||||
</template>
|
||||
</attachment-selector-viewer>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import AttachmentSelectorAdd from "./AttachmentSelectorAdd.vue";
|
||||
import AttachmentSelectorItem from "./AttachmentSelectorItem.vue";
|
||||
import AttachmentSelectorViewer from "./AttachmentSelectorViewer.vue";
|
||||
import type {
|
||||
AttachmentSelectorItem as AttachmentItem,
|
||||
AttachmentSelectorProps,
|
||||
AttachmentSelectorSelection,
|
||||
AttachmentSelectorViewerItem
|
||||
} from "./types";
|
||||
import { getMediaKind, getMediaPreviewURL } from "./utils";
|
||||
import { CenterTips } from "../../../../timi-web";
|
||||
|
||||
defineOptions({
|
||||
name: "AttachmentSelector",
|
||||
inheritAttrs: false
|
||||
});
|
||||
|
||||
const props = withDefaults(defineProps<AttachmentSelectorProps>(), {
|
||||
busy: false,
|
||||
emptyText: "暂无附件",
|
||||
items: () => [],
|
||||
readonly: false,
|
||||
required: false,
|
||||
title: "附件列表"
|
||||
});
|
||||
|
||||
const emit = defineEmits<{
|
||||
"update:items": [value: AttachmentItem[]];
|
||||
select: [value: AttachmentSelectorSelection];
|
||||
"select-start": [source: AttachmentSelectorSelection["source"]];
|
||||
open: [item: AttachmentItem, index: number];
|
||||
}>();
|
||||
|
||||
const isBusy = computed(() => props.busy);
|
||||
const mediaViewerVisible = ref(false);
|
||||
const mediaViewerItems = ref<AttachmentSelectorViewerItem[]>([]);
|
||||
const mediaViewerImages = computed(() => mediaViewerItems.value.map(getMediaPreviewURL));
|
||||
|
||||
function openMedia(index: number): void {
|
||||
const item = props.items[index];
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
if (getMediaKind(item) === "file") {
|
||||
emit("open", item, index);
|
||||
return;
|
||||
}
|
||||
|
||||
const orderedItems = props.items.map((attachmentItem, sourceIndex) => ({ attachmentItem, sourceIndex }));
|
||||
const activeIndex = orderedItems.findIndex(({ sourceIndex }) => sourceIndex === index);
|
||||
if (activeIndex < 0) {
|
||||
return;
|
||||
}
|
||||
const rotatedItems = [...orderedItems.slice(activeIndex), ...orderedItems.slice(0, activeIndex)];
|
||||
mediaViewerItems.value = rotatedItems
|
||||
.filter(({ attachmentItem }) => getMediaKind(attachmentItem) !== "file")
|
||||
.map(({ attachmentItem, sourceIndex }) => ({ ...attachmentItem, sourceIndex }));
|
||||
mediaViewerVisible.value = true;
|
||||
}
|
||||
|
||||
function removeMedia(index: number): void {
|
||||
if (isBusy.value || props.readonly || !props.items[index]) {
|
||||
return;
|
||||
}
|
||||
emit("update:items", props.items.filter((_, itemIndex) => itemIndex !== index));
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.attachment-selector {
|
||||
gap: .375rem;
|
||||
width: 100%;
|
||||
display: grid;
|
||||
|
||||
.label {
|
||||
|
||||
&.required::before {
|
||||
color: var(--td-error-color, #e34d59);
|
||||
content: "*";
|
||||
margin-right: .25rem;
|
||||
}
|
||||
}
|
||||
|
||||
.media-shell {
|
||||
gap: .75rem;
|
||||
display: grid;
|
||||
align-items: start;
|
||||
grid-template-columns: minmax(0, 1fr) 5.5rem;
|
||||
|
||||
&.readonly {
|
||||
grid-template-columns: minmax(0, 1fr);
|
||||
}
|
||||
|
||||
.items {
|
||||
gap: .5rem;
|
||||
height: 100%;
|
||||
padding: .25rem;
|
||||
display: flex;
|
||||
min-width: 0;
|
||||
overflow-x: auto;
|
||||
overflow-y: hidden;
|
||||
flex-wrap: nowrap;
|
||||
background: var(--td-bg-color-page);
|
||||
align-items: center;
|
||||
border-radius: 1.25rem;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,45 @@
|
||||
/** 附件选择器展示的条目。 */
|
||||
export interface AttachmentSelectorItem {
|
||||
/** 附件地址。 */
|
||||
url: string;
|
||||
/** 可选的稳定标识。 */
|
||||
id?: string | number;
|
||||
/** 原始预览地址。 */
|
||||
previewUrl?: string;
|
||||
/** 列表缩略图地址。 */
|
||||
thumbnailUrl?: string;
|
||||
/** MIME 类型。 */
|
||||
mimeType?: string;
|
||||
/** 文件名称。 */
|
||||
name?: string;
|
||||
}
|
||||
|
||||
/** 从文件选择器中选择的文件。 */
|
||||
export interface AttachmentSelectorSelection {
|
||||
files: File[];
|
||||
input: HTMLInputElement;
|
||||
source: "file" | "photo" | "video";
|
||||
}
|
||||
|
||||
/** 当前预览条目及其在原列表中的位置。 */
|
||||
export type AttachmentSelectorViewerItem = AttachmentSelectorItem & {
|
||||
sourceIndex: number;
|
||||
};
|
||||
|
||||
/** 附件选择器属性。 */
|
||||
export interface AttachmentSelectorProps {
|
||||
/** 当前附件条目。 */
|
||||
items: readonly AttachmentSelectorItem[];
|
||||
/** 是否正在处理附件操作。 */
|
||||
busy?: boolean;
|
||||
/** 列表为空时显示的文案。 */
|
||||
emptyText?: string;
|
||||
/** 是否只读。 */
|
||||
readonly?: boolean;
|
||||
/** 是否显示必填标记。 */
|
||||
required?: boolean;
|
||||
/** 列表标题。 */
|
||||
title?: string;
|
||||
/** 预览器层级。 */
|
||||
viewerZIndex?: number;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import type { AttachmentSelectorItem } from "./types";
|
||||
|
||||
export type MediaKind = "image" | "video" | "file";
|
||||
|
||||
/** 取得媒体预览地址。 */
|
||||
export function getMediaPreviewURL(item: AttachmentSelectorItem): string {
|
||||
return item.previewUrl || item.url;
|
||||
}
|
||||
|
||||
/** 根据 MIME 类型和文件扩展名判断媒体类型。 */
|
||||
export function getMediaKind(item: AttachmentSelectorItem): MediaKind {
|
||||
const mimeType = (item.mimeType || "").toLowerCase();
|
||||
if (mimeType.startsWith("video/")) {
|
||||
return "video";
|
||||
}
|
||||
if (mimeType.startsWith("image/")) {
|
||||
return "image";
|
||||
}
|
||||
if (mimeType) {
|
||||
return "file";
|
||||
}
|
||||
|
||||
const url = item.url.split(/[?#]/, 1)[0] || "";
|
||||
if (/\.(avi|m4v|mkv|mov|mp4|mpeg|mpg|ogv|webm|wmv)$/i.test(url)) {
|
||||
return "video";
|
||||
}
|
||||
if (/\.(pdf|docx?|xlsx?|pptx?|zip|rar|txt)$/i.test(url)) {
|
||||
return "file";
|
||||
}
|
||||
|
||||
return "image";
|
||||
}
|
||||
|
||||
/** 取得媒体列表的缩略图地址。 */
|
||||
export function getMediaThumbnailURL(item: AttachmentSelectorItem): string {
|
||||
if (item.thumbnailUrl) {
|
||||
return item.thumbnailUrl;
|
||||
}
|
||||
|
||||
return getMediaKind(item) === "image" ? item.url : "";
|
||||
}
|
||||
|
||||
/** 取得可读的文件名称。 */
|
||||
export function getMediaName(item: AttachmentSelectorItem): string {
|
||||
if (item.name) {
|
||||
return item.name;
|
||||
}
|
||||
|
||||
const url = item.url.split(/[?#]/, 1)[0] || "";
|
||||
const fileName = url.split("/").pop();
|
||||
if (!fileName) {
|
||||
return "文件";
|
||||
}
|
||||
try {
|
||||
return decodeURIComponent(fileName);
|
||||
} catch {
|
||||
return fileName;
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import ArticleView from "./article-view";
|
||||
import NotifyEditor from "./notify-editor";
|
||||
import NotifyView from "./notify-view";
|
||||
import BottomPopup from "./bottom-popup";
|
||||
import AttachmentSelector from "./attachment-selector";
|
||||
import Space from "./space";
|
||||
import CardContainer from "./card-container";
|
||||
import {
|
||||
@@ -31,6 +32,7 @@ export default [
|
||||
NotifyEditor,
|
||||
NotifyView,
|
||||
BottomPopup,
|
||||
AttachmentSelector,
|
||||
Space,
|
||||
CardContainer,
|
||||
AiChatComposer,
|
||||
@@ -51,6 +53,7 @@ export {
|
||||
NotifyEditor,
|
||||
NotifyView,
|
||||
BottomPopup,
|
||||
AttachmentSelector,
|
||||
Space,
|
||||
CardContainer,
|
||||
AiChatComposer,
|
||||
@@ -98,6 +101,13 @@ export type {
|
||||
CardContainerProps
|
||||
} from "./card-container";
|
||||
|
||||
export type {
|
||||
AttachmentSelectorItem,
|
||||
AttachmentSelectorProps,
|
||||
AttachmentSelectorSelection,
|
||||
AttachmentSelectorViewerItem
|
||||
} from "./attachment-selector";
|
||||
|
||||
export type {
|
||||
PageTransitionBackground,
|
||||
PageTransitionClass,
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { resolve } from "path";
|
||||
import { Alias, defineConfig } from "vite";
|
||||
import vue from "@vitejs/plugin-vue";
|
||||
import VueSetupExtend from "vite-plugin-vue-setup-extend";
|
||||
import AutoImport from "unplugin-auto-import/vite";
|
||||
import Components from "unplugin-vue-components/vite";
|
||||
import { TDesignResolver } from "unplugin-vue-components/resolvers";
|
||||
@@ -53,7 +52,6 @@ export default defineConfig({
|
||||
vue({
|
||||
include: [/\.vue$/, /\.md$/]
|
||||
}),
|
||||
VueSetupExtend(),
|
||||
dts({
|
||||
pathsToAliases: false,
|
||||
aliasesExclude: [/^timi-web\/editor$/]
|
||||
|
||||
Reference in New Issue
Block a user