wikijs-fork/client/components/editor.vue

294 lines
9.7 KiB
Vue
Raw Normal View History

2018-02-11 05:20:17 +00:00
<template lang="pug">
2018-12-02 04:03:14 +00:00
v-app.editor(:dark='darkMode')
nav-header(dense)
template(slot='mid')
v-spacer
.subheading.grey--text {{currentPageTitle}}
v-spacer
template(slot='actions')
v-btn.animated.fadeInDown(
flat
2018-08-13 04:12:44 +00:00
color='green'
@click.native.stop='save'
:class='{ "is-icon": $vuetify.breakpoint.mdAndDown }'
)
v-icon(color='green', :left='$vuetify.breakpoint.lgAndUp') check
span.white--text(v-if='$vuetify.breakpoint.lgAndUp') {{ mode === 'create' ? $t('common:actions.create') : $t('common:actions.save') }}
v-btn.animated.fadeInDown.wait-p1s(
flat
2018-08-13 04:12:44 +00:00
color='blue'
2018-12-09 05:46:06 +00:00
@click.native.stop='openPropsModal'
2019-01-26 23:35:56 +00:00
:class='{ "is-icon": $vuetify.breakpoint.mdAndDown, "mx-0": !welcomeMode, "ml-0": welcomeMode }'
2018-08-13 04:12:44 +00:00
)
v-icon(color='blue', :left='$vuetify.breakpoint.lgAndUp') sort_by_alpha
span.white--text(v-if='$vuetify.breakpoint.lgAndUp') {{ $t('editor:page') }}
v-btn.animated.fadeInDown.wait-p2s(
2019-01-07 03:03:34 +00:00
v-if='!welcomeMode'
flat
color='red'
:class='{ "is-icon": $vuetify.breakpoint.mdAndDown }'
2018-11-24 02:10:24 +00:00
@click.native.stop='exit'
)
v-icon(color='red', :left='$vuetify.breakpoint.lgAndUp') close
span.white--text(v-if='$vuetify.breakpoint.lgAndUp') {{ $t('editor:close') }}
v-content
component(:is='currentEditor', :save='save')
2018-12-09 05:46:06 +00:00
editor-modal-properties(v-model='dialogProps')
editor-modal-editorselect(v-model='dialogEditorSelector')
editor-modal-unsaved(v-model='dialogUnsaved', @discard='exitGo')
component(:is='activeModal')
2018-07-22 04:29:39 +00:00
loader(v-model='dialogProgress', :title='$t(`editor:save.processing`)', :subtitle='$t(`editor:save.pleaseWait`)')
notify
2018-02-11 05:20:17 +00:00
</template>
<script>
import _ from 'lodash'
2018-07-22 04:29:39 +00:00
import { get, sync } from 'vuex-pathify'
2018-07-15 23:16:19 +00:00
import { AtomSpinner } from 'epic-spinners'
import { Base64 } from 'js-base64'
2018-07-15 23:16:19 +00:00
2018-07-22 04:29:39 +00:00
import createPageMutation from 'gql/editor/create.gql'
import updatePageMutation from 'gql/editor/update.gql'
2018-07-15 23:16:19 +00:00
import editorStore from '@/store/editor'
/* global WIKI */
WIKI.$store.registerModule('editor', editorStore)
2018-02-11 05:20:17 +00:00
export default {
2019-01-07 03:03:34 +00:00
i18nOptions: { namespaces: 'editor' },
2018-02-11 05:20:17 +00:00
components: {
2018-07-15 23:16:19 +00:00
AtomSpinner,
2018-09-16 22:36:15 +00:00
editorCode: () => import(/* webpackChunkName: "editor-code", webpackMode: "lazy" */ './editor/editor-code.vue'),
editorMarkdown: () => import(/* webpackChunkName: "editor-markdown", webpackMode: "lazy" */ './editor/editor-markdown.vue'),
editorWysiwyg: () => import(/* webpackChunkName: "editor-wysiwyg", webpackMode: "lazy" */ './editor/editor-wysiwyg.vue'),
editorModalEditorselect: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-editorselect.vue'),
editorModalProperties: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-properties.vue'),
editorModalUnsaved: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-unsaved.vue'),
editorModalMedia: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-media.vue'),
editorModalBlocks: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-blocks.vue')
2018-09-16 22:36:15 +00:00
},
props: {
locale: {
type: String,
default: 'en'
},
path: {
type: String,
default: 'home'
},
title: {
type: String,
default: 'Untitled Page'
},
description: {
type: String,
default: ''
},
tags: {
type: Array,
default: () => ([])
},
isPublished: {
type: Boolean,
2018-12-09 05:46:06 +00:00
default: true
2018-09-16 22:36:15 +00:00
},
initEditor: {
type: String,
default: null
},
initMode: {
type: String,
default: 'create'
},
initContent: {
type: String,
default: null
},
pageId: {
type: Number,
default: 0
2018-09-16 22:36:15 +00:00
}
},
data() {
return {
2018-12-09 05:46:06 +00:00
dialogProps: false,
dialogProgress: false,
dialogEditorSelector: false,
dialogUnsaved: false,
initContentParsed: ''
}
},
2018-07-16 02:40:41 +00:00
computed: {
currentEditor: sync('editor/editor'),
2018-12-02 04:03:14 +00:00
darkMode: get('site/dark'),
activeModal: sync('editor/activeModal'),
2018-07-22 04:29:39 +00:00
mode: get('editor/mode'),
welcomeMode() { return this.mode === `create` && this.path === `home` },
currentPageTitle: get('page/title')
2018-07-16 02:40:41 +00:00
},
watch: {
currentEditor(newValue, oldValue) {
if (newValue !== '' && this.mode === 'create') {
_.delay(() => {
this.dialogProps = true
}, 500)
}
}
},
2018-10-29 02:09:58 +00:00
created() {
this.$store.commit('page/SET_ID', this.pageId)
2018-10-29 02:09:58 +00:00
this.$store.commit('page/SET_DESCRIPTION', this.description)
this.$store.commit('page/SET_IS_PUBLISHED', this.isPublished)
this.$store.commit('page/SET_LOCALE', this.locale)
this.$store.commit('page/SET_PATH', this.path)
this.$store.commit('page/SET_TAGS', this.tags)
this.$store.commit('page/SET_TITLE', this.title)
this.$store.commit('page/SET_MODE', 'edit')
2018-10-29 02:09:58 +00:00
},
2018-07-16 02:40:41 +00:00
mounted() {
2018-09-16 22:36:15 +00:00
this.$store.set('editor/mode', this.initMode || 'create')
this.initContentParsed = this.initContent ? Base64.decode(this.initContent) : '# Header\n\nYour content here'
this.$store.set('editor/content', this.initContentParsed)
2018-07-16 02:40:41 +00:00
if (this.mode === 'create') {
_.delay(() => {
this.dialogEditorSelector = true
2018-07-16 02:40:41 +00:00
}, 500)
2018-09-16 22:36:15 +00:00
} else {
this.currentEditor = `editor${_.startCase(this.initEditor || 'markdown')}`
2018-07-16 02:40:41 +00:00
}
},
methods: {
2018-12-09 05:46:06 +00:00
openPropsModal(name) {
this.dialogProps = true
},
showProgressDialog(textKey) {
this.dialogProgress = true
},
hideProgressDialog() {
this.dialogProgress = false
},
2018-07-15 23:16:19 +00:00
async save() {
this.showProgressDialog('saving')
try {
if (this.$store.get('editor/mode') === 'create') {
// --------------------------------------------
// -> CREATE PAGE
// --------------------------------------------
let resp = await this.$apollo.mutate({
mutation: createPageMutation,
variables: {
content: this.$store.get('editor/content'),
description: this.$store.get('page/description'),
editor: 'markdown',
locale: this.$store.get('page/locale'),
isPrivate: false,
isPublished: this.$store.get('page/isPublished'),
path: this.$store.get('page/path'),
publishEndDate: this.$store.get('page/publishEndDate') || '',
publishStartDate: this.$store.get('page/publishStartDate') || '',
tags: this.$store.get('page/tags'),
title: this.$store.get('page/title')
}
2018-07-22 04:29:39 +00:00
})
resp = _.get(resp, 'data.pages.create', {})
if (_.get(resp, 'responseResult.succeeded')) {
this.$store.commit('showNotification', {
message: this.$t('editor:save.createSuccess'),
style: 'success',
icon: 'check'
})
this.$store.set('editor/id', _.get(resp, 'page.id'))
this.$store.set('editor/mode', 'update')
window.location.assign(`/${this.$store.get('page/path')}`)
} else {
throw new Error(_.get(resp, 'responseResult.message'))
}
2018-07-22 04:29:39 +00:00
} else {
// --------------------------------------------
// -> UPDATE EXISTING PAGE
// --------------------------------------------
2018-07-22 04:29:39 +00:00
let resp = await this.$apollo.mutate({
mutation: updatePageMutation,
variables: {
id: this.$store.get('page/id'),
content: this.$store.get('editor/content'),
description: this.$store.get('page/description'),
editor: 'markdown',
locale: this.$store.get('page/locale'),
isPrivate: false,
isPublished: this.$store.get('page/isPublished'),
path: this.$store.get('page/path'),
publishEndDate: this.$store.get('page/publishEndDate') || '',
publishStartDate: this.$store.get('page/publishStartDate') || '',
tags: this.$store.get('page/tags'),
title: this.$store.get('page/title')
}
})
resp = _.get(resp, 'data.pages.update', {})
if (_.get(resp, 'responseResult.succeeded')) {
this.$store.commit('showNotification', {
message: this.$t('editor:save.updateSuccess'),
style: 'success',
icon: 'check'
})
} else {
throw new Error(_.get(resp, 'responseResult.message'))
}
2018-07-22 04:29:39 +00:00
}
2019-01-07 03:03:34 +00:00
this.initContentParsed = this.$store.get('editor/content')
} catch (err) {
this.$store.commit('showNotification', {
message: err.message,
style: 'error',
icon: 'warning'
})
2018-07-22 04:29:39 +00:00
}
this.hideProgressDialog()
2018-11-24 02:10:24 +00:00
},
async exit() {
if (this.initContentParsed !== this.$store.get('editor/content')) {
this.dialogUnsaved = true
} else {
this.exitGo()
}
},
exitGo() {
this.$store.commit(`loadingStart`, 'editor-close')
this.currentEditor = ''
_.delay(() => {
2019-01-01 22:08:16 +00:00
if (this.$store.get('editor/mode') === 'create') {
window.location.assign(`/`)
} else {
window.location.assign(`/${this.$store.get('page/path')}`)
}
}, 500)
}
2018-02-11 05:20:17 +00:00
}
}
</script>
<style lang='scss'>
2018-09-16 22:42:41 +00:00
.editor {
2018-12-09 05:46:06 +00:00
background-color: mc('grey', '900') !important;
2018-09-16 22:42:41 +00:00
min-height: 100vh;
2019-01-26 23:35:56 +00:00
.application--wrap {
background-color: mc('grey', '900');
}
2018-09-16 22:42:41 +00:00
}
2018-07-15 23:16:19 +00:00
.atom-spinner.is-inline {
display: inline-block;
}
2018-02-11 05:20:17 +00:00
</style>