upper attachment-selector to lib
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
<template>
|
||||
<shared-attachment-selector
|
||||
v-bind="$attrs"
|
||||
:items="attachmentItems"
|
||||
:busy="isBusy"
|
||||
:empty-text="props.emptyText"
|
||||
:readonly="props.readonly"
|
||||
:required="props.required"
|
||||
:title="props.title"
|
||||
:viewer-z-index="props.viewerZIndex"
|
||||
@select="handleSelect"
|
||||
@select-start="emit('select-start', $event)"
|
||||
@update:items="handleItemsUpdate"
|
||||
@open="handleOpen"
|
||||
>
|
||||
<template v-if="$slots.title" #title>
|
||||
<slot name="title" />
|
||||
</template>
|
||||
<template v-if="$slots['add-actions']" #add-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>
|
||||
<template v-if="$slots['item-label']" #item-label="slotProps">
|
||||
<slot name="item-label" :item="props.items[slotProps.index]" :index="slotProps.index" />
|
||||
</template>
|
||||
<template v-if="$slots['item-actions']" #item-actions="slotProps">
|
||||
<slot
|
||||
name="item-actions"
|
||||
:item="props.items[slotProps.index]"
|
||||
:index="slotProps.index"
|
||||
:remove="slotProps.remove"
|
||||
/>
|
||||
</template>
|
||||
<template v-if="$slots['viewer-actions']" #viewer-actions="slotProps">
|
||||
<slot
|
||||
name="viewer-actions"
|
||||
:item="props.items[slotProps.index]"
|
||||
:index="slotProps.index"
|
||||
:close="slotProps.close"
|
||||
/>
|
||||
</template>
|
||||
</shared-attachment-selector>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { AttachmentSelector as SharedAttachmentSelector } from "timi-tdesign-mobile";
|
||||
import type {
|
||||
AttachmentSelectorItem,
|
||||
AttachmentSelectorSelection
|
||||
} from "timi-tdesign-mobile";
|
||||
import type { PhotoItem } from "@/types/Photo.ts";
|
||||
import { getAttachmentReadURL } from "@/utils/attachment.ts";
|
||||
import { Message } from "@/utils/message.ts";
|
||||
import { saveCapturedFilesFromInput } from "@/utils/twaBridge";
|
||||
|
||||
defineOptions({
|
||||
inheritAttrs: false
|
||||
});
|
||||
|
||||
interface Props {
|
||||
items: readonly PhotoItem[];
|
||||
busy?: boolean;
|
||||
emptyText?: string;
|
||||
readonly?: boolean;
|
||||
required?: boolean;
|
||||
title?: string;
|
||||
upload?: (files: File[]) => Promise<PhotoItem[]>;
|
||||
viewerZIndex?: number;
|
||||
}
|
||||
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
busy: false,
|
||||
items: () => [],
|
||||
readonly: false,
|
||||
required: false,
|
||||
});
|
||||
|
||||
const emit = defineEmits<{
|
||||
"update:items": [items: PhotoItem[]];
|
||||
select: [selection: AttachmentSelectorSelection];
|
||||
"select-start": [source: AttachmentSelectorSelection["source"]];
|
||||
open: [item: PhotoItem, index: number];
|
||||
}>();
|
||||
|
||||
const isUploading = ref(false);
|
||||
const isBusy = computed(() => props.busy || isUploading.value);
|
||||
const attachmentItems = computed(() => props.items.map(toAttachmentSelectorItem));
|
||||
|
||||
async function handleSelect(selection: AttachmentSelectorSelection): Promise<void> {
|
||||
saveCapturedFilesFromInput(selection.input, selection.files);
|
||||
if (!props.upload) {
|
||||
emit("select", selection);
|
||||
return;
|
||||
}
|
||||
if (isBusy.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
isUploading.value = true;
|
||||
try {
|
||||
const uploadedItems = await props.upload(selection.files);
|
||||
if (uploadedItems.length) {
|
||||
emit("update:items", [...props.items, ...uploadedItems.map((item, index) => ({
|
||||
...item,
|
||||
mimeType: item.mimeType || item.attachment?.mimeType || selection.files[index]?.type
|
||||
}))]);
|
||||
}
|
||||
} catch {
|
||||
Message.error("媒体上传失败");
|
||||
} finally {
|
||||
isUploading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function handleItemsUpdate(items: AttachmentSelectorItem[]): void {
|
||||
emit("update:items", items.map(toPhotoItem));
|
||||
}
|
||||
|
||||
function handleOpen(item: AttachmentSelectorItem, index: number): void {
|
||||
const photoItem = props.items[index];
|
||||
if (photoItem) {
|
||||
emit("open", photoItem, index);
|
||||
window.open(photoItem.url, "_blank", "noopener");
|
||||
return;
|
||||
}
|
||||
const nextItem = toPhotoItem(item);
|
||||
emit("open", nextItem, index);
|
||||
window.open(nextItem.url, "_blank", "noopener");
|
||||
}
|
||||
|
||||
function toAttachmentSelectorItem(item: PhotoItem): AttachmentSelectorItem {
|
||||
const attachmentId = item.attachment?.id;
|
||||
const thumbnailUrl = item.url.includes("thumbWidth=")
|
||||
? item.url
|
||||
: attachmentId ? getAttachmentReadURL(attachmentId, 128) : item.url;
|
||||
|
||||
return {
|
||||
...item,
|
||||
mimeType: item.mimeType || item.attachment?.mimeType,
|
||||
name: item.attachment?.name,
|
||||
thumbnailUrl
|
||||
};
|
||||
}
|
||||
|
||||
function toPhotoItem(item: AttachmentSelectorItem): PhotoItem {
|
||||
const photoItem = { ...item };
|
||||
delete photoItem.name;
|
||||
delete photoItem.thumbnailUrl;
|
||||
return photoItem as PhotoItem;
|
||||
}
|
||||
</script>
|
||||
Reference in New Issue
Block a user