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.

287 lines
9.2 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
6 years ago
  1. <template lang="pug">
  2. v-app.editor(:dark='darkMode')
  3. nav-header(dense)
  4. template(slot='actions')
  5. v-btn(
  6. outline
  7. color='green'
  8. @click.native.stop='save'
  9. :class='{ "is-icon": $vuetify.breakpoint.mdAndDown }'
  10. )
  11. v-icon(color='green', :left='$vuetify.breakpoint.lgAndUp') check
  12. span.white--text(v-if='$vuetify.breakpoint.lgAndUp') {{ mode === 'create' ? $t('common:actions.create') : $t('common:actions.save') }}
  13. v-btn(
  14. outline
  15. color='blue'
  16. @click.native.stop='openPropsModal'
  17. :class='{ "is-icon": $vuetify.breakpoint.mdAndDown, "mx-0": mode === `create`, "ml-0": mode !== `create` }'
  18. )
  19. v-icon(color='blue', :left='$vuetify.breakpoint.lgAndUp') sort_by_alpha
  20. span.white--text(v-if='$vuetify.breakpoint.lgAndUp') {{ $t('editor:page') }}
  21. v-btn(
  22. v-if='path !== `home`'
  23. outline
  24. color='red'
  25. :class='{ "is-icon": $vuetify.breakpoint.mdAndDown }'
  26. @click.native.stop='exit'
  27. )
  28. v-icon(color='red', :left='$vuetify.breakpoint.lgAndUp') close
  29. span.white--text(v-if='$vuetify.breakpoint.lgAndUp') {{ $t('common:actions.discard') }}
  30. v-content
  31. component(:is='currentEditor')
  32. editor-modal-properties(v-model='dialogProps')
  33. editor-modal-editorselect(v-model='dialogEditorSelector')
  34. editor-modal-unsaved(v-model='dialogUnsaved', @discard='exitGo')
  35. loader(v-model='dialogProgress', :title='$t(`editor:save.processing`)', :subtitle='$t(`editor:save.pleaseWait`)')
  36. v-snackbar(
  37. :color='notification.style'
  38. bottom,
  39. right,
  40. multi-line,
  41. v-model='notificationState'
  42. )
  43. .text-xs-left
  44. v-icon.mr-3(dark) {{ notification.icon }}
  45. span {{ notification.message }}
  46. </template>
  47. <script>
  48. import _ from 'lodash'
  49. import { get, sync } from 'vuex-pathify'
  50. import { AtomSpinner } from 'epic-spinners'
  51. import { Base64 } from 'js-base64'
  52. import createPageMutation from 'gql/editor/create.gql'
  53. import updatePageMutation from 'gql/editor/update.gql'
  54. import editorStore from '@/store/editor'
  55. /* global WIKI */
  56. WIKI.$store.registerModule('editor', editorStore)
  57. export default {
  58. components: {
  59. AtomSpinner,
  60. editorCode: () => import(/* webpackChunkName: "editor-code", webpackMode: "lazy" */ './editor/editor-code.vue'),
  61. editorMarkdown: () => import(/* webpackChunkName: "editor-markdown", webpackMode: "lazy" */ './editor/editor-markdown.vue'),
  62. editorWysiwyg: () => import(/* webpackChunkName: "editor-wysiwyg", webpackMode: "lazy" */ './editor/editor-wysiwyg.vue'),
  63. editorModalEditorselect: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-editorselect.vue'),
  64. editorModalProperties: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-properties.vue'),
  65. editorModalUnsaved: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-unsaved.vue')
  66. },
  67. props: {
  68. locale: {
  69. type: String,
  70. default: 'en'
  71. },
  72. path: {
  73. type: String,
  74. default: 'home'
  75. },
  76. title: {
  77. type: String,
  78. default: 'Untitled Page'
  79. },
  80. description: {
  81. type: String,
  82. default: ''
  83. },
  84. tags: {
  85. type: Array,
  86. default: () => ([])
  87. },
  88. isPublished: {
  89. type: Boolean,
  90. default: true
  91. },
  92. initEditor: {
  93. type: String,
  94. default: null
  95. },
  96. initMode: {
  97. type: String,
  98. default: 'create'
  99. },
  100. initContent: {
  101. type: String,
  102. default: null
  103. },
  104. pageId: {
  105. type: Number,
  106. default: 0
  107. }
  108. },
  109. data() {
  110. return {
  111. dialogProps: false,
  112. dialogProgress: false,
  113. dialogEditorSelector: false,
  114. dialogUnsaved: false,
  115. initContentParsed: ''
  116. }
  117. },
  118. computed: {
  119. currentEditor: sync('editor/editor'),
  120. darkMode: get('site/dark'),
  121. mode: get('editor/mode'),
  122. notification: get('notification'),
  123. notificationState: sync('notification@isActive')
  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.success'),
  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.success'),
  227. style: 'success',
  228. icon: 'check'
  229. })
  230. } else {
  231. throw new Error(_.get(resp, 'responseResult.message'))
  232. }
  233. }
  234. } catch (err) {
  235. this.$store.commit('showNotification', {
  236. message: err.message,
  237. style: 'error',
  238. icon: 'warning'
  239. })
  240. }
  241. this.hideProgressDialog()
  242. },
  243. async exit() {
  244. if (this.initContentParsed !== this.$store.get('editor/content')) {
  245. this.dialogUnsaved = true
  246. } else {
  247. this.exitGo()
  248. }
  249. },
  250. exitGo() {
  251. this.$store.commit(`loadingStart`, 'editor-close')
  252. this.currentEditor = ''
  253. _.delay(() => {
  254. if (this.$store.get('editor/mode') === 'create') {
  255. window.location.assign(`/`)
  256. } else {
  257. window.location.assign(`/${this.$store.get('page/path')}`)
  258. }
  259. }, 500)
  260. }
  261. }
  262. }
  263. </script>
  264. <style lang='scss'>
  265. .editor {
  266. background-color: mc('grey', '900') !important;
  267. min-height: 100vh;
  268. }
  269. .atom-spinner.is-inline {
  270. display: inline-block;
  271. }
  272. </style>