editor.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. 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. },
  65. props: {
  66. locale: {
  67. type: String,
  68. default: 'en'
  69. },
  70. path: {
  71. type: String,
  72. default: 'home'
  73. },
  74. title: {
  75. type: String,
  76. default: 'Untitled Page'
  77. },
  78. description: {
  79. type: String,
  80. default: ''
  81. },
  82. tags: {
  83. type: Array,
  84. default: () => ([])
  85. },
  86. isPublished: {
  87. type: Boolean,
  88. default: true
  89. },
  90. initEditor: {
  91. type: String,
  92. default: null
  93. },
  94. initMode: {
  95. type: String,
  96. default: 'create'
  97. },
  98. initContent: {
  99. type: String,
  100. default: null
  101. },
  102. pageId: {
  103. type: Number,
  104. default: 0
  105. }
  106. },
  107. data() {
  108. return {
  109. dialogProps: false,
  110. dialogProgress: false,
  111. dialogEditorSelector: false,
  112. dialogUnsaved: false,
  113. initContentParsed: ''
  114. }
  115. },
  116. computed: {
  117. currentEditor: sync('editor/editor'),
  118. darkMode: get('site/dark'),
  119. activeModal: sync('editor/activeModal'),
  120. mode: get('editor/mode'),
  121. welcomeMode() { return this.mode === `create` && this.path === `home` },
  122. currentPageTitle: get('page/title')
  123. },
  124. watch: {
  125. currentEditor(newValue, oldValue) {
  126. if (newValue !== '' && this.mode === 'create') {
  127. _.delay(() => {
  128. this.dialogProps = true
  129. }, 500)
  130. }
  131. }
  132. },
  133. created() {
  134. this.$store.commit('page/SET_ID', this.pageId)
  135. this.$store.commit('page/SET_DESCRIPTION', this.description)
  136. this.$store.commit('page/SET_IS_PUBLISHED', this.isPublished)
  137. this.$store.commit('page/SET_LOCALE', this.locale)
  138. this.$store.commit('page/SET_PATH', this.path)
  139. this.$store.commit('page/SET_TAGS', this.tags)
  140. this.$store.commit('page/SET_TITLE', this.title)
  141. this.$store.commit('page/SET_MODE', 'edit')
  142. },
  143. mounted() {
  144. this.$store.set('editor/mode', this.initMode || 'create')
  145. this.initContentParsed = this.initContent ? Base64.decode(this.initContent) : '# Header\n\nYour content here'
  146. this.$store.set('editor/content', this.initContentParsed)
  147. if (this.mode === 'create') {
  148. _.delay(() => {
  149. this.dialogEditorSelector = true
  150. }, 500)
  151. } else {
  152. this.currentEditor = `editor${_.startCase(this.initEditor || 'markdown')}`
  153. }
  154. },
  155. methods: {
  156. openPropsModal(name) {
  157. this.dialogProps = true
  158. },
  159. showProgressDialog(textKey) {
  160. this.dialogProgress = true
  161. },
  162. hideProgressDialog() {
  163. this.dialogProgress = false
  164. },
  165. async save() {
  166. this.showProgressDialog('saving')
  167. try {
  168. if (this.$store.get('editor/mode') === 'create') {
  169. // --------------------------------------------
  170. // -> CREATE PAGE
  171. // --------------------------------------------
  172. let resp = await this.$apollo.mutate({
  173. mutation: createPageMutation,
  174. variables: {
  175. content: this.$store.get('editor/content'),
  176. description: this.$store.get('page/description'),
  177. editor: 'markdown',
  178. locale: this.$store.get('page/locale'),
  179. isPrivate: false,
  180. isPublished: this.$store.get('page/isPublished'),
  181. path: this.$store.get('page/path'),
  182. publishEndDate: this.$store.get('page/publishEndDate') || '',
  183. publishStartDate: this.$store.get('page/publishStartDate') || '',
  184. tags: this.$store.get('page/tags'),
  185. title: this.$store.get('page/title')
  186. }
  187. })
  188. resp = _.get(resp, 'data.pages.create', {})
  189. if (_.get(resp, 'responseResult.succeeded')) {
  190. this.$store.commit('showNotification', {
  191. message: this.$t('editor:save.success'),
  192. style: 'success',
  193. icon: 'check'
  194. })
  195. this.$store.set('editor/id', _.get(resp, 'page.id'))
  196. this.$store.set('editor/mode', 'update')
  197. window.location.assign(`/${this.$store.get('page/path')}`)
  198. } else {
  199. throw new Error(_.get(resp, 'responseResult.message'))
  200. }
  201. } else {
  202. // --------------------------------------------
  203. // -> UPDATE EXISTING PAGE
  204. // --------------------------------------------
  205. let resp = await this.$apollo.mutate({
  206. mutation: updatePageMutation,
  207. variables: {
  208. id: this.$store.get('page/id'),
  209. content: this.$store.get('editor/content'),
  210. description: this.$store.get('page/description'),
  211. editor: 'markdown',
  212. locale: this.$store.get('page/locale'),
  213. isPrivate: false,
  214. isPublished: this.$store.get('page/isPublished'),
  215. path: this.$store.get('page/path'),
  216. publishEndDate: this.$store.get('page/publishEndDate') || '',
  217. publishStartDate: this.$store.get('page/publishStartDate') || '',
  218. tags: this.$store.get('page/tags'),
  219. title: this.$store.get('page/title')
  220. }
  221. })
  222. resp = _.get(resp, 'data.pages.update', {})
  223. if (_.get(resp, 'responseResult.succeeded')) {
  224. this.$store.commit('showNotification', {
  225. message: this.$t('editor:save.success'),
  226. style: 'success',
  227. icon: 'check'
  228. })
  229. } else {
  230. throw new Error(_.get(resp, 'responseResult.message'))
  231. }
  232. }
  233. this.initContentParsed = this.$store.get('editor/content')
  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. .application--wrap {
  269. background-color: mc('grey', '900');
  270. }
  271. }
  272. .atom-spinner.is-inline {
  273. display: inline-block;
  274. }
  275. </style>