rich-text.uvue 3.4 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<template>
  <view>
    <page-head title="rich-text"></page-head>
    <view class="uni-padding-wrap uni-common-mt">
      <view class="uni-btn-v">
        <navigator url="/pages/component/rich-text/rich-text-tags">
          <button type="primary">rich-text渲染单个HTML标签示例</button>
        </navigator>
      </view>
      <view class="uni-btn-v">
        <navigator url="/pages/component/rich-text/rich-text-complex">
          <button type="primary">rich-text渲染复杂HTML示例</button>
        </navigator>
      </view>
      <view class="uni-title">
        <text class="uni-subtitle-text">selectable</text>
        <button type="default" @click="changeText">修改文本内容</button>
      </view>
19
      <view class="text-box" id="rich-text-parent" @click="richTextParentClick">
DCloud-WZF's avatar
DCloud-WZF 已提交
20 21
        <rich-text id="richtext" style="border: 1px; border-style: solid; border-color: red;" :selectable="true"
          :nodes="text"></rich-text>
22 23 24 25
        <view>
          <text>rich-text-parent</text>
          <text id='rich-text-str'>{{richTextStr}}</text>
        </view>
26 27 28 29
      </view>
      <view class="text-box2">
        <rich-text style="height: 80px;" :selectable="true"
          :nodes="text"></rich-text>
DCloud-WZF's avatar
DCloud-WZF 已提交
30 31
      </view>
    </view>
32
  </view>
33
  <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-WZF's avatar
DCloud-WZF 已提交
34 35 36 37 38 39 40 41
</template>

<script>
  export default {
    data() {
      return {
        text: "<span>hello uni-app x!</span><br/><span>uni-app x,终极跨平台方案</span>",
        richTextHeight: 0,
42 43 44 45 46 47
        richTextElement: null as UniElement | null,
        // 自动化测试
        autoTest: false,
        testNodes: '<img src="https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni@2x.png"></img>',
        isItemClickTrigger: false,
        richTextStr: false
DCloud-WZF's avatar
DCloud-WZF 已提交
48 49 50 51 52 53
      }
    },
    onReady() {
      this.richTextElement = uni.getElementById('richtext') as UniElement
      setTimeout(() => {
        this.updateRichTextHeight()
54
      }, 2500)
DCloud-WZF's avatar
DCloud-WZF 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    },
    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)
        }
73 74 75 76 77 78 79 80 81 82 83 84 85
      },
      // 自动化测试
      itemClickForTest(_ : RichTextItemClickEvent) {
        this.isItemClickTrigger = true;
      },
      getBoundingClientRectForTest() : DOMRect {
        return uni.getElementById('test-rich-text')?.getBoundingClientRect()!;
      },
      getWindowInfoForTest() : GetWindowInfoResult {
        return uni.getWindowInfo();
      },
      richTextParentClick() {
        this.richTextStr = true;
DCloud-WZF's avatar
DCloud-WZF 已提交
86 87 88 89 90 91 92 93 94
      }
    }
  }
</script>

<style>
  .text-box {
    padding: 20px 0;
    background-color: white;
95 96 97 98 99 100 101
  }

  .text-box2 {
    top: 20px;
    background-color: white;
  }

102
</style>