-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.js
158 lines (149 loc) · 4.6 KB
/
editor.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
Component({
behaviors: [],
// 属性定义(详情参见下文)
properties: {
myProperty: { // 属性名
type: String,
value: ''
},
myProperty2: String // 简化的定义方式
},
data: {
formats: {},
readOnly: false,
placeholder: '开始输入...',
editorHeight: 300,
keyboardHeight: 0,
isIOS: false
},
lifetimes: {
created: function () {
console.log('created');
},
attached: function () {
// 在组件实例进入页面节点树时执行
console.log('attached');
},
detached: function () {
// 在组件实例被从页面节点树移除时执行
console.log('detached')
},
},
pageLifetimes: {
// 组件所在页面的生命周期函数
show: function () {
console.log('show');
},
hide: function () {
},
resize: function () {
},
},
ready() {
console.log('ready');
this.onLoad();
},
methods: {
readOnlyChange() {
this.setData({
readOnly: !this.data.readOnly
})
},
onLoad() {
const platform = wx.getSystemInfoSync().platform;
const isIOS = platform === 'ios';
this.setData({isIOS});
const that = this;
this.updatePosition(0);
let keyboardHeight = 0;
wx.onKeyboardHeightChange(res => {
if (res.height === keyboardHeight) return
const duration = res.height > 0 ? res.duration * 1000 : 0;
keyboardHeight = res.height;
setTimeout(() => {
wx.pageScrollTo({
scrollTop: 0,
success() {
that.updatePosition(keyboardHeight);
that.editorCtx.scrollIntoView()
}
})
}, duration)
})
},
updatePosition(keyboardHeight) {
const toolbarHeight = 50;
const {windowHeight, platform} = wx.getSystemInfoSync();
let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight;
this.setData({editorHeight, keyboardHeight})
},
calNavigationBarAndStatusBar() {
const systemInfo = wx.getSystemInfoSync();
const {statusBarHeight, platform} = systemInfo;
const isIOS = platform === 'ios';
const navigationBarHeight = isIOS ? 44 : 48;
return statusBarHeight + navigationBarHeight
},
onEditorReady() {
const that = this;
that.createSelectorQuery().select('#myeditor').context(function (res) {
console.log(res);
that.editorCtx = res.context
}).exec()
},
blur() {
this.editorCtx.blur()
},
format(e) {
let {name, value} = e.target.dataset
if (!name) return
// console.log('format', name, value)
this.editorCtx.format(name, value)
},
onStatusChange(e) {
const formats = e.detail;
this.setData({formats})
},
insertDivider() {
this.editorCtx.insertDivider({
success: function () {
console.log('insert divider success')
}
})
},
clear() {
this.editorCtx.clear({
success: function (res) {
console.log("clear success")
}
})
},
removeFormat() {
this.editorCtx.removeFormat()
},
insertDate() {
const date = new Date();
const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
this.editorCtx.insertText({
text: formatDate
})
},
insertImage() {
const that = this;
wx.chooseImage({
count: 1,
success: function (res) {
that.editorCtx.insertImage({
src: res.tempFilePaths[0],
success: function () {
console.log('insert image success')
}
})
}
})
},
onInput(detail) {
this.triggerEvent('myevent', detail)
}
},
});