Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-app
提交
7dc4d548
U
uni-app
项目概览
DCloud
/
uni-app
2 个月 前同步成功
通知
715
Star
38705
Fork
3642
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
7
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
7
Issue
7
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
7dc4d548
编写于
8月 08, 2019
作者:
L
liguanpingdev
提交者:
GitHub
8月 08, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Create rich-text.js
上级
12b59d5c
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
180 addition
and
0 deletion
+180
-0
src/platforms/app-plus-nvue/runtime/components/rich-text.js
src/platforms/app-plus-nvue/runtime/components/rich-text.js
+180
-0
未找到文件。
src/platforms/app-plus-nvue/runtime/components/rich-text.js
0 → 100644
浏览文件 @
7dc4d548
import
{
cached
,
normalizeUnit
}
from
'
../helpers/index
'
const
parseStyleText
=
cached
(
function
(
cssText
)
{
const
res
=
{}
const
listDelimiter
=
/;
(?![^
(
]
*
\))
/g
const
propertyDelimiter
=
/:
(
.+
)
/
cssText
.
split
(
listDelimiter
).
forEach
(
function
(
item
)
{
if
(
item
)
{
const
tmp
=
item
.
split
(
propertyDelimiter
)
tmp
.
length
>
1
&&
(
res
[
tmp
[
0
].
trim
()]
=
tmp
[
1
].
trim
())
}
})
return
res
})
function
parseStyle
(
node
,
ctx
)
{
const
styles
=
Object
.
create
(
null
)
if
(
!
node
.
attrs
)
{
return
styles
}
const
classList
=
(
node
.
attrs
.
class
||
''
).
split
(
'
'
)
const
cssMap
=
ctx
.
$options
.
style
||
{}
classList
.
forEach
(
name
=>
{
if
(
name
&&
cssMap
[
name
])
{
Object
.
assign
(
styles
,
cssMap
[
name
])
}
})
Object
.
assign
(
styles
,
parseStyleText
(
node
.
attrs
.
style
||
''
))
return
styles
}
const
TAGS
=
[
'
span
'
,
'
a
'
,
'
image
'
,
'
img
'
]
function
block
(
node
)
{
node
.
attr
.
value
=
(
node
.
attr
.
value
||
''
)
+
'
\n
'
return
node
}
function
heading
(
node
,
em
,
options
)
{
!
node
.
style
.
fontSize
&&
(
node
.
style
.
fontSize
=
options
.
defaultFontSize
*
em
)
return
block
(
bold
(
node
))
}
function
createHeading
(
em
)
{
return
function
(
node
,
options
)
{
return
heading
(
node
,
em
,
options
)
}
}
function
bold
(
node
)
{
!
node
.
style
.
fontWeight
&&
(
node
.
style
.
fontWeight
=
'
bold
'
)
return
node
}
const
strategies
=
{
'
blockquote
'
:
block
,
'
br
'
:
block
,
'
div
'
:
block
,
'
dl
'
:
block
,
'
h1
'
:
createHeading
(
2
),
'
h2
'
:
createHeading
(
1.5
),
'
h3
'
:
createHeading
(
1.17
),
'
h4
'
:
createHeading
(
1
),
'
h5
'
:
createHeading
(
0.83
),
'
h6
'
:
createHeading
(
0.67
),
'
hr
'
:
block
,
'
ol
'
:
block
,
'
p
'
:
block
,
'
strong
'
:
bold
,
'
table
'
:
block
,
'
tbody
'
:
block
,
'
tfoot
'
:
block
,
'
thead
'
:
block
,
'
ul
'
:
block
}
const
HTML_RE
=
/&
(
amp|gt|lt|nbsp|quot|apos
)
;/g
const
CHARS
=
{
'
amp
'
:
'
&
'
,
'
gt
'
:
'
>
'
,
'
lt
'
:
'
<
'
,
'
nbsp
'
:
'
'
,
'
quot
'
:
'
"
'
,
'
apos
'
:
"
'
"
}
function
normalizeText
(
str
)
{
return
str
.
replace
(
HTML_RE
,
function
(
match
,
entity
)
{
return
CHARS
[
entity
]
})
}
function
normalizeNode
(
node
,
ctx
,
options
)
{
let
type
=
(
node
.
name
||
''
).
toLowerCase
()
const
strategy
=
strategies
[
type
]
if
(
TAGS
.
indexOf
(
type
)
===
-
1
)
{
type
=
'
span
'
}
if
(
type
===
'
img
'
)
{
type
=
'
image
'
}
const
nvueNode
=
{
type
,
attr
:
Object
.
create
(
null
)
}
if
(
node
.
type
===
'
text
'
||
node
.
text
)
{
nvueNode
.
attr
.
value
=
normalizeText
((
node
.
text
||
''
).
trim
())
}
if
(
node
.
attrs
)
{
Object
.
keys
(
node
.
attrs
).
forEach
(
name
=>
{
if
(
name
!==
'
class
'
&&
name
!==
'
style
'
)
{
nvueNode
.
attr
[
name
]
=
node
.
attrs
[
name
]
}
})
}
nvueNode
.
style
=
normalizeUnit
(
parseStyle
(
node
,
ctx
))
if
(
strategy
)
{
strategy
(
nvueNode
,
options
)
}
nvueNode
.
children
=
normalizeNodes
(
node
.
children
,
ctx
,
options
)
return
nvueNode
}
function
normalizeNodes
(
nodes
,
ctx
,
options
)
{
if
(
Array
.
isArray
(
nodes
))
{
return
nodes
.
map
(
node
=>
normalizeNode
(
node
,
ctx
,
options
))
}
return
[]
}
function
getRichText
(
weex
)
{
const
{
scale
,
deviceWidth
}
=
weex
.
config
.
env
const
defaultFontSize
=
deviceWidth
/
scale
/
20
return
{
props
:
{
nodes
:
{
type
:
[
Array
,
String
],
default
:
function
()
{
return
[]
}
}
},
render
(
createElement
)
{
// TODO 待处理 String 类型
const
nodes
=
normalizeNodes
(
this
.
nodes
||
[],
this
.
$vnode
.
context
,
{
defaultFontSize
})
return
createElement
(
'
u-rich-text
'
,
this
.
_g
({
attrs
:
{
value
:
nodes
}
},
this
.
$listeners
))
}
}
}
export
default
function
init
(
Vue
,
weex
)
{
Vue
.
component
(
'
rich-text
'
,
getRichText
(
weex
))
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录