Skip to content

Commit b3b588a

Browse files
authored
Merge pull request #6 from jiananshi/url-formatter
feat($url): add url formatter method
2 parents 950d24a + eeb76dd commit b3b588a

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ VueImg.getSrc({ ... }) // [Function] 获取图片地址
6666
| adapt | [Boolean] 图片尺寸是否适配设备屏幕 ||||
6767
| delay | [Number] 设置延迟加载最大等待时长(ms) ||||
6868
| defer | [Boolean] 图片是否进行延迟加载 ||||
69+
| urlFormatter | [Function] 修改 v-img 生成的 url ||||
6970

7071
- `suffix` 参数可用于模糊、旋转等特殊处理,具体请参考[《七牛 CDN 开发者文档》](http://developer.qiniu.com/code/v6/api/kodo-api/image/imagemogr2.html)
7172
- `adapt`图片尺寸是否适配设备屏幕大小,指令参数会覆盖全局配置,例如:当全局配置`adapt: true`时,指令参数`adpat: false`,那么该图片不会根据设备viewport调整尺寸。
7273
- `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。
7476

7577
## 贡献代码
7678

build/test/core.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,11 @@ describe('检测核心函数 getSrc', () => {
6161
expect(VueImg.getSrc(config))
6262
.to.include('format/png')
6363
})
64+
65+
it('{ urlFormatter }', () => {
66+
const _config = Object.assign({}, config)
67+
_config.urlFormatter = url => url.replace(/\b(imageMogr)\b/, $1 => `${$1}2`)
68+
expect(VueImg.getSrc(_config))
69+
.to.include('imageMogr2/')
70+
})
6471
})

dist/vue-img.es6.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ const getSrc = ({
101101
width, height, quality,
102102
format, fallback,
103103
prefix, suffix,
104+
urlFormatter,
104105
} = {}) => {
105106
if (!hash || typeof hash !== 'string') return ''
106107

@@ -110,8 +111,9 @@ const getSrc = ({
110111
const _size = getSize({ width, height, adapt });
111112
const _suffix = typeof suffix === 'string' ? suffix : '';
112113
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
115117
};
116118

117119
var getImageClass = (opt = {}) => {

dist/vue-img.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ var getSrc = function (ref) {
124124
var fallback = ref.fallback;
125125
var prefix = ref.prefix;
126126
var suffix = ref.suffix;
127+
var urlFormatter = ref.urlFormatter;
127128

128129
if (!hash || typeof hash !== 'string') { return '' }
129130

@@ -133,8 +134,9 @@ var getSrc = function (ref) {
133134
var _size = getSize({ width: width, height: height, adapt: adapt });
134135
var _suffix = typeof suffix === 'string' ? suffix : '';
135136
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
138140
};
139141

140142
var getImageClass = function (opt) {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-img",
3-
"version": "2.8.2",
3+
"version": "2.9.0",
44
"description": "hash2path wrapper for vue 2",
55
"main": "dist/vue-img.js",
66
"scripts": {

src/src.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const getSrc = ({
3737
width, height, quality,
3838
format, fallback,
3939
prefix, suffix,
40+
urlFormatter,
4041
} = {}) => {
4142
if (!hash || typeof hash !== 'string') return ''
4243

@@ -46,8 +47,9 @@ const getSrc = ({
4647
const _size = getSize({ width, height, adapt })
4748
const _suffix = typeof suffix === 'string' ? suffix : ''
4849
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
5153
}
5254

5355
export default getSrc

0 commit comments

Comments
 (0)