|
1 | 1 | <template> |
2 | | - <image |
3 | | - :mode="node.attr.mode" |
4 | | - :lazy-load="node.attr.lazyLoad" |
5 | | - :class="node.classStr" |
6 | | - :style="newStyleStr || node.styleStr" |
7 | | - :data-src="node.attr.src" |
8 | | - :src="node.attr.src" |
9 | | - @tap="wxParseImgTap" |
10 | | - @load="wxParseImgLoad" |
11 | | - /> |
| 2 | + <image :mode="node.attr.mode" :lazy-load="node.attr.lazyLoad" :class="node.classStr" :style="newStyleStr || node.styleStr" |
| 3 | + :data-src="node.attr.src" :src="node.attr.src" @tap="wxParseImgTap" @load="wxParseImgLoad" /> |
12 | 4 | </template> |
13 | 5 |
|
14 | 6 | <script> |
15 | | -export default { |
16 | | - name: 'wxParseImg', |
17 | | - data() { |
18 | | - return { |
19 | | - newStyleStr: '', |
20 | | - preview: true, |
21 | | - }; |
22 | | - }, |
23 | | - props: { |
24 | | - node: { |
25 | | - type: Object, |
26 | | - default() { |
27 | | - return {}; |
28 | | - }, |
29 | | - }, |
30 | | - }, |
31 | | - methods: { |
32 | | - wxParseImgTap(e) { |
33 | | - if (!this.preview) return; |
34 | | - const { src } = e.currentTarget.dataset; |
35 | | - if (!src) return; |
36 | | - let parent = this.$parent; |
37 | | - while(!parent.preview || typeof parent.preview !== 'function') {// TODO 遍历获取父节点执行方法 |
38 | | - parent = parent.$parent; |
39 | | - } |
40 | | - parent.preview(src, e); |
41 | | - }, |
42 | | - // 图片视觉宽高计算函数区 |
43 | | - wxParseImgLoad(e) { |
44 | | - const { src } = e.currentTarget.dataset; |
45 | | - if (!src) return; |
46 | | - const { width, height } = e.mp.detail; |
47 | | - const recal = this.wxAutoImageCal(width, height); |
48 | | - const { imageheight, imageWidth } = recal; |
49 | | - const { padding, mode } = this.node.attr; |
50 | | - const { styleStr } = this.node; |
51 | | - const imageHeightStyle = mode === 'widthFix' ? '' : `height: ${imageheight}px;`; |
52 | | - this.newStyleStr = `${styleStr}; ${imageHeightStyle}; width: ${imageWidth}px; padding: 0 ${+padding}px;`; |
53 | | - }, |
54 | | - // 计算视觉优先的图片宽高 |
55 | | - wxAutoImageCal(originalWidth, originalHeight) { |
56 | | - // 获取图片的原始长宽 |
57 | | - const { padding } = this.node.attr; |
58 | | - const windowWidth = this.node.$screen.width - (2 * padding); |
59 | | - const results = {}; |
| 7 | + export default { |
| 8 | + name: 'wxParseImg', |
| 9 | + data() { |
| 10 | + return { |
| 11 | + newStyleStr: '', |
| 12 | + preview: true, |
| 13 | + }; |
| 14 | + }, |
| 15 | + props: { |
| 16 | + node: { |
| 17 | + type: Object, |
| 18 | + default () { |
| 19 | + return {}; |
| 20 | + }, |
| 21 | + }, |
| 22 | + }, |
| 23 | + inject: ['uparse'], |
| 24 | + methods: { |
| 25 | + wxParseImgTap(e) { |
| 26 | + if (!this.preview) return; |
| 27 | + const { |
| 28 | + src |
| 29 | + } = e.currentTarget.dataset; |
| 30 | + if (!src) return; |
| 31 | + this.uparse.preview(src, e); |
| 32 | + }, |
| 33 | + // 图片视觉宽高计算函数区 |
| 34 | + wxParseImgLoad(e) { |
| 35 | + const { |
| 36 | + src |
| 37 | + } = e.currentTarget.dataset; |
| 38 | + if (!src) return; |
| 39 | + const { |
| 40 | + width, |
| 41 | + height |
| 42 | + } = e.mp.detail; |
| 43 | + const recal = this.wxAutoImageCal(width, height); |
| 44 | + const { |
| 45 | + imageheight, |
| 46 | + imageWidth |
| 47 | + } = recal; |
| 48 | + const { |
| 49 | + padding, |
| 50 | + mode |
| 51 | + } = this.node.attr; |
| 52 | + const { |
| 53 | + styleStr |
| 54 | + } = this.node; |
| 55 | + const imageHeightStyle = mode === 'widthFix' ? '' : `height: ${imageheight}px;`; |
| 56 | + this.newStyleStr = `${styleStr}; ${imageHeightStyle}; width: ${imageWidth}px; padding: 0 ${+padding}px;`; |
| 57 | + }, |
| 58 | + // 计算视觉优先的图片宽高 |
| 59 | + wxAutoImageCal(originalWidth, originalHeight) { |
| 60 | + // 获取图片的原始长宽 |
| 61 | + const { |
| 62 | + padding |
| 63 | + } = this.node.attr; |
| 64 | + const windowWidth = this.node.$screen.width - (2 * padding); |
| 65 | + const results = {}; |
60 | 66 |
|
61 | | - if (originalWidth < 60 || originalHeight < 60) { |
62 | | - const { src } = this.node.attr; |
63 | | - let parent = this.$parent; |
64 | | - while(!parent.preview || typeof parent.preview !== 'function') { |
65 | | - parent = parent.$parent; |
| 67 | + if (originalWidth < 60 || originalHeight < 60) { |
| 68 | + const { |
| 69 | + src |
| 70 | + } = this.node.attr; |
| 71 | + this.uparse.removeImageUrl(src); |
| 72 | + this.preview = false; |
66 | 73 | } |
67 | | - parent.removeImageUrl(src); |
68 | | - this.preview = false; |
69 | | - } |
70 | 74 |
|
71 | | - // 判断按照那种方式进行缩放 |
72 | | - if (originalWidth > windowWidth) { |
73 | | - // 在图片width大于手机屏幕width时候 |
74 | | - results.imageWidth = windowWidth; |
75 | | - results.imageheight = windowWidth * (originalHeight / originalWidth); |
76 | | - } else { |
77 | | - // 否则展示原来的数据 |
78 | | - results.imageWidth = originalWidth; |
79 | | - results.imageheight = originalHeight; |
80 | | - } |
| 75 | + // 判断按照那种方式进行缩放 |
| 76 | + if (originalWidth > windowWidth) { |
| 77 | + // 在图片width大于手机屏幕width时候 |
| 78 | + results.imageWidth = windowWidth; |
| 79 | + results.imageheight = windowWidth * (originalHeight / originalWidth); |
| 80 | + } else { |
| 81 | + // 否则展示原来的数据 |
| 82 | + results.imageWidth = originalWidth; |
| 83 | + results.imageheight = originalHeight; |
| 84 | + } |
81 | 85 |
|
82 | | - return results; |
83 | | - }, |
84 | | - }, |
85 | | -}; |
| 86 | + return results; |
| 87 | + } |
| 88 | + } |
| 89 | + }; |
86 | 90 | </script> |
0 commit comments