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
222
223
'use strict'
/* global $, siteRoot */
let mde
export default {
name: 'editor',
props: ['currentPath'],
data() {
return {}
},
computed: {
insertContent() {
return this.$store.state.editor.insertContent
}
},
methods: {
insert(content) {
if (mde.codemirror.doc.somethingSelected()) {
mde.codemirror.execCommand('singleSelection')
}
mde.codemirror.doc.replaceSelection(this.insertContent)
},
save() {
let self = this
this.$http.put(window.location.href, {
markdown: mde.value()
}).then(resp => {
return resp.json()
}).then(resp => {
if (resp.ok) {
window.location.assign(siteRoot + '/' + self.currentPath)
} else {
self.$store.dispatch('alert', {
style: 'red',
icon: 'ui-2_square-remove-09',
msg: resp.msg
})
}
}).catch(err => {
self.$store.dispatch('alert', {
style: 'red',
icon: 'ui-2_square-remove-09',
msg: 'Error: ' + err.body.msg
})
})
}
},
mounted() {
let self = this
FuseBox.import('/js/simplemde/simplemde.min.js', (SimpleMDE) => {
mde = new SimpleMDE({
autofocus: true,
autoDownloadFontAwesome: false,
element: this.$refs.editorTextArea,
placeholder: 'Enter Markdown formatted content here...',
spellChecker: false,
status: false,
toolbar: [
{
name: 'bold',
action: SimpleMDE.toggleBold,
className: 'icon-bold',
title: 'Bold'
},
{
name: 'italic',
action: SimpleMDE.toggleItalic,
className: 'icon-italic',
title: 'Italic'
},
{
name: 'strikethrough',
action: SimpleMDE.toggleStrikethrough,
className: 'icon-strikethrough',
title: 'Strikethrough'
},
'|',
{
name: 'heading-1',
action: SimpleMDE.toggleHeading1,
className: 'icon-header fa-header-x fa-header-1',
title: 'Header (Level 1)'
},
{
name: 'heading-2',
action: SimpleMDE.toggleHeading2,
className: 'icon-header fa-header-x fa-header-2',
title: 'Header (Level 2)'
},
{
name: 'heading-3',
action: SimpleMDE.toggleHeading3,
className: 'icon-header fa-header-x fa-header-3',
title: 'Header (Level 3)'
},
{
name: 'quote',
action: SimpleMDE.toggleBlockquote,
className: 'nc-icon-outline text_quote',
title: 'Quote'
},
'|',
{
name: 'unordered-list',
action: SimpleMDE.toggleUnorderedList,
className: 'nc-icon-outline text_list-bullet',
title: 'Bullet List'
},
{
name: 'ordered-list',
action: SimpleMDE.toggleOrderedList,
className: 'nc-icon-outline text_list-numbers',
title: 'Numbered List'
},
'|',
{
name: 'link',
action: (editor) => {
window.alert('Coming soon!')
// todo
},
className: 'nc-icon-outline ui-2_link-68',
title: 'Insert Link'
},
{
name: 'image',
action: (editor) => {
self.$store.dispatch('editorFile/open', { mode: 'image' })
},
className: 'nc-icon-outline design_image',
title: 'Insert Image'
},
{
name: 'file',
action: (editor) => {
self.$store.dispatch('editorFile/open', { mode: 'file' })
},
className: 'nc-icon-outline files_zip-54',
title: 'Insert File'
},
{
name: 'video',
action: (editor) => {
self.$store.dispatch('editorVideo/open')
},
className: 'nc-icon-outline media-1_video-64',
title: 'Insert Video Player'
},
'|',
{
name: 'inline-code',
action: (editor) => {
if (!editor.codemirror.doc.somethingSelected()) {
return self.$store.dispatch('alert', {
style: 'orange',
icon: 'design_drag',
msg: 'Invalid selection. Select at least 1 character.'
})
}
let curSel = editor.codemirror.doc.getSelections()
curSel = self._.map(curSel, (s) => {
return '`' + s + '`'
})
editor.codemirror.doc.replaceSelections(curSel)
},
className: 'nc-icon-outline arrows-4_enlarge-46',
title: 'Inline Code'
},
{
name: 'code-block',
action: (editor) => {
self.$store.dispatch('editorCodeblock/open', {
initialContent: (mde.codemirror.doc.somethingSelected()) ? mde.codemirror.doc.getSelection() : ''
})
},
className: 'nc-icon-outline design_code',
title: 'Code Block'
},
'|',
{
name: 'table',
action: (editor) => {
window.alert('Coming soon!')
// todo
},
className: 'nc-icon-outline ui-2_grid-square',
title: 'Insert Table'
},
{
name: 'horizontal-rule',
action: SimpleMDE.drawHorizontalRule,
className: 'nc-icon-outline design_distribute-vertical',
title: 'Horizontal Rule'
}
],
shortcuts: {
'toggleBlockquote': null,
'toggleFullScreen': null
}
})
// Save
$(window).bind('keydown', (ev) => {
if (ev.ctrlKey || ev.metaKey) {
switch (String.fromCharCode(ev.which).toLowerCase()) {
case 's':
ev.preventDefault()
self.save()
break
}
}
})
// Listeners
this.$root.$on('editor/save', this.save)
this.$root.$on('editor/insert', this.insert)
this.$store.dispatch('pageLoader/complete')
})
}
}