| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- const n = `<template>
- <s-modal v-model:value="visible" :title="currentTitle" width="800px" @handCancel="handleCancel">
- <div class="preview-content">
- <div class="prev" @click="step(-1)"><LeftOutlined /></div>
- <div class="view-content">
- <img alt="preview" class="view" :src="currentUrl" />
- </div>
- <div class="next" @click="step(1)"><RightOutlined /></div>
- </div>
- </s-modal>
- </template>
- <script setup>
- import { computed, ref, watch } from 'vue'
- import { LeftOutlined, RightOutlined } from '@ant-design/icons-vue'
- /**
- * 文件预览弹窗(SFileUpload / SDraggerUpload / SCopperUpload 共用)
- * 只负责图片大图预览与左右翻页,非图片类型由调用方决定是否走 window.open。
- */
- const props = defineProps({
- /**
- * 是否显示,配合 v-model:value 使用
- */
- value: {
- type: Boolean,
- default: false
- },
- /**
- * 文件列表,用于左右翻页
- */
- list: {
- type: Array,
- default: () => []
- },
- /**
- * 初始预览项下标,弹窗打开时生效
- */
- active: {
- type: Number,
- default: 0
- },
- /**
- * 自定义标题,为空时取文件名
- */
- title: {
- type: String,
- default: ''
- }
- })
- const emit = defineEmits(['update:value', 'change'])
- const visible = computed({
- get: () => props.value,
- set: (v) => emit('update:value', v)
- })
- // 列表项结构同步 PictureUpload(sort/imageUrl),并兼容基座业务契约(uid/url)
- const fileUrl = (item = {}) => item.imageUrl || item.url || item.uid || ''
- const nameFromUrl = (url = '') => {
- const clean = String(url).split('?')[0].split('#')[0]
- const base = clean.substring(clean.lastIndexOf('/') + 1)
- try {
- return decodeURIComponent(base)
- } catch (e) {
- return base
- }
- }
- const index = ref(0)
- const clamp = (i) => {
- const len = (props.list || []).length
- if (!len) return 0
- return Math.min(Math.max(i, 0), len - 1)
- }
- // 只在打开瞬间同步外部下标,翻页后由组件内部维护
- watch(
- () => props.value,
- (v) => {
- if (v) index.value = clamp(props.active)
- },
- { immediate: true }
- )
- const currentItem = computed(() => (props.list || [])[index.value] || {})
- const currentUrl = computed(() => fileUrl(currentItem.value))
- const currentTitle = computed(
- () => props.title || currentItem.value.name || nameFromUrl(currentUrl.value) || window.$l('预览')
- )
- // 到达首尾后不再移动,避免越界取到空项
- const step = (delta) => {
- const next = clamp(index.value + delta)
- if (next === index.value) return
- index.value = next
- emit('change', next)
- }
- const handleCancel = () => {
- visible.value = false
- }
- <\/script>
- <style lang="less" scoped>
- .preview-content {
- width: 100%;
- display: flex;
- justify-content: center;
- align-items: center;
- margin: 40px 0 10px;
- .view-content {
- min-height: 400px;
- flex: 1;
- display: flex;
- justify-content: center;
- align-items: center;
- }
- .view {
- margin: 0 20px;
- }
- .prev,
- .next {
- font-size: 20px;
- width: 40px;
- height: 40px;
- background-color: var(--s-mask-color);
- color: var(--s-text-color-inverse);
- display: flex;
- justify-content: center;
- align-items: center;
- cursor: pointer;
- border-radius: 8px;
- }
- }
- </style>
- `;
- export {
- n as default
- };
|