当前位置:   article > 正文

微信小程序--支持ckeditor编辑器中mathquill数学公式_小程序解决数学公式渲染问题

小程序解决数学公式渲染问题

使用Towxml解析ckeditor文本编辑器中mathquill数学公式

显示如下效果:

 

一、微信小程序中构建Towxml

参考之前文章:https://blog.csdn.net/qq_33848658/article/details/115234194

二、使用

1、单独封装mathquill.css

2、towxml/towxml.wxss中引入mathquill.css

  1. /* mathquill数学公式样式 */
  2. @import './style/theme/mathquill.wxss'

3、mathquill/index.js 转化数学公式

  1. // pages/mathquill/index.js
  2. const app = getApp()
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. questionsContent: null, //单选题目
  9. option: [{
  10. option: "A",
  11. value: '<p><span class="mq-math-mode" latex-data="\sum_{n=0}^{\infty}k^2"><span class="mq-textarea"><textarea autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" x-palm-disable-ste-all="true"></textarea></span><span class="mq-root-block" mathquill-block-id="1"><span class="mq-large-operator mq-non-leaf" mathquill-command-id="3"><span class="mq-to"><span mathquill-block-id="5"><span mathquill-command-id="12">&infin;</span></span></span><big>&sum;</big><span class="mq-from"><span mathquill-block-id="4"><var mathquill-command-id="6">n</var><span class="mq-binary-operator" mathquill-command-id="8">=</span><span mathquill-command-id="10">0</span></span></span></span><var mathquill-command-id="15">k</var><span class="mq-supsub mq-non-leaf mq-sup-only" mathquill-command-id="17"><span class="mq-sup" mathquill-block-id="19"><span mathquill-command-id="18">2</span></span></span></span></span><span>&nbsp;</span></p>↵'
  12. },
  13. {
  14. option: "B",
  15. value: '<p><span class="mq-math-mode" latex-data="\sum_2^1"><span class="mq-textarea"><textarea autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" x-palm-disable-ste-all="true"></textarea></span><span class="mq-root-block" mathquill-block-id="1"><span class="mq-large-operator mq-non-leaf" mathquill-command-id="3"><span class="mq-to"><span mathquill-block-id="5"><span mathquill-command-id="7">1</span></span></span><big>&sum;</big><span class="mq-from"><span mathquill-block-id="4"><span mathquill-command-id="8">2</span></span></span></span></span></span><span>&nbsp;</span></p>'
  16. },
  17. {
  18. option: "C",
  19. value: '<p><span class="mq-math-mode" latex-data="\log_1^n"><span class="mq-textarea"><textarea autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" x-palm-disable-ste-all="true"></textarea></span><span class="mq-root-block" mathquill-block-id="1"><var class="mq-operator-name" mathquill-command-id="4">l</var><var class="mq-operator-name" mathquill-command-id="5">o</var><var class="mq-operator-name" mathquill-command-id="6">g</var><span class="mq-supsub mq-non-leaf" mathquill-command-id="11"><span class="mq-sup" mathquill-block-id="13"><var mathquill-command-id="12">n</var></span><span class="mq-sub" mathquill-block-id="9"><span mathquill-command-id="15">1</span></span><span style="display:inline-block;width:0">​</span></span></span></span><span>&nbsp;</span></p>'
  20. },
  21. {
  22. option: "D",
  23. value: '<p><span class="mq-math-mode" latex-data="\ln2"><span class="mq-textarea"><textarea autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" x-palm-disable-ste-all="true"></textarea></span><span class="mq-root-block" mathquill-block-id="1"><var class="mq-operator-name" mathquill-command-id="28">l</var><var class="mq-operator-name mq-last" mathquill-command-id="29">n</var><span mathquill-command-id="31">2</span></span></span><span>&nbsp;</span></p>'
  24. }
  25. ] //选项
  26. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. onLoad: function(options) {
  31. let questionsContent = '<p><span class="mq-math-mode" latex-data="^1/_2"><span class="mq-textarea"><textarea autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" x-palm-disable-ste-all="true"></textarea></span><span class="mq-root-block" mathquill-block-id="1"><span class="mq-supsub mq-non-leaf mq-sup-only" mathquill-command-id="3"><span class="mq-sup" mathquill-block-id="4"><span mathquill-command-id="11">1</span></span></span><span mathquill-command-id="6">/</span><span class="mq-supsub mq-non-leaf" mathquill-command-id="8"><span class="mq-sub" mathquill-block-id="9"><span mathquill-command-id="12">2</span></span><span style="display:inline-block;width:0">​</span></span></span></span><span>&nbsp;</span><span class="mq-math-mode" latex-data="\overleftarrow{ab}"><span class="mq-textarea"><textarea autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" x-palm-disable-ste-all="true"></textarea></span><span class="mq-root-block" mathquill-block-id="1"><span class="mq-non-leaf mq-overarrow mq-arrow-left" mathquill-block-id="23" mathquill-command-id="21"><var mathquill-command-id="22">a</var><var mathquill-command-id="24">b</var></span></span></span><span>&nbsp;</span><span class="mq-math-mode" latex-data="x^1"><span class="mq-textarea"><textarea autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" x-palm-disable-ste-all="true"></textarea></span><span class="mq-root-block" mathquill-block-id="1"><var mathquill-command-id="27">x</var><span class="mq-supsub mq-non-leaf mq-sup-only" mathquill-command-id="29"><span class="mq-sup" mathquill-block-id="30"><span mathquill-command-id="32">1</span></span></span></span></span><span>&nbsp;</span><span class="mq-math-mode" latex-data="x_1"><span class="mq-textarea"><textarea autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" x-palm-disable-ste-all="true"></textarea></span><span class="mq-root-block" mathquill-block-id="1"><var mathquill-command-id="33">x</var><span class="mq-supsub mq-non-leaf" mathquill-command-id="35"><span class="mq-sub" mathquill-block-id="36"><span mathquill-command-id="38">1</span></span><span style="display:inline-block;width:0">​</span></span></span></span><span>&nbsp;</span><span class="mq-math-mode" latex-data="x_2^1"><span class="mq-textarea"><textarea autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" x-palm-disable-ste-all="true"></textarea></span><span class="mq-root-block" mathquill-block-id="1"><var mathquill-command-id="39">x</var><span class="mq-supsub mq-non-leaf" mathquill-command-id="44"><span class="mq-sup" mathquill-block-id="42"><span mathquill-command-id="47">1</span></span><span class="mq-sub" mathquill-block-id="45"><span mathquill-command-id="48">2</span></span><span style="display:inline-block;width:0">​</span></span></span></span><span>&nbsp;</span></p>'
  32. this.data.questionsContent = app.towxml(questionsContent, 'markdown'); //towxml解析题内容
  33. this.data.option.forEach((item, index) => {
  34. this.data.option[index].value = app.towxml(item.value, 'markdown')
  35. })
  36. // 更新解析数据
  37. this.setData({
  38. questionsContent: this.data.questionsContent,
  39. option: this.data.option
  40. });
  41. },
  42. })

备注:mathquill数学公式转化在线转化:http://www.jmeditor.com/jme/demo.html

如下图所示:

 

4、mathquill/index.json 中构建组件

    "towxml": "/towxml/towxml"

5、显示mathquill/index.wxml中渲染数学公式

  1. <!--pages/mathquill/index.wxml-->
  2. <view>
  3. <towxml nodes="{{questionsContent}}" />
  4. <view class="radio my-choosebox" wx:for="{{option}}">
  5. <text>{{item.option}}</text>
  6. <towxml nodes="{{item.value}}" />
  7. </view>
  8. </view>

  6、demo源码参考:https://download.csdn.net/download/qq_33848658/16265383

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/91232
推荐阅读
相关标签
  

闽ICP备14008679号