Skip to content

Commit 4dc8220

Browse files
authored
Merge pull request icindy#106 from Yan1920/master
optimze many image size update
2 parents 8c9b772 + c2d697a commit 4dc8220

File tree

2 files changed

+52
-26
lines changed

2 files changed

+52
-26
lines changed

wxParse/html2json.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ function html2json(html, bindName) {
6666
images:[],
6767
imageUrls:[]
6868
};
69+
var index = 0;
6970
HTMLParser(html, {
7071
start: function (tag, attrs, unary) {
7172
//debug(tag, attrs, unary);
@@ -75,6 +76,17 @@ function html2json(html, bindName) {
7576
tag: tag,
7677
};
7778

79+
if (bufArray.length === 0) {
80+
node.index = index.toString()
81+
index += 1
82+
} else {
83+
var parent = bufArray[0];
84+
if (parent.nodes === undefined) {
85+
parent.nodes = [];
86+
}
87+
node.index = parent.index + '.' + parent.nodes.length
88+
}
89+
7890
if (block[tag]) {
7991
node.tagType = "block";
8092
} else if (inline[tag]) {
@@ -212,6 +224,7 @@ function html2json(html, bindName) {
212224
if (parent.nodes === undefined) {
213225
parent.nodes = [];
214226
}
227+
node.index = parent.index + '.' + parent.nodes.length
215228
parent.nodes.push(node);
216229
}
217230
},

wxParse/wxParse.js

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ import HtmlToJson from './html2json.js';
1717
/**
1818
* 配置及公有属性
1919
**/
20+
var realWindowWidth = 0;
21+
var realWindowHeight = 0;
22+
wx.getSystemInfo({
23+
success: function (res) {
24+
realWindowWidth = res.windowWidth
25+
realWindowHeight = res.windowHeight
26+
}
27+
})
2028
/**
2129
* 主函数入口区
2230
**/
@@ -76,12 +84,21 @@ function calMoreImageInfo(e, idx, that, bindName) {
7684
var temImages = temData.images;
7785
//因为无法获取view宽度 需要自定义padding进行计算,稍后处理
7886
var recal = wxAutoImageCal(e.detail.width, e.detail.height,that,bindName);
79-
temImages[idx].width = recal.imageWidth;
80-
temImages[idx].height = recal.imageheight;
81-
temData.images = temImages;
82-
var bindData = {};
83-
bindData[bindName] = temData;
84-
that.setData(bindData);
87+
// temImages[idx].width = recal.imageWidth;
88+
// temImages[idx].height = recal.imageheight;
89+
// temData.images = temImages;
90+
// var bindData = {};
91+
// bindData[bindName] = temData;
92+
// that.setData(bindData);
93+
var index = temImages[idx].index
94+
var key = `${bindName}`
95+
for (var i of index.split('.')) key+=`.nodes[${i}]`
96+
var keyW = key + '.width'
97+
var keyH = key + '.height'
98+
that.setData({
99+
[keyW]: recal.imageWidth,
100+
[keyH]: recal.imageheight,
101+
})
85102
}
86103

87104
// 计算视觉优先的图片宽高
@@ -90,26 +107,22 @@ function wxAutoImageCal(originalWidth, originalHeight,that,bindName) {
90107
var windowWidth = 0, windowHeight = 0;
91108
var autoWidth = 0, autoHeight = 0;
92109
var results = {};
93-
wx.getSystemInfo({
94-
success: function (res) {
95-
var padding = that.data[bindName].view.imagePadding;
96-
windowWidth = res.windowWidth-2*padding;
97-
windowHeight = res.windowHeight;
98-
//判断按照那种方式进行缩放
99-
console.log("windowWidth" + windowWidth);
100-
if (originalWidth > windowWidth) {//在图片width大于手机屏幕width时候
101-
autoWidth = windowWidth;
102-
console.log("autoWidth" + autoWidth);
103-
autoHeight = (autoWidth * originalHeight) / originalWidth;
104-
console.log("autoHeight" + autoHeight);
105-
results.imageWidth = autoWidth;
106-
results.imageheight = autoHeight;
107-
} else {//否则展示原来的数据
108-
results.imageWidth = originalWidth;
109-
results.imageheight = originalHeight;
110-
}
111-
}
112-
})
110+
var padding = that.data[bindName].view.imagePadding;
111+
windowWidth = realWindowWidth-2*padding;
112+
windowHeight = realWindowHeight;
113+
//判断按照那种方式进行缩放
114+
// console.log("windowWidth" + windowWidth);
115+
if (originalWidth > windowWidth) {//在图片width大于手机屏幕width时候
116+
autoWidth = windowWidth;
117+
// console.log("autoWidth" + autoWidth);
118+
autoHeight = (autoWidth * originalHeight) / originalWidth;
119+
// console.log("autoHeight" + autoHeight);
120+
results.imageWidth = autoWidth;
121+
results.imageheight = autoHeight;
122+
} else {//否则展示原来的数据
123+
results.imageWidth = originalWidth;
124+
results.imageheight = originalHeight;
125+
}
113126
return results;
114127
}
115128

0 commit comments

Comments
 (0)