富文本子组件 xHtmlNode
预览
介绍
x-html 在 Web / 小程序端的递归节点渲染器。只能作为 x-html 的内部子节点使用,不要单独引用或自行挂载。 App 端不使用本组件:x-html 在 App 走官方 rich-text;本组件仅在 Web / MP-WEIXIN 条件编译分支中由 x-html 引用并渲染。 负责把 htmlparser2 解析出的节点树递归渲染为 text / image / view,并向上抛出链接、图片点击。 示例与能力说明见同页 xHtml。
平台兼容
| Harmony | H5 | andriod | IOS | 小程序 | UTS | UNIAPP-X SDK | version |
|---|---|---|---|---|---|---|---|
| ☑ | ☑️ | 4.76+ | 1.1.18 |
文件路径
ts
@/uni_modules/tmx-ui/components/x-html-node/x-html-node.uvue使用
ts
<x-html-node></x-html-node>Props 属性
| 名称 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| node | 当前 HTML 解析节点 | HtmlNode | `` |
| parentStyle | 继承自父级的文本样式 | TextStyle | `` |
| depth | 递归深度,由 x-html 传入,一般从 0 开始 | number | 0 |
| userSelect | 文本是否可选,透传自 x-html | boolean | true |
| space | 连续空格显示方式:ensp / emsp / nbsp,透传自 x-html | string | '' |
Events 事件
| 名称 | 参数 | 说明 |
|---|---|---|
| linkclick | undefinedundefined | 链接点击 |
| imageclick | undefined | 图片点击 |
Slots 插槽
| 名称 | 说明 | 数据 |
|---|
Ref 方法
| 名称 | 参数 | 返回值 | 说明 |
|---|
示例文件路径
json
/pages/zhanshi/html示例源码
uvue
vue
<template>
<!-- #ifdef MP-WEIXIN || MP-ALIPAY -->
<page-meta :page-style="`background-color:${xThemeConfigBgColor}`">
<navigation-bar :background-color="xThemeConfigNavBgColor"
:front-color="xThemeConfigNavFontColor"></navigation-bar>
</page-meta>
<!-- #endif -->
<x-sheet>
<x-text font-size="18" class=" text-weight-b mb-8">富文本 HTML xHtml</x-text>
<x-text color="#999999">
App 端基于官方 rich-text,Web / 小程序走节点渲染;只支持 HTML,不解析 Markdown。
App 端使用官方 rich-text 原生渲染;Web/小程序通过条件编译使用 htmlparser2 解析节点,再由 x-html-node 递归渲染,支持链接和图片点击。
App 端支持的标签以官方 rich-text 为准,Web/小程序额外支持表格、pre、blockquote、ol 等。
</x-text>
</x-sheet>
<x-sheet>
<x-html :value="html" @itemClick="onItemClick"></x-html>
<x-button class="mt-20" @click="appendHtml" :block="true">追加一段 HTML</x-button>
</x-sheet>
<!-- #ifdef WEB || MP-WEIXIN || MP-ALIPAY -->
<x-sheet>
<x-text font-size="16" class="text-weight-b mb-8">Web / 小程序扩展标签</x-text>
<x-text color="#999999" class="mb-12">
App 端以官方 rich-text 支持范围为准;Web/小程序节点渲染额外支持表格、引用、代码块和有序列表。
</x-text>
<x-html :value="richHtml" @itemClick="onItemClick"></x-html>
</x-sheet>
<!-- #endif -->
</template>
<script setup lang="ts">
import { ref } from "vue"
const html = ref<string>(`<h1>富文本 HTML</h1>
<p style="line-height:1.8;">
这是 <strong>加粗</strong>、<i>斜体</i>、<u>下划线</u>、<del>删除线</del> 和
<span style="color:#ff6600;">彩色文字</span> 的示例。
<br/>第二行还可以放 <big>大号文字</big> 和 <small>小号文字</small>。
</p>
<h2>列表</h2>
<ul>
<li>第一项</li>
<li>第二项</li>
<li>第三项</li>
</ul>
<h2>链接和图片</h2>
<p>
<a href="https://xui.tmui.design">点击打开 TMUI 官网</a>
</p>
<p>
<img src="https://xui.tmui.design/assets/logo-a483vlZl.png" style="width:120px;height:auto;" />
</p>
顶层行内混排(没有 p 包裹):<strong>加粗</strong>、<a href="https://xui.tmui.design">链接</a>和普通文字应该保持在同一行。
`)
// #ifdef WEB || MP-WEIXIN || MP-ALIPAY
const richHtml = ref<string>(`<h2>表格</h2>
<table>
<caption>组件对照</caption>
<thead>
<tr><th>组件</th><th>说明</th></tr>
</thead>
<tbody>
<tr><td>x-html</td><td>HTML 渲染入口</td></tr>
<tr><td>x-html-node</td><td>内部递归节点渲染器,不单独使用</td></tr>
</tbody>
</table>
<h2>引用和代码</h2>
<blockquote>Web / 小程序节点渲染支持 blockquote。</blockquote>
<pre><code>const html = "<strong>hello</strong>";
console.log(html);</code></pre>
<h2>有序列表</h2>
<ol start="3">
<li>第三项</li>
<li>第四项</li>
</ol>
`)
// #endif
function onItemClick(detail : Record<string, any>) : void {
const href = detail['href'] as string
const src = detail['src'] as string
let text = ''
if (href != null && href != '') {
text = href
} else if (src != null && src != '') {
text = src
}
if (text == '') return
uni.showToast({
title: text,
icon: 'none'
})
}
function appendHtml() : void {
html.value = html.value + `<p style="line-height:1.8;">
追加的一段 HTML:<strong>动态更新</strong>、<span style="color:#007aff;">蓝色文字</span>。
</p>`
}
</script>
<style>
</style>