wxParse.js
2.61 KB
"use strict";var _showdown=_interopRequireDefault(require("./showdown.js")),_html2json=_interopRequireDefault(require("./html2json.js"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _defineProperty(e,t,a){return t in e?Object.defineProperty(e,t,{value:a,enumerable:!0,configurable:!0,writable:!0}):e[t]=a,e}var realWindowWidth=0,realWindowHeight=0;function wxParse(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"wxParseData",t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"html",a=2<arguments.length&&void 0!==arguments[2]?arguments[2]:'<div class="color:red;">数据不能为空</div>',i=3<arguments.length?arguments[3]:void 0,r=4<arguments.length?arguments[4]:void 0,n=i,o={};if("html"==t)o=_html2json.default.html2json(a,e);else if("md"==t||"markdown"==t){var d=(new _showdown.default.Converter).makeHtml(a);o=_html2json.default.html2json(d,e)}o.view={},void(o.view.imagePadding=0)!==r&&(o.view.imagePadding=r);var l={};l[e]=o,n.setData(l),n.wxParseImgLoad=wxParseImgLoad,n.wxParseImgTap=wxParseImgTap}function wxParseImgTap(e){var t=e.target.dataset.src,a=e.target.dataset.from;void 0!==a&&0<a.length&&wx.previewImage({current:t,urls:this.data[a].imageUrls})}function wxParseImgLoad(e){var t=e.target.dataset.from,a=e.target.dataset.idx;void 0!==t&&0<t.length&&calMoreImageInfo(e,a,this,t)}function calMoreImageInfo(e,t,a,i){var r,n=a.data[i];if(n&&0!=n.images.length){var o=n.images,d=wxAutoImageCal(e.detail.width,e.detail.height,a,i),l=o[t].index,s="".concat(i),g=!0,m=!1,h=void 0;try{for(var w,u=l.split(".")[Symbol.iterator]();!(g=(w=u.next()).done);g=!0){var v=w.value;s+=".nodes[".concat(v,"]")}}catch(e){m=!0,h=e}finally{try{g||null==u.return||u.return()}finally{if(m)throw h}}var f=s+".width",c=s+".height";a.setData((_defineProperty(r={},f,d.imageWidth),_defineProperty(r,c,d.imageheight),r))}}function wxAutoImageCal(e,t,a,i){var r,n=0,o=0,d={},l=a.data[i].view.imagePadding;return r=realWindowWidth-2*l,realWindowHeight,d.imageheight=r<e?(o=(n=r)*t/e,d.imageWidth=n,o):(d.imageWidth=e,t),d}function wxParseTemArray(e,t,a,i){for(var r=[],n=i.data,o=null,d=0;d<a;d++){var l=n[t+d].nodes;r.push(l)}e=e||"wxParseTemArray",(o=JSON.parse('{"'+e+'":""}'))[e]=r,i.setData(o)}function emojisInit(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"/wxParse/emojis/",a=2<arguments.length?arguments[2]:void 0;_html2json.default.emojisInit(e,t,a)}wx.getSystemInfo({success:function(e){realWindowWidth=e.windowWidth,realWindowHeight=e.windowHeight}}),module.exports={wxParse:wxParse,wxParseTemArray:wxParseTemArray,emojisInit:emojisInit};