Skip to content

Commit

Permalink
升级 wxParse 组件到最新版
Browse files Browse the repository at this point in the history
  • Loading branch information
gooking committed Sep 4, 2017
1 parent 10691d9 commit c5a92f0
Show file tree
Hide file tree
Showing 5 changed files with 877 additions and 792 deletions.
29 changes: 28 additions & 1 deletion wxParse/html2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,19 @@ function removeDOCTYPE(html) {
.replace(/<.*!DOCTYPE.*\>\n/, '');
}

function trimHtml(html) {
return html
.replace(/\r?\n+/g, '')
.replace(/<!--.*?-->/ig, '')
.replace(/\/\*.*?\*\//ig, '')
.replace(/[ ]+</ig, '<')
}


function html2json(html, bindName) {
//处理字符串
html = removeDOCTYPE(html);
html = trimHtml(html);
html = wxDiscode.strDiscode(html);
//生成node节点
var bufArray = [];
Expand All @@ -66,6 +75,7 @@ function html2json(html, bindName) {
images:[],
imageUrls:[]
};
var index = 0;
HTMLParser(html, {
start: function (tag, attrs, unary) {
//debug(tag, attrs, unary);
Expand All @@ -75,6 +85,17 @@ function html2json(html, bindName) {
tag: tag,
};

if (bufArray.length === 0) {
node.index = index.toString()
index += 1
} else {
var parent = bufArray[0];
if (parent.nodes === undefined) {
parent.nodes = [];
}
node.index = parent.index + '.' + parent.nodes.length
}

if (block[tag]) {
node.tagType = "block";
} else if (inline[tag]) {
Expand Down Expand Up @@ -127,6 +148,9 @@ function html2json(html, bindName) {
if (node.tag === 'img') {
node.imgIndex = results.images.length;
var imgUrl = node.attr.src;
if (imgUrl[0] == '') {
imgUrl.splice(0, 1);
}
imgUrl = wxDiscode.urlToHttpUrl(imgUrl, __placeImgeUrlHttps);
node.attr.src = imgUrl;
node.from = bindName;
Expand Down Expand Up @@ -181,7 +205,7 @@ function html2json(html, bindName) {
//当有缓存source资源时于于video补上src资源
if(node.tag === 'video' && results.source){
node.attr.src = results.source;
delete result.source;
delete results.source;
}

if (bufArray.length === 0) {
Expand All @@ -203,12 +227,15 @@ function html2json(html, bindName) {
};

if (bufArray.length === 0) {
node.index = index.toString()
index += 1
results.nodes.push(node);
} else {
var parent = bufArray[0];
if (parent.nodes === undefined) {
parent.nodes = [];
}
node.index = parent.index + '.' + parent.nodes.length
parent.nodes.push(node);
}
},
Expand Down
3 changes: 2 additions & 1 deletion wxParse/wxDiscode.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ function strcharacterDiscode(str){

str = str.replace(/&lt;/g, '<');
str = str.replace(/&gt;/g, '>');
str = str.replace(/&#8226;/g, '•');

return str;
}
Expand Down Expand Up @@ -170,7 +171,7 @@ function strOtherDiscode(str){
str = str.replace(/&hearts;/g, '♥');

str = str.replace(/&diams;/g, '♦');

str = str.replace(/&#39;/g, '\'');
return str;
}

Expand Down
67 changes: 40 additions & 27 deletions wxParse/wxParse.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ import HtmlToJson from './html2json.js';
/**
* 配置及公有属性
**/
var realWindowWidth = 0;
var realWindowHeight = 0;
wx.getSystemInfo({
success: function (res) {
realWindowWidth = res.windowWidth
realWindowHeight = res.windowHeight
}
})
/**
* 主函数入口区
**/
Expand Down Expand Up @@ -70,18 +78,27 @@ function wxParseImgLoad(e) {
// 假循环获取计算图片视觉最佳宽高
function calMoreImageInfo(e, idx, that, bindName) {
var temData = that.data[bindName];
if (temData.images.length == 0) {
if (!temData || temData.images.length == 0) {
return;
}
var temImages = temData.images;
//因为无法获取view宽度 需要自定义padding进行计算,稍后处理
var recal = wxAutoImageCal(e.detail.width, e.detail.height,that,bindName);
temImages[idx].width = recal.imageWidth;
temImages[idx].height = recal.imageheight;
temData.images = temImages;
var bindData = {};
bindData[bindName] = temData;
that.setData(bindData);
// temImages[idx].width = recal.imageWidth;
// temImages[idx].height = recal.imageheight;
// temData.images = temImages;
// var bindData = {};
// bindData[bindName] = temData;
// that.setData(bindData);
var index = temImages[idx].index
var key = `${bindName}`
for (var i of index.split('.')) key+=`.nodes[${i}]`
var keyW = key + '.width'
var keyH = key + '.height'
that.setData({
[keyW]: recal.imageWidth,
[keyH]: recal.imageheight,
})
}

// 计算视觉优先的图片宽高
Expand All @@ -90,26 +107,22 @@ function wxAutoImageCal(originalWidth, originalHeight,that,bindName) {
var windowWidth = 0, windowHeight = 0;
var autoWidth = 0, autoHeight = 0;
var results = {};
wx.getSystemInfo({
success: function (res) {
var padding = that.data[bindName].view.imagePadding;
windowWidth = res.windowWidth-2*padding;
windowHeight = res.windowHeight;
//判断按照那种方式进行缩放
console.log("windowWidth" + windowWidth);
if (originalWidth > windowWidth) {//在图片width大于手机屏幕width时候
autoWidth = windowWidth;
console.log("autoWidth" + autoWidth);
autoHeight = (autoWidth * originalHeight) / originalWidth;
console.log("autoHeight" + autoHeight);
results.imageWidth = autoWidth;
results.imageheight = autoHeight;
} else {//否则展示原来的数据
results.imageWidth = originalWidth;
results.imageheight = originalHeight;
}
}
})
var padding = that.data[bindName].view.imagePadding;
windowWidth = realWindowWidth-2*padding;
windowHeight = realWindowHeight;
//判断按照那种方式进行缩放
// console.log("windowWidth" + windowWidth);
if (originalWidth > windowWidth) {//在图片width大于手机屏幕width时候
autoWidth = windowWidth;
// console.log("autoWidth" + autoWidth);
autoHeight = (autoWidth * originalHeight) / originalWidth;
// console.log("autoHeight" + autoHeight);
results.imageWidth = autoWidth;
results.imageheight = autoHeight;
} else {//否则展示原来的数据
results.imageWidth = originalWidth;
results.imageheight = originalHeight;
}
return results;
}

Expand Down
Loading

0 comments on commit c5a92f0

Please sign in to comment.