mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-06-04 16:32:58 +00:00

Former-commit-id: 6523e78400ba84510f754e05f589b8f7c779319f [formerly 2d473b45716aaf6e35f2757bf9dc27adcafefd88] [formerly 5b34db9a57e050b307f1c5803cad82d2af155867 [formerly c206bea84a3e667047920d7dbcd0fc1c89cbced9]] Former-commit-id: 703d059a7455131dfa20d89050c40367f692b4b5 [formerly a320aa2603c47685e484cae3715eb788b1218fe9] Former-commit-id: 3819065ec37278f32ebceeda9cbb7af6f8723392
128 lines
3.0 KiB
Vue
128 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 () {
|
|
if (this.req.content === undefined || this.req.content === null) {
|
|
this.req.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,
|
|
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')
|
|
})
|
|
.catch(error => {
|
|
buttons.done('save')
|
|
console.log(error)
|
|
})
|
|
}
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<style>
|
|
|
|
</style>
|