editor.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. effectivePermissions: {
  131. type: String,
  132. default: ''
  133. }
  134. },
  135. data() {
  136. return {
  137. isSaving: false,
  138. isConflict: false,
  139. dialogProps: false,
  140. dialogProgress: false,
  141. dialogEditorSelector: false,
  142. dialogUnsaved: false,
  143. exitConfirmed: false,
  144. initContentParsed: '',
  145. savedState: {
  146. description: '',
  147. isPublished: false,
  148. publishEndDate: '',
  149. publishStartDate: '',
  150. tags: '',
  151. title: ''
  152. }
  153. }
  154. },
  155. computed: {
  156. currentEditor: sync('editor/editor'),
  157. activeModal: sync('editor/activeModal'),
  158. mode: get('editor/mode'),
  159. welcomeMode() { return this.mode === `create` && this.path === `home` },
  160. currentPageTitle: sync('page/title'),
  161. checkoutDateActive: sync('editor/checkoutDateActive'),
  162. isDirty () {
  163. return _.some([
  164. this.initContentParsed !== this.$store.get('editor/content'),
  165. this.locale !== this.$store.get('page/locale'),
  166. this.path !== this.$store.get('page/path'),
  167. this.savedState.title !== this.$store.get('page/title'),
  168. this.savedState.description !== this.$store.get('page/description'),
  169. this.savedState.tags !== this.$store.get('page/tags'),
  170. this.savedState.isPublished !== this.$store.get('page/isPublished')
  171. ], Boolean)
  172. }
  173. },
  174. watch: {
  175. currentEditor(newValue, oldValue) {
  176. if (newValue !== '' && this.mode === 'create') {
  177. _.delay(() => {
  178. this.dialogProps = true
  179. }, 500)
  180. }
  181. }
  182. },
  183. created() {
  184. this.$store.commit('page/SET_ID', this.pageId)
  185. this.$store.commit('page/SET_DESCRIPTION', this.description)
  186. this.$store.commit('page/SET_IS_PUBLISHED', this.isPublished)
  187. this.$store.commit('page/SET_LOCALE', this.locale)
  188. this.$store.commit('page/SET_PATH', this.path)
  189. this.$store.commit('page/SET_TAGS', this.tags)
  190. this.$store.commit('page/SET_TITLE', this.title)
  191. this.$store.commit('page/SET_MODE', 'edit')
  192. this.setCurrentSavedState()
  193. this.checkoutDateActive = this.checkoutDate
  194. if (this.effectivePermissions) {
  195. this.$store.set('page/effectivePermissions', JSON.parse(Buffer.from(this.effectivePermissions, 'base64').toString()))
  196. }
  197. },
  198. mounted() {
  199. this.$store.set('editor/mode', this.initMode || 'create')
  200. this.initContentParsed = this.initContent ? Base64.decode(this.initContent) : ''
  201. this.$store.set('editor/content', this.initContentParsed)
  202. if (this.mode === 'create' && !this.initEditor) {
  203. _.delay(() => {
  204. this.dialogEditorSelector = true
  205. }, 500)
  206. } else {
  207. this.currentEditor = `editor${_.startCase(this.initEditor || 'markdown')}`
  208. }
  209. window.onbeforeunload = () => {
  210. if (!this.exitConfirmed && this.initContentParsed !== this.$store.get('editor/content')) {
  211. return this.$t('editor:unsavedWarning')
  212. } else {
  213. return undefined
  214. }
  215. }
  216. this.$root.$on('resetEditorConflict', () => {
  217. this.isConflict = false
  218. })
  219. // this.$store.set('editor/mode', 'edit')
  220. // this.currentEditor = `editorApi`
  221. },
  222. methods: {
  223. openPropsModal(name) {
  224. this.dialogProps = true
  225. },
  226. showProgressDialog(textKey) {
  227. this.dialogProgress = true
  228. },
  229. hideProgressDialog() {
  230. this.dialogProgress = false
  231. },
  232. openConflict() {
  233. this.$root.$emit('saveConflict')
  234. },
  235. async save({ rethrow = false, overwrite = false } = {}) {
  236. this.showProgressDialog('saving')
  237. this.isSaving = true
  238. const saveTimeoutHandle = setTimeout(() => {
  239. throw new Error('Save operation timed out.')
  240. }, 30000)
  241. try {
  242. if (this.$store.get('editor/mode') === 'create') {
  243. // --------------------------------------------
  244. // -> CREATE PAGE
  245. // --------------------------------------------
  246. let resp = await this.$apollo.mutate({
  247. mutation: createPageMutation,
  248. variables: {
  249. content: this.$store.get('editor/content'),
  250. description: this.$store.get('page/description'),
  251. editor: this.$store.get('editor/editorKey'),
  252. locale: this.$store.get('page/locale'),
  253. isPrivate: false,
  254. isPublished: this.$store.get('page/isPublished'),
  255. path: this.$store.get('page/path'),
  256. publishEndDate: this.$store.get('page/publishEndDate') || '',
  257. publishStartDate: this.$store.get('page/publishStartDate') || '',
  258. tags: this.$store.get('page/tags'),
  259. title: this.$store.get('page/title')
  260. }
  261. })
  262. resp = _.get(resp, 'data.pages.create', {})
  263. if (_.get(resp, 'responseResult.succeeded')) {
  264. this.checkoutDateActive = _.get(resp, 'page.updatedAt', this.checkoutDateActive)
  265. this.isConflict = false
  266. this.$store.commit('showNotification', {
  267. message: this.$t('editor:save.createSuccess'),
  268. style: 'success',
  269. icon: 'check'
  270. })
  271. this.$store.set('editor/id', _.get(resp, 'page.id'))
  272. this.$store.set('editor/mode', 'update')
  273. this.exitConfirmed = true
  274. window.location.assign(`/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  275. } else {
  276. throw new Error(_.get(resp, 'responseResult.message'))
  277. }
  278. } else {
  279. // --------------------------------------------
  280. // -> UPDATE EXISTING PAGE
  281. // --------------------------------------------
  282. const conflictResp = await this.$apollo.query({
  283. query: gql`
  284. query ($id: Int!, $checkoutDate: Date!) {
  285. pages {
  286. checkConflicts(id: $id, checkoutDate: $checkoutDate)
  287. }
  288. }
  289. `,
  290. fetchPolicy: 'network-only',
  291. variables: {
  292. id: this.pageId,
  293. checkoutDate: this.checkoutDateActive
  294. }
  295. })
  296. if (_.get(conflictResp, 'data.pages.checkConflicts', false)) {
  297. this.$root.$emit('saveConflict')
  298. throw new Error(this.$t('editor:conflict.warning'))
  299. }
  300. let resp = await this.$apollo.mutate({
  301. mutation: updatePageMutation,
  302. variables: {
  303. id: this.$store.get('page/id'),
  304. content: this.$store.get('editor/content'),
  305. description: this.$store.get('page/description'),
  306. editor: this.$store.get('editor/editorKey'),
  307. locale: this.$store.get('page/locale'),
  308. isPrivate: false,
  309. isPublished: this.$store.get('page/isPublished'),
  310. path: this.$store.get('page/path'),
  311. publishEndDate: this.$store.get('page/publishEndDate') || '',
  312. publishStartDate: this.$store.get('page/publishStartDate') || '',
  313. tags: this.$store.get('page/tags'),
  314. title: this.$store.get('page/title')
  315. }
  316. })
  317. resp = _.get(resp, 'data.pages.update', {})
  318. if (_.get(resp, 'responseResult.succeeded')) {
  319. this.checkoutDateActive = _.get(resp, 'page.updatedAt', this.checkoutDateActive)
  320. this.isConflict = false
  321. this.$store.commit('showNotification', {
  322. message: this.$t('editor:save.updateSuccess'),
  323. style: 'success',
  324. icon: 'check'
  325. })
  326. if (this.locale !== this.$store.get('page/locale') || this.path !== this.$store.get('page/path')) {
  327. _.delay(() => {
  328. window.location.replace(`/e/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  329. }, 1000)
  330. }
  331. } else {
  332. throw new Error(_.get(resp, 'responseResult.message'))
  333. }
  334. }
  335. this.initContentParsed = this.$store.get('editor/content')
  336. this.setCurrentSavedState()
  337. } catch (err) {
  338. this.$store.commit('showNotification', {
  339. message: err.message,
  340. style: 'error',
  341. icon: 'warning'
  342. })
  343. if (rethrow === true) {
  344. clearTimeout(saveTimeoutHandle)
  345. this.isSaving = false
  346. this.hideProgressDialog()
  347. throw err
  348. }
  349. }
  350. clearTimeout(saveTimeoutHandle)
  351. this.isSaving = false
  352. this.hideProgressDialog()
  353. },
  354. async saveAndClose() {
  355. try {
  356. if (this.$store.get('editor/mode') === 'create') {
  357. await this.save()
  358. } else {
  359. await this.save({ rethrow: true })
  360. await this.exit()
  361. }
  362. } catch (err) {
  363. // Error is already handled
  364. }
  365. },
  366. async exit() {
  367. if (this.isDirty) {
  368. this.dialogUnsaved = true
  369. } else {
  370. this.exitGo()
  371. }
  372. },
  373. exitGo() {
  374. this.$store.commit(`loadingStart`, 'editor-close')
  375. this.currentEditor = ''
  376. this.exitConfirmed = true
  377. _.delay(() => {
  378. if (this.$store.get('editor/mode') === 'create') {
  379. window.location.assign(`/`)
  380. } else {
  381. window.location.assign(`/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  382. }
  383. }, 500)
  384. },
  385. setCurrentSavedState () {
  386. this.savedState = {
  387. description: this.$store.get('page/description'),
  388. isPublished: this.$store.get('page/isPublished'),
  389. publishEndDate: this.$store.get('page/publishEndDate') || '',
  390. publishStartDate: this.$store.get('page/publishStartDate') || '',
  391. tags: this.$store.get('page/tags'),
  392. title: this.$store.get('page/title')
  393. }
  394. }
  395. },
  396. apollo: {
  397. isConflict: {
  398. query: gql`
  399. query ($id: Int!, $checkoutDate: Date!) {
  400. pages {
  401. checkConflicts(id: $id, checkoutDate: $checkoutDate)
  402. }
  403. }
  404. `,
  405. fetchPolicy: 'network-only',
  406. pollInterval: 5000,
  407. variables () {
  408. return {
  409. id: this.pageId,
  410. checkoutDate: this.checkoutDateActive
  411. }
  412. },
  413. update: (data) => _.cloneDeep(data.pages.checkConflicts),
  414. skip () {
  415. return this.mode === 'create' || this.isSaving || !this.isDirty
  416. }
  417. }
  418. }
  419. }
  420. </script>
  421. <style lang='scss'>
  422. .editor {
  423. background-color: mc('grey', '900') !important;
  424. min-height: 100vh;
  425. .application--wrap {
  426. background-color: mc('grey', '900');
  427. }
  428. &-title-input input {
  429. text-align: center;
  430. }
  431. }
  432. .atom-spinner.is-inline {
  433. display: inline-block;
  434. }
  435. </style>