web-component-gallery 2.0.18 → 2.0.20

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.
@@ -1,5 +1,5 @@
1
1
  import Modal from 'ant-design-vue/es/modal'
2
- import {Browse} from '../lib/index'
2
+ import { Browse } from '../lib'
3
3
 
4
4
  const defaultComponents = {
5
5
  Browse
@@ -117,7 +117,7 @@ export default Vue => {
117
117
  )
118
118
  })
119
119
 
120
- const componentContent = component instanceof String ? defaultComponents[ component ] : component
120
+ const componentContent = typeof component === 'string' ? defaultComponents[ component ] : component
121
121
 
122
122
  return h(Modal, ModalProps, [h(componentContent, ComponentProps)])
123
123
  }
@@ -2,67 +2,102 @@ import PropTypes from 'ant-design-vue/es/_util/vue-types'
2
2
 
3
3
  import { Base64 } from '../../utils/Base64'
4
4
 
5
- import { getPictureAttrs } from '../../utils/Utils'
6
-
7
- const pictureType = [ "gif", "jpeg", "png", "jpg", "bmp", "tif", "svg", "psd", "raw", "WMF", "webp", "apng" ]
8
- const audioType = [ "mp3", "wma", "flac", "aac", "mmf", "amr", "m4a", "m4r", "ogg", "mp2", "wav" ]
9
- const videoType = [ "avi", "flv", "mpg", "mpeg", "mpe", "m1v", "m2v", "mpv2", "mp2v", "dat", "ts", "tp", "tpr", "pva", "pss", "mp4", "m4v",
10
- "m4p", "m4b", "3gp", "3gpp", "3g2", "3gp2", "ogg", "mov", "qt", "amr", "rm", "ram", "rmvb", "rpm" ]
11
-
12
5
  const BrowseProps = {
13
6
  data: PropTypes.oneOfType([PropTypes.string, PropTypes.array, PropTypes.object]).def([]),
14
7
  /** 支持http地址可配置 */
15
8
  httpsUrl: PropTypes.object.def({}),
16
9
  /** 限制高度 */
17
- astrictH: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
10
+ astrictH: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
11
+ }
12
+
13
+ // 文件类型常量
14
+ const FILE_TYPES = {
15
+ PICTURE: ["gif", "jpeg", "png", "jpg", "bmp", "tif", "svg", "psd", "raw", "WMF", "webp", "apng"],
16
+ AUDIO: ["mp3", "wma", "flac", "aac", "mmf", "amr", "m4a", "m4r", "ogg", "mp2", "wav"],
17
+ VIDEO: ["avi", "flv", "mpg", "mpeg", "mpe", "m1v", "m2v", "mpv2", "mp2v", "dat", "ts", "tp", "tpr", "pva", "pss", "mp4", "m4v", "m4p", "m4b", "3gp", "3gpp", "3g2", "3gp2", "ogg", "mov", "qt", "amr", "rm", "ram", "rmvb", "rpm"]
18
18
  }
19
19
 
