index.js
4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//Parser组件
const html2nodes = require('./Parser.js');
const initData = function(Component) {
setTimeout(() => {
Component.createSelectorQuery().select('#contain').boundingClientRect(res => {
Component.triggerEvent('ready', res);
}).exec();
Component.videoContext = [];
let nodes = [Component.selectComponent('#contain')];
nodes = nodes.concat(Component.selectAllComponents('#contain>>>#node'));
for (let node of nodes) {
for (let item of node.data.nodes) {
if (item.name == 'video') {
Component.videoContext.push({
id: item.attrs.id,
context: wx.createVideoContext(item.attrs.id, node)
});
} else if (item.name == 'audio' && item.attrs.autoplay)
wx.createAudioContext(item.attrs.id, node).play();
}
}
}, 10)
}
Component({
properties: {
'html': {
type: null,
value: '',
observer: function(html) {
let hideAnimation = {},
showAnimation = {};
if (this.data.showWithAnimation) {
hideAnimation = wx.createAnimation({
duration: this.data.animationDuration,
timingFunction: "ease"
}).opacity(0).step().export();
showAnimation = wx.createAnimation({
duration: this.data.animationDuration,
timingFunction: "ease"
}).opacity(1).step().export();
}
if (!html) {
this.setData({
nodes: []
})
} else if (typeof html == 'string') {
html2nodes(html, this.data.tagStyle).then(res => {
this.setData({
nodes: res.nodes,
controls: {
imgMode: this.data.imgMode
},
showAnimation,
hideAnimation
}, initData(this))
if (res.title) {
wx.setNavigationBarTitle({
title: res.title
})
}
this.imgList = res.imgList;
this.triggerEvent('parse', res);
}).catch(err => {
this.triggerEvent('error', err);
})
} else if (html.constructor == Array) {
this.setData({
controls: {
imgMode: this.data.imgMode
},
showAnimation,
hideAnimation
}, initData(this))
this.imgList = [];
} else if (typeof html == 'object') {
if (!html.nodes || html.nodes.constructor != Array) {
this.triggerEvent('error', {
message: "传入的nodes数组格式不正确!应该传入的类型是array,实际传入的类型是:" + typeof html.nodes
});
return;
}
this.setData({
controls: {
imgMode: this.data.imgMode
},
showAnimation,
hideAnimation
}, initData(this))
if (html.title) {
wx.setNavigationBarTitle({
title: html.title
})
}
this.imgList = html.imgList || [];
} else {
this.triggerEvent('error', {
message: "错误的html类型:" + typeof html
});
}
}
},
'autocopy': {
type: Boolean,
value: true
},
'autopause': {
type: Boolean,
value: true
},
'imgMode': {
type: String,
value: "default"
},
'selectable': {
type: Boolean,
value: false
},
'tagStyle': {
type: Object,
value: {}
},
'showWithAnimation': {
type: Boolean,
value: false
},
'animationDuration': {
type: Number,
value: 400
}
},
methods: {
//事件
tapEvent(e) {
if (this.data.autocopy && e.detail && /^http/.test(e.detail)) {
wx.setClipboardData({
data: e.detail,
success() {
wx.showToast({
title: '链接已复制',
})
}
})
}
this.triggerEvent('linkpress', e.detail);
},
errorEvent(e) {
this.triggerEvent('error', e.detail);
},
//内部方法
_previewImg(e) {
wx.previewImage({
current: e.detail,
urls: this.imgList.length ? this.imgList : [e.detail],
})
},
_playVideo(e) {
if (this.videoContext.length > 1 && this.data.autopause) {
for (let video of this.videoContext) {
if (video.id == e.detail) continue;
video.context.pause();
}
}
}
}
})