editor.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <template lang="pug">
  2. v-app.editor(:dark='darkMode')
  3. nav-header(dense)
  4. template(slot='mid')
  5. v-spacer
  6. .subtitle-1.grey--text {{currentPageTitle}}
  7. v-spacer
  8. template(slot='actions')
  9. v-btn.animated.fadeInDown(
  10. text
  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') mdi-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. text
  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') mdi-tag-text-outline
  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. text
  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') mdi-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. exitConfirmed: false,
  115. initContentParsed: ''
  116. }
  117. },
  118. computed: {
  119. currentEditor: sync('editor/editor'),
  120. darkMode: get('site/dark'),
  121. activeModal: sync('editor/activeModal'),
  122. mode: get('editor/mode'),
  123. welcomeMode() { return this.mode === `create` && this.path === `home` },
  124. currentPageTitle: get('page/title')
  125. },
  126. watch: {
  127. currentEditor(newValue, oldValue) {
  128. if (newValue !== '' && this.mode === 'create') {
  129. _.delay(() => {
  130. this.dialogProps = true
  131. }, 500)
  132. }
  133. }
  134. },
  135. created() {
  136. this.$store.commit('page/SET_ID', this.pageId)
  137. this.$store.commit('page/SET_DESCRIPTION', this.description)
  138. this.$store.commit('page/SET_IS_PUBLISHED', this.isPublished)
  139. this.$store.commit('page/SET_LOCALE', this.locale)
  140. this.$store.commit('page/SET_PATH', this.path)
  141. this.$store.commit('page/SET_TAGS', this.tags)
  142. this.$store.commit('page/SET_TITLE', this.title)
  143. this.$store.commit('page/SET_MODE', 'edit')
  144. },
  145. mounted() {
  146. this.$store.set('editor/mode', this.initMode || 'create')
  147. this.initContentParsed = this.initContent ? Base64.decode(this.initContent) : '# Header\n\nYour content here'
  148. this.$store.set('editor/content', this.initContentParsed)
  149. if (this.mode === 'create') {
  150. _.delay(() => {
  151. this.dialogEditorSelector = true
  152. }, 500)
  153. } else {
  154. this.currentEditor = `editor${_.startCase(this.initEditor || 'markdown')}`
  155. }
  156. window.onbeforeunload = () => {
  157. if (!this.exitConfirmed && this.initContentParsed !== this.$store.get('editor/content')) {
  158. return 'You have unsaved edits. Are you sure you want to leave the editor?'
  159. } else {
  160. return undefined
  161. }
  162. }
  163. },
  164. methods: {
  165. openPropsModal(name) {
  166. this.dialogProps = true
  167. },
  168. showProgressDialog(textKey) {
  169. this.dialogProgress = true
  170. },
  171. hideProgressDialog() {
  172. this.dialogProgress = false
  173. },
  174. async save() {
  175. this.showProgressDialog('saving')
  176. try {
  177. if (this.$store.get('editor/mode') === 'create') {
  178. // --------------------------------------------
  179. // -> CREATE PAGE
  180. // --------------------------------------------
  181. let resp = await this.$apollo.mutate({
  182. mutation: createPageMutation,
  183. variables: {
  184. content: this.$store.get('editor/content'),
  185. description: this.$store.get('page/description'),
  186. editor: 'markdown',
  187. locale: this.$store.get('page/locale'),
  188. isPrivate: false,
  189. isPublished: this.$store.get('page/isPublished'),
  190. path: this.$store.get('page/path'),
  191. publishEndDate: this.$store.get('page/publishEndDate') || '',
  192. publishStartDate: this.$store.get('page/publishStartDate') || '',
  193. tags: this.$store.get('page/tags'),
  194. title: this.$store.get('page/title')
  195. }
  196. })
  197. resp = _.get(resp, 'data.pages.create', {})
  198. if (_.get(resp, 'responseResult.succeeded')) {
  199. this.$store.commit('showNotification', {
  200. message: this.$t('editor:save.createSuccess'),
  201. style: 'success',
  202. icon: 'check'
  203. })
  204. this.$store.set('editor/id', _.get(resp, 'page.id'))
  205. this.$store.set('editor/mode', 'update')
  206. window.location.assign(`/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  207. } else {
  208. throw new Error(_.get(resp, 'responseResult.message'))
  209. }
  210. } else {
  211. // --------------------------------------------
  212. // -> UPDATE EXISTING PAGE
  213. // --------------------------------------------
  214. let resp = await this.$apollo.mutate({
  215. mutation: updatePageMutation,
  216. variables: {
  217. id: this.$store.get('page/id'),
  218. content: this.$store.get('editor/content'),
  219. description: this.$store.get('page/description'),
  220. editor: 'markdown',
  221. locale: this.$store.get('page/locale'),
  222. isPrivate: false,
  223. isPublished: this.$store.get('page/isPublished'),
  224. path: this.$store.get('page/path'),
  225. publishEndDate: this.$store.get('page/publishEndDate') || '',
  226. publishStartDate: this.$store.get('page/publishStartDate') || '',
  227. tags: this.$store.get('page/tags'),
  228. title: this.$store.get('page/title')
  229. }
  230. })
  231. resp = _.get(resp, 'data.pages.update', {})
  232. if (_.get(resp, 'responseResult.succeeded')) {
  233. this.$store.commit('showNotification', {
  234. message: this.$t('editor:save.updateSuccess'),
  235. style: 'success',
  236. icon: 'check'
  237. })
  238. } else {
  239. throw new Error(_.get(resp, 'responseResult.message'))
  240. }
  241. }
  242. this.initContentParsed = this.$store.get('editor/content')
  243. } catch (err) {
  244. this.$store.commit('showNotification', {
  245. message: err.message,
  246. style: 'error',
  247. icon: 'warning'
  248. })
  249. }
  250. this.hideProgressDialog()
  251. },
  252. async exit() {
  253. if (this.initContentParsed !== this.$store.get('editor/content')) {
  254. this.dialogUnsaved = true
  255. } else {
  256. this.exitGo()
  257. }
  258. },
  259. exitGo() {
  260. this.$store.commit(`loadingStart`, 'editor-close')
  261. this.currentEditor = ''
  262. this.exitConfirmed = true
  263. _.delay(() => {
  264. if (this.$store.get('editor/mode') === 'create') {
  265. window.location.assign(`/`)
  266. } else {
  267. window.location.assign(`/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  268. }
  269. }, 500)
  270. }
  271. }
  272. }
  273. </script>
  274. <style lang='scss'>
  275. .editor {
  276. background-color: mc('grey', '900') !important;
  277. min-height: 100vh;
  278. .application--wrap {
  279. background-color: mc('grey', '900');
  280. }
  281. }
  282. .atom-spinner.is-inline {
  283. display: inline-block;
  284. }
  285. </style>