Error box, download button, editor save and more.
Former-commit-id: c14972384cc8afad28d49388b0b4545669e20b7d [formerly 0af2e5f9aff2aac576044ca9590045f656fbffda] [formerly 02248a86b7063e1c9d6779c3574a848708ff86f2 [formerly efaa8439a9]]
Former-commit-id: 4ce0a81ea04b56cef11d62107d692502e266a16e [formerly 2c46db9398c267351c10f06c089ed08dc3625232]
Former-commit-id: 1c93a0643217ab69668600b41e2d8263266d7e23
This commit is contained in:
1 parent
7def1b2325
commit
0012601652
14 files changed
+180
-102
No files matched your search
@@ -1,16 +1,18 @@
|
||||
<template>
|
||||
<form id="editor" :class="req.language">
|
||||
<h2 v-if="hasMetadata">Metadata</h2>
|
||||
<textarea v-model="req.metadata" v-if="hasMetadata" id="metadata"></textarea>
|
||||
<div v-if="hasMetadata" id="metadata">
|
||||
<h2>Metadata</h2>
|
||||
</div>
|
||||
|
||||
<h2 v-if="hasMetadata">Body</h2>
|
||||
<textarea v-model="req.content" id="content"></textarea>
|
||||
</form>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { mapState } from 'vuex'
|
||||
import CodeMirror from '@/utils/codemirror'
|
||||
import api from '@/utils/api'
|
||||
import buttons from '@/utils/buttons'
|
||||
|
||||
export default {
|
||||
name: 'editor',
|
||||
@@ -23,11 +25,22 @@ export default {
|
||||
data: function () {
|
||||
return {
|
||||
metadata: null,
|
||||
metalang: null,
|
||||
content: null
|
||||
}
|
||||
},
|
||||
created () {
|
||||
window.addEventListener('keydown', this.keyEvent)
|
||||
document.getElementById('save-button').addEventListener('click', this.save)
|
||||
},
|
||||
beforeDestroy () {
|
||||
window.removeEventListener('keydown', this.keyEvent)
|
||||
document.getElementById('save-button').removeEventListener('click', this.save)
|
||||
},
|
||||
mounted: function () {
|
||||
this.content = CodeMirror.fromTextArea(document.getElementById('content'), {
|
||||
// Set up the main content editor.
|
||||
this.content = CodeMirror(document.getElementById('editor'), {
|
||||
value: this.req.content,
|
||||
lineNumbers: (this.req.language !== 'markdown'),
|
||||
viewportMargin: Infinity,
|
||||
autofocus: true,
|
||||
@@ -42,25 +55,66 @@ export default {
|
||||
return
|
||||
}
|
||||
|
||||
this.metadata = CodeMirror.fromTextArea(document.getElementById('metadata'), {
|
||||
this.parseMetadata()
|
||||
|
||||
// Set up metadata editor.
|
||||
this.metadata = CodeMirror(document.getElementById('metadata'), {
|
||||
value: this.req.metadata,
|
||||
viewportMargin: Infinity,
|
||||
lineWrapping: true,
|
||||
theme: 'markdown'
|
||||
})
|
||||
|
||||
if (this.req.metadata.startsWith('{')) {
|
||||
CodeMirror.autoLoadMode(this.metadata, 'json')
|
||||
}
|
||||
|
||||
if (this.req.metadata.startsWith('---')) {
|
||||
CodeMirror.autoLoadMode(this.metadata, 'yaml')
|
||||
}
|
||||
|
||||
if (this.req.metadata.startsWith('+++')) {
|
||||
CodeMirror.autoLoadMode(this.metadata, 'toml')
|
||||
}
|
||||
CodeMirror.autoLoadMode(this.metadata, this.metalang)
|
||||
},
|
||||
methods: {
|
||||
// Saves the content when the user presses CTRL-S.
|
||||
keyEvent (event) {
|
||||
if (!event.ctrlKey && !event.metaKey) {
|
||||
return
|
||||
}
|
||||
|
||||
if (String.fromCharCode(event.which).toLowerCase() !== 's') {
|
||||
return
|
||||
}
|
||||
|
||||
event.preventDefault()
|
||||
this.save()
|
||||
},
|
||||
// Parses the metadata and gets the language in which
|
||||
// it is written.
|
||||
parseMetadata () {
|
||||
if (this.req.metadata.startsWith('{')) {
|
||||
this.metalang = 'json'
|
||||
}
|
||||
|
||||
if (this.req.metadata.startsWith('---')) {
|
||||
this.metalang = 'yaml'
|
||||
}
|
||||
|
||||
if (this.req.metadata.startsWith('+++')) {
|
||||
this.metalang = 'toml'
|
||||
}
|
||||
},
|
||||
// Saves the file.
|
||||
save () {
|
||||
buttons.loading('save')
|
||||
let content = this.content.getValue()
|
||||
|
||||
if (this.hasMetadata) {
|
||||
content = this.metadata.getValue() + '\n\n' + content
|
||||
}
|
||||
|
||||
api.put(this.$route.path, content)
|
||||
.then(() => {
|
||||
buttons.done('save')
|
||||
console.log('Saved!')
|
||||
})
|
||||
.catch(error => {
|
||||
buttons.done('save')
|
||||
console.log(error)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in new issue
Block a user