editor.vue 11 KB

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