editor.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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-divider.ml-3(vertical)
  35. v-content
  36. component(:is='currentEditor', :save='save')
  37. editor-modal-properties(v-model='dialogProps')
  38. editor-modal-editorselect(v-model='dialogEditorSelector')
  39. editor-modal-unsaved(v-model='dialogUnsaved', @discard='exitGo')
  40. component(:is='activeModal')
  41. loader(v-model='dialogProgress', :title='$t(`editor:save.processing`)', :subtitle='$t(`editor:save.pleaseWait`)')
  42. notify
  43. </template>
  44. <script>
  45. import _ from 'lodash'
  46. import { get, sync } from 'vuex-pathify'
  47. import { AtomSpinner } from 'epic-spinners'
  48. import { Base64 } from 'js-base64'
  49. import createPageMutation from 'gql/editor/create.gql'
  50. import updatePageMutation from 'gql/editor/update.gql'
  51. import editorStore from '../store/editor'
  52. /* global WIKI */
  53. WIKI.$store.registerModule('editor', editorStore)
  54. export default {
  55. i18nOptions: { namespaces: 'editor' },
  56. components: {
  57. AtomSpinner,
  58. editorCode: () => import(/* webpackChunkName: "editor-code", webpackMode: "lazy" */ './editor/editor-code.vue'),
  59. editorCkeditor: () => import(/* webpackChunkName: "editor-ckeditor", webpackMode: "lazy" */ './editor/editor-ckeditor.vue'),
  60. editorMarkdown: () => import(/* webpackChunkName: "editor-markdown", webpackMode: "lazy" */ './editor/editor-markdown.vue'),
  61. editorWysiwyg: () => import(/* webpackChunkName: "editor-wysiwyg", webpackMode: "lazy" */ './editor/editor-wysiwyg.vue'),
  62. editorModalEditorselect: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-editorselect.vue'),
  63. editorModalProperties: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-properties.vue'),
  64. editorModalUnsaved: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-unsaved.vue'),
  65. editorModalMedia: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-media.vue'),
  66. editorModalBlocks: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-blocks.vue')
  67. },
  68. props: {
  69. locale: {
  70. type: String,
  71. default: 'en'
  72. },
  73. path: {
  74. type: String,
  75. default: 'home'
  76. },
  77. title: {
  78. type: String,
  79. default: 'Untitled Page'
  80. },
  81. description: {
  82. type: String,
  83. default: ''
  84. },
  85. tags: {
  86. type: Array,
  87. default: () => ([])
  88. },
  89. isPublished: {
  90. type: Boolean,
  91. default: true
  92. },
  93. initEditor: {
  94. type: String,
  95. default: null
  96. },
  97. initMode: {
  98. type: String,
  99. default: 'create'
  100. },
  101. initContent: {
  102. type: String,
  103. default: null
  104. },
  105. pageId: {
  106. type: Number,
  107. default: 0
  108. }
  109. },
  110. data() {
  111. return {
  112. dialogProps: false,
  113. dialogProgress: false,
  114. dialogEditorSelector: false,
  115. dialogUnsaved: false,
  116. exitConfirmed: false,
  117. initContentParsed: ''
  118. }
  119. },
  120. computed: {
  121. currentEditor: sync('editor/editor'),
  122. darkMode: get('site/dark'),
  123. activeModal: sync('editor/activeModal'),
  124. mode: get('editor/mode'),
  125. welcomeMode() { return this.mode === `create` && this.path === `home` },
  126. currentPageTitle: get('page/title')
  127. },
  128. watch: {
  129. currentEditor(newValue, oldValue) {
  130. if (newValue !== '' && this.mode === 'create') {
  131. _.delay(() => {
  132. this.dialogProps = true
  133. }, 500)
  134. }
  135. }
  136. },
  137. created() {
  138. this.$store.commit('page/SET_ID', this.pageId)
  139. this.$store.commit('page/SET_DESCRIPTION', this.description)
  140. this.$store.commit('page/SET_IS_PUBLISHED', this.isPublished)
  141. this.$store.commit('page/SET_LOCALE', this.locale)
  142. this.$store.commit('page/SET_PATH', this.path)
  143. this.$store.commit('page/SET_TAGS', this.tags)
  144. this.$store.commit('page/SET_TITLE', this.title)
  145. this.$store.commit('page/SET_MODE', 'edit')
  146. },
  147. mounted() {
  148. this.$store.set('editor/mode', this.initMode || 'create')
  149. this.initContentParsed = this.initContent ? Base64.decode(this.initContent) : ''
  150. this.$store.set('editor/content', this.initContentParsed)
  151. if (this.mode === 'create') {
  152. _.delay(() => {
  153. this.dialogEditorSelector = true
  154. }, 500)
  155. } else {
  156. this.currentEditor = `editor${_.startCase(this.initEditor || 'markdown')}`
  157. }
  158. window.onbeforeunload = () => {
  159. if (!this.exitConfirmed && this.initContentParsed !== this.$store.get('editor/content')) {
  160. return 'You have unsaved edits. Are you sure you want to leave the editor?'
  161. } else {
  162. return undefined
  163. }
  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: this.$store.get('editor/editorKey'),
  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.createSuccess'),
  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. this.exitConfirmed = true
  209. window.location.assign(`/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  210. } else {
  211. throw new Error(_.get(resp, 'responseResult.message'))
  212. }
  213. } else {
  214. // --------------------------------------------
  215. // -> UPDATE EXISTING PAGE
  216. // --------------------------------------------
  217. let resp = await this.$apollo.mutate({
  218. mutation: updatePageMutation,
  219. variables: {
  220. id: this.$store.get('page/id'),
  221. content: this.$store.get('editor/content'),
  222. description: this.$store.get('page/description'),
  223. editor: this.$store.get('editor/editorKey'),
  224. locale: this.$store.get('page/locale'),
  225. isPrivate: false,
  226. isPublished: this.$store.get('page/isPublished'),
  227. path: this.$store.get('page/path'),
  228. publishEndDate: this.$store.get('page/publishEndDate') || '',
  229. publishStartDate: this.$store.get('page/publishStartDate') || '',
  230. tags: this.$store.get('page/tags'),
  231. title: this.$store.get('page/title')
  232. }
  233. })
  234. resp = _.get(resp, 'data.pages.update', {})
  235. if (_.get(resp, 'responseResult.succeeded')) {
  236. this.$store.commit('showNotification', {
  237. message: this.$t('editor:save.updateSuccess'),
  238. style: 'success',
  239. icon: 'check'
  240. })
  241. } else {
  242. throw new Error(_.get(resp, 'responseResult.message'))
  243. }
  244. }
  245. this.initContentParsed = this.$store.get('editor/content')
  246. } catch (err) {
  247. this.$store.commit('showNotification', {
  248. message: err.message,
  249. style: 'error',
  250. icon: 'warning'
  251. })
  252. }
  253. this.hideProgressDialog()
  254. },
  255. async exit() {
  256. if (this.initContentParsed !== this.$store.get('editor/content')) {
  257. this.dialogUnsaved = true
  258. } else {
  259. this.exitGo()
  260. }
  261. },
  262. exitGo() {
  263. this.$store.commit(`loadingStart`, 'editor-close')
  264. this.currentEditor = ''
  265. this.exitConfirmed = true
  266. _.delay(() => {
  267. if (this.$store.get('editor/mode') === 'create') {
  268. window.location.assign(`/`)
  269. } else {
  270. window.location.assign(`/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  271. }
  272. }, 500)
  273. }
  274. }
  275. }
  276. </script>
  277. <style lang='scss'>
  278. .editor {
  279. background-color: mc('grey', '900') !important;
  280. min-height: 100vh;
  281. .application--wrap {
  282. background-color: mc('grey', '900');
  283. }
  284. }
  285. .atom-spinner.is-inline {
  286. display: inline-block;
  287. }
  288. </style>