SFilePreview2.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const n = `<template>
  2. <s-modal v-model:value="visible" :title="currentTitle" width="800px" @handCancel="handleCancel">
  3. <div class="preview-content">
  4. <div class="prev" @click="step(-1)"><LeftOutlined /></div>
  5. <div class="view-content">
  6. <img alt="preview" class="view" :src="currentUrl" />
  7. </div>
  8. <div class="next" @click="step(1)"><RightOutlined /></div>
  9. </div>
  10. </s-modal>
  11. </template>
  12. <script setup>
  13. import { computed, ref, watch } from 'vue'
  14. import { LeftOutlined, RightOutlined } from '@ant-design/icons-vue'
  15. /**
  16. * 文件预览弹窗(SFileUpload / SDraggerUpload / SCopperUpload 共用)
  17. * 只负责图片大图预览与左右翻页,非图片类型由调用方决定是否走 window.open。
  18. */
  19. const props = defineProps({
  20. /**
  21. * 是否显示,配合 v-model:value 使用
  22. */
  23. value: {
  24. type: Boolean,
  25. default: false
  26. },
  27. /**
  28. * 文件列表,用于左右翻页
  29. */
  30. list: {
  31. type: Array,
  32. default: () => []
  33. },
  34. /**
  35. * 初始预览项下标,弹窗打开时生效
  36. */
  37. active: {
  38. type: Number,
  39. default: 0
  40. },
  41. /**
  42. * 自定义标题,为空时取文件名
  43. */
  44. title: {
  45. type: String,
  46. default: ''
  47. }
  48. })
  49. const emit = defineEmits(['update:value', 'change'])
  50. const visible = computed({
  51. get: () => props.value,
  52. set: (v) => emit('update:value', v)
  53. })
  54. // 列表项结构同步 PictureUpload(sort/imageUrl),并兼容基座业务契约(uid/url)
  55. const fileUrl = (item = {}) => item.imageUrl || item.url || item.uid || ''
  56. const nameFromUrl = (url = '') => {
  57. const clean = String(url).split('?')[0].split('#')[0]
  58. const base = clean.substring(clean.lastIndexOf('/') + 1)
  59. try {
  60. return decodeURIComponent(base)
  61. } catch (e) {
  62. return base
  63. }
  64. }
  65. const index = ref(0)
  66. const clamp = (i) => {
  67. const len = (props.list || []).length
  68. if (!len) return 0
  69. return Math.min(Math.max(i, 0), len - 1)
  70. }
  71. // 只在打开瞬间同步外部下标,翻页后由组件内部维护
  72. watch(
  73. () => props.value,
  74. (v) => {
  75. if (v) index.value = clamp(props.active)
  76. },
  77. { immediate: true }
  78. )
  79. const currentItem = computed(() => (props.list || [])[index.value] || {})
  80. const currentUrl = computed(() => fileUrl(currentItem.value))
  81. const currentTitle = computed(
  82. () => props.title || currentItem.value.name || nameFromUrl(currentUrl.value) || window.$l('预览')
  83. )
  84. // 到达首尾后不再移动,避免越界取到空项
  85. const step = (delta) => {
  86. const next = clamp(index.value + delta)
  87. if (next === index.value) return
  88. index.value = next
  89. emit('change', next)
  90. }
  91. const handleCancel = () => {
  92. visible.value = false
  93. }
  94. <\/script>
  95. <style lang="less" scoped>
  96. .preview-content {
  97. width: 100%;
  98. display: flex;
  99. justify-content: center;
  100. align-items: center;
  101. margin: 40px 0 10px;
  102. .view-content {
  103. min-height: 400px;
  104. flex: 1;
  105. display: flex;
  106. justify-content: center;
  107. align-items: center;
  108. }
  109. .view {
  110. margin: 0 20px;
  111. }
  112. .prev,
  113. .next {
  114. font-size: 20px;
  115. width: 40px;
  116. height: 40px;
  117. background-color: var(--s-mask-color);
  118. color: var(--s-text-color-inverse);
  119. display: flex;
  120. justify-content: center;
  121. align-items: center;
  122. cursor: pointer;
  123. border-radius: 8px;
  124. }
  125. }
  126. </style>
  127. `;
  128. export {
  129. n as default
  130. };