wxParse.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. } else if (type == 'md' || type == 'markdown') {
  36. var converter = new showdown.Converter();
  37. var html = converter.makeHtml(data);
  38. transData = HtmlToJson.html2json(html, bindName);
  39. }
  40. transData.view = {};
  41. transData.view.imagePadding = 0;
  42. if(typeof(imagePadding) != 'undefined'){
  43. transData.view.imagePadding = imagePadding
  44. }
  45. var bindData = {};
  46. bindData[bindName] = transData;
  47. that.setData(bindData)
  48. that.wxParseImgLoad = wxParseImgLoad;
  49. that.wxParseImgTap = wxParseImgTap;
  50. }
  51. // 图片点击事件
  52. function wxParseImgTap(e) {
  53. var that = this;
  54. var nowImgUrl = e.target.dataset.src;
  55. var tagFrom = e.target.dataset.from;
  56. if (typeof (tagFrom) != 'undefined' && tagFrom.length > 0) {
  57. wx.previewImage({
  58. current: nowImgUrl, // 当前显示图片的http链接
  59. urls: that.data[tagFrom].imageUrls // 需要预览的图片http链接列表
  60. })
  61. }
  62. }
  63. /**
  64. * 图片视觉宽高计算函数区
  65. **/
  66. function wxParseImgLoad(e) {
  67. var that = this;
  68. var tagFrom = e.target.dataset.from;
  69. var idx = e.target.dataset.idx;
  70. if (typeof (tagFrom) != 'undefined' && tagFrom.length > 0) {
  71. calMoreImageInfo(e, idx, that, tagFrom)
  72. }
  73. }
  74. // 假循环获取计算图片视觉最佳宽高
  75. function calMoreImageInfo(e, idx, that, bindName) {
  76. var temData = that.data[bindName];
  77. if (!temData || temData.images.length == 0) {
  78. return;
  79. }
  80. var temImages = temData.images;
  81. //因为无法获取view宽度 需要自定义padding进行计算,稍后处理
  82. var recal = wxAutoImageCal(e.detail.width, e.detail.height,that,bindName);
  83. // temImages[idx].width = recal.imageWidth;
  84. // temImages[idx].height = recal.imageheight;
  85. // temData.images = temImages;
  86. // var bindData = {};
  87. // bindData[bindName] = temData;
  88. // that.setData(bindData);
  89. var index = temImages[idx].index
  90. var key = `${bindName}`
  91. for (var i of index.split('.')) key+=`.nodes[${i}]`
  92. var keyW = key + '.width'
  93. var keyH = key + '.height'
  94. that.setData({
  95. [keyW]: recal.imageWidth,
  96. [keyH]: recal.imageheight,
  97. })
  98. }
  99. // 计算视觉优先的图片宽高
  100. function wxAutoImageCal(originalWidth, originalHeight,that,bindName) {
  101. //获取图片的原始长宽
  102. var windowWidth = 0, windowHeight = 0;
  103. var autoWidth = 0, autoHeight = 0;
  104. var results = {};
  105. var padding = that.data[bindName].view.imagePadding;
  106. windowWidth = realWindowWidth-2*padding;
  107. windowHeight = realWindowHeight;
  108. //判断按照那种方式进行缩放
  109. // console.log("windowWidth" + windowWidth);
  110. if (originalWidth > windowWidth) {//在图片width大于手机屏幕width时候
  111. autoWidth = windowWidth;
  112. // console.log("autoWidth" + autoWidth);
  113. autoHeight = (autoWidth * originalHeight) / originalWidth;
  114. // console.log("autoHeight" + autoHeight);
  115. results.imageWidth = autoWidth;
  116. results.imageheight = autoHeight;
  117. } else {//否则展示原来的数据
  118. results.imageWidth = originalWidth;
  119. results.imageheight = originalHeight;
  120. }
  121. return results;
  122. }
  123. function wxParseTemArray(temArrayName,bindNameReg,total,that){
  124. var array = [];
  125. var temData = that.data;
  126. var obj = null;
  127. for(var i = 0; i < total; i++){
  128. var simArr = temData[bindNameReg+i].nodes;
  129. array.push(simArr);
  130. }
  131. temArrayName = temArrayName || 'wxParseTemArray';
  132. obj = JSON.parse('{"'+ temArrayName +'":""}');
  133. obj[temArrayName] = array;
  134. that.setData(obj);
  135. }
  136. /**
  137. * 配置emojis
  138. *
  139. */
  140. function emojisInit(reg='',baseSrc="/wxParse/emojis/",emojis){
  141. HtmlToJson.emojisInit(reg,baseSrc,emojis);
  142. }
  143. module.exports = {
  144. wxParse: wxParse,
  145. wxParseTemArray:wxParseTemArray,
  146. emojisInit:emojisInit
  147. }