markdown 的代码块,q请求加个复制按钮
我是改了大佬源码实现了,求指点
在html2xml.wxml 113行添加
<view class="copyMdBox" wx:if="{{item.tag == 'pre'}}"> <view class="copyMd copyMd-title">code</view> <view class="copyMd"> <text bind:tap="copyMdCode" data-copy="{{item.nodes}}" wx:if="{{!textCopy}}">点击复制</text> <icon wx:else class="icon-small" type="success_no_circle" size="23"></icon> </view> </view>
html2xml.js
copyMdCode(e) { console.log(e); let that = this; let dt = e.currentTarget.dataset.copy; let res = this.getText(dt); wx.setClipboardData({ data: res, success: function (res) { that.setData({ textCopy: "点击复制" }); } }) }, getText(data) { let text = ''; for (let i = 0; i < data.length; i++) { if (data[i].text) { text += data[i].text; } if (data[i].nodes && data[i].nodes.length > 0) { text += this.getText(data[i].nodes); } } return text; }, },
效果展示