@jr-public/jr-upload-ui 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@jr-public/jr-upload-ui",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "JR upload modal components for Vue2 and Vue3",
5
5
  "license": "MIT",
6
6
  "private": false,
7
- "sideEffects": false,
7
+ "sideEffects": ["*.vue", "*.css"],
8
8
  "main": "./packages/vue2/src/index.js",
9
9
  "module": "./packages/vue3/src/index.js",
10
10
  "exports": {
@@ -163,6 +163,14 @@
163
163
  </div>
164
164
  <div class="file-side-panel">
165
165
  <div class="file-actions">
166
+ <button
167
+ v-if="isPreviewableFile(file.name)"
168
+ @click="showPreview(file)"
169
+ class="file-action preview-btn"
170
+ title="预览"
171
+ >
172
+ 预览
173
+ </button>
166
174
  <button
167
175
  v-if="file.status !== 'uploading' && file.status !== 'success'"
168
176
  @click="removeFile(file)"
@@ -307,7 +315,10 @@ export default {
307
315
  uploadPath: { type: String, default: '' },
308
316
 
309
317
  // bucket名称(可选)
310
- bucketName: { type: String, default: '' }
318
+ bucketName: { type: String, default: '' },
319
+
320
+ // 是否将弹框DOM挂载到document.body,避免父容器overflow:hidden影响
321
+ appendToBody: { type: Boolean, default: true }
311
322
  },
312
323
 
313
324
  data() {
@@ -415,6 +426,12 @@ export default {
415
426
  visible(newVal) {
416
427
  if (newVal) {
417
428
  this.initializeComponent()
429
+ // append-to-body: 弹框显示时将根元素移动到document.body
430
+ if (this.appendToBody) {
431
+ this.$nextTick(() => {
432
+ document.body.appendChild(this.$el)
433
+ })
434
+ }
418
435
  // 调试信息:打印当前组件参数
419
436
  console.log('🔧 JrUploadModal 组件参数:', {
420
437
  accept: this.accept,
@@ -458,11 +475,20 @@ export default {
458
475
  await this.initializeComponent()
459
476
  }
460
477
 
478
+ // append-to-body: 组件挂载时将根元素移动到document.body
479
+ if (this.appendToBody) {
480
+ document.body.appendChild(this.$el)
481
+ }
482
+
461
483
  // 动态设置CSS背景图片
462
484
  this.setDynamicBackgroundImages()
463
485
  },
464
486
 
465
487
  beforeDestroy() {
488
+ // append-to-body cleanup: 从document.body移除已挂载的元素
489
+ if (this.appendToBody && this.$el && this.$el.parentNode === document.body) {
490
+ document.body.removeChild(this.$el)
491
+ }
466
492
  this.cleanup()
467
493
  },
468
494
 
@@ -1609,6 +1635,89 @@ export default {
1609
1635
  },
1610
1636
 
1611
1637
  // 工具方法
1638
+
1639
+ // 判断是否为可预览文件(图片和PDF)
1640
+ isPreviewableFile(fileName) {
1641
+ if (!fileName) return false
1642
+ const extension = fileName.split('.').pop().toLowerCase()
1643
+ return /^(jpg|jpeg|png|gif|bmp|webp|svg|pdf)$/i.test(extension)
1644
+ },
1645
+
1646
+ // 判断是否为图片文件
1647
+ isImageFile(fileName) {
1648
+ if (!fileName) return false
1649
+ const extension = fileName.split('.').pop().toLowerCase()
1650
+ return /^(jpg|jpeg|png|gif|bmp|webp|svg)$/i.test(extension)
1651
+ },
1652
+
1653
+ // 判断是否为PDF文件
1654
+ isPdfFile(fileName) {
1655
+ if (!fileName) return false
1656
+ const extension = fileName.split('.').pop().toLowerCase()
1657
+ return /^pdf$/i.test(extension)
1658
+ },
1659
+
1660
+ // 预览文件 - 在新标签页中打开
1661
+ showPreview(fileObj) {
1662
+ try {
1663
+ const fileName = fileObj.file ? fileObj.file.name : fileObj.name
1664
+ const fileSize = fileObj.file ? fileObj.file.size : fileObj.size
1665
+ const fileBlob = fileObj.file || fileObj
1666
+
1667
+ if (!this.isPreviewableFile(fileName)) {
1668
+ console.log('不支持预览此文件类型')
1669
+ return
1670
+ }
1671
+
1672
+ // 创建Blob URL
1673
+ const blobUrl = URL.createObjectURL(fileBlob)
1674
+ const fileType = this.isImageFile(fileName) ? 'image' : 'pdf'
1675
+
1676
+ // 在新标签页中打开预览
1677
+ this.openPreviewInNewTab(blobUrl, fileName, fileType, fileSize)
1678
+ } catch (error) {
1679
+ console.error('预览出错:', error)
1680
+ this.showError('预览失败:' + error.message)
1681
+ }
1682
+ },
1683
+
1684
+ // 在新标签页中打开预览
1685
+ openPreviewInNewTab(fileSrc, fileName, fileType, fileSize) {
1686
+ const fileSizeInfo = fileSize ? ' (' + this.formatFileSize(fileSize) + ')' : ''
1687
+
1688
+ const previewWindow = window.open('', '_blank')
1689
+ if (!previewWindow) {
1690
+ this.showError('无法打开新标签页,请检查浏览器是否阻止了弹出窗口')
1691
+ return
1692
+ }
1693
+
1694
+ let contentHtml = ''
1695
+ if (fileType === 'image') {
1696
+ contentHtml = '<div class="content"><img src="' + fileSrc + '" alt="' + fileName + '" /></div>'
1697
+ } else if (fileType === 'pdf') {
1698
+ contentHtml = '<div class="content"><iframe src="' + fileSrc + '" title="' + fileName + '"></iframe></div>'
1699
+ }
1700
+
1701
+ const previewHtml = '<!DOCTYPE html><html><head><meta charset="UTF-8">' +
1702
+ '<meta name="viewport" content="width=device-width, initial-scale=1.0">' +
1703
+ '<title>' + fileName + fileSizeInfo + ' - 预览</title>' +
1704
+ '<style>' +
1705
+ 'body{margin:0;padding:0;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","PingFang SC",sans-serif;background-color:#f5f5f5;display:flex;flex-direction:column;height:100vh;}' +
1706
+ '.header{background-color:#fff;border-bottom:1px solid #e0e0e0;padding:15px 20px;box-shadow:0 2px 4px rgba(0,0,0,0.1);}' +
1707
+ '.header h1{margin:0;font-size:18px;color:#333;font-weight:500;}' +
1708
+ '.file-size{color:#999;font-size:14px;margin-left:10px;}' +
1709
+ '.content{flex:1;display:flex;align-items:center;justify-content:center;padding:20px;overflow:auto;}' +
1710
+ '.content img{max-width:100%;max-height:100%;object-fit:contain;box-shadow:0 4px 8px rgba(0,0,0,0.1);}' +
1711
+ '.content iframe{width:100%;height:100%;border:none;background-color:#fff;box-shadow:0 4px 8px rgba(0,0,0,0.1);}' +
1712
+ '</style></head><body>' +
1713
+ '<div class="header"><h1>' + fileName + '<span class="file-size">' + fileSizeInfo + '</span></h1></div>' +
1714
+ contentHtml +
1715
+ '</body></html>'
1716
+
1717
+ previewWindow.document.write(previewHtml)
1718
+ previewWindow.document.close()
1719
+ },
1720
+
1612
1721
  formatFileSize(bytes) {
1613
1722
  if (bytes === 0) return '0 B'
1614
1723
  const k = 1024
@@ -2975,6 +3084,17 @@ export default {
2975
3084
  color: #6b7280;
2976
3085
  }
2977
3086
 
3087
+ .preview-btn {
3088
+ color: #3b82f6;
3089
+ border-color: rgba(59, 130, 246, 0.3);
3090
+ }
3091
+
3092
+ .preview-btn:hover {
3093
+ background: rgba(59, 130, 246, 0.1);
3094
+ border-color: rgba(59, 130, 246, 0.5);
3095
+ color: #2563eb;
3096
+ }
3097
+
2978
3098
  .remove-btn {
2979
3099
  color: #ef4444;
2980
3100
  border-color: rgba(239, 68, 68, 0.3);
@@ -163,6 +163,14 @@
163
163
  </div>
164
164
  <div class="file-side-panel">
165
165
  <div class="file-actions">
166
+ <button
167
+ v-if="isPreviewableFile(file.name)"
168
+ @click="showPreview(file)"
169
+ class="file-action preview-btn"
170
+ title="预览"
171
+ >
172
+ 预览
173
+ </button>
166
174
  <button
167
175
  v-if="file.status !== 'uploading' && file.status !== 'success'"
168
176
  @click="removeFile(file)"
@@ -249,7 +257,7 @@
249
257
  </template>
250
258
 
251
259
  <script>
252
- import { ref, reactive, computed, watch, onMounted, onBeforeUnmount, defineEmits, defineProps, nextTick } from 'vue'
260
+ import { ref, reactive, computed, watch, onMounted, onBeforeUnmount, defineEmits, defineProps, nextTick, getCurrentInstance } from 'vue'
253
261
 
254
262
  export default {
255
263
  name: 'JrUploadModalVue3',
@@ -309,7 +317,10 @@ export default {
309
317
  uploadPath: { type: String, default: '' },
310
318
 
311
319
  // bucket名称(可选)
312
- bucketName: { type: String, default: '' }
320
+ bucketName: { type: String, default: '' },
321
+
322
+ // 是否将弹框DOM挂载到document.body,避免父容器overflow:hidden影响
323
+ appendToBody: { type: Boolean, default: true }
313
324
  },
314
325
 
315
326
  emits: [
@@ -325,6 +336,9 @@ export default {
325
336
  ],
326
337
 
327
338
  setup(props, { emit }) {
339
+ // 获取组件实例(用于 append-to-body)
340
+ const instance = getCurrentInstance()
341
+
328
342
  // 响应式数据
329
343
  const filesToUpload = ref([])
330
344
  const isUploading = ref(false)
@@ -433,6 +447,11 @@ export default {
433
447
  watch(() => props.visible, async (newVal) => {
434
448
  if (newVal) {
435
449
  await initializeComponent()
450
+ // append-to-body: 弹框显示时将根元素移动到document.body
451
+ if (props.appendToBody && instance && instance.vnode.el) {
452
+ await nextTick()
453
+ document.body.appendChild(instance.vnode.el)
454
+ }
436
455
  // 调试信息:打印当前组件参数
437
456
  console.log('🔧 JrUploadModal 组件参数:', {
438
457
  accept: props.accept,
@@ -479,11 +498,20 @@ export default {
479
498
  await initializeComponent()
480
499
  }
481
500
 
501
+ // append-to-body: 组件挂载时将根元素移动到document.body
502
+ if (props.appendToBody && instance && instance.vnode.el) {
503
+ document.body.appendChild(instance.vnode.el)
504
+ }
505
+
482
506
  // 动态设置CSS背景图片
483
507
  setDynamicBackgroundImages()
484
508
  })
485
509
 
486
510
  onBeforeUnmount(() => {
511
+ // append-to-body cleanup: 从document.body移除已挂载的元素
512
+ if (props.appendToBody && instance && instance.vnode.el && instance.vnode.el.parentNode === document.body) {
513
+ document.body.removeChild(instance.vnode.el)
514
+ }
487
515
  cleanup()
488
516
  })
489
517
 
@@ -1632,6 +1660,89 @@ export default {
1632
1660
  }
1633
1661
 
1634
1662
  // 工具方法
1663
+
1664
+ // 判断是否为可预览文件(图片和PDF)
1665
+ const isPreviewableFile = (fileName) => {
1666
+ if (!fileName) return false
1667
+ const extension = fileName.split('.').pop().toLowerCase()
1668
+ return /^(jpg|jpeg|png|gif|bmp|webp|svg|pdf)$/i.test(extension)
1669
+ }
1670
+
1671
+ // 判断是否为图片文件
1672
+ const isImageFile = (fileName) => {
1673
+ if (!fileName) return false
1674
+ const extension = fileName.split('.').pop().toLowerCase()
1675
+ return /^(jpg|jpeg|png|gif|bmp|webp|svg)$/i.test(extension)
1676
+ }
1677
+
1678
+ // 判断是否为PDF文件
1679
+ const isPdfFile = (fileName) => {
1680
+ if (!fileName) return false
1681
+ const extension = fileName.split('.').pop().toLowerCase()
1682
+ return /^pdf$/i.test(extension)
1683
+ }
1684
+
1685
+ // 预览文件 - 在新标签页中打开
1686
+ const showPreview = (fileObj) => {
1687
+ try {
1688
+ const fileName = fileObj.file ? fileObj.file.name : fileObj.name
1689
+ const fileSize = fileObj.file ? fileObj.file.size : fileObj.size
1690
+ const fileBlob = fileObj.file || fileObj
1691
+
1692
+ if (!isPreviewableFile(fileName)) {
1693
+ console.log('不支持预览此文件类型')
1694
+ return
1695
+ }
1696
+
1697
+ // 创建Blob URL
1698
+ const blobUrl = URL.createObjectURL(fileBlob)
1699
+ const fileType = isImageFile(fileName) ? 'image' : 'pdf'
1700
+
1701
+ // 在新标签页中打开预览
1702
+ openPreviewInNewTab(blobUrl, fileName, fileType, fileSize)
1703
+ } catch (error) {
1704
+ console.error('预览出错:', error)
1705
+ showError('预览失败:' + error.message)
1706
+ }
1707
+ }
1708
+
1709
+ // 在新标签页中打开预览
1710
+ const openPreviewInNewTab = (fileSrc, fileName, fileType, fileSize) => {
1711
+ const fileSizeInfo = fileSize ? ' (' + formatFileSize(fileSize) + ')' : ''
1712
+
1713
+ const previewWindow = window.open('', '_blank')
1714
+ if (!previewWindow) {
1715
+ showError('无法打开新标签页,请检查浏览器是否阻止了弹出窗口')
1716
+ return
1717
+ }
1718
+
1719
+ let contentHtml = ''
1720
+ if (fileType === 'image') {
1721
+ contentHtml = '<div class="content"><img src="' + fileSrc + '" alt="' + fileName + '" /></div>'
1722
+ } else if (fileType === 'pdf') {
1723
+ contentHtml = '<div class="content"><iframe src="' + fileSrc + '" title="' + fileName + '"></iframe></div>'
1724
+ }
1725
+
1726
+ const previewHtml = '<!DOCTYPE html><html><head><meta charset="UTF-8">' +
1727
+ '<meta name="viewport" content="width=device-width, initial-scale=1.0">' +
1728
+ '<title>' + fileName + fileSizeInfo + ' - 预览</title>' +
1729
+ '<style>' +
1730
+ 'body{margin:0;padding:0;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","PingFang SC",sans-serif;background-color:#f5f5f5;display:flex;flex-direction:column;height:100vh;}' +
1731
+ '.header{background-color:#fff;border-bottom:1px solid #e0e0e0;padding:15px 20px;box-shadow:0 2px 4px rgba(0,0,0,0.1);}' +
1732
+ '.header h1{margin:0;font-size:18px;color:#333;font-weight:500;}' +
1733
+ '.file-size{color:#999;font-size:14px;margin-left:10px;}' +
1734
+ '.content{flex:1;display:flex;align-items:center;justify-content:center;padding:20px;overflow:auto;}' +
1735
+ '.content img{max-width:100%;max-height:100%;object-fit:contain;box-shadow:0 4px 8px rgba(0,0,0,0.1);}' +
1736
+ '.content iframe{width:100%;height:100%;border:none;background-color:#fff;box-shadow:0 4px 8px rgba(0,0,0,0.1);}' +
1737
+ '</style></head><body>' +
1738
+ '<div class="header"><h1>' + fileName + '<span class="file-size">' + fileSizeInfo + '</span></h1></div>' +
1739
+ contentHtml +
1740
+ '</body></html>'
1741
+
1742
+ previewWindow.document.write(previewHtml)
1743
+ previewWindow.document.close()
1744
+ }
1745
+
1635
1746
  const formatFileSize = (bytes) => {
1636
1747
  if (bytes === 0) return '0 B'
1637
1748
  const k = 1024
@@ -2307,6 +2418,11 @@ export default {
2307
2418
 
2308
2419
  // 工具方法
2309
2420
  formatFileSize,
2421
+ isPreviewableFile,
2422
+ isImageFile,
2423
+ isPdfFile,
2424
+ showPreview,
2425
+ openPreviewInNewTab,
2310
2426
  generateFileId,
2311
2427
  generateUUID32,
2312
2428
  truncateFileName,
@@ -3081,6 +3197,17 @@ export default {
3081
3197
  color: #6b7280;
3082
3198
  }
3083
3199
 
3200
+ .preview-btn {
3201
+ color: #3b82f6;
3202
+ border-color: rgba(59, 130, 246, 0.3);
3203
+ }
3204
+
3205
+ .preview-btn:hover {
3206
+ background: rgba(59, 130, 246, 0.1);
3207
+ border-color: rgba(59, 130, 246, 0.5);
3208
+ color: #2563eb;
3209
+ }
3210
+
3084
3211
  .remove-btn {
3085
3212
  color: #ef4444;
3086
3213
  border-color: rgba(239, 68, 68, 0.3);