This repository has been archived by the owner on May 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 247
/
editor.js
190 lines (189 loc) · 5.83 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
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
import RangeHandler from './range/handler'
import './style.css'
import template from './editor.html'
/**
* Created by peak on 2017/2/9.
*/
export default {
template,
props: {
content: {
type: String,
required: true,
default: ''
},
height: {
type: Number,
default: 300,
validator(val){
return val >= 100
}
},
zIndex: {
type: Number,
default: 1000
},
autoHeight: {
type: Boolean,
default: true
},
showModuleName: {}
},
data(){
return {
// defaultShowModuleName:false
// locale: {},
// modules:{},
fullScreen: false,
dashboard: null,
dashboardStyle: {}
}
},
watch: {
content(val) {
const content = this.$refs.content.innerHTML
if (val !== content) {
this.$res.content.innerHTML = val
}
},
fullScreen(val){
const component = this
if (val) {
component.parentEl = component.$el.parentNode
component.nextEl = component.$el.nextSibling
document.body.appendChild(component.$el)
return
}
if (component.nextEl) {
component.parentEl.insertBefore(component.$el, component.nextEl)
return
}
component.parentEl.appendChild(component.$el)
}
},
computed: {
contentStyle(){
const style = {}
if (this.fullScreen) {
style.height = `${window.innerHeight - this.$refs.toolbar.clientHeight - 1}px`
return style
}
if (!this.autoHeight) {
style.height = `${this.height}px`
return style
}
style['min-height'] = `${this.height}px`
return style
}
},
methods: {
toggleFullScreen(){
this.fullScreen = !this.fullScreen
},
enableFullScreen(){
this.fullScreen = true
},
exitFullScreen(){
this.fullScreen = false
},
focus(){
this.$refs.content.focus()
},
toggleDashboard(dashboard){
this.dashboard = this.dashboard === dashboard ? null : dashboard
},
execCommand(command, arg){
this.restoreSelection()
if (this.range) {
new RangeHandler(this.range).execCommand(command, arg)
}
this.toggleDashboard()
this.$emit('change', this.$refs.content.innerHTML)
},
getCurrentRange(){
return this.range
},
saveCurrentRange(){
const selection = window.getSelection ? window.getSelection() : document.getSelection()
if (!selection.rangeCount) {
return
}
const content = this.$refs.content
for (let i = 0; i < selection.rangeCount; i++) {
const range = selection.getRangeAt(0)
let start = range.startContainer
let end = range.endContainer
// for IE11 : node.contains(textNode) always return false
start = start.nodeType === Node.TEXT_NODE ? start.parentNode : start
end = end.nodeType === Node.TEXT_NODE ? end.parentNode : end
if (content.contains(start) && content.contains(end)) {
this.range = range
break
}
}
},
restoreSelection(){
const selection = window.getSelection ? window.getSelection() : document.getSelection()
selection.removeAllRanges()
if (this.range) {
selection.addRange(this.range)
} else {
const content = this.$refs.content
const div = document.createElement('div')
const range = document.createRange()
content.appendChild(div)
range.setStart(div, 0)
range.setEnd(div, 0)
selection.addRange(range)
this.range = range
}
},
activeModule(module){
if (typeof module.handler === 'function') {
module.handler(this)
return
}
if (module.hasDashboard) {
this.toggleDashboard(`dashboard-${module.name}`)
}
}
},
created(){
this.modules.forEach((module) => {
if (typeof module.init === 'function') {
module.init(this)
}
})
},
mounted(){
const content = this.$refs.content
content.innerHTML = this.content
content.addEventListener('mouseup', this.saveCurrentRange, false)
content.addEventListener('keyup', () => {
this.$emit('change', content.innerHTML)
this.saveCurrentRange()
}, false)
content.addEventListener('mouseout', (e) => {
if (e.target === content) {
this.saveCurrentRange()
}
}, false)
this.touchHandler = (e) => {
if (content.contains(e.target)) {
this.saveCurrentRange()
}
}
window.addEventListener('touchend', this.touchHandler, false)
},
updated(){
this.dashboardStyle = {'max-height': `${this.$refs.content.clientHeight}px`}
},
beforeDestroy(){
window.removeEventListener('touchend', this.touchHandler)
this.modules.forEach((module) => {
if (typeof module.destroyed === 'function') {
module.destroyed(this)
}
})
}
}