File tree 6 files changed +23
-8
lines changed
6 files changed +23
-8
lines changed Original file line number Diff line number Diff line change @@ -66,11 +66,13 @@ VueImg.getSrc({ ... }) // [Function] 获取图片地址
66
66
| adapt | [ Boolean] 图片尺寸是否适配设备屏幕 | 〇 | 〇 | 〇 |
67
67
| delay | [ Number] 设置延迟加载最大等待时长(ms) | 〇 | ✕ | ✕ |
68
68
| defer | [ Boolean] 图片是否进行延迟加载 | ✕ | 〇 | ✕ |
69
+ | urlFormatter | [ Function] 修改 v-img 生成的 url | ✕ | 〇 | 〇 |
69
70
70
71
- ` suffix ` 参数可用于模糊、旋转等特殊处理,具体请参考[ 《七牛 CDN 开发者文档》] ( http://developer.qiniu.com/code/v6/api/kodo-api/image/imagemogr2.html ) 。
71
72
- ` adapt ` 图片尺寸是否适配设备屏幕大小,指令参数会覆盖全局配置,例如:当全局配置` adapt: true ` 时,指令参数` adpat: false ` ,那么该图片不会根据设备viewport调整尺寸。
72
73
- ` defer ` 延迟加载的含义,当` defer: false ` 时,图片在` v-img ` 指令的` bind ` 钩子函数中加载,当` defer: true ` 时,又分两种情况,图片在首屏和不在首屏中。在首屏中的图片会在` v-img ` 指令的` inserted ` 钩子函数中加载,非首屏的图片将等待` defer: false ` 和首屏中图片都加载完全后才加载。
73
- - ` delay ` 延迟加载最大等待时长,默认值** 5000ms** 。
74
+ - ` delay ` 延迟加载最大等待时长,默认值** 5000ms** 。/
75
+ - ` urlFormatter ` 可以不依赖组件更新让 src 属性适配 CDN 源更新或者添加自定义参数,举例:七牛云的 imageMogr1 => imageMogr2。
74
76
75
77
## 贡献代码
76
78
Original file line number Diff line number Diff line change @@ -61,4 +61,11 @@ describe('检测核心函数 getSrc', () => {
61
61
expect ( VueImg . getSrc ( config ) )
62
62
. to . include ( 'format/png' )
63
63
} )
64
+
65
+ it ( '{ urlFormatter }' , ( ) => {
66
+ const _config = Object . assign ( { } , config )
67
+ _config . urlFormatter = url => url . replace ( / \b ( i m a g e M o g r ) \b / , $1 => `${ $1 } 2` )
68
+ expect ( VueImg . getSrc ( _config ) )
69
+ . to . include ( 'imageMogr2/' )
70
+ } )
64
71
} )
Original file line number Diff line number Diff line change @@ -101,6 +101,7 @@ const getSrc = ({
101
101
width, height, quality,
102
102
format, fallback,
103
103
prefix, suffix,
104
+ urlFormatter,
104
105
} = { } ) => {
105
106
if ( ! hash || typeof hash !== 'string' ) return ''
106
107
@@ -110,8 +111,9 @@ const getSrc = ({
110
111
const _size = getSize ( { width, height, adapt } ) ;
111
112
const _suffix = typeof suffix === 'string' ? suffix : '' ;
112
113
const params = `${ _quality } ${ _format } ${ _size } ${ _suffix } ` ;
113
-
114
- return _prefix + hashToPath ( hash ) + ( params ? `?imageMogr/${ params } ` : '' )
114
+ let src = _prefix + hashToPath ( hash ) + ( params ? `?imageMogr/${ params } ` : '' ) ;
115
+ if ( typeof urlFormatter === 'function' ) src = urlFormatter ( src ) ;
116
+ return src
115
117
} ;
116
118
117
119
var getImageClass = ( opt = { } ) => {
Original file line number Diff line number Diff line change @@ -124,6 +124,7 @@ var getSrc = function (ref) {
124
124
var fallback = ref . fallback ;
125
125
var prefix = ref . prefix ;
126
126
var suffix = ref . suffix ;
127
+ var urlFormatter = ref . urlFormatter ;
127
128
128
129
if ( ! hash || typeof hash !== 'string' ) { return '' }
129
130
@@ -133,8 +134,9 @@ var getSrc = function (ref) {
133
134
var _size = getSize ( { width : width , height : height , adapt : adapt } ) ;
134
135
var _suffix = typeof suffix === 'string' ? suffix : '' ;
135
136
var params = "" + _quality + _format + _size + _suffix ;
136
-
137
- return _prefix + hashToPath ( hash ) + ( params ? ( "?imageMogr/" + params ) : '' )
137
+ var src = _prefix + hashToPath ( hash ) + ( params ? ( "?imageMogr/" + params ) : '' ) ;
138
+ if ( typeof urlFormatter === 'function' ) { src = urlFormatter ( src ) ; }
139
+ return src
138
140
} ;
139
141
140
142
var getImageClass = function ( opt ) {
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " vue-img" ,
3
- "version" : " 2.8.2 " ,
3
+ "version" : " 2.9.0 " ,
4
4
"description" : " hash2path wrapper for vue 2" ,
5
5
"main" : " dist/vue-img.js" ,
6
6
"scripts" : {
Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ const getSrc = ({
37
37
width, height, quality,
38
38
format, fallback,
39
39
prefix, suffix,
40
+ urlFormatter,
40
41
} = { } ) => {
41
42
if ( ! hash || typeof hash !== 'string' ) return ''
42
43
@@ -46,8 +47,9 @@ const getSrc = ({
46
47
const _size = getSize ( { width, height, adapt } )
47
48
const _suffix = typeof suffix === 'string' ? suffix : ''
48
49
const params = `${ _quality } ${ _format } ${ _size } ${ _suffix } `
49
-
50
- return _prefix + hashToPath ( hash ) + ( params ? `?imageMogr/${ params } ` : '' )
50
+ let src = _prefix + hashToPath ( hash ) + ( params ? `?imageMogr/${ params } ` : '' )
51
+ if ( typeof urlFormatter === 'function' ) src = urlFormatter ( src )
52
+ return src
51
53
}
52
54
53
55
export default getSrc
You can’t perform that action at this time.
0 commit comments