editor.vue 14 KB

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