20
- const renderContent = (h, item, https, astrictH) => {
20
+ // 初始化数据
21
+ const setInnerData = (data, https) => {
22
+
23
+ let innerData = data
21
24
 
22
- const type = item.url.slice(item.url.lastIndexOf('.') + 1).toLowerCase()
25
+ if (typeof(data) === 'string') {
26
+ try {
27
+ innerData = JSON.parse(data)
28
+ } catch(error) { innerData = [] }
29
+ }
23
30
 
24
- const CustomTag =
25
- pictureType.includes(type) && 'img' ||
26
- audioType.includes(type) && 'audio' ||
27
- videoType.includes(type) && 'video' || 'iframe'
31
+ const { FILEURL, KKFILEURL, OVERVIEWFILEURL } = https
32
+
33
+ innerData = Array.isArray(innerData) ? innerData : [innerData]
34
+
35
+ innerData.forEach(dataItem => {
36
+ if (!dataItem.url.startsWith('http')) {
37
+ dataItem.url =
38
+ getCustomTag(dataItem.url) === 'iframe'
39
+ ? KKFILEURL + encodeURIComponent( Base64.encode( OVERVIEWFILEURL + dataItem.url ) )
40
+ : FILEURL + dataItem.url
41
+ }
42
+ })
43
+
44
+ return innerData
45
+ }
46
+
47
+ // 不同类型不同容器承载
48
+ const getCustomTag = (url) => {
49
+ const ext = url.slice(url.lastIndexOf('.') + 1).toLowerCase()
50
+ if (FILE_TYPES.PICTURE.includes(ext)) return 'img'
51
+ if (FILE_TYPES.AUDIO.includes(ext)) return 'audio'
52
+ if (FILE_TYPES.VIDEO.includes(ext)) return 'video'
53
+ return 'iframe'
54
+ }
55
+
56
+ function renderContent(h, url, images) {
57
+ const { astrictH, $viewerApi } = this
58
+
59
+ const CustomTag = getCustomTag(url)
28
60
 
29
61
  const attrs = {
30
- controls: true,
31
- src: https.FILEURL + item.url
32
- }
62
+ src: url,
63
+ ...(astrictH && { style: `width: auto; height: ${astrictH}px; objectFit: contain;` })
64
+ }
65
+
66
+ if (CustomTag === 'iframe') attrs['controls'] = true
33
67
 
34
- CustomTag === 'iframe'
35
- && ( attrs.src = https.KKFILEURL + encodeURIComponent( Base64.encode( https.OVERVIEWFILEURL + item.url ) ) )
68
+ const handleClick = () => {
69
+ if (CustomTag !== 'img') return
36
70
 
37
- if(astrictH) {
38
- const style = getPictureAttrs(attrs.src, astrictH)
39
- attrs.style = `width: ${style.width}px; height: ${style.height}px;`
71
+ try {
72
+ $viewerApi({
73
+ options: {
74
+ url: 'url',
75
+ toolbar: true,
76
+ initialViewIndex: images.findIndex(file => file.url == url)
77
+ },
78
+ images
79
+ })
80
+ } catch(error){ console.error('未安装v-viewer@1.6.4组件库') }
40
81
  }
41
-
42
- return (
43
- <CustomTag {...{ attrs }} />
44
- )
82
+
83
+ return <CustomTag {...{ attrs }} onClick={handleClick} />
45
84
  }
46
85
 
47
86
  const Browse = {
48
87
  name: 'Browse',
49
88
  props: BrowseProps,
50
89
  render(h, content) {
51
- let { data, $https, httpsUrl, astrictH } = this
52
- const https = { ...$https, ...httpsUrl }
53
- if(typeof(data) === 'string') {
54
- try {
55
- data = JSON.parse(data)
56
- } catch(error) { data = [] }
57
- }
90
+ const {data, $https, httpsUrl} = this
91
+ const innerData = setInnerData(data, {...$https, ...httpsUrl})
92
+
58
93
  return (
59
94
  <div class="Browse">
60
95
  {
61
- data instanceof Array ?
62
- data.map( dataItem => renderContent(h, dataItem, https, astrictH) ) :
63
- renderContent(h, data, https, astrictH)
96
+ innerData.map( dataItem =>
97
+ renderContent.call(this, h, dataItem.url, innerData)
98
+ )
64
99
  }
65
- </div>
100
+ </div>
66
101
  )
67
102
  }
68
103
  }
@@ -121,8 +121,8 @@ export default {
121
121
  }
122
122
 
123
123
  this.$dialog(
124
- 'Brower',
125
- { data: url },
124
+ 'Browse',
125
+ { data: { url } },
126
126
  {
127
127
  title: '预览文件',
128
128
  width: 1104,
@@ -1,4 +1,4 @@
1
- import NoData from './index.vue'
1
+ import NoData from '../index'
2
2
 
3
3
  // <!-- 基础用法 -->
4
4
  // <div v-no-data="list"></div>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-component-gallery",
3
- "version": "2.0.18",
3
+ "version": "2.0.20",
4
4
  "description": "基础vue、antdvue、less实现的私有组件库",
5
5
  "main": "dist/index.umd.js",
6
6
  "files": [
package/utils/Utils.js CHANGED
@@ -29,23 +29,33 @@ export function downLoadFn( response, name, type = "application/vnd.ms-excel" )
29
29
  }
30
30
 
31
31
  /**
32
- * 获取图片宽高 (如超出限制高度,则根据高度比计算出对应的宽度比)
33
- * url 图片地址
34
- * astrictH 限制高度
32
+ * 获取图片宽高 (如超出限制高度,则根据高度比计算出对应的宽度比)
33
+ * @param {string} url - 图片地址
34
+ * @param {number} astrictH - 限制高度
35
+ * @returns {Object} 包含width和height属性的对象
35
36
  */
36
- export function getPictureAttrs( url, astrictH ) {
37
- let image = new Image()
38
- image.src = url
39
- function getPictureSize(image) {
40
- /** 解决图片过大还处于加载中出现的第一次获取宽高为0的情况 */
41
- if(image.width == 0) return setTimeout(() =>getPictureSize(image), 600)
42
- return { width: image.width, height: image.height }
43
- }
44
- let style = getPictureSize(image)
45
- if(style.height > astrictH) {
46
- const scale = astrictH / style.height
47
- style.width = style.width * scale
48
- style.height = astrictH
49
- }
50
- return style
37
+ export function getPictureAttrs(url, astrictH) {
38
+ return new Promise((resolve) => {
39
+ const image = new Image()
40
+ image.src = url
41
+
42
+ // 确保图片加载完成
43
+ image.onload = () => {
44
+ const { width, height } = image
45
+
46
+ if (height > astrictH) {
47
+ const scale = astrictH / height
48
+ resolve({
49
+ width: width * scale,
50
+ height: astrictH
51
+ })
52
+ return
53
+ }
54
+
55
+ resolve({ width, height })
56
+ }
57
+
58
+ // 错误处理
59
+ image.onerror = () => resolve({ width: 0, height: 0 })
60
+ })
51
61
  }