wxParse.js 4.3 KB

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