微信小程式富文字寫法

炊煙渺渺發表於2018-08-02

JS:
引用的JS
var WxParse = require('../common/wxParse/wxParse.js');

富文字詳情JS
var introduce = WxParse.wxParse('introduce', 'html', res.data.dat[0].introduce,
that, 5);


WXML:

引用的WXML
<import src="../common/wxParse/wxParse.wxml" />

富文字詳情WXML
<view class="content">
<view class="wxParse">
<template is="wxParse" data="{{wxParseData:introduce.nodes}}" />
</view>
</view>


被引用的JS: wxParse.js

/**
 * author: Di (微信小程式開發工程師)
 * organization: WeAppDev(微信小程式開發論壇)(http://weappdev.com)
 *               垂直微信小程式開發交流社群
 * 
 * github地址: https://github.com/icindy/wxParse
 * 
 * for: 微信小程式富文字解析
 * detail : http://weappdev.com/t/wxparse-alpha0-1-html-markdown/184
 */

/**
 * utils函式引入
 **/
import showdown from './showdown.js';
import HtmlToJson from './html2json.js';
/**
 * 配置及公有屬性
 **/
var realWindowWidth = 0;
var realWindowHeight = 0;
wx.getSystemInfo({
  success: function (res) {
    realWindowWidth = res.windowWidth
    realWindowHeight = res.windowHeight
  }
})
/**
 * 主函式入口區
 **/
function wxParse(bindName = 'wxParseData', type = 'html', data = '<div
class="color:red;">資料不能為空</div>', target, imagePadding) {
  var that = target;
  var transData = {};//存放轉化後的資料
  if (type == 'html') {
    transData = HtmlToJson.html2json(data, bindName);
    // console.log(JSON.stringify(transData, ' ', ' '));
  } else if (type == 'md' || type == 'markdown') {
    var converter = new showdown.Converter();
    var html = converter.makeHtml(data);
    transData = HtmlToJson.html2json(html, bindName);
    // console.log(JSON.stringify(transData, ' ', ' '));
  }
  transData.view = {};
  transData.view.imagePadding = 0;
  if (typeof (imagePadding) != 'undefined') {
    transData.view.imagePadding = imagePadding
  }
  var bindData = {};
  bindData[bindName] = transData;
  that.setData(bindData)
  that.wxParseImgLoad = wxParseImgLoad;
  that.wxParseImgTap = wxParseImgTap;
}
// 圖片點選事件
function wxParseImgTap(e) {
  var that = this;
  var nowImgUrl = e.target.dataset.src;
  var tagFrom = e.target.dataset.from;
  if (typeof (tagFrom) != 'undefined' && tagFrom.length > 0) {
    wx.previewImage({
      current: nowImgUrl, // 當前顯示圖片的http連結
      urls: that.data[tagFrom].imageUrls // 需要預覽的圖片http連結列表
    })
  }
}

/**
 * 圖片視覺寬高計算函式區 
 **/
function wxParseImgLoad(e) {
  var that = this;
  var tagFrom = e.target.dataset.from;
  var idx = e.target.dataset.idx;
  if (typeof (tagFrom) != 'undefined' && tagFrom.length > 0) {
    calMoreImageInfo(e, idx, that, tagFrom)
  }
}
// 假迴圈獲取計算圖片視覺最佳寬高
function calMoreImageInfo(e, idx, that, bindName) {
  var temData = that.data[bindName];
  if (!temData || temData.images.length == 0) {
    return;
  }
  var temImages = temData.images;
  //因為無法獲取view寬度 需要自定義padding進行計算,稍後處理
  var recal = wxAutoImageCal(e.detail.width, e.detail.height, that, bindName);
  // temImages[idx].width = recal.imageWidth;
  // temImages[idx].height = recal.imageheight; 
  // temData.images = temImages;
  // var bindData = {};
  // bindData[bindName] = temData;
  // that.setData(bindData);
  var index = temImages[idx].index
  var key = `${bindName}`
  for (var i of index.split('.')) key += `.nodes[${i}]`
  var keyW = key + '.width'
  var keyH = key + '.height'
  that.setData({
    [keyW]: recal.imageWidth,
    [keyH]: recal.imageheight,
  })
}

// 計算視覺優先的圖片寬高
function wxAutoImageCal(originalWidth, originalHeight, that, bindName) {
  //獲取圖片的原始長寬
  var windowWidth = 0, windowHeight = 0;
  var autoWidth = 0, autoHeight = 0;
  var results = {};
  var padding = that.data[bindName].view.imagePadding;
  windowWidth = realWindowWidth - 2 * padding;
  windowHeight = realWindowHeight;
  //判斷按照那種方式進行縮放
  // console.log("windowWidth" + windowWidth);
  if (originalWidth > windowWidth) {//在圖片width大於手機螢幕width時候
    autoWidth = windowWidth;
    // console.log("autoWidth" + autoWidth);
    autoHeight = (autoWidth * originalHeight) / originalWidth;
    // console.log("autoHeight" + autoHeight);
    results.imageWidth = autoWidth;
    results.imageheight = autoHeight;
  } else {//否則展示原來的資料
    results.imageWidth = originalWidth;
    results.imageheight = originalHeight;
  }
  return results;
}

function wxParseTemArray(temArrayName, bindNameReg, total, that) {
  var array = [];
  var temData = that.data;
  var obj = null;
  for (var i = 0; i < total; i++) {
    var simArr = temData[bindNameReg + i].nodes;
    array.push(simArr);
  }

  temArrayName = temArrayName || 'wxParseTemArray';
  obj = JSON.parse('{"' + temArrayName + '":""}');
  obj[temArrayName] = array;
  that.setData(obj);
}

/**
 * 配置emojis
 * 
 */

function emojisInit(reg = '', baseSrc = "/wxParse/emojis/", emojis) {
  HtmlToJson.emojisInit(reg, baseSrc, emojis);
}

module.exports = {
  wxParse: wxParse,
  wxParseTemArray: wxParseTemArray,
  emojisInit: emojisInit,
  wxParseImgTap: wxParseImgTap
}


 

相關文章