-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from ecomfe/feature/html
Feature/html
- Loading branch information
Showing
20 changed files
with
354 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# v-html | ||
|
||
> `okam` 从 `okam-core: 0.4.22` 版本开始支持 | ||
默认不开启,需在配置项中,配置 `v-html` 支持 | ||
|
||
|
||
``` base.config.js | ||
{ | ||
framework: [ | ||
// ... | ||
'vhtml' | ||
// ... | ||
] | ||
} | ||
``` | ||
|
||
## 支持项 | ||
|
||
|平台|百度|微信|支付宝|头条|快应用| | ||
|---|---|---|---|---| | ||
|---|√|√|√|√|x| | ||
|---|nodes/string|nodes/string|nodes|nodes/string|x| | ||
|
||
> 1.指令为 `v-html`,而不是像基础指令一样为 `html`,这样是以防止与原生或用户属性冲突;<br> | ||
2.想要 让所有指令保证统一 推荐开启 `v-` 指令支持, [详见](template/vueSyntax.md); | ||
|
||
|
||
## v-html 实现原理 | ||
|
||
原生 `rich-text` 的封装,`v-html` 是其的一个语法糖,其对应的取值依赖原生, 因此对于的标签、属性及事件限制都与原生对齐 | ||
|
||
!> 原生 `rich-text` 受限的 `v-html` 同样受限 | ||
!> 支付宝 `rich-text` 不支持 `string` 类型 | ||
!> nodes 不推荐使用 String 类型,性能会有所下降 | ||
!> 其他更多限制请查看 原生 `rich-text`限制 | ||
|
||
## 多平台 v-html 使用 | ||
* 第一步:在 `base.config.js` 配置了 `vhtml` 支持; | ||
|
||
``` base.config.js | ||
{ | ||
framework: [ | ||
// ... | ||
'vhtml' | ||
// ... | ||
] | ||
} | ||
``` | ||
|
||
* 第二步:如果需要支持支付宝平台, `npm install mini-html-parser2 --save` | ||
``` ant.config.js | ||
{ | ||
api: { | ||
htmlToNodes: 'mini-html-parser2' | ||
} | ||
} | ||
``` | ||
|
||
* 第三步:多平台支持 | ||
|
||
``` pages/index.vue | ||
<view class="sub-title">v-html 多平台 arrayData 支持</view> | ||
<div v-html="arrayNodes"></div> | ||
<view class="sub-title">v-html 多平台 string 支持</view> | ||
<div v-html="stringNodes"></div> | ||
let str = '<div class="desc">div 标签</div>'; | ||
let arr = [{ | ||
name: 'div', | ||
attrs: { | ||
class: 'test_div_class', | ||
style: 'color: green;' | ||
}, | ||
children: [{ | ||
type: 'text', | ||
text: 'Hello World! This is a text node.' | ||
}] | ||
}]; | ||
export default { | ||
data: { | ||
arrayNodes: arr, | ||
stringNodes: process.env.APP_TYPE === 'ant' ? [] : str | ||
}, | ||
onLoad() { | ||
if (process.env.APP_TYPE === 'ant') { | ||
// 对应 api 配置项 | ||
this.$api.htmlToNodes(str, (err, nodes) => { | ||
if (!err) { | ||
this.stringNodes = nodes; | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
* 注意: | ||
此处支付宝小程序提供的 `mini-html-parser2` 部分情况可支持跨平台的 `nodes` 转换,酌情选择,如果想多平台在`rich-text` 用 `nodes` 类型 则将 第二步的配置项加在 `base.config.js` 配置项中,将第三步的环境判断去掉 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
packages/okam-build/example/base/src/pages/data/vhtml.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<template> | ||
<view class="data-html-wrap"> | ||
<view class="example-item"> | ||
<view class="sub-title">原生 rich-text 支持</view> | ||
<rich-text :nodes="divTag">inner</rich-text> | ||
<rich-text :nodes="aTag" @click="handle"></rich-text> | ||
<rich-text :nodes="viewTag"></rich-text> | ||
<rich-text :nodes="imgTag" @click="handle"></rich-text> | ||
<rich-text :nodes="text"></rich-text> | ||
<ant-env> | ||
<view>支付宝不支持 string 类型</view> | ||
</ant-env> | ||
</view> | ||
<view class="example-item"> | ||
<view class="sub-title">v-html 支持</view> | ||
<div v-html="divTag">inner</div> | ||
<view v-html="divTag"></view> | ||
<view v-html="aTag" @click="handle"></view> | ||
<view v-html="viewTag"></view> | ||
<view v-html="imgTag" @click="handle"></view> | ||
<view v-html="text"></view> | ||
<ant-env> | ||
<view>支付宝不支持 string 类型</view> | ||
</ant-env> | ||
</view> | ||
<view class="example-item"> | ||
<view class="sub-title">v-html 多平台 arrayData 支持</view> | ||
<div v-html="arrayTag"></div> | ||
|
||
<view class="sub-title">v-html 多平台 string 支持</view> | ||
<div v-html="nodes"></div> | ||
</view> | ||
</view> | ||
</template> | ||
|
||
<script> | ||
import ModelComponent from '../../components/ModelComponent'; | ||
import SpModelComponent from '../../components/SpModelComponent'; | ||
let htmlData = { | ||
divTag: '<div class="desc">div 标签</div>', | ||
aTag: '<a class="link" href="www.baidu.com">a 百度</a><div>div 标签</div>', | ||
viewTag: '<view class="desc">view 标签</view>', | ||
imgTag: '<img class="desc" src="https://b.bdstatic.com/searchbox/icms/other/img/ico-share.png" /> img 标签', | ||
text: '文本' | ||
}; | ||
export default { | ||
config: { | ||
title: 'v-html支持' | ||
}, | ||
components: { | ||
}, | ||
data: { | ||
divTag: htmlData.divTag, | ||
aTag: htmlData.aTag, | ||
viewTag: htmlData.viewTag, | ||
imgTag: htmlData.imgTag, | ||
text: htmlData.text, | ||
arrayTag: [{ | ||
name: 'div', | ||
attrs: { | ||
class: 'test_div_class', | ||
style: 'color: green;' | ||
}, | ||
children: [{ | ||
type: 'text', | ||
text: 'Hello World! This is a text node.' | ||
}] | ||
}], | ||
nodes: process.env.APP_TYPE === 'ant' ? [] : htmlData.divTag | ||
}, | ||
onLoad() { | ||
if (process.env.APP_TYPE === 'ant') { | ||
this.$api.htmlToNodes(htmlData.divTag, (err, nodes) => { | ||
if (!err) { | ||
this.setData({nodes}); | ||
// this.nodes = nodes; | ||
} | ||
}); | ||
} | ||
}, | ||
methods: { | ||
handle(evt) { | ||
console.log(evt); | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<style lang="stylus"> | ||
.data-html-wrap | ||
position: relative | ||
padding: 20px | ||
box-sizing: border-box | ||
background: #ccc | ||
.sub-title | ||
font-size: 28px | ||
text-align: center | ||
background: #e0dede | ||
margin-bottom: 10px | ||
.example-item | ||
padding: 10px 15px | ||
margin: 20px 0 | ||
background: #fff | ||
.disabled | ||
color: #ccc | ||
.desc | ||
color: #ff4949 | ||
.link | ||
color: #3c76ff | ||
.div | ||
color: #277524 | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/okam-build/lib/processor/template/plugins/v-html-plugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* @file v-html | ||
* @author [email protected] | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const {createSyntaxPlugin} = require('./helper'); | ||
|
||
const propName = 'nodes'; | ||
|
||
function transfromVHtml(attrs, name, tplOpts, opts, element) { | ||
const {logger, file, appType} = tplOpts; | ||
|
||
if (appType === 'quick') { | ||
logger.warn('not support v-html in quick env'); | ||
return; | ||
} | ||
|
||
let vhtmlValue = attrs['v-html']; | ||
if (!vhtmlValue) { | ||
return; | ||
} | ||
|
||
if (attrs[propName]) { | ||
logger.warn( | ||
`${file.path} template attribute 「v-html="${attrs[name]}"」`, | ||
`is conflicted with 「${propName}」on element <${element.name}>` | ||
); | ||
} | ||
|
||
attrs[propName] = `{{${vhtmlValue}}}`; | ||
element.name = 'rich-text'; | ||
delete attrs['v-html']; | ||
} | ||
|
||
module.exports = createSyntaxPlugin({ | ||
attribute: { | ||
html: { | ||
match: 'v-html', | ||
transform: transfromVHtml | ||
} | ||
} | ||
}); |
Oops, something went wrong.