You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
11 KiB

6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
  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. editorApi: () => import(/* webpackChunkName: "editor-api", webpackMode: "lazy" */ './editor/editor-api.vue'),
  59. editorCode: () => import(/* webpackChunkName: "editor-code", webpackMode: "lazy" */ './editor/editor-code.vue'),
  60. editorCkeditor: () => import(/* webpackChunkName: "editor-ckeditor", webpackMode: "lazy" */ './editor/editor-ckeditor.vue'),
  61. editorMarkdown: () => import(/* webpackChunkName: "editor-markdown", webpackMode: "lazy" */ './editor/editor-markdown.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. // this.$store.set('editor/mode', 'edit')
  166. // this.currentEditor = `editorApi`
  167. },
  168. methods: {
  169. openPropsModal(name) {
  170. this.dialogProps = true
  171. },
  172. showProgressDialog(textKey) {
  173. this.dialogProgress = true
  174. },
  175. hideProgressDialog() {
  176. this.dialogProgress = false
  177. },
  178. async save() {
  179. this.showProgressDialog('saving')
  180. try {
  181. if (this.$store.get('editor/mode') === 'create') {
  182. // --------------------------------------------
  183. // -> CREATE PAGE
  184. // --------------------------------------------
  185. let resp = await this.$apollo.mutate({
  186. mutation: createPageMutation,
  187. variables: {
  188. content: this.$store.get('editor/content'),
  189. description: this.$store.get('page/description'),
  190. editor: this.$store.get('editor/editorKey'),
  191. locale: this.$store.get('page/locale'),
  192. isPrivate: false,
  193. isPublished: this.$store.get('page/isPublished'),
  194. path: this.$store.get('page/path'),
  195. publishEndDate: this.$store.get('page/publishEndDate') || '',
  196. publishStartDate: this.$store.get('page/publishStartDate') || '',
  197. tags: this.$store.get('page/tags'),
  198. title: this.$store.get('page/title')
  199. }
  200. })
  201. resp = _.get(resp, 'data.pages.create', {})
  202. if (_.get(resp, 'responseResult.succeeded')) {
  203. this.$store.commit('showNotification', {
  204. message: this.$t('editor:save.createSuccess'),
  205. style: 'success',
  206. icon: 'check'
  207. })
  208. this.$store.set('editor/id', _.get(resp, 'page.id'))
  209. this.$store.set('editor/mode', 'update')
  210. this.exitConfirmed = true
  211. window.location.assign(`/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  212. } else {
  213. throw new Error(_.get(resp, 'responseResult.message'))
  214. }
  215. } else {
  216. // --------------------------------------------
  217. // -> UPDATE EXISTING PAGE
  218. // --------------------------------------------
  219. let resp = await this.$apollo.mutate({
  220. mutation: updatePageMutation,
  221. variables: {
  222. id: this.$store.get('page/id'),
  223. content: this.$store.get('editor/content'),
  224. description: this.$store.get('page/description'),
  225. editor: this.$store.get('editor/editorKey'),
  226. locale: this.$store.get('page/locale'),
  227. isPrivate: false,
  228. isPublished: this.$store.get('page/isPublished'),
  229. path: this.$store.get('page/path'),
  230. publishEndDate: this.$store.get('page/publishEndDate') || '',
  231. publishStartDate: this.$store.get('page/publishStartDate') || '',
  232. tags: this.$store.get('page/tags'),
  233. title: this.$store.get('page/title')
  234. }
  235. })
  236. resp = _.get(resp, 'data.pages.update', {})
  237. if (_.get(resp, 'responseResult.succeeded')) {
  238. this.$store.commit('showNotification', {
  239. message: this.$t('editor:save.updateSuccess'),
  240. style: 'success',
  241. icon: 'check'
  242. })
  243. if (this.locale !== this.$store.get('page/locale') || this.path !== this.$store.get('page/path')) {
  244. _.delay(() => {
  245. window.location.replace(`/e/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  246. }, 1000)
  247. }
  248. } else {
  249. throw new Error(_.get(resp, 'responseResult.message'))
  250. }
  251. }
  252. this.initContentParsed = this.$store.get('editor/content')
  253. } catch (err) {
  254. this.$store.commit('showNotification', {
  255. message: err.message,
  256. style: 'error',
  257. icon: 'warning'
  258. })
  259. }
  260. this.hideProgressDialog()
  261. },
  262. async exit() {
  263. if (this.initContentParsed !== this.$store.get('editor/content')) {
  264. this.dialogUnsaved = true
  265. } else {
  266. this.exitGo()
  267. }
  268. },
  269. exitGo() {
  270. this.$store.commit(`loadingStart`, 'editor-close')
  271. this.currentEditor = ''
  272. this.exitConfirmed = true
  273. _.delay(() => {
  274. if (this.$store.get('editor/mode') === 'create') {
  275. window.location.assign(`/`)
  276. } else {
  277. window.location.assign(`/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  278. }
  279. }, 500)
  280. }
  281. }
  282. }
  283. </script>
  284. <style lang='scss'>
  285. .editor {
  286. background-color: mc('grey', '900') !important;
  287. min-height: 100vh;
  288. .application--wrap {
  289. background-color: mc('grey', '900');
  290. }
  291. }
  292. .atom-spinner.is-inline {
  293. display: inline-block;
  294. }
  295. </style>