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.

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