Former-commit-id: c14972384cc8afad28d49388b0b4545669e20b7d [formerly 0af2e5f9aff2aac576044ca9590045f656fbffda] [formerly 02248a86b7063e1c9d6779c3574a848708ff86f2 [formerly efaa8439a9dbbf9b829cf9fc32ca789c2a16df7d]] Former-commit-id: 4ce0a81ea04b56cef11d62107d692502e266a16e [formerly 2c46db9398c267351c10f06c089ed08dc3625232] Former-commit-id: 1c93a0643217ab69668600b41e2d8263266d7e23
125 lines
3.0 KiB
Vue
125 lines
3.0 KiB
Vue
<template>
|
|
<form id="editor" :class="req.language">
|
|
<div v-if="hasMetadata" id="metadata">
|
|
<h2>Metadata</h2>
|
|
</div>
|
|
|
|
<h2 v-if="hasMetadata">Body</h2>
|
|
</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',
|
|
computed: {
|
|
...mapState(['req']),
|
|
hasMetadata: function () {
|
|
return (this.req.metadata !== undefined && this.req.metadata !== null)
|
|
}
|
|
},
|
|
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 () {
|
|
// 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,
|
|
theme: (this.req.language === 'markdown') ? 'markdown' : 'ttcn',
|
|
lineWrapping: (this.req.language === 'markdown')
|
|
})
|
|
|
|
CodeMirror.autoLoadMode(this.content, this.req.language)
|
|
|
|
// Prevent of going on if there is no metadata.
|
|
if (!this.hasMetadata) {
|
|
return
|
|
}
|
|
|
|
this.parseMetadata()
|
|
|
|
// Set up metadata editor.
|
|
this.metadata = CodeMirror(document.getElementById('metadata'), {
|
|
value: this.req.metadata,
|
|
viewportMargin: Infinity,
|
|
lineWrapping: true,
|
|
theme: 'markdown'
|
|
})
|
|
|
|
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>
|
|
|
|
<style>
|
|
|
|
</style>
|