rich-text.uvue 3.2 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1 2
<template>
  <view>
W
微调  
wanganxp 已提交
3
    <page-head title="rich-text"></page-head>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
4 5
    <view class="uni-padding-wrap uni-common-mt">
      <view class="uni-btn-v">
W
微调  
wanganxp 已提交
6 7 8
        <navigator url="/pages/component/rich-text/rich-text-tags">
          <button type="primary">rich-text渲染单个HTML标签示例</button>
        </navigator>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
9 10
      </view>
      <view class="uni-btn-v">
W
微调  
wanganxp 已提交
11
        <navigator url="/pages/component/rich-text/rich-text-complex">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
12
          <button type="primary">rich-text渲染复杂HTML示例</button>
W
微调  
wanganxp 已提交
13
        </navigator>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
14
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
15 16
      <view class="uni-title">
        <text class="uni-subtitle-text">selectable</text>
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
17
        <button type="default" @click="changeText">修改文本内容</button>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
18
      </view>
19
      <view class="text-box" id="rich-text-parent" @click="richTextParentClick">
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
20
        <rich-text id="richtext" style="border: 1px; border-style: solid; border-color: red;" :selectable="true"
21
          :nodes="text"></rich-text>
22 23 24 25
        <view>
          <text>rich-text-parent</text>
          <text id='rich-text-str'>{{richTextStr}}</text>
        </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
26
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
27 28
    </view>
  </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
29
  <rich-text v-if="autoTest" id="test-rich-text" :nodes="testNodes" :selectable="true" @itemclick="itemClickForTest" style="position: fixed;width: 100px;height: 100px;"></rich-text>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
30 31 32
</template>

<script>
33 34 35 36 37
  export default {
    data() {
      return {
        text: "<span>hello uni-app x!</span><br/><span>uni-app x,终极跨平台方案</span>",
        richTextHeight: 0,
38 39 40 41
        richTextElement: null as UniElement | null,
        // 自动化测试
        autoTest: false,
        testNodes: '<img src="https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni@2x.png"></img>',
42 43
        isItemClickTrigger: false,
        richTextStr: false
44 45 46 47 48 49
      }
    },
    onReady() {
      this.richTextElement = uni.getElementById('richtext') as UniElement
      setTimeout(() => {
        this.updateRichTextHeight()
50
      }, 1200)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    },
    methods: {
      changeText() {
        if (this.text === "<span>hello uni-app x!</span><br/><span>uni-app x,终极跨平台方案</span>") {
          this.text = "<h1>hello uni-app x!</h1><br/><h2>uni-app x,终极跨平台方案</h2>"
        } else {
          this.text = "<span>hello uni-app x!</span><br/><span>uni-app x,终极跨平台方案</span>"
        }
        setTimeout(() => {
          this.updateRichTextHeight()
        }, 200)
      },
      updateRichTextHeight() {
        if (this.richTextElement != null) {
          const elRect = this.richTextElement!.getBoundingClientRect()
          this.richTextHeight = elRect.height
          console.log('richTextHeight:', this.richTextHeight)
        }
69 70 71 72 73 74 75 76 77 78
      },
      // 自动化测试
      itemClickForTest(_ : RichTextItemClickEvent) {
        this.isItemClickTrigger = true;
      },
      getBoundingClientRectForTest() : DOMRect {
        return uni.getElementById('test-rich-text')?.getBoundingClientRect()!;
      },
      getWindowInfoForTest() : GetWindowInfoResult {
        return uni.getWindowInfo();
79 80 81
      },
      richTextParentClick() {
        this.richTextStr = true;
82 83 84
      }
    }
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
85 86 87
</script>

<style>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
88
  .text-box {
H
hdx 已提交
89
    padding: 20px 0;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
90 91
    background-color: white;
  }
H
hdx 已提交
92
</style>