wxParse.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. //取消es6转es5后这里报错 所以采用下面写法做兼容 (其实我表示很费解 难道小程序不支持import么 还是说这个写法有问题???)
  17. //TODO 这个貌似是富文本插件插件 也不知道在哪理用到了 --youyawu -2018-12-14
  18. let showdown= require('./showdown.js'), HtmlToJson =require('./html2json.js');
  19. /**
  20. * 配置及公有属性
  21. **/
  22. var realWindowWidth = 0;
  23. var realWindowHeight = 0;
  24. wx.getSystemInfo({
  25. success: function (res) {
  26. realWindowWidth = res.windowWidth
  27. realWindowHeight = res.windowHeight
  28. }
  29. })
  30. /**
  31. * 主函数入口区
  32. **/
  33. function wxParse(bindName = 'wxParseData', type='html', data='<div class="color:red;">数据不能为空</div>', target,imagePadding) {
  34. var that = target;
  35. var transData = {};//存放转化后的数据
  36. if (type == 'html') {
  37. transData = HtmlToJson.html2json(data, bindName);
  38. } else if (type == 'md' || type == 'markdown') {
  39. var converter = new showdown.Converter();
  40. var html = converter.makeHtml(data);
  41. transData = HtmlToJson.html2json(html, bindName);
  42. }
  43. transData.view = {};
  44. transData.view.imagePadding = 0;
  45. if(typeof(imagePadding) != 'undefined'){
  46. transData.view.imagePadding = imagePadding
  47. }
  48. var bindData = {};
  49. bindData[bindName] = transData;
  50. that.setData(bindData)
  51. that.wxParseImgLoad = wxParseImgLoad;
  52. that.wxParseImgTap = wxParseImgTap;
  53. }
  54. // 图片点击事件
  55. function wxParseImgTap(e) {
  56. var that = this;
  57. var nowImgUrl = e.target.dataset.src;
  58. var tagFrom = e.target.dataset.from;
  59. if (typeof (tagFrom) != 'undefined' && tagFrom.length > 0) {
  60. wx.previewImage({
  61. current: nowImgUrl, // 当前显示图片的http链接
  62. urls: that.data[tagFrom].imageUrls // 需要预览的图片http链接列表
  63. })
  64. }
  65. }
  66. /**
  67. * 图片视觉宽高计算函数区
  68. **/
  69. function wxParseImgLoad(e) {
  70. var that = this;
  71. var tagFrom = e.target.dataset.from;
  72. var idx = e.target.dataset.idx;
  73. if (typeof (tagFrom) != 'undefined' && tagFrom.length > 0) {
  74. calMoreImageInfo(e, idx, that, tagFrom)
  75. }
  76. }
  77. // 假循环获取计算图片视觉最佳宽高
  78. function calMoreImageInfo(e, idx, that, bindName) {
  79. var temData = that.data[bindName];
  80. if (!temData || temData.images.length == 0) {
  81. return;
  82. }
  83. var temImages = temData.images;
  84. //因为无法获取view宽度 需要自定义padding进行计算,稍后处理
  85. var recal = wxAutoImageCal(e.detail.width, e.detail.height,that,bindName);
  86. // temImages[idx].width = recal.imageWidth;
  87. // temImages[idx].height = recal.imageheight;
  88. // temData.images = temImages;
  89. // var bindData = {};
  90. // bindData[bindName] = temData;
  91. // that.setData(bindData);
  92. var index = temImages[idx].index
  93. var key = `${bindName}`
  94. for (var i of index.split('.')) key+=`.nodes[${i}]`
  95. var keyW = key + '.width'
  96. var keyH = key + '.height'
  97. that.setData({
  98. [keyW]: recal.imageWidth,
  99. [keyH]: recal.imageheight,
  100. })
  101. }
  102. // 计算视觉优先的图片宽高
  103. function wxAutoImageCal(originalWidth, originalHeight,that,bindName) {
  104. //获取图片的原始长宽
  105. var windowWidth = 0, windowHeight = 0;
  106. var autoWidth = 0, autoHeight = 0;
  107. var results = {};
  108. var padding = that.data[bindName].view.imagePadding;
  109. windowWidth = realWindowWidth-2*padding;
  110. windowHeight = realWindowHeight;
  111. //判断按照那种方式进行缩放
  112. // console.log("windowWidth" + windowWidth);
  113. if (originalWidth > windowWidth) {//在图片width大于手机屏幕width时候
  114. autoWidth = windowWidth;
  115. // console.log("autoWidth" + autoWidth);
  116. autoHeight = (autoWidth * originalHeight) / originalWidth;
  117. // console.log("autoHeight" + autoHeight);
  118. results.imageWidth = autoWidth;
  119. results.imageheight = autoHeight;
  120. } else {//否则展示原来的数据
  121. results.imageWidth = originalWidth;
  122. results.imageheight = originalHeight;
  123. }
  124. return results;
  125. }
  126. function wxParseTemArray(temArrayName,bindNameReg,total,that){
  127. var array = [];
  128. var temData = that.data;
  129. var obj = null;
  130. for(var i = 0; i < total; i++){
  131. var simArr = temData[bindNameReg+i].nodes;
  132. array.push(simArr);
  133. }
  134. temArrayName = temArrayName || 'wxParseTemArray';
  135. obj = JSON.parse('{"'+ temArrayName +'":""}');
  136. obj[temArrayName] = array;
  137. that.setData(obj);
  138. }
  139. /**
  140. * 配置emojis
  141. *
  142. */
  143. function emojisInit(reg='',baseSrc="/wxParse/emojis/",emojis){
  144. HtmlToJson.emojisInit(reg,baseSrc,emojis);
  145. }
  146. module.exports = {
  147. wxParse: wxParse,
  148. wxParseTemArray:wxParseTemArray,
  149. emojisInit:emojisInit
  150. }