wxParse.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. var _showdown = require("./showdown.js"), _showdown2 = _interopRequireDefault(_showdown), _html2json = require("./html2json.js"), _html2json2 = _interopRequireDefault(_html2json);
  2. function _interopRequireDefault(e) {
  3. return e && e.__esModule ? e : {
  4. default: e
  5. };
  6. }
  7. function _defineProperty(e, t, a) {
  8. return t in e ? Object.defineProperty(e, t, {
  9. value: a,
  10. enumerable: !0,
  11. configurable: !0,
  12. writable: !0
  13. }) : e[t] = a, e;
  14. }
  15. var realWindowWidth = 0, realWindowHeight = 0;
  16. function wxParse() {
  17. 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 = arguments[3], r = arguments[4], n = i, o = {};
  18. if ("html" == t) o = _html2json2.default.html2json(a, e); else if ("md" == t || "markdown" == t) {
  19. var d = new _showdown2.default.Converter().makeHtml(a);
  20. o = _html2json2.default.html2json(d, e);
  21. }
  22. o.view = {}, void (o.view.imagePadding = 0) !== r && (o.view.imagePadding = r);
  23. var s = {};
  24. s[e] = o, n.setData(s), n.wxParseImgLoad = wxParseImgLoad, n.wxParseImgTap = wxParseImgTap;
  25. }
  26. function wxParseImgTap(e) {
  27. var t = e.target.dataset.src, a = e.target.dataset.from;
  28. void 0 !== a && 0 < a.length && wx.previewImage({
  29. current: t,
  30. urls: this.data[a].imageUrls
  31. });
  32. }
  33. function wxParseImgLoad(e) {
  34. var t = e.target.dataset.from, a = e.target.dataset.idx;
  35. void 0 !== t && 0 < t.length && calMoreImageInfo(e, a, this, t);
  36. }
  37. function calMoreImageInfo(e, t, a, i) {
  38. var r, n = a.data[i];
  39. if (n && 0 != n.images.length) {
  40. var o = n.images, d = wxAutoImageCal(e.detail.width, e.detail.height, a, i), s = o[t].index, l = "" + i, m = !0, h = !1, g = void 0;
  41. try {
  42. for (var w, u = s.split(".")[Symbol.iterator](); !(m = (w = u.next()).done); m = !0) {
  43. l += ".nodes[" + w.value + "]";
  44. }
  45. } catch (e) {
  46. h = !0, g = e;
  47. } finally {
  48. try {
  49. !m && u.return && u.return();
  50. } finally {
  51. if (h) throw g;
  52. }
  53. }
  54. var f = l + ".width", v = l + ".height";
  55. a.setData((_defineProperty(r = {}, f, d.imageWidth), _defineProperty(r, v, d.imageheight),
  56. r));
  57. }
  58. }
  59. function wxAutoImageCal(e, t, a, i) {
  60. var r, n = 0, o = 0, d = {}, s = a.data[i].view.imagePadding;
  61. return realWindowHeight, (r = realWindowWidth - 2 * s) < e ? (o = (n = r) * t / e,
  62. d.imageWidth = n, d.imageheight = o) : (d.imageWidth = e, d.imageheight = t), d;
  63. }
  64. function wxParseTemArray(e, t, a, i) {
  65. for (var r = [], n = i.data, o = null, d = 0; d < a; d++) {
  66. var s = n[t + d].nodes;
  67. r.push(s);
  68. }
  69. e = e || "wxParseTemArray", (o = JSON.parse('{"' + e + '":""}'))[e] = r, i.setData(o);
  70. }
  71. function emojisInit() {
  72. var e = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : "", t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : "/wxParse/emojis/", a = arguments[2];
  73. _html2json2.default.emojisInit(e, t, a);
  74. }
  75. wx.getSystemInfo({
  76. success: function(e) {
  77. realWindowWidth = e.windowWidth, realWindowHeight = e.screenHeight;
  78. }
  79. }), module.exports = {
  80. wxParse: wxParse,
  81. wxParseTemArray: wxParseTemArray,
  82. emojisInit: emojisInit
  83. };