cabinet/frontend/src/components/MainEditor.vue

90 lines
2.3 KiB
Vue
Raw Normal View History

2019-11-01 17:09:17 +00:00
<template>
<div>
<b-container fluid>
<b-row>
<b-col>
2019-11-08 01:54:00 +00:00
<Editor :content="content" @change="updateContent"/>
2019-11-01 17:09:17 +00:00
</b-col>
</b-row>
<b-row>
2019-11-08 01:54:00 +00:00
<b-col cols="1"><label style="padding-top: 0.5em" for="tagList">Tags</label></b-col>
2019-11-01 17:09:17 +00:00
<b-col cols="10">
2019-11-08 01:54:00 +00:00
<TagList id="tagList" :tags="tags" @change="tagUpdate"/>
2019-11-01 17:09:17 +00:00
</b-col>
</b-row>
</b-container>
</div>
</template>
<script>
2019-11-08 01:54:00 +00:00
import Editor from "./Editor"
import TagList from "./TagList"
import _ from 'lodash'
2019-11-01 17:09:17 +00:00
export default {
name: "MainEditor",
props: {
slug: String
},
created() {
if (this.$props.slug) {
this.getFile(this.$props.slug)
}
2019-11-08 01:54:00 +00:00
this.save = _.debounce(this.save, 2000)
2019-11-01 17:09:17 +00:00
},
computed: {
2019-11-08 01:54:00 +00:00
tags: function () {
if (this.$store.state.file) {
return this.$store.state.file.Tags
2019-11-01 17:09:17 +00:00
}
return []
},
content: function () {
2019-11-08 01:54:00 +00:00
if (this.$store.state.file) {
return this.$store.state.file.Content
2019-11-01 17:09:17 +00:00
}
return "= Main Editor"
}
},
watch: {
slug: function (newValue) {
this.getFile(newValue)
}
},
methods: {
2019-11-08 01:54:00 +00:00
getFile: function (slug) {
2019-11-01 17:09:17 +00:00
this.$store.dispatch('getFile', slug)
2019-11-08 01:54:00 +00:00
},
tagUpdate: function(newTags) {
2019-11-08 04:39:16 +00:00
if (JSON.stringify(newTags) === JSON.stringify(this.$store.state.file.Tags))
2019-11-08 01:54:00 +00:00
return
2019-11-08 04:39:16 +00:00
this.$store.commit('setTags', newTags)
2019-11-08 01:54:00 +00:00
this.$emit('markDirty', true)
this.save()
},
updateContent: function (newContent) {
if (this.$store.state.file.Content === newContent)
return
2019-11-08 04:39:16 +00:00
this.$store.commit('setContent', newContent)
2019-11-08 01:54:00 +00:00
this.$emit('markDirty', true)
this.save()
},
save: function () {
2019-11-08 04:39:16 +00:00
this.$store.commit('setContent', this.content)
this.$store.dispatch('saveFile')
2019-11-08 01:54:00 +00:00
.then(res => {
this.$emit('markDirty', false)
this.$store.dispatch('updateSearch')
if (res.data.Slug !== this.$route.params.slug)
2019-11-08 01:54:00 +00:00
this.$router.replace({params: { slug: res.data.Slug }})
})
2019-11-08 05:35:20 +00:00
.catch(() => { })
2019-11-01 17:09:17 +00:00
}
},
components: {TagList, Editor}
}
</script>
<style scoped>
</style>