refactor: 确立独立子工程架构 (backend, admin, mpuniapp) 并同步更新 AGENTS.md 规范
这个提交包含在:
@@ -0,0 +1,78 @@
|
||||
// 多次截图 增加超时时间
|
||||
jest.setTimeout(60000)
|
||||
|
||||
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
|
||||
const isWeb = platformInfo.startsWith('web')
|
||||
const isDom2 = process.env.UNI_APP_X_DOM2 === "true"
|
||||
const isAndroid = platformInfo.startsWith('android')
|
||||
const isIos = platformInfo.startsWith('ios')
|
||||
const isHarmony = platformInfo.startsWith('harmony')
|
||||
const isApp = isAndroid || isIos || isHarmony
|
||||
const isMP = platformInfo.startsWith('mp')
|
||||
|
||||
describe('chat-stream-reply', () => {
|
||||
if (isMP) {
|
||||
it('not supported in mp', () => {
|
||||
expect(1).toBe(1)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
let page;
|
||||
|
||||
beforeAll(async () => {
|
||||
page = await program.reLaunch('/pages/template/chat-stream-reply/chat-stream-reply');
|
||||
await page.waitFor('view');
|
||||
});
|
||||
|
||||
async function takeScreenshot() {
|
||||
const image = await program.screenshot({ fullPage: true });
|
||||
expect(image).toSaveImageSnapshot();
|
||||
await page.waitFor(500);
|
||||
}
|
||||
|
||||
async function waitForData(dataPath) {
|
||||
await page.waitFor(async () => {
|
||||
return await page.data(dataPath) == true
|
||||
})
|
||||
}
|
||||
|
||||
it('ai-chat-initial-screenshot', async () => {
|
||||
await takeScreenshot();
|
||||
});
|
||||
|
||||
// dom2 暂不支持 textareaEl.input,临时跳过
|
||||
if(!isDom2){
|
||||
it('ai-chat-send-message', async () => {
|
||||
// 获取 textarea 元素
|
||||
const textareaEl = await page.$('.input-el');
|
||||
// 设置发送的文字
|
||||
await textareaEl.input('你好,这是一条测试消息');
|
||||
await page.waitFor(300);
|
||||
// 获取发送按钮并点击
|
||||
const sendBtn = await page.$('.send-msg');
|
||||
await sendBtn.tap();
|
||||
|
||||
await waitForData('autoTest.waitNode1');
|
||||
await takeScreenshot();
|
||||
|
||||
await waitForData('autoTest.waitNode2');
|
||||
await takeScreenshot();
|
||||
|
||||
await waitForData('autoTest.waitNode3');
|
||||
await takeScreenshot();
|
||||
|
||||
await waitForData('autoTest.waitNode4');
|
||||
await takeScreenshot();
|
||||
|
||||
await waitForData('autoTest.waitNode5');
|
||||
await takeScreenshot();
|
||||
|
||||
// 等待页面中消息流式返回完成
|
||||
await waitForData('autoTest.done');
|
||||
// 截图
|
||||
await takeScreenshot();
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
@@ -0,0 +1,561 @@
|
||||
<template>
|
||||
<view style="flex:1;background-color: #f5f5f5;">
|
||||
<scroll-view @click="hideKeyboard" @touchmove="onUserScroll" @touchend="onUserScrollEnd" :show-scrollbar="false" :scroll-with-animation="true" style="flex: 1;padding-bottom: 10px;" :bounces="false">
|
||||
<list-view ref="messageView" @scrolltoupper="clearData" @scrolltolower="loadData" :scroll-with-animation="true"
|
||||
:show-scrollbar="false" style="flex: 1;transform: scale(1, -1)" @scroll="onListScroll">
|
||||
<list-item v-for="(item, index) in messageList" @longpress="listItemLongPress(index)" :key="'listItem' + index"
|
||||
:id="'listItem' + item.id" style="transform: scale(1, -1);padding: 10px 12px;" type="0">
|
||||
<view class="item" :class="{self: item.self}">
|
||||
<image :fade-show="true" :src="item.avatar"
|
||||
:style="{width: avatarWidth + 'px',height: avatarWidth + 'px',borderRadius: avatarWidth + 'px'}"></image>
|
||||
<view :id="'listItemContent' + item.id" class="flex-row content" :class="{'self-content': item.self}">
|
||||
<view v-if="item.type == 'rich-text'" class="content-rich-wrap">
|
||||
<rich-text-msg :nodes="item.content" />
|
||||
</view>
|
||||
<text v-else class="content-text" :class="{'self-content-text': item.self}">
|
||||
{{ item.content }}
|
||||
</text>
|
||||
</view>
|
||||
<view style="flex: 1;">
|
||||
</view>
|
||||
</view>
|
||||
</list-item>
|
||||
<list-item class="loading">
|
||||
<loading v-if="load" style="border-color:#999;"></loading>
|
||||
</list-item>
|
||||
</list-view>
|
||||
</scroll-view>
|
||||
<view class="input-box">
|
||||
<textarea class="input-el" v-model="message" placeholder="发消息体验流式回复" :adjust-position="false"
|
||||
:hold-keyboard="true" :auto-height="true" @keyboardheightchange="onKeyboardheightchange"
|
||||
/>
|
||||
<text class="send-msg" :class="{active: message.length > 0}" @click="send">发送</text>
|
||||
<view class="placeholder-box">
|
||||
<view v-if="keyboardIsShow" :style="{height: maxKeyboardHeight}"></view>
|
||||
<!-- Android 端的键盘高度不包括底部安全区,其他包括 -->
|
||||
<view v-if="!keyboardIsShow || isAndroid" class="safe-area-inset-bottom"></view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
/**
|
||||
* @description: 支持发送消息后展示流式回复内容,并在聊天过程中切换纯文本和富文本消息的聊天页面。
|
||||
* @logic: 页面维护倒序消息列表,用户输入并发送消息后插入自己的消息,再通过模拟流式输出逐段更新机器人回复内容。
|
||||
* @optimization: 通过复用同一个回复消息项并逐步更新其内容,避免每次流式输出都新增整条消息,减少列表重排次数。
|
||||
*/
|
||||
import richTextMsg from './rich-text-msg.uvue'
|
||||
import type { TextNode, ElementNode, RichNode, RichNodes } from './types.uts'
|
||||
|
||||
type Nodes = RichNodes
|
||||
type MessageItem = {
|
||||
id : number;
|
||||
avatar : string;
|
||||
self : boolean;
|
||||
content : string | Nodes;
|
||||
type : string;
|
||||
height : number;
|
||||
}
|
||||
// 自动化测试使用
|
||||
type AutoTest = {
|
||||
done : boolean;
|
||||
waitNode1:boolean;
|
||||
waitNode2:boolean;
|
||||
waitNode3:boolean;
|
||||
waitNode4:boolean;
|
||||
waitNode5:boolean;
|
||||
}
|
||||
|
||||
const messageView = ref<UniElement | null>(null)
|
||||
const bottom = ref<UniElement | null>(null)
|
||||
const avatarWidth = ref(38)
|
||||
let scrollTop = 0
|
||||
const message = ref('')
|
||||
const showBottom = ref(false)
|
||||
const load = ref(false)
|
||||
const messageList = ref<MessageItem[]>([])
|
||||
const maxKeyboardHeight = ref(0)
|
||||
const keyboardIsShow = ref(false)
|
||||
const isAndroid = ref(uni.getSystemInfoSync().platform == 'android')
|
||||
const isUserScrolling = ref(false)
|
||||
let scrollEndTimer : number = -1
|
||||
// 自动化测试使用,在不同节点截图
|
||||
let autoTest = reactive<AutoTest>({
|
||||
done:false,
|
||||
waitNode1:false,
|
||||
waitNode2:false,
|
||||
waitNode3:false,
|
||||
waitNode4:false,
|
||||
waitNode5:false,
|
||||
})
|
||||
|
||||
const screenWidth = computed(() : number => {
|
||||
return 1080
|
||||
})
|
||||
const clearData = () => {
|
||||
if (messageList.value.length >= 40) {
|
||||
messageList.value.splice(39, messageList.value.length - 1)
|
||||
}
|
||||
}
|
||||
|
||||
const listItemLongPress = (index : number) => {
|
||||
uni.showToast({
|
||||
title: '触发长按:' + index,
|
||||
position: "bottom"
|
||||
})
|
||||
}
|
||||
|
||||
const loadData = () => {
|
||||
load.value = true
|
||||
setTimeout(() => {
|
||||
load.value = false
|
||||
let length = messageList.value.length
|
||||
for (let i = 0; i < 20; i++) {
|
||||
let item = {
|
||||
id: length + i,
|
||||
avatar: "/static/test-image/logo.png",
|
||||
type: 'text',
|
||||
self: true,
|
||||
content: '这是一些占位消息',
|
||||
height: 0
|
||||
} as MessageItem
|
||||
messageList.value.push(item)
|
||||
}
|
||||
}, 800)
|
||||
}
|
||||
const getMessage = () => {
|
||||
let idList : number[] = []
|
||||
for (let index = 0; index < 20; index++) {
|
||||
let item = {
|
||||
id: index,
|
||||
avatar: '/static/test-image/logo.png',
|
||||
type: 'text',
|
||||
self: false,
|
||||
content: `这是一条消息${index}`,
|
||||
height: 0
|
||||
} as MessageItem
|
||||
messageList.value.push(item)
|
||||
idList.push(item.id)
|
||||
}
|
||||
}
|
||||
function hideKeyboard() {
|
||||
uni.hideKeyboard()
|
||||
keyboardIsShow.value = false
|
||||
}
|
||||
|
||||
function sleep(ms : number) : Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => resolve(), ms)
|
||||
})
|
||||
}
|
||||
|
||||
function onUserScroll() {
|
||||
isUserScrolling.value = true
|
||||
if (scrollEndTimer >= 0) {
|
||||
clearTimeout(scrollEndTimer)
|
||||
scrollEndTimer = -1
|
||||
}
|
||||
}
|
||||
|
||||
function onUserScrollEnd() {
|
||||
if (scrollEndTimer >= 0) clearTimeout(scrollEndTimer)
|
||||
scrollEndTimer = setTimeout(() => {
|
||||
isUserScrolling.value = false
|
||||
scrollEndTimer = -1
|
||||
}, 600) as number
|
||||
}
|
||||
|
||||
function onListScroll() {
|
||||
// #ifdef WEB
|
||||
onUserScroll()
|
||||
onUserScrollEnd()
|
||||
// #endif
|
||||
}
|
||||
|
||||
async function waitUntilNotScrolling() {
|
||||
while (isUserScrolling.value) {
|
||||
await sleep(80)
|
||||
}
|
||||
}
|
||||
|
||||
/** demo:模拟“发送成功” */
|
||||
async function mockSendSuccess() : Promise<void> {
|
||||
await sleep(250)
|
||||
}
|
||||
|
||||
/** 列表项:text + 是否加粗;与 SegmentInput 统一用 bold 表示加粗 */
|
||||
type ListItemInput = { text : string; bold? : boolean }
|
||||
|
||||
async function demoStreamRichText() {
|
||||
// content 由 rootTail 组成,发送成功后只对 content 增加节点
|
||||
let rootTail : RichNode[] = [] as RichNode[]
|
||||
let content : Nodes = rootTail
|
||||
|
||||
let item = {
|
||||
id: Date.now(),
|
||||
avatar: '/static/test-image/logo.png',
|
||||
self: false,
|
||||
type: 'rich-text',
|
||||
content: content,
|
||||
height: 0
|
||||
} as MessageItem
|
||||
messageList.value.unshift(item)
|
||||
item = messageList.value[0]
|
||||
await mockSendSuccess()
|
||||
|
||||
function rebuildAndFlush() {
|
||||
content = [...rootTail] as Nodes
|
||||
item.content = content
|
||||
messageList.value = messageList.value.slice() as MessageItem[]
|
||||
}
|
||||
|
||||
/** 在 rootTail 末尾追加一个块级节点并逐字打出 text */
|
||||
async function typeBlock(tagName : string, text : string, charDelay = 60) {
|
||||
rootTail = [...rootTail, { name: tagName, children: [{ type: 'text', text: '' } as TextNode] as Nodes } as ElementNode] as Nodes
|
||||
rebuildAndFlush()
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
await waitUntilNotScrolling()
|
||||
const buf = text.substring(0, i + 1)
|
||||
rootTail = [...rootTail.slice(0, -1), { name: tagName, children: [{ type: 'text', text: buf } as TextNode] as Nodes } as ElementNode] as Nodes
|
||||
rebuildAndFlush()
|
||||
await sleep(charDelay)
|
||||
}
|
||||
}
|
||||
|
||||
/** 类型守卫:判断是否为 ElementNode */
|
||||
function isElementNode(node : RichNode) : boolean {
|
||||
if (typeof node === 'string') return false
|
||||
try {
|
||||
const en = node as ElementNode
|
||||
return en.name != null
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/** 段落内多段(纯文本 / strong / i / code / a),每段逐字打出;加粗与 ListItemInput 统一用 bold */
|
||||
type SegmentInput = { text : string; bold? : boolean; i? : boolean; code? : boolean; href? : string }
|
||||
async function typeBlockMixed(tagName : string, segments : SegmentInput[], charDelay = 60) {
|
||||
const emptyNodes : RichNode[] = [] as RichNode[]
|
||||
rootTail = [...rootTail, { name: tagName, children: emptyNodes } as ElementNode] as Nodes
|
||||
rebuildAndFlush()
|
||||
let children : RichNode[] = [] as RichNode[]
|
||||
for (let s = 0; s < segments.length; s++) {
|
||||
const seg = segments[s]
|
||||
for (let c = 0; c < seg.text.length; c++) {
|
||||
await waitUntilNotScrolling()
|
||||
const buf = seg.text.substring(0, c + 1)
|
||||
const tn = { type: 'text', text: buf } as TextNode
|
||||
const inner : Nodes = [tn] as Nodes
|
||||
let node : ElementNode | TextNode = tn
|
||||
if (seg.href != null) {
|
||||
const attrs = new Map<string, string>()
|
||||
attrs.set('href', seg.href!)
|
||||
node = { name: 'a', attrs: attrs, children: inner } as ElementNode
|
||||
} else if (seg.bold == true) {
|
||||
node = { name: 'strong', children: inner } as ElementNode
|
||||
} else if (seg.i == true) {
|
||||
node = { name: 'i', children: inner } as ElementNode
|
||||
} else if (seg.code == true) {
|
||||
node = { name: 'code', children: inner } as ElementNode
|
||||
}
|
||||
const segNodes : Nodes = [node] as Nodes
|
||||
rootTail = [...rootTail.slice(0, -1), { name: tagName, children: [...children, ...segNodes] as Nodes } as ElementNode] as Nodes
|
||||
rebuildAndFlush()
|
||||
await sleep(charDelay)
|
||||
}
|
||||
const fullTn = { type: 'text', text: seg.text } as TextNode
|
||||
const fullInner : Nodes = [fullTn] as Nodes
|
||||
let fullNode : RichNode = fullTn
|
||||
if (seg.href != null) {
|
||||
const attrs = new Map<string, string>()
|
||||
attrs.set('href', seg.href!)
|
||||
fullNode = { name: 'a', attrs: attrs, children: fullInner } as ElementNode
|
||||
} else if (seg.bold == true) {
|
||||
fullNode = { name: 'strong', children: fullInner } as ElementNode
|
||||
} else if (seg.i == true) {
|
||||
fullNode = { name: 'i', children: fullInner } as ElementNode
|
||||
} else if (seg.code == true) {
|
||||
fullNode = { name: 'code', children: fullInner } as ElementNode
|
||||
}
|
||||
children = [...children, fullNode] as Nodes
|
||||
rootTail = [...rootTail.slice(0, -1), { name: tagName, children: children } as ElementNode] as Nodes
|
||||
rebuildAndFlush()
|
||||
}
|
||||
}
|
||||
|
||||
// 列表项支持 { text, bold },逐字打出,bold 时包在 strong 里
|
||||
async function typeList(listName : string, items : ListItemInput[], charDelay = 60, itemDelay = 160) {
|
||||
const listIndex = rootTail.length
|
||||
let listChildren : RichNode[] = [] as RichNode[]
|
||||
rootTail = [...rootTail, { name: listName, children: listChildren } as ElementNode] as Nodes
|
||||
rebuildAndFlush()
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
await waitUntilNotScrolling()
|
||||
const it = items[i]
|
||||
const emptyChildren : RichNode[] = [] as RichNode[]
|
||||
let liNode : ElementNode = { name: 'li', children: emptyChildren }
|
||||
listChildren = [...listChildren, liNode] as Nodes
|
||||
rootTail = [
|
||||
...rootTail.slice(0, listIndex),
|
||||
{ name: listName, children: listChildren } as ElementNode,
|
||||
...rootTail.slice(listIndex + 1)
|
||||
] as Nodes
|
||||
rebuildAndFlush()
|
||||
|
||||
let buf = ''
|
||||
for (let c = 0; c < it.text.length; c++) {
|
||||
await waitUntilNotScrolling()
|
||||
buf = buf + it.text.substring(c, c + 1)
|
||||
const inner : Nodes = it.bold == true
|
||||
? [{ name: 'strong', children: [{ type: 'text', text: buf } as TextNode] as Nodes } as ElementNode] as Nodes
|
||||
: [{ type: 'text', text: buf } as TextNode] as Nodes
|
||||
liNode = { name: 'li', children: inner }
|
||||
listChildren = [...listChildren.slice(0, listChildren.length - 1), liNode] as Nodes
|
||||
rootTail = [
|
||||
...rootTail.slice(0, listIndex),
|
||||
{ name: listName, children: listChildren } as ElementNode,
|
||||
...rootTail.slice(listIndex + 1)
|
||||
] as Nodes
|
||||
rebuildAndFlush()
|
||||
await sleep(charDelay)
|
||||
}
|
||||
await sleep(itemDelay)
|
||||
}
|
||||
}
|
||||
|
||||
// 完整项目样例请参考 uni-ai x,末尾链接用 a 标签以在 rich-text-msg 中可点击,整段逐字出
|
||||
await typeBlockMixed('p', [
|
||||
{ text: '完整项目样例请参考 uni-ai x,地址:' }
|
||||
] as SegmentInput[], 60)
|
||||
// 自动化测试例使用
|
||||
autoTest.waitNode1 = true
|
||||
// 换行
|
||||
rootTail = [...rootTail, { name: 'br' } as ElementNode] as Nodes
|
||||
rebuildAndFlush()
|
||||
await sleep(180)
|
||||
|
||||
await typeBlockMixed('p', [
|
||||
{ text: 'https://ext.dcloud.net.cn/plugin?id=23902', href: 'https://ext.dcloud.net.cn/plugin?id=23902' }
|
||||
] as SegmentInput[], 60)
|
||||
rootTail = [...rootTail, { name: 'br' } as ElementNode] as Nodes
|
||||
rebuildAndFlush()
|
||||
await sleep(180)
|
||||
// 1)由简到繁:先不同大小的标题,逐字出
|
||||
await typeBlock('h1', '大标题', 60)
|
||||
rootTail = [...rootTail, { name: 'br' } as ElementNode] as Nodes
|
||||
rebuildAndFlush()
|
||||
await sleep(180)
|
||||
// 自动化测试例使用
|
||||
autoTest.waitNode2 = true
|
||||
|
||||
await typeBlock('h2', '中标题', 60)
|
||||
rootTail = [...rootTail, { name: 'br' } as ElementNode] as Nodes
|
||||
rebuildAndFlush()
|
||||
await sleep(180)
|
||||
await typeBlock('h3', '小标题', 60)
|
||||
rebuildAndFlush()
|
||||
|
||||
// 2)段落,逐字出
|
||||
await sleep(220)
|
||||
rootTail = [...rootTail, { name: 'br' } as ElementNode] as Nodes
|
||||
rebuildAndFlush()
|
||||
await sleep(180)
|
||||
await typeBlock('p', '这是段落文字。', 60)
|
||||
// 自动化测试例使用
|
||||
autoTest.waitNode3 = true
|
||||
|
||||
// 3)加粗斜体,逐字出
|
||||
await sleep(220)
|
||||
rootTail = [...rootTail, { name: 'br' } as ElementNode] as Nodes
|
||||
rebuildAndFlush()
|
||||
await sleep(180)
|
||||
await typeBlockMixed('p', [
|
||||
{ text: '这是 ' },
|
||||
{ text: '加粗', bold: true },
|
||||
{ text: ' 和 ' },
|
||||
{ text: '斜体', i: true },
|
||||
{ text: ' 的示例。' }
|
||||
] as SegmentInput[], 60)
|
||||
// 自动化测试例使用
|
||||
autoTest.waitNode4 = true
|
||||
|
||||
// 4)无序列表(其中一项加粗),标题逐字出
|
||||
await sleep(220)
|
||||
rootTail = [...rootTail, { name: 'br' } as ElementNode] as Nodes
|
||||
rebuildAndFlush()
|
||||
await sleep(180)
|
||||
await typeBlock('p', '无序列表:', 60)
|
||||
await sleep(180)
|
||||
await typeList('ul', [
|
||||
{ text: '苹果' },
|
||||
{ text: '香蕉(加粗)', bold: true },
|
||||
{ text: '橙子' }
|
||||
] as ListItemInput[], 70, 180)
|
||||
// 自动化测试例使用
|
||||
autoTest.waitNode5 = true
|
||||
|
||||
// 5)有序列表(其中一项加粗),标题逐字出
|
||||
await sleep(220)
|
||||
rootTail = [...rootTail, { name: 'br' } as ElementNode] as Nodes
|
||||
rebuildAndFlush()
|
||||
await sleep(180)
|
||||
await typeBlock('p', '有序列表:', 60)
|
||||
await sleep(180)
|
||||
await typeList('ol', [
|
||||
{ text: '第一步' },
|
||||
{ text: '第二步(加粗)', bold: true },
|
||||
{ text: '第三步' }
|
||||
] as ListItemInput[], 70, 180)
|
||||
|
||||
// 6)链接与行内代码、引用,全部逐字出
|
||||
await sleep(220)
|
||||
rootTail = [...rootTail, { name: 'br' } as ElementNode] as Nodes
|
||||
rebuildAndFlush()
|
||||
await sleep(180)
|
||||
await typeBlockMixed('p', [
|
||||
{ text: '链接:' },
|
||||
{ text: 'uni-app 官网', href: 'https://uniapp.dcloud.net.cn' }
|
||||
] as SegmentInput[], 60)
|
||||
await sleep(180)
|
||||
await typeBlockMixed('p', [
|
||||
{ text: '行内代码 ' },
|
||||
{ text: 'const x = 1', code: true },
|
||||
{ text: '。' }
|
||||
] as SegmentInput[], 60)
|
||||
await sleep(180)
|
||||
await typeBlock('blockquote', '这是一段引用。', 60)
|
||||
//仅供测试例使用
|
||||
autoTest.done = true
|
||||
}
|
||||
|
||||
function send() {
|
||||
const content = message.value.trim()
|
||||
if (content.length == 0) {
|
||||
return
|
||||
}
|
||||
let id = messageList.value.length
|
||||
let item = {
|
||||
id: id,
|
||||
avatar: '/static/test-image/logo.png',
|
||||
self: true,
|
||||
type: 'text',
|
||||
content: content,
|
||||
height: 0
|
||||
} as MessageItem
|
||||
messageList.value.unshift(item)
|
||||
message.value = ''
|
||||
setTimeout(() => {
|
||||
demoStreamRichText()
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
// setTimeout(() => {
|
||||
// demoStreamRichText()
|
||||
// }, 1000)
|
||||
|
||||
function onKeyboardheightchange(e: UniInputKeyboardHeightChangeEvent) {
|
||||
if (e.detail.height > maxKeyboardHeight.value) {
|
||||
maxKeyboardHeight.value = e.detail.height
|
||||
}
|
||||
keyboardIsShow.value = e.detail.height > 0
|
||||
}
|
||||
|
||||
onReady(() => {
|
||||
getMessage()
|
||||
})
|
||||
|
||||
defineExpose({autoTest})
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.flex-row {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
}
|
||||
|
||||
.item {
|
||||
flex-direction: row;
|
||||
}
|
||||
|
||||
.content {
|
||||
margin: 0 8px;
|
||||
max-width: 562rpx;
|
||||
min-width: 0;
|
||||
flex-shrink: 1;
|
||||
overflow: hidden;
|
||||
border-radius: 7px;
|
||||
padding: 9px 11px 9px 11px;
|
||||
align-items: center;
|
||||
background-color: #fff;
|
||||
}
|
||||
|
||||
.content-rich-wrap {
|
||||
max-width: 540rpx;
|
||||
min-width: 0;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.content-text {
|
||||
display: flex;
|
||||
font-size: 13px;
|
||||
line-height: 20px;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.loading {
|
||||
height: 30px;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
transform: scale(1, -1)
|
||||
}
|
||||
|
||||
.input-box {
|
||||
flex-direction: row;
|
||||
flex-wrap: wrap;
|
||||
padding: 10px;
|
||||
padding-bottom: 5px;
|
||||
border-top: 1px solid #e5e5e5;
|
||||
width: 100%;
|
||||
}
|
||||
.placeholder-box {
|
||||
width: 100%;
|
||||
}
|
||||
.safe-area-inset-bottom {
|
||||
height: env(safe-area-inset-bottom);
|
||||
}
|
||||
.input-el {
|
||||
flex: 1;
|
||||
height: 30px;
|
||||
padding: 5px 10px;
|
||||
background-color: #EEE;
|
||||
color: #333;
|
||||
}
|
||||
.send-msg {
|
||||
margin-left: 10px;
|
||||
font-size: 12px;
|
||||
border-radius: 4px;
|
||||
color: #999;
|
||||
margin-top: 2px;
|
||||
padding: 0 10px;
|
||||
height: 26px;
|
||||
line-height: 26px;
|
||||
border: 1px solid #eee;
|
||||
}
|
||||
.active {
|
||||
color: #fff;
|
||||
background-color: #3c99ff;
|
||||
border-color: #3c99ff;
|
||||
}
|
||||
.self {
|
||||
flex-direction: row-reverse;
|
||||
}
|
||||
.self-content {
|
||||
background-color: #3c99ff;
|
||||
}
|
||||
.self-content-text {
|
||||
color: #fff;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,319 @@
|
||||
<template>
|
||||
<view class="rich-text-msg-root">
|
||||
<template v-for="(node, i) in nodes" :key="i">
|
||||
<!-- 纯文本节点 type: 'text';鸿蒙不支持组合选择器,用 inlineClassName 单类 -->
|
||||
<text v-if="isTextNode(node)" :class="'rich-text-inline' + (inlineClassName.length > 0 ? ' ' + inlineClassName : '')">{{ getText(node) }}</text>
|
||||
<!-- 换行 -->
|
||||
<view v-else-if="isBr(node)" class="rich-text-br"></view>
|
||||
<!-- 无序列表 ul:每项前加 • -->
|
||||
<view v-else-if="isUl(node)" :style="getNodeStyle(node)" class="rich-text-ul">
|
||||
<view v-for="(li, liIdx) in getChildren(node)" :key="liIdx" class="rich-text-li">
|
||||
<text class="rich-text-li-marker">• </text>
|
||||
<rich-text-msg :nodes="getChildren(li)" :inline-class-name="inlineClassName" />
|
||||
</view>
|
||||
</view>
|
||||
<!-- 有序列表 ol:每项前加 1. 2. 3. ... -->
|
||||
<view v-else-if="isOl(node)" :style="getNodeStyle(node)" class="rich-text-ol">
|
||||
<view v-for="(li, liIdx) in getChildren(node)" :key="liIdx" class="rich-text-li">
|
||||
<text class="rich-text-li-marker">{{ liIdx + 1 }}. </text>
|
||||
<rich-text-msg :nodes="getChildren(li)" :inline-class-name="inlineClassName" />
|
||||
</view>
|
||||
</view>
|
||||
<!-- 块级标签:p, div, h1-h6, li, blockquote, pre 等;子节点用 getBlockInlineClass 单类 -->
|
||||
<view v-else-if="isBlockTag(node)" :style="getNodeStyle(node)" class="rich-text-block" :class="getBlockTagClass(node)">
|
||||
<rich-text-msg v-if="hasChildren(node)" :nodes="getChildren(node)" :inline-class-name="getBlockInlineClass(node)" />
|
||||
</view>
|
||||
<!-- 链接 a:单类 rich-text-link-inline -->
|
||||
<view v-else-if="isLink(node)" :style="getNodeStyle(node)" class="rich-text-inline-wrap rich-text-link"
|
||||
@click.stop="onLinkClick(node)">
|
||||
<rich-text-msg v-if="hasChildren(node)" :nodes="getChildren(node)" inline-class-name="rich-text-link-inline" />
|
||||
</view>
|
||||
<!-- 行内代码 code:单类 rich-text-code-inline -->
|
||||
<view v-else-if="isCode(node)" :style="getNodeStyle(node)" class="rich-text-inline-wrap rich-text-code">
|
||||
<rich-text-msg v-if="hasChildren(node)" :nodes="getChildren(node)" inline-class-name="rich-text-code-inline" />
|
||||
</view>
|
||||
<!-- 行内标签:span, strong, i 等;子节点用 getInlineWrapInlineClass 单类 -->
|
||||
<view v-else :style="getNodeStyle(node)" class="rich-text-inline-wrap" :class="getInlineTagClass(node)">
|
||||
<rich-text-msg v-if="hasChildren(node)" :nodes="getChildren(node)" :inline-class-name="getInlineWrapInlineClass(node)" />
|
||||
</view>
|
||||
</template>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
/**
|
||||
* @description: 作为聊天页的富文本渲染子组件,支持按节点类型递归渲染文本、标题、列表、链接和代码块。
|
||||
* @logic: 组件根据传入节点类型判断使用不同模板分支,并在需要时递归渲染子节点,同时统一处理链接点击和样式映射。
|
||||
* @optimization: 通过把节点判断和样式映射集中到当前组件内递归复用,避免在聊天页主逻辑中重复处理富文本节点结构。
|
||||
*/
|
||||
import type { TextNode, ElementNode, RichNode, RichNodes } from './types.uts'
|
||||
|
||||
const props = defineProps<{
|
||||
nodes : RichNodes
|
||||
/** 鸿蒙等端不支持组合选择器,由父传入作用于 .rich-text-inline 的单类名 */
|
||||
inlineClassName? : string
|
||||
}>()
|
||||
|
||||
const nodes = computed(() => props.nodes ?? [])
|
||||
const inlineClassName = computed(() => props.inlineClassName ?? '')
|
||||
|
||||
/** 类型守卫:判断是否为 TextNode */
|
||||
function isTextNodeType(node : RichNode) : boolean {
|
||||
if (typeof node === 'string') return false
|
||||
try {
|
||||
const tn = node as TextNode
|
||||
return tn.type == 'text' && (tn.text != null)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/** 类型守卫:判断是否为 ElementNode */
|
||||
function isElementNodeType(node : RichNode) : boolean {
|
||||
if (typeof node === 'string') return false
|
||||
try {
|
||||
const en = node as ElementNode
|
||||
return en.name != null
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
function isTextNode(node : RichNode) : boolean {
|
||||
if (typeof node === 'string') return true
|
||||
return isTextNodeType(node)
|
||||
}
|
||||
|
||||
function getText(node : RichNode) : string {
|
||||
if (typeof node === 'string') return node
|
||||
if (isTextNodeType(node)) {
|
||||
const tn = node as TextNode
|
||||
return tn.text ?? ''
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
function isBr(node : RichNode) : boolean {
|
||||
if (typeof node === 'string') return false
|
||||
if (isElementNodeType(node)) {
|
||||
const en = node as ElementNode
|
||||
return en.name == 'br'
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function getName(node : RichNode) : string {
|
||||
if (typeof node === 'string') return ''
|
||||
if (isTextNodeType(node)) return ''
|
||||
if (isElementNodeType(node)) {
|
||||
const en = node as ElementNode
|
||||
return en.name ?? ''
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
function isUl(node : RichNode) : boolean {
|
||||
return getName(node) == 'ul'
|
||||
}
|
||||
|
||||
function isOl(node : RichNode) : boolean {
|
||||
return getName(node) == 'ol'
|
||||
}
|
||||
|
||||
function isLink(node : RichNode) : boolean {
|
||||
return getName(node) == 'a'
|
||||
}
|
||||
|
||||
function isCode(node : RichNode) : boolean {
|
||||
return getName(node) == 'code'
|
||||
}
|
||||
|
||||
function getInlineTagClass(node : RichNode) : string {
|
||||
const name = getName(node)
|
||||
if (name == 'strong' || name == 'b') return 'rt-strong'
|
||||
if (name == 'i' || name == 'em') return 'rt-italic'
|
||||
if (name == 'u') return 'rt-underline'
|
||||
if (name == 'del' || name == 's' || name == 'strike') return 'rt-line-through'
|
||||
return ''
|
||||
}
|
||||
|
||||
function getHref(node : RichNode) : string {
|
||||
if (typeof node === 'string') return ''
|
||||
if (isElementNodeType(node)) {
|
||||
const en = node as ElementNode
|
||||
const attrs = en.attrs
|
||||
if (attrs == null) return ''
|
||||
return attrs.get('href') ?? ''
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
function onLinkClick(node : RichNode) {
|
||||
const href = getHref(node)
|
||||
if (href.length == 0) return
|
||||
uni.setClipboardData({
|
||||
data: href,
|
||||
success: () => {
|
||||
uni.showToast({ title: '复制成功', icon: 'none' })
|
||||
},
|
||||
fail: () => {
|
||||
uni.showToast({ title: '复制失败', icon: 'none' })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const blockTags = ['p', 'div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li', 'blockquote', 'pre']
|
||||
|
||||
function getBlockTagClass(node : RichNode) : string {
|
||||
const name = getName(node)
|
||||
if (name == 'h1') return 'rt-h1'
|
||||
if (name == 'h2') return 'rt-h2'
|
||||
if (name == 'h3') return 'rt-h3'
|
||||
if (name == 'h4') return 'rt-h4'
|
||||
if (name == 'h5') return 'rt-h5'
|
||||
if (name == 'h6') return 'rt-h6'
|
||||
if (name == 'blockquote') return 'rt-blockquote'
|
||||
return ''
|
||||
}
|
||||
|
||||
/** 块级子节点用的单类名,鸿蒙等端不支持组合选择器 */
|
||||
function getBlockInlineClass(node : RichNode) : string {
|
||||
const name = getName(node)
|
||||
if (name == 'h1') return 'rt-h1-inline'
|
||||
if (name == 'h2') return 'rt-h2-inline'
|
||||
if (name == 'h3') return 'rt-h3-inline'
|
||||
if (name == 'h4') return 'rt-h4-inline'
|
||||
if (name == 'h5') return 'rt-h5-inline'
|
||||
if (name == 'h6') return 'rt-h6-inline'
|
||||
if (name == 'blockquote') return 'rt-blockquote-inline'
|
||||
return ''
|
||||
}
|
||||
|
||||
/** 行内包裹子节点用的单类名 */
|
||||
function getInlineWrapInlineClass(node : RichNode) : string {
|
||||
const name = getName(node)
|
||||
if (name == 'strong' || name == 'b') return 'rt-strong-inline'
|
||||
if (name == 'i' || name == 'em') return 'rt-italic-inline'
|
||||
if (name == 'u') return 'rt-underline-inline'
|
||||
if (name == 'del' || name == 's' || name == 'strike') return 'rt-line-through-inline'
|
||||
return ''
|
||||
}
|
||||
|
||||
function isBlockTag(node : RichNode) : boolean {
|
||||
const name = getName(node)
|
||||
return blockTags.indexOf(name) >= 0
|
||||
}
|
||||
|
||||
function getNodeStyle(node : RichNode) : string {
|
||||
if (typeof node === 'string') return ''
|
||||
if (isElementNodeType(node)) {
|
||||
const en = node as ElementNode
|
||||
const attrs = en.attrs
|
||||
if (attrs == null) return ''
|
||||
return attrs.get('style') ?? ''
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
function hasChildren(node : RichNode) : boolean {
|
||||
if (typeof node === 'string') return false
|
||||
if (isElementNodeType(node)) {
|
||||
const en = node as ElementNode
|
||||
const children = en.children
|
||||
if (children == null) return false
|
||||
return children.length > 0
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function getChildren(node : RichNode) : RichNodes {
|
||||
const empty : RichNode[] = [] as RichNode[]
|
||||
if (typeof node === 'string') return empty
|
||||
if (isElementNodeType(node)) {
|
||||
const en = node as ElementNode
|
||||
const children = en.children
|
||||
return children ?? empty
|
||||
}
|
||||
return empty
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
/* 鸿蒙等端:max-width 仅支持 number|rpx;限制宽度防溢出 */
|
||||
.rich-text-msg-root {
|
||||
flex-direction: row;
|
||||
flex-wrap: wrap;
|
||||
align-items: flex-start;
|
||||
max-width: 540rpx;
|
||||
min-width: 0;
|
||||
overflow: hidden;
|
||||
}
|
||||
.rich-text-inline {
|
||||
font-size: 13px;
|
||||
color: #666;
|
||||
max-width: 540rpx;
|
||||
flex-shrink: 1;
|
||||
}
|
||||
.rich-text-br {
|
||||
width: 100%;
|
||||
height: 6px;
|
||||
}
|
||||
.rich-text-block {
|
||||
flex-direction: column;
|
||||
width: 100%;
|
||||
}
|
||||
.rt-h1-inline { font-size: 18px; font-weight: bold; color: #333;}
|
||||
.rt-h2-inline { font-size: 16px; font-weight: bold; color: #333; }
|
||||
.rt-h3-inline { font-size: 15px; font-weight: bold; color: #444; }
|
||||
.rt-h4-inline { font-size: 14px; font-weight: bold; color: #444; }
|
||||
.rt-h5-inline { font-size: 13px; font-weight: bold; color: #555; }
|
||||
.rt-h6-inline { font-size: 12px; font-weight: bold; color: #555; }
|
||||
.rt-blockquote {
|
||||
border-left: 4px solid #ccc;
|
||||
padding-left: 8px;
|
||||
margin: 4px 0;
|
||||
}
|
||||
.rt-blockquote-inline { color: #555; font-style: italic; }
|
||||
.rich-text-ul,
|
||||
.rich-text-ol {
|
||||
flex-direction: column;
|
||||
width: 100%;
|
||||
padding-left: 8px;
|
||||
}
|
||||
.rich-text-li {
|
||||
flex-direction: row;
|
||||
flex-wrap: wrap;
|
||||
margin-bottom: 2px;
|
||||
}
|
||||
.rich-text-li-marker {
|
||||
font-size: 13px;
|
||||
line-height: 20px;
|
||||
margin-right: 4px;
|
||||
color: #999;
|
||||
}
|
||||
.rich-text-inline-wrap {
|
||||
flex-direction: row;
|
||||
flex-wrap: wrap;
|
||||
flex-shrink: 1;
|
||||
max-width: 540rpx;
|
||||
min-width: 0;
|
||||
}
|
||||
.rich-text-link-inline {
|
||||
color: #007aff;
|
||||
}
|
||||
.rt-strong-inline { font-weight: bold; }
|
||||
.rt-italic-inline { font-style: italic; }
|
||||
/* 预留:部分端不支持时可在此补充 text-decoration */
|
||||
.rt-underline-inline { }
|
||||
.rt-line-through-inline { }
|
||||
.rich-text-code {
|
||||
background-color: #333;
|
||||
padding: 2px 6px;
|
||||
border-radius: 4px;
|
||||
flex-shrink: 1;
|
||||
margin-left: 4px;
|
||||
}
|
||||
.rich-text-code-inline {
|
||||
color: #e0e0e0;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,9 @@
|
||||
/** 富文本节点类型定义 - 供 chat-stream-reply.uvue 和 rich-text-msg.uvue 共享使用 */
|
||||
export type TextNode = { type : string; text : string }
|
||||
export type ElementNode = {
|
||||
name : string;
|
||||
attrs? : Map<string, string>;
|
||||
children? : RichNode[];
|
||||
}
|
||||
export type RichNode = string | TextNode | ElementNode
|
||||
export type RichNodes = RichNode[]
|
||||
在新工单中引用
屏蔽一个用户