1
0
Fork 0
mirror of https://github.com/Luzifer/wiki.git synced 2024-10-08 00:23:22 +00:00
wiki/src/edit.vue

93 lines
1.8 KiB
Vue
Raw Permalink Normal View History

2019-08-04 22:42:37 +00:00
<template>
<div class="container">
<div class="row">
<div class="col">
2019-08-04 22:42:37 +00:00
<textarea ref="editor" />
<button
class="btn btn-primary"
2019-08-04 22:42:37 +00:00
@click="save"
>
Save
</button>
</div>
</div>
</div>
2019-08-04 22:42:37 +00:00
</template>
<script>
import EasyMDE from 'easymde'
export default {
data() {
return {
editor: null,
}
},
methods: {
loadPage(pageName) {
console.debug(`Loading ${pageName}...`)
return fetch(`/_content/${pageName}`)
2019-08-04 22:42:37 +00:00
.then(resp => {
if (resp.status === 404) {
return { content: `# ${pageName}` }
}
return resp.json()
})
.then(data => {
if (this.editor) {
this.editor.toTextArea()
this.editor = null
}
this.editor = new EasyMDE({
element: this.$refs.editor,
forceSync: true,
indentWithTabs: false,
initialValue: data.content,
})
// this.editor.codemirror.setValue(data.content)
2019-08-04 22:42:37 +00:00
})
.catch(err => {
if (err.response && err.response.status === 404) {
return
}
console.error(err)
})
},
save() {
return fetch(`/_content/${this.$route.params.page}`, {
body: JSON.stringify({ content: this.$refs.editor.value }),
method: 'POST',
2019-08-04 22:42:37 +00:00
})
.then(() => {
this.$router.push({ name: 'view', params: { page: this.$route.params.page } })
})
.catch(err => {
console.error(err)
})
},
},
mounted() {
this.loadPage(this.$route.params.page)
},
name: 'WikiEdit',
watch: {
'$route'(to, from) {
if (to.params.page === from.params.page) {
return
}
this.loadPage(to.params.page)
},
},
2019-08-04 22:42:37 +00:00
}
</script>