Fork me on GitHub

[小程序]-图文编辑器页面制作

希望用户直接在小程序内容进行文章编辑发布,但是小程序没有合适的富文本编辑器,只好自己一个,效果不错,功能也够用,希望对有需要的同学有所帮助,分享代码如下(如需转载请注明出处):

image

wxml

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
<view class='ceng' hidden='{{!showceng}}' catchtouchmove='true'></view>
<!-- editTxt-titt -->
<view class='edit-txt' hidden='{{!showedittitt}}' catchtouchmove='true'>
<textarea placeholder='请编辑文字' value='{{edittitt}}' bindconfirm="editedtitt" maxlength="300" />
</view>
<!-- editTxt-content -->
<view class='edit-txt' hidden='{{!showeditcontent}}' catchtouchmove='true'>
<textarea placeholder='请编辑文字' value='{{editcontent}}' bindconfirm="editedcontent" maxlength="300" />
</view>
<!-- reditTxt-content 标题输入,首编辑输入,重新编辑输入是分别使用三个不同的编辑框实现-->
<view class='edit-txt' hidden='{{!showreditcontent}}' catchtouchmove='true'>
<textarea placeholder='请编辑文字' value='{{reditcontent}}' bindconfirm="reditedcontent" maxlength="300" />
</view>
<view class='k-bai'>
<view class='txt titt' data-name='titt' bindtap='towrite'>
{{titt}}
<image src='/images/delet.png' class='delet-icon' data-name='titt' catchtap='ondelettitt' wx:if='{{titt!="请输入标题文字"}}'></image>
</view>
</view>
<view class='content k-bai' wx:for="{{content}}" wx:key="" data-leixing='{{item.leixing}}' data-index='{{index}}' bindtap='redit'>
<view class='txt' wx:if="{{item.leixing=='txt'}}">{{item.neirong}}</view>
<image src='{{item.url}}' class='image' mode="widthFix" wx:if="{{item.leixing=='img'}}"></image>
<image src='/images/delet.png' class='delet-icon' data-index='{{index}}' catchtap='ondelet' wx:if="{{content[index]!=''}}"></image>
</view>
<view class='add'>
<image src='/images/add_06.png' bindtap='addtxt'></image>
<image src='/images/add_03.png' bindtap='addimg'></image>
</view>
<view class='save-btn' bindtap='onsave'>上传</view>

js

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// pages/edit/edit.js
Page({

/**
* 页面的初始数据
*/
data: {
titt: '请输入标题文字',
showceng: false,
showedittitt: false,
showeditcontent: false,
showreditcontent: false,
edittitt: '',
editcontent: '',
reditcontent: '',
target: '',
content: [{
leixing: 'txt',
neirong: '我爱你'
},
{
leixing: 'img',
url: 'http://wechatpx.oss-cn-beijing.aliyuncs.com/card1_03.png'
}
],
},

/**
* 生命周期函数--监听页面加载
*/
towrite: function(e) {
var that = this
var target = e.currentTarget.dataset.name
that.setData({
showceng: true,
showedittitt: true,
target: target,
edittitt: ''
})
},
editedtitt: function(e) {
var that = this
var target = that.target
that.setData({
titt: e.detail.value,
showceng: false,
showedittitt: false,
[target]: e.detail.value
})
},
ondelettitt: function(e) {
var that = this
wx.showModal({
title: '重置标题',
content: '您确定要重置标题吗?',
success(res) {
if (res.confirm) {
that.setData({
titt: '请输入标题文字'
})
} else if (res.cancel) { }
},
confirmColor: '#5677fc'
})
},
ondelet: function(e) {
var that = this
var index = e.currentTarget.dataset.index
var content = that.data.content
wx.showModal({
title: '删除提示',
content: '您确定要删除这段编辑吗?',
success(res) {
if (res.confirm) {
content.splice(index, 1)
that.setData({
content: content
})
} else if (res.cancel) {}
},
confirmColor: '#5677fc'
})

},
addtxt: function() {
var that = this
var content = that.data.content
that.setData({
editcontent:'',
showceng: true,
showeditcontent: true
})
},
editedcontent: function(e) {
var that = this
var input = new Object
input.leixing = 'txt'
input.neirong = e.detail.value
var content = that.data.content
content.push(input)
that.setData({
content:content,
showceng: false,
showeditcontent: false
})
},
redit:function(e){
var that = this
var index = e.currentTarget.dataset.index
var leixing = e.currentTarget.dataset.leixing
var target = that.data.target
if(leixing=='txt'){
target = "content["+index+"].neirong"
that.setData({
reditcontent:'',
showceng: true,
showreditcontent: true,
target: target
})
}else if(leixing=='img'){
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success(res) {
const tempFilePaths = res.tempFilePaths
target = "content[" + index + "].url"
that.setData({
[target]: tempFilePaths
})
}
})
}
},
reditedcontent: function (e) {
var that = this
var target = that.data.target
that.setData({
[target]: e.detail.value,
showceng: false,
showreditcontent: false,
})
},
addimg:function(){
var that = this
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success(res) {
const tempFilePaths = res.tempFilePaths
var input = new Object
input.leixing = 'img'
input.url = tempFilePaths
var content = that.data.content
content.push(input)
that.setData({
content: content,
})
}
})
},
onsave:function(){

wx.showToast({
title: '上传成功!',
})
},
onLoad: function(options) {

},

