You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
9.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. <template lang="pug">
  2. v-app.editor(:dark='darkMode')
  3. nav-header(dense)
  4. template(slot='mid')
  5. v-spacer
  6. .subheading.grey--text {{currentPageTitle}}
  7. v-spacer
  8. template(slot='actions')
  9. v-btn.animated.fadeInDown(
  10. flat
  11. color='green'
  12. @click.native.stop='save'
  13. :class='{ "is-icon": $vuetify.breakpoint.mdAndDown }'
  14. )
  15. v-icon(color='green', :left='$vuetify.breakpoint.lgAndUp') check
  16. span.white--text(v-if='$vuetify.breakpoint.lgAndUp') {{ mode === 'create' ? $t('common:actions.create') : $t('common:actions.save') }}
  17. v-btn.animated.fadeInDown.wait-p1s(
  18. flat
  19. color='blue'
  20. @click.native.stop='openPropsModal'
  21. :class='{ "is-icon": $vuetify.breakpoint.mdAndDown, "mx-0": !welcomeMode, "ml-0": welcomeMode }'
  22. )
  23. v-icon(color='blue', :left='$vuetify.breakpoint.lgAndUp') sort_by_alpha
  24. span.white--text(v-if='$vuetify.breakpoint.lgAndUp') {{ $t('common:actions.page') }}
  25. v-btn.animated.fadeInDown.wait-p2s(
  26. v-if='!welcomeMode'
  27. flat
  28. color='red'
  29. :class='{ "is-icon": $vuetify.breakpoint.mdAndDown }'
  30. @click.native.stop='exit'
  31. )
  32. v-icon(color='red', :left='$vuetify.breakpoint.lgAndUp') close
  33. span.white--text(v-if='$vuetify.breakpoint.lgAndUp') {{ $t('common:actions.close') }}
  34. v-content
  35. component(:is='currentEditor', :save='save')
  36. editor-modal-properties(v-model='dialogProps')
  37. editor-modal-editorselect(v-model='dialogEditorSelector')
  38. editor-modal-unsaved(v-model='dialogUnsaved', @discard='exitGo')
  39. component(:is='activeModal')
  40. loader(v-model='dialogProgress', :title='$t(`editor:save.processing`)', :subtitle='$t(`editor:save.pleaseWait`)')
  41. notify
  42. </template>
  43. <script>
  44. import _ from 'lodash'
  45. import { get, sync } from 'vuex-pathify'
  46. import { AtomSpinner } from 'epic-spinners'
  47. import { Base64 } from 'js-base64'
  48. import createPageMutation from 'gql/editor/create.gql'
  49. import updatePageMutation from 'gql/editor/update.gql'
  50. import editorStore from '@/store/editor'
  51. /* global WIKI */
  52. WIKI.$store.registerModule('editor', editorStore)
  53. export default {
  54. i18nOptions: { namespaces: 'editor' },
  55. components: {
  56. AtomSpinner,
  57. editorCode: () => import(/* webpackChunkName: "editor-code", webpackMode: "lazy" */ './editor/editor-code.vue'),
  58. editorMarkdown: () => import(/* webpackChunkName: "editor-markdown", webpackMode: "lazy" */ './editor/editor-markdown.vue'),
  59. editorWysiwyg: () => import(/* webpackChunkName: "editor-wysiwyg", webpackMode: "lazy" */ './editor/editor-wysiwyg.vue'),
  60. editorModalEditorselect: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-editorselect.vue'),
  61. editorModalProperties: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-properties.vue'),
  62. editorModalUnsaved: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-unsaved.vue'),
  63. editorModalMedia: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-media.vue'),
  64. editorModalBlocks: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-blocks.vue')
  65. },
  66. props: {
  67. locale: {
  68. type: String,
  69. default: 'en'
  70. },
  71. path: {
  72. type: String,
  73. default: 'home'
  74. },
  75. title: {
  76. type: String,
  77. default: 'Untitled Page'
  78. },
  79. description: {
  80. type: String,
  81. default: ''
  82. },
  83. tags: {
  84. type: Array,
  85. default: () => ([])
  86. },
  87. isPublished: {
  88. type: Boolean,
  89. default: true
  90. },
  91. initEditor: {
  92. type: String,
  93. default: null
  94. },
  95. initMode: {
  96. type: String,
  97. default: 'create'
  98. },
  99. initContent: {
  100. type: String,
  101. default: null
  102. },
  103. pageId: {
  104. type: Number,
  105. default: 0
  106. }
  107. },
  108. data() {
  109. return {
  110. dialogProps: false,
  111. dialogProgress: false,
  112. dialogEditorSelector: false,
  113. dialogUnsaved: false,
  114. initContentParsed: ''
  115. }
  116. },
  117. computed: {
  118. currentEditor: sync('editor/editor'),
  119. darkMode: get('site/dark'),
  120. activeModal: sync('editor/activeModal'),
  121. mode: get('editor/mode'),
  122. welcomeMode() { return this.mode === `create` && this.path === `home` },
  123. currentPageTitle: get('page/title')
  124. },
  125. watch: {
  126. currentEditor(newValue, oldValue) {
  127. if (newValue !== '' && this.mode === 'create') {
  128. _.delay(() => {
  129. this.dialogProps = true
  130. }, 500)
  131. }
  132. }
  133. },
  134. created() {
  135. this.$store.commit('page/SET_ID', this.pageId)
  136. this.$store.commit('page/SET_DESCRIPTION', this.description)
  137. this.$store.commit('page/SET_IS_PUBLISHED', this.isPublished)
  138. this.$store.commit('page/SET_LOCALE', this.locale)
  139. this.$store.commit('page/SET_PATH', this.path)
  140. this.$store.commit('page/SET_TAGS', this.tags)
  141. this.$store.commit('page/SET_TITLE', this.title)
  142. this.$store.commit('page/SET_MODE', 'edit')
  143. },
  144. mounted() {
  145. this.$store.set('editor/mode', this.initMode || 'create')
  146. this.initContentParsed = this.initContent ? Base64.decode(this.initContent) : '# Header\n\nYour content here'
  147. this.$store.set('editor/content', this.initContentParsed)
  148. if (this.mode === 'create') {
  149. _.delay(() => {
  150. this.dialogEditorSelector = true
  151. }, 500)
  152. } else {
  153. this.currentEditor = `editor${_.startCase(this.initEditor || 'markdown')}`
  154. }
  155. },
  156. methods: {
  157. openPropsModal(name) {
  158. this.dialogProps = true
  159. },
  160. showProgressDialog(textKey) {
  161. this.dialogProgress = true
  162. },
  163. hideProgressDialog() {
  164. this.dialogProgress = false
  165. },
  166. async save() {
  167. this.showProgressDialog('saving')
  168. try {
  169. if (this.$store.get('editor/mode') === 'create') {
  170. // --------------------------------------------
  171. // -> CREATE PAGE
  172. // --------------------------------------------
  173. let resp = await this.$apollo.mutate({
  174. mutation: createPageMutation,
  175. variables: {
  176. content: this.$store.get('editor/content'),
  177. description: this.$store.get('page/description'),
  178. editor: 'markdown',
  179. locale: this.$store.get('page/locale'),
  180. isPrivate: false,
  181. isPublished: this.$store.get('page/isPublished'),
  182. path: this.$store.get('page/path'),
  183. publishEndDate: this.$store.get('page/publishEndDate') || '',
  184. publishStartDate: this.$store.get('page/publishStartDate') || '',
  185. tags: this.$store.get('page/tags'),
  186. title: this.$store.get('page/title')
  187. }
  188. })
  189. resp = _.get(resp, 'data.pages.create', {})
  190. if (_.get(resp, 'responseResult.succeeded')) {
  191. this.$store.commit('showNotification', {
  192. message: this.$t('editor:save.createSuccess'),
  193. style: 'success',
  194. icon: 'check'
  195. })
  196. this.$store.set('editor/id', _.get(resp, 'page.id'))
  197. this.$store.set('editor/mode', 'update')
  198. window.location.assign(`/${this.$store.get('page/path')}`)
  199. } else {
  200. throw new Error(_.get(resp, 'responseResult.message'))
  201. }
  202. } else {
  203. // --------------------------------------------
  204. // -> UPDATE EXISTING PAGE
  205. // --------------------------------------------
  206. let resp = await this.$apollo.mutate({
  207. mutation: updatePageMutation,
  208. variables: {
  209. id: this.$store.get('page/id'),
  210. content: this.$store.get('editor/content'),
  211. description: this.$store.get('page/description'),
  212. editor: 'markdown',
  213. locale: this.$store.get('page/locale'),
  214. isPrivate: false,
  215. isPublished: this.$store.get('page/isPublished'),
  216. path: this.$store.get('page/path'),
  217. publishEndDate: this.$store.get('page/publishEndDate') || '',
  218. publishStartDate: this.$store.get('page/publishStartDate') || '',
  219. tags: this.$store.get('page/tags'),
  220. title: this.$store.get('page/title')
  221. }
  222. })
  223. resp = _.get(resp, 'data.pages.update', {})
  224. if (_.get(resp, 'responseResult.succeeded')) {
  225. this.$store.commit('showNotification', {
  226. message: this.$t('editor:save.updateSuccess'),
  227. style: 'success',
  228. icon: 'check'
  229. })
  230. } else {
  231. throw new Error(_.get(resp, 'responseResult.message'))
  232. }
  233. }
  234. this.initContentParsed = this.$store.get('editor/content')
  235. } catch (err) {
  236. this.$store.commit('showNotification', {
  237. message: err.message,
  238. style: 'error',
  239. icon: 'warning'
  240. })
  241. }
  242. this.hideProgressDialog()
  243. },
  244. async exit() {
  245. if (this.initContentParsed !== this.$store.get('editor/content')) {
  246. this.dialogUnsaved = true
  247. } else {
  248. this.exitGo()
  249. }
  250. },
  251. exitGo() {
  252. this.$store.commit(`loadingStart`, 'editor-close')
  253. this.currentEditor = ''
  254. _.delay(() => {
  255. if (this.$store.get('editor/mode') === 'create') {
  256. window.location.assign(`/`)
  257. } else {
  258. window.location.assign(`/${this.$store.get('page/path')}`)
  259. }
  260. }, 500)
  261. }
  262. }
  263. }
  264. </script>
  265. <style lang='scss'>
  266. .editor {
  267. background-color: mc('grey', '900') !important;
  268. min-height: 100vh;
  269. .application--wrap {
  270. background-color: mc('grey', '900');
  271. }
  272. }
  273. .atom-spinner.is-inline {
  274. display: inline-block;
  275. }
  276. </style>