Skip to content

Commit

Permalink
add all file
Browse files Browse the repository at this point in the history
  • Loading branch information
YaoKaiLun committed Apr 26, 2017
0 parents commit 7b78bda
Show file tree
Hide file tree
Showing 17 changed files with 1,980 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 paco

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
### 步骤

---

1.`wxParser` 目录放到小程序项目的根目录下
2. 在需要富文本解析的的 `WXML` 内引入 `wxParser/index.wxml`
3. 在页面 `JS` 文件内使用 `wxParser.parse(options)` 方法解析 `HTML` 内容
4. 在小程序项目根目录的 `app.wxss` 内引入 `wxParser` 的默认样式库

---

**`wxParser.parse(options)` 方法的 `options` 参数说明**

| 参数名 | 类型 | 必填 |描述 |
| :---: | :----: | :----: |:----: |
| bind | String || 要绑定的数据名称 |
| html | String || HTML 内容 |
| target | Object || 绑定数据的模块对象 |
| enablePreviewImage | Boolean || 是否启用富文本内的图片预览功能,默认是 |
| tapLink | Function || 点击超链接时的回调函数 |

### 示例

---

**WXML**

```
<import src="../../wxParser/index.wxml"/>
<view class="wxParser">
<template is="wxParser" data="{{wxParserData:richText.nodes}}"/>
</view>
```

**JS**

```
const wxParser = require('../../wxParser/index');
Page({
data: {},
onLoad: function () {
let that = this;
let html = `<div style="color: #f00;">hello, wxParser!</div>`;
wxParser.parse({
bind: 'richText',
html: html,
target: that,
enablePreviewImage: false, // 禁用图片预览功能
tapLink: (url) => { // 点击超链接时的回调函数
// url 就是 HTML 富文本中 a 标签的 href 属性值
// 这里可以自定义点击事件逻辑,比如页面跳转
wx.navigateTo({
url
});
}
});
}
})
```

**WXSS**

```
@import '../wxParser/index.wxss'
```

### 注意

---

- `JS` 中的 `richText` 可以自定义,但是必须要与 `WXML` 中的 `richText` 对应
- 推荐把 template 放到 `<view class="wxParser"></view>` 内部,这样可以受 `wxParser` 控制并具有 `wxParser` 的一些默认样式
- 不建议直接修改 `wxParser` 的默认样式(因为内容库样式会有定期更新),应该新增一个样式补丁文件用来自定义样式

30 changes: 30 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//app.js
App({
onLaunch: function () {
//调用API从本地缓存中获取数据
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs);
},
getUserInfo: function (cb) {
var that = this
if (this.globalData.userInfo) {
typeof cb == "function" && cb(this.globalData.userInfo)
} else {
//调用登录接口
wx.login({
success: function () {
wx.getUserInfo({
success: function (res) {
that.globalData.userInfo = res.userInfo
typeof cb == "function" && cb(that.globalData.userInfo)
}
})
}
})
}
},
globalData: {
userInfo: null
}
})
12 changes: 12 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"pages": [
"pages/index/index",
"pages/logs/logs"
],
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "WeChat",
"navigationBarTextStyle": "black"
}
}
15 changes: 15 additions & 0 deletions app.wxss
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* 引入内容渲染模块样式 */

@import "/wxParser/index.wxss";

/**app.wxss**/

.container {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
padding: 200rpx 0;
box-sizing: border-box;
}
23 changes: 23 additions & 0 deletions pages/index/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 获取应用实例
let app = getApp();

let wxParser = require('../../wxParser/index');

Page({
data: {},
onLoad: function () {
let that = this;
let html = `<a href="/pages/logs/logs">文本内容</a><br><p class="test-class-name" style="text-align: center;" style="color: #ccc;">p 直属内容<u><i><strike color="#f00">tes<b color="#000">t</b></strike></i></u></p><p style="text-align: center;" checked width="100"><img src="https://mp.weixin.qq.com/debug/wxadoc/dev/image/cat/3.png?t=2017213" alt="image"></p><p style="text-align: center; "><b style="background-color: rgb(146, 208, 80);">&nbsp; &nbsp; 分类 &nbsp; &nbsp;&nbsp;</b></p><p style="text-align: center; "><span style="background-color: rgb(255, 255, 255);">&nbsp; <span style="color:#ff0000"><span style="font-size:10px">介</span><span style="font-size:12px">绍</span><font size="3">信</font><font size="4">息</font><font size="5">哈</font><font size="6">哈</font><font size="7">哈</font></span></span></p>`;
wxParser.parse({
bind: 'richText',
html: html,
target: that,
enablePreviewImage: false,
tapLink: (url) => {
wx.navigateTo({
url
})
}
});
}
})
6 changes: 6 additions & 0 deletions pages/index/index.wxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<import src="../../wxParser/index.wxml"/>

<view class="wxParser">
<template is="wxParser" data="{{wxParserData:richText.nodes}}"/>
</view>

26 changes: 26 additions & 0 deletions pages/index/index.wxss
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**index.wxss**/

.userinfo {
display: flex;
flex-direction: column;
align-items: center;
}

.userinfo-avatar {
width: 128rpx;
height: 128rpx;
margin: 20rpx;
border-radius: 50%;
}

.userinfo-nickname {
color: #aaa;
}

.usermotto {
margin-top: 200px;
}

.hehe {
color: #f00;
}
21 changes: 21 additions & 0 deletions utils/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function formatTime(date) {
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()

var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()


return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
n = n.toString()
return n[1] ? n : '0' + n
}

module.exports = {
formatTime: formatTime
}
Loading

0 comments on commit 7b78bda

Please sign in to comment.