/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function() {

},

/**
* 生命周期函数--监听页面显示
*/
onShow: function() {

},

/**
* 生命周期函数--监听页面隐藏
*/
onHide: function() {

},

/**
* 生命周期函数--监听页面卸载
*/
onUnload: function() {

},

/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function() {

},

/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function() {

},

/**
* 用户点击右上角分享
*/
onShareAppMessage: function() {

}
})

wxss

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
/* pages/edit/edit.wxss */

page {
background-color: #f1f1f1;
font-size: 28rpx;
padding-bottom: 150rpx;
}

.k-bai {
background-color: #fff;
padding: 30rpx;
color: #666;
}

.txt {
padding: 30rpx;
border: 1rpx solid #eee;
margin-top: 20rpx;
position: relative;
}

.delet-icon {
display: block;
width: 50rpx;
height: 50rpx;
border-radius: 50rpx;
position: absolute;
right: -20rpx;
top: -20rpx;
z-index: 10;
}

.ceng {
width: 750rpx;
height: 1334rpx;
position: fixed;
left: 0;
top: 0;
z-index: 50;
background-color: rgba(0, 0, 0, 0.3);
}

.edit-txt {
width: 660rpx;
background-color: #fff;
padding: 30rpx;
margin: 0 auto;
position: fixed;
left: 50%;
margin-left: -360rpx;
top: 220rpx;
z-index: 60;
}

.edit-txt .save-btn {
display: block;
width: 450rpx;
height: 80rpx;
border-radius: 80rpx;
background-color: #5677fc;
color: #fff;
text-align: center;
line-height: 80rpx;
position: absolute;
left: 50%;
margin-left: -225rpx;
bottom: -40rpx;
box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.2);
}

.edit-txt textarea {
width: 660rpx;
height: 500rpx;
}

.add {
position: relative;
padding: 20rpx 0;
width: 200rpx;
margin: 0 auto;
height: 70rpx;
background-color: #fff;
border-radius: 0 0 20rpx 20rpx;
box-shadow: 0 7rpx 5rpx rgba(0, 0, 0, 0.1);
}

.add image {
display: inline-block;
width: 70rpx;
height: 70rpx;
}

.add image:first-child {
margin-right: 20rpx;
margin-left: 20rpx;
}

.content {
margin-top: 30rpx;
position: relative;
}

.content .delet-icon {
right: 12rpx;
top: 25rpx;
}

.content .image {
display: block;
padding: 30rpx;
border: 1rpx solid #eee;
margin-top: 20rpx;
position: relative;
width: 630rpx;
}

.save-btn {
width: 690rpx;
height: 90rpx;
text-align: center;
line-height: 90rpx;
background-color: #5677fc;
color: #fff;
font-size: 34rpx;
border-radius: 90rpx;
position: fixed;
left: 50%;
margin-left: -345rpx;
bottom: 30rpx;
z-index: 100;
}
支持作者