wxParse.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * author: Di (微信小程序开发工程师)
  3. * organization: WeAppDev(微信小程序开发论坛)(http://weappdev.com)
  4. * 垂直微信小程序开发交流社区
  5. *
  6. * github地址: https://github.com/icindy/wxParse
  7. *
  8. * for: 微信小程序富文本解析
  9. * detail : http://weappdev.com/t/wxparse-alpha0-1-html-markdown/184
  10. */
  11. /**
  12. * utils函数引入
  13. **/
  14. import showdown from './showdown.js';
  15. import HtmlToJson from './html2json.js';
  16. /**
  17. * 配置及公有属性
  18. **/
  19. var realWindowWidth = 0;
  20. var realWindowHeight = 0;
  21. wx.getSystemInfo({
  22. success: function (res) {
  23. realWindowWidth = res.windowWidth
  24. realWindowHeight = res.windowHeight
  25. }
  26. })
  27. /**
  28. * 主函数入口区
  29. **/
  30. function wxParse(bindName = 'wxParseData', type='html', data='<div class="color:red;">数据不能为空</div>', target,imagePadding) {
  31. var that = target;
  32. var transData = {};//存放转化后的数据
  33. if (type == 'html') {
  34. transData = HtmlToJson.html2json(data, bindName);
  35. // console.log(JSON.stringify(transData, ' ', ' '));
  36. } else if (type == 'md' || type == 'markdown') {
  37. var converter = new showdown.Converter();
  38. var html = converter.makeHtml(data);
  39. transData = HtmlToJson.html2json(html, bindName);
  40. // console.log(JSON.stringify(transData, ' ', ' '));
  41. }
  42. transData.view = {};
  43. transData.view.imagePadding = 0;
  44. if(typeof(imagePadding) != 'undefined'){
  45. transData.view.imagePadding = imagePadding
  46. }
  47. var bindData = {};
  48. bindData[bindName] = transData;
  49. that.setData(bindData)
  50. that.wxParseImgLoad = wxParseImgLoad;
  51. that.wxParseImgTap = wxParseImgTap;
  52. }
  53. // 图片点击事件
  54. function wxParseImgTap(e) {
  55. var that = this;
  56. var nowImgUrl = e.target.dataset.src;
  57. var tagFrom = e.target.dataset.from;
  58. if (typeof (tagFrom) != 'undefined' && tagFrom.length > 0) {
  59. wx.previewImage({
  60. current: nowImgUrl, // 当前显示图片的http链接
  61. urls: that.data[tagFrom].imageUrls // 需要预览的图片http链接列表
  62. })
  63. }
  64. }
  65. /**
  66. * 图片视觉宽高计算函数区
  67. **/
  68. function wxParseImgLoad(e) {
  69. var that = this;
  70. var tagFrom = e.target.dataset.from;
  71. var idx = e.target.dataset.idx;
  72. if (typeof (tagFrom) != 'undefined' && tagFrom.length > 0) {
  73. calMoreImageInfo(e, idx, that, tagFrom)
  74. }
  75. }
  76. // 假循环获取计算图片视觉最佳宽高
  77. function calMoreImageInfo(e, idx, that, bindName) {
  78. var temData = that.data[bindName];
  79. if (!temData || temData.images.length == 0) {
  80. return;
  81. }
  82. var temImages = temData.images;
  83. //因为无法获取view宽度 需要自定义padding进行计算,稍后处理
  84. var recal = wxAutoImageCal(e.detail.width, e.detail.height,that,bindName);
  85. // temImages[idx].width = recal.imageWidth;
  86. // temImages[idx].height = recal.imageheight;
  87. // temData.images = temImages;
  88. // var bindData = {};
  89. // bindData[bindName] = temData;
  90. // that.setData(bindData);
  91. var index = temImages[idx].index
  92. var key = `${bindName}`
  93. for (var i of index.split('.')) key+=`.nodes[${i}]`
  94. var keyW = key + '.width'
  95. var keyH = key + '.height'
  96. that.setData({
  97. [keyW]: recal.imageWidth,
  98. [keyH]: recal.imageheight,
  99. })
  100. }
  101. // 计算视觉优先的图片宽高
  102. function wxAutoImageCal(originalWidth, originalHeight,that,bindName) {
  103. //获取图片的原始长宽
  104. var windowWidth = 0, windowHeight = 0;
  105. var autoWidth = 0, autoHeight = 0;
  106. var results = {};
  107. var padding = that.data[bindName].view.imagePadding;
  108. windowWidth = realWindowWidth-2*padding;
  109. windowHeight = realWindowHeight;
  110. //判断按照那种方式进行缩放
  111. // console.log("windowWidth" + windowWidth);
  112. if (originalWidth > windowWidth) {//在图片width大于手机屏幕width时候
  113. autoWidth = windowWidth;
  114. // console.log("autoWidth" + autoWidth);
  115. autoHeight = (autoWidth * originalHeight) / originalWidth;
  116. // console.log("autoHeight" + autoHeight);
  117. results.imageWidth = autoWidth;
  118. results.imageheight = autoHeight;
  119. } else {//否则展示原来的数据
  120. results.imageWidth = originalWidth;
  121. results.imageheight = originalHeight;
  122. }
  123. return results;
  124. }
  125. function wxParseTemArray(temArrayName,bindNameReg,total,that){
  126. var array = [];
  127. var temData = that.data;
  128. var obj = null;
  129. for(var i = 0; i < total; i++){
  130. var simArr = temData[bindNameReg+i].nodes;
  131. array.push(simArr);
  132. }
  133. temArrayName = temArrayName || 'wxParseTemArray';
  134. obj = JSON.parse('{"'+ temArrayName +'":""}');
  135. obj[temArrayName] = array;
  136. that.setData(obj);
  137. }
  138. /**
  139. * 配置emojis
  140. *
  141. */
  142. function emojisInit(reg='',baseSrc="/wxParse/emojis/",emojis){
  143. HtmlToJson.emojisInit(reg,baseSrc,emojis);
  144. }
  145. module.exports = {
  146. wxParse: wxParse,
  147. wxParseTemArray:wxParseTemArray,
  148. emojisInit:emojisInit
  149. }