<p>思路</p><p>既然wxParse能够显示富文本而且它的思路是将整个HTML String转换成一个Node数组,数组按照传入的HTML String生成HTML DOM结构。所以既然它能够处理每一个DOM,那么我们也可以在其中的一环上做一些事情,例如标注。然而实际开发的时候是在显示的时候进行处理的。</p><p>分析wxParse</p><p>wxParse的使用文档里写的很清楚。</p><p>WxParse.wxParse(&#39;article&#39;, &#39;html&#39;, article, that, 5);</p><p>在对应的Component或者page组件中调用其这句话,其实就是将article处理成node数组,存在that的‘article’字段里,而that就是一个component组件。</p><p>接下来</p><p>// 引入模板</p><p>&lt;import src=&quot;你的路径/wxParse/wxParse.wxml&quot;/&gt;</p><p>//这里data中article为bindName</p><p>&lt;template is=&quot;wxParse&quot; data=&quot;{{wxParseData:article.nodes}}&quot;/&gt;</p><p>通过这个template来进行展示,基本思路就是这样子。</p><p>这个wxParse.wxml目前是一个拥有11层的template嵌套,也就是最多支持11层的DOM树了。</p><p>然而这是无法满足我们的需求的,因为标注功能需要操作DOM,也就是通过点击,你要知道你点了哪一个DOM,而template在目前的版本是无法传递方法的。所以有了接下来的故事,也就是把wxParse组件化,将其变为组件不就可以传递方法到最内层,然后做任何事情了嘛。</p><p>组件化</p><p>组件化之后的结构长这样,其实就是把template中的每一层都抽出来写成组件,而转node数组的那部分,还是用之前的方法,这里只是负责显示的部分。</p><p><img src="/up_pic/201907/061007345487.png" title="061007345487.png" alt="1.png"/></p><p>组件化后的结构.png</p><p>wxEmojiView、wxParseBr、wxParseImg、wxParseVideo等等都是简单的组件,负责显示传入的DOM就可以了,而wxParse比较复杂一些,根据node的类型,来指定渲染哪些组件。直接贴代码大家感受一下吧。</p><p>这里出现了组件自身又引用自身的情况(第5行),这个在微信小程序原生开发中是可行的。</p><p>&lt;block wx:if=&quot;{{item.node == &#39;element&#39;}}&quot;&gt;</p><p> &lt;block wx:if=&quot;{{item.tag == &#39;button&#39;}}&quot;&gt;</p><p> &lt;button type=&quot;default&quot; size=&quot;mini&quot;&gt;</p><p> &lt;block wx:for=&quot;{{item.nodes}}&quot; wx:for-item=&quot;item&quot; wx:key=&quot;&quot;&gt;</p><p> &lt;custom-parse item=&quot;{{item}}&quot; notes=&quot;{{notes}}&quot;&gt;&lt;/custom-parse&gt;</p><p> &lt;/block&gt;</p><p> &lt;/button&gt;</p><p> &lt;/block&gt;</p><p> &lt;!--li类型--&gt;</p><p> &lt;block wx:elif=&quot;{{item.tag == &#39;li&#39;}}&quot;&gt;</p><p> &lt;view class=&quot;{{item.classStr}} wxParse-li&quot; style=&quot;{{item.styleStr}}&quot;&gt;</p><p> &lt;view class=&quot;{{item.classStr}} wxParse-li-inner&quot;&gt;</p><p> &lt;view class=&quot;{{item.classStr}} wxParse-li-text&quot;&gt;</p><p> &lt;view class=&quot;{{item.classStr}} wxParse-li-circle&quot;&gt;&lt;/view&gt;</p><p> &lt;/view&gt;</p><p> &lt;view class=&quot;{{item.classStr}} wxParse-li-text&quot;&gt;</p><p> &lt;block wx:for=&quot;{{item.nodes}}&quot; wx:for-item=&quot;item&quot; wx:key=&quot;&quot;&gt;</p><p> &lt;custom-parse item=&quot;{{item}}&quot; notes=&quot;{{notes}}&quot;&gt;&lt;/custom-parse&gt;</p><p> &lt;/block&gt;</p><p> &lt;/view&gt;</p><p> &lt;/view&gt;</p><p> &lt;/view&gt;</p><p> &lt;/block&gt;</p><p> &lt;!--video类型--&gt;</p><p> &lt;block wx:elif=&quot;{{item.tag == &#39;video&#39;}}&quot;&gt;</p><p> &lt;custom-parse-video item=&quot;{{item}}&quot;&gt;&lt;/custom-parse-video&gt;</p><p> &lt;/block&gt;</p><p> &lt;!--img类型--&gt;</p><p> &lt;block wx:elif=&quot;{{item.tag == &#39;img&#39;}}&quot;&gt;</p><p> &lt;custom-parse-img item=&quot;{{item}}&quot;&gt;&lt;/custom-parse-img&gt;</p><p> &lt;/block&gt;</p><p> &lt;!--a类型--&gt;</p><p> &lt;block wx:elif=&quot;{{item.tag == &#39;a&#39;}}&quot;&gt;</p><p> &lt;view bindtap=&quot;wxParseTagATap&quot; class=&quot;wxParse-inline {{item.classStr}} wxParse-{{item.tag}}&quot; data-src=&quot;{{item.attr.href}}&quot; style=&quot;{{item.styleStr}}&quot;&gt;</p><p> &lt;block wx:for=&quot;{{item.nodes}}&quot; wx:for-item=&quot;item&quot; wx:key=&quot;&quot;&gt;</p><p> &lt;custom-parse item=&quot;{{item}}&quot; notes=&quot;{{notes}}&quot;&gt;&lt;/custom-parse&gt;</p><p> &lt;/block&gt;</p><p> &lt;/view&gt;</p><p> &lt;/block&gt;</p><p> &lt;block wx:elif=&quot;{{item.tag == &#39;table&#39;}}&quot;&gt;</p><p> &lt;view class=&quot;{{item.classStr}} wxParse-{{item.tag}}&quot; style=&quot;{{item.styleStr}}&quot;&gt;</p><p> &lt;block wx:for=&quot;{{item.nodes}}&quot; wx:for-item=&quot;item&quot; wx:key=&quot;&quot;&gt;</p><p> &lt;custom-parse item=&quot;{{item}}&quot; notes=&quot;{{notes}}&quot;&gt;&lt;/custom-parse&gt;</p><p> &lt;/block&gt;</p><p> &lt;/view&gt;</p><p> &lt;/block&gt;</p><p> &lt;block wx:elif=&quot;{{item.tag == &#39;br&#39;}}&quot;&gt;</p><p> &lt;custom-parse-br item=&quot;{{item}}&quot;&gt;&lt;/custom-parse-br&gt;</p><p> &lt;/block&gt;</p><p> &lt;!--其他块级标签--&gt;</p><p> &lt;block wx:elif=&quot;{{item.tagType == &#39;block&#39;}}&quot;&gt;</p><p> &lt;view class=&quot;{{item.classStr}} wxParse-{{item.tag}}&quot; style=&quot;{{item.styleStr}}&quot;&gt;</p><p> &lt;block wx:for=&quot;{{item.nodes}}&quot; wx:for-item=&quot;item&quot; wx:key=&quot;&quot;&gt;</p><p> &lt;custom-parse item=&quot;{{item}}&quot; notes=&quot;{{notes}}&quot;&gt;&lt;/custom-parse&gt;</p><p> &lt;/block&gt;</p><p> &lt;/view&gt;</p><p> &lt;/block&gt;</p><p> &lt;!--内联标签--&gt;</p><p> &lt;view wx:else class=&quot;{{item.classStr}} wxParse-{{item.tag}} wxParse-{{item.tagType}}&quot; style=&quot;{{item.styleStr}}&quot;&gt;</p><p> &lt;block wx:for=&quot;{{item.nodes}}&quot; wx:for-item=&quot;item&quot; wx:key=&quot;&quot;&gt;</p><p> &lt;custom-parse item=&quot;{{item}}&quot; notes=&quot;{{notes}}&quot;&gt;&lt;/custom-parse&gt;</p><p> &lt;/block&gt;</p><p> &lt;/view&gt;</p><p> &lt;/block&gt;</p><p> &lt;!--判断是否是文本节点--&gt;</p><p> &lt;block wx:elif=&quot;{{item.node == &#39;text&#39;}}&quot;&gt;</p><p> &lt;!--如果是,直接进行--&gt;</p><p> &lt;custom-emoji-view item=&quot;{{item}}&quot; bind:onMark=&quot;onMark&quot; notes=&quot;{{notes}}&quot;&gt;&lt;/custom-emoji-view&gt;</p><p> &lt;/block&gt;</p><p>完成之后,只需要在你需要的地方调用这个组件就可以了。</p><p>&lt;html-view id=&quot;htmlView&quot; wxParseData=&quot;{{nodes}}&quot;</p><p> notes=&quot;{{notes}}&quot;</p><p> bind:onMark=&quot;onMark&quot;</p><p> bind:unMark=&quot;unMark&quot;</p><p> bind:wxParseImgTap=&quot;wxParseImgTap&quot; bind:wxParseImgLoad=&quot;wxParseImgLoad&quot; &gt;&lt;/html-view&gt;</p><p>需要哪些方法,就可以像其他组件一样,往内部传递下去就行了。只需要在最内层设置一下冒泡和跨越组件边界就可以了</p><p>onMark: function (event) {</p><p> var myEventOption = { bubbles: true, composed:true} // 触发事件的选项</p><p> this.triggerEvent(&#39;onMark&#39;, event, myEventOption);</p><p> }</p><p>在Taro(bate3)中使用</p><p>因为开发中遇到了全局状态管理的的问题,第二版采用Taro重构。在这个功能上又遇到了问题,我试图用同样的思路来组件化。然后Taro目前的版本还没有解决组件嵌套自身的问题,大家可以看我箭头,这是在CustomParse组件中,在第二个箭头的位置,又引用了它自身。这种写法,会导致编译的时候循环引用。我猜想是编译转成小程序原生结构的时候,导致了死循环。所以这个方法就行不通了。</p><p><img src="/up_pic/201907/061008103056.png" title="061008103056.png" alt="2.png"/></p>
T:0.007340s,M:251.35 KB
返回顶部 留言