editor.vue 13 KB

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