-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVueEditor.vue
127 lines (118 loc) · 3.85 KB
/
VueEditor.vue
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
<template>
<div class="vue-editor">
<div class="toolbar">
<button type="button" class="fa fa-bold" @click="bold" title="bold"/>
<button type="button" class="fa fa-italic" @click="italic" title="italic"/>
<button type="button" class="fa fa-underline" @click="underline" title="underline"/>
<button type="button" class="fa fa-link" @click="link" title="insert link"/>
<button type="button" class="fa fa-unlink" @click="unlink" title="remove link"/>
<button type="button" class="fa fa-undo" @click="undo" title="undo"/>
<button type="button" class="fa fa-redo" @click="redo" title="redo"/>
<button type="button" class="fa fa-paragraph" @click="paragraph" title="insert paragraph"/>
<button type="button" class="fa fa-align-left" @click="left" title="align left"/>
<button type="button" class="fa fa-align-center" @click="center" title="align center"/>
<button type="button" class="fa fa-align-right" @click="right" title="align right"/>
<button type="button" class="fa fa-hr" @click="hr" title="insert horizontal rule"/>
</div>
<div
class="editor"
ref="editable"
contenteditable
v-on="listeners"
/>
</div>
</template>
<script>
export default {
props: {
value: {
type: String,
default: '',
}
},
computed: {
listeners () {
return {...this.$listeners, input: this.onInput};
}
},
mounted () {
this.$refs.editable.innerHTML = this.$props.value;
},
methods: {
onInput (e) {
this.$emit('input', e.target.innerHTML);
},
underline () {
document.execCommand('underline', false, '')
},
bold () {
document.execCommand('bold', false, '')
},
italic () {
document.execCommand('italic', false, '')
},
undo () {
document.execCommand('undo', false, '')
},
redo () {
document.execCommand('redo', false, '')
},
paragraph () {
document.execCommand('insertParagraph',false,'')
},
left () {
document.execCommand('justifyLeft',false,'')
},
center () {
document.execCommand('justifyCenter',false,'')
},
right () {
document.execCommand('justifyRight',false,'')
},
hr () {
document.execCommand('insertHorizontalRule',false,'')
},
unlink () {
document.execCommand('unlink',false,'')
},
link () {
let url = prompt("Enter the URL")
document.execCommand("createLink", false, url)
}
},
};
</script>
<style lang="scss">
.vue-editor {
a,
a:visited,
a:active {
color: darkblue;
&:hover {
color: darkblue;
}
}
font-weight: normal;
.toolbar {
margin-bottom: .25rem;
button {
display: inline;
width: 2rem;
height: 2rem;
padding: .5rem 0;
color: white;
background: #393939;
border: none;
}
// font awesome free doesn't have an hr icon
.fa-hr:before {
content: "—";
}
}
.editor {
background: white;
padding: 1rem;
border: 1px solid #e7f5fe;
}
}
</style>