editor.vue 11 KB

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