add attachment-selector
This commit is contained in:
@@ -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>
|
||||
Reference in New Issue
Block a user