Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
qianlong66
uni-app
提交
5a65b4bd
U
uni-app
项目概览
qianlong66
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5a65b4bd
编写于
5月 31, 2022
作者:
D
DCloud_LXH
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(h5): rich-text ssr
上级
1ec26b08
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
66 addition
and
17 deletion
+66
-17
packages/uni-components/src/vue/rich-text/index.tsx
packages/uni-components/src/vue/rich-text/index.tsx
+57
-12
packages/uni-components/src/vue/rich-text/nodes-parser.js
packages/uni-components/src/vue/rich-text/nodes-parser.js
+9
-5
未找到文件。
packages/uni-components/src/vue/rich-text/index.tsx
浏览文件 @
5a65b4bd
import
{
onMounted
,
ref
,
watch
,
getCurrentInstance
}
from
'
vue
'
import
{
onMounted
,
ref
,
watch
,
getCurrentInstance
,
h
,
VNode
}
from
'
vue
'
import
{
hasOwn
}
from
'
@vue/shared
'
import
{
import
{
defineBuiltInComponent
,
defineBuiltInComponent
,
useCustomEvent
,
useCustomEvent
,
EmitEvent
,
EmitEvent
,
}
from
'
@dcloudio/uni-components
'
}
from
'
@dcloudio/uni-components
'
import
parseNodes
from
'
./nodes-parser
'
import
parseNodes
,
{
TAGS
,
decodeEntities
}
from
'
./nodes-parser
'
import
{
props
,
parseHtml
}
from
'
../../components/rich-text
'
import
{
props
,
parseHtml
}
from
'
../../components/rich-text
'
import
{
ssrRef
}
from
'
@dcloudio/uni-app
'
interface
Node
{
type
:
string
text
?:
string
name
:
string
attrs
:
Object
children
:
Node
[]
}
function
_createVNode
(
nodeList
:
Node
[]):
Array
<
VNode
|
undefined
>
{
if
(
!
nodeList
)
return
[]
return
nodeList
.
map
((
node
)
=>
{
if
(
node
.
name
)
{
const
tagName
=
node
.
name
.
toLowerCase
()
if
(
!
hasOwn
(
TAGS
,
tagName
))
{
return
}
}
const
isNode
=
!
hasOwn
(
node
,
'
type
'
)
||
node
.
type
===
'
node
'
return
h
(
isNode
?
node
.
name
:
'
span
'
,
node
.
attrs
,
isNode
?
_createVNode
(
node
.
children
)
:
decodeEntities
(
node
.
text
)
)
})
}
export
default
/*#__PURE__*/
defineBuiltInComponent
({
export
default
/*#__PURE__*/
defineBuiltInComponent
({
name
:
'
RichText
'
,
name
:
'
RichText
'
,
...
@@ -23,7 +52,9 @@ export default /*#__PURE__*/ defineBuiltInComponent({
...
@@ -23,7 +52,9 @@ export default /*#__PURE__*/ defineBuiltInComponent({
],
],
setup
(
props
,
{
emit
,
attrs
})
{
setup
(
props
,
{
emit
,
attrs
})
{
const
vm
=
getCurrentInstance
()
const
vm
=
getCurrentInstance
()
const
scopeId
=
(
vm
&&
vm
.
vnode
.
scopeId
)
||
''
const
rootRef
=
ref
<
HTMLElement
|
null
>
(
null
)
const
rootRef
=
ref
<
HTMLElement
|
null
>
(
null
)
const
nodelist
=
ssrRef
(
props
.
nodes
,
'
nodelist
'
)
const
trigger
=
useCustomEvent
<
EmitEvent
<
typeof
emit
>>
(
rootRef
,
emit
)
const
trigger
=
useCustomEvent
<
EmitEvent
<
typeof
emit
>>
(
rootRef
,
emit
)
const
hasItemClick
=
!!
attrs
.
onItemclick
const
hasItemClick
=
!!
attrs
.
onItemclick
...
@@ -31,14 +62,21 @@ export default /*#__PURE__*/ defineBuiltInComponent({
...
@@ -31,14 +62,21 @@ export default /*#__PURE__*/ defineBuiltInComponent({
trigger
(
'
itemclick
'
,
e
,
detail
)
trigger
(
'
itemclick
'
,
e
,
detail
)
}
}
// ssr 处理
if
(
__NODE_JS__
)
{
if
(
typeof
props
.
nodes
===
'
string
'
)
{
nodelist
.
value
=
parseHtml
(
props
.
nodes
)
}
}
function
_renderNodes
(
nodes
:
string
|
unknown
[])
{
function
_renderNodes
(
nodes
:
string
|
unknown
[])
{
if
(
typeof
nodes
===
'
string
'
)
{
if
(
typeof
nodes
===
'
string
'
)
{
node
s
=
parseHtml
(
nodes
)
node
list
.
value
=
parseHtml
(
nodes
)
}
}
const
nodeList
=
parseNodes
(
const
nodeList
=
parseNodes
(
node
s
,
node
list
.
value
,
document
.
createDocumentFragment
(),
document
.
createDocumentFragment
(),
(
vm
&&
vm
.
vnode
.
scopeId
)
||
''
,
scopeId
,
hasItemClick
&&
triggerItemClick
hasItemClick
&&
triggerItemClick
)
)
rootRef
.
value
!
.
firstElementChild
!
.
innerHTML
=
''
rootRef
.
value
!
.
firstElementChild
!
.
innerHTML
=
''
...
@@ -53,15 +91,22 @@ export default /*#__PURE__*/ defineBuiltInComponent({
...
@@ -53,15 +91,22 @@ export default /*#__PURE__*/ defineBuiltInComponent({
)
)
onMounted
(()
=>
{
onMounted
(()
=>
{
_renderNodes
(
props
.
nodes
)
_renderNodes
(
nodelist
.
value
as
[]
)
})
})
return
()
=>
{
return
()
=>
return
(
h
(
<
uni
-
rich
-
text
ref
=
{
rootRef
}
>
'
uni-rich-text
'
,
<
div
/>
{
</
uni
-
rich
-
text
>
ref
:
rootRef
,
},
[
h
(
'
div
'
,
{},
__NODE_JS__
?
_createVNode
(
nodelist
.
value
as
Node
[])
:
[]
),
]
)
)
}
},
},
})
})
packages/uni-components/src/vue/rich-text/nodes-parser.js
浏览文件 @
5a65b4bd
import
{
hasOwn
,
isPlainObject
}
from
'
@vue/shared
'
import
{
hasOwn
,
isPlainObject
}
from
'
@vue/shared
'
import
{
getRealPath
}
from
'
@dcloudio/uni-platform
'
import
{
getRealPath
}
from
'
@dcloudio/uni-platform
'
const
TAGS
=
{
export
const
TAGS
=
{
a
:
''
,
a
:
''
,
abbr
:
''
,
abbr
:
''
,
address
:
''
,
address
:
''
,
...
@@ -77,7 +77,7 @@ const CHARS = {
...
@@ -77,7 +77,7 @@ const CHARS = {
apos
:
"
'
"
,
apos
:
"
'
"
,
}
}
function
decodeEntities
(
htmlString
)
{
export
function
decodeEntities
(
htmlString
)
{
return
htmlString
.
replace
(
return
htmlString
.
replace
(
/&
(([
a-zA-Z
]
+
)
|
(
#x
{0,1}[\d
a-zA-Z
]
+
))
;/gi
,
/&
(([
a-zA-Z
]
+
)
|
(
#x
{0,1}[\d
a-zA-Z
]
+
))
;/gi
,
function
(
match
,
stage
)
{
function
(
match
,
stage
)
{
...
@@ -90,9 +90,13 @@ function decodeEntities(htmlString) {
...
@@ -90,9 +90,13 @@ function decodeEntities(htmlString) {
if
(
/^#x
[
0-9a-f
]{1,4}
$/i
.
test
(
stage
))
{
if
(
/^#x
[
0-9a-f
]{1,4}
$/i
.
test
(
stage
))
{
return
String
.
fromCharCode
(
'
0
'
+
stage
.
slice
(
1
))
return
String
.
fromCharCode
(
'
0
'
+
stage
.
slice
(
1
))
}
}
if
(
!
__NODE_JS__
)
{
const
wrap
=
document
.
createElement
(
'
div
'
)
const
wrap
=
document
.
createElement
(
'
div
'
)
wrap
.
innerHTML
=
match
wrap
.
innerHTML
=
match
return
wrap
.
innerText
||
wrap
.
textContent
return
wrap
.
innerText
||
wrap
.
textContent
}
else
{
return
match
}
}
}
)
)
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录