editor.vue 15 KB

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