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.

433 lines
14 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
6 years ago
  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. }
  142. },
  143. computed: {
  144. currentEditor: sync('editor/editor'),
  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 this.$t('editor:unsavedWarning')
  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. const saveTimeoutHandle = setTimeout(() => {
  223. throw new Error('Save operation timed out.')
  224. }, 30000)
  225. try {
  226. if (this.$store.get('editor/mode') === 'create') {
  227. // --------------------------------------------
  228. // -> CREATE PAGE
  229. // --------------------------------------------
  230. let resp = await this.$apollo.mutate({
  231. mutation: createPageMutation,
  232. variables: {
  233. content: this.$store.get('editor/content'),
  234. description: this.$store.get('page/description'),
  235. editor: this.$store.get('editor/editorKey'),
  236. locale: this.$store.get('page/locale'),
  237. isPrivate: false,
  238. isPublished: this.$store.get('page/isPublished'),
  239. path: this.$store.get('page/path'),
  240. publishEndDate: this.$store.get('page/publishEndDate') || '',
  241. publishStartDate: this.$store.get('page/publishStartDate') || '',
  242. tags: this.$store.get('page/tags'),
  243. title: this.$store.get('page/title')
  244. }
  245. })
  246. resp = _.get(resp, 'data.pages.create', {})
  247. if (_.get(resp, 'responseResult.succeeded')) {
  248. this.checkoutDateActive = _.get(resp, 'page.updatedAt', this.checkoutDateActive)
  249. this.isConflict = false
  250. this.$store.commit('showNotification', {
  251. message: this.$t('editor:save.createSuccess'),
  252. style: 'success',
  253. icon: 'check'
  254. })
  255. this.$store.set('editor/id', _.get(resp, 'page.id'))
  256. this.$store.set('editor/mode', 'update')
  257. this.exitConfirmed = true
  258. window.location.assign(`/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  259. } else {
  260. throw new Error(_.get(resp, 'responseResult.message'))
  261. }
  262. } else {
  263. // --------------------------------------------
  264. // -> UPDATE EXISTING PAGE
  265. // --------------------------------------------
  266. const conflictResp = await this.$apollo.query({
  267. query: gql`
  268. query ($id: Int!, $checkoutDate: Date!) {
  269. pages {
  270. checkConflicts(id: $id, checkoutDate: $checkoutDate)
  271. }
  272. }
  273. `,
  274. fetchPolicy: 'network-only',
  275. variables: {
  276. id: this.pageId,
  277. checkoutDate: this.checkoutDateActive
  278. }
  279. })
  280. if (_.get(conflictResp, 'data.pages.checkConflicts', false)) {
  281. this.$root.$emit('saveConflict')
  282. throw new Error(this.$t('editor:conflict.warning'))
  283. }
  284. let resp = await this.$apollo.mutate({
  285. mutation: updatePageMutation,
  286. variables: {
  287. id: this.$store.get('page/id'),
  288. content: this.$store.get('editor/content'),
  289. description: this.$store.get('page/description'),
  290. editor: this.$store.get('editor/editorKey'),
  291. locale: this.$store.get('page/locale'),
  292. isPrivate: false,
  293. isPublished: this.$store.get('page/isPublished'),
  294. path: this.$store.get('page/path'),
  295. publishEndDate: this.$store.get('page/publishEndDate') || '',
  296. publishStartDate: this.$store.get('page/publishStartDate') || '',
  297. tags: this.$store.get('page/tags'),
  298. title: this.$store.get('page/title')
  299. }
  300. })
  301. resp = _.get(resp, 'data.pages.update', {})
  302. if (_.get(resp, 'responseResult.succeeded')) {
  303. this.checkoutDateActive = _.get(resp, 'page.updatedAt', this.checkoutDateActive)
  304. this.isConflict = false
  305. this.$store.commit('showNotification', {
  306. message: this.$t('editor:save.updateSuccess'),
  307. style: 'success',
  308. icon: 'check'
  309. })
  310. if (this.locale !== this.$store.get('page/locale') || this.path !== this.$store.get('page/path')) {
  311. _.delay(() => {
  312. window.location.replace(`/e/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  313. }, 1000)
  314. }
  315. } else {
  316. throw new Error(_.get(resp, 'responseResult.message'))
  317. }
  318. }
  319. this.initContentParsed = this.$store.get('editor/content')
  320. } catch (err) {
  321. this.$store.commit('showNotification', {
  322. message: err.message,
  323. style: 'error',
  324. icon: 'warning'
  325. })
  326. if (rethrow === true) {
  327. clearTimeout(saveTimeoutHandle)
  328. this.isSaving = false
  329. this.hideProgressDialog()
  330. throw err
  331. }
  332. }
  333. clearTimeout(saveTimeoutHandle)
  334. this.isSaving = false
  335. this.hideProgressDialog()
  336. },
  337. async saveAndClose() {
  338. try {
  339. if (this.$store.get('editor/mode') === 'create') {
  340. await this.save()
  341. } else {
  342. await this.save({ rethrow: true })
  343. await this.exit()
  344. }
  345. } catch (err) {
  346. // Error is already handled
  347. }
  348. },
  349. async exit() {
  350. if (this.isDirty) {
  351. this.dialogUnsaved = true
  352. } else {
  353. this.exitGo()
  354. }
  355. },
  356. exitGo() {
  357. this.$store.commit(`loadingStart`, 'editor-close')
  358. this.currentEditor = ''
  359. this.exitConfirmed = true
  360. _.delay(() => {
  361. if (this.$store.get('editor/mode') === 'create') {
  362. window.location.assign(`/`)
  363. } else {
  364. window.location.assign(`/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  365. }
  366. }, 500)
  367. }
  368. },
  369. apollo: {
  370. isConflict: {
  371. query: gql`
  372. query ($id: Int!, $checkoutDate: Date!) {
  373. pages {
  374. checkConflicts(id: $id, checkoutDate: $checkoutDate)
  375. }
  376. }
  377. `,
  378. fetchPolicy: 'network-only',
  379. pollInterval: 5000,
  380. variables () {
  381. return {
  382. id: this.pageId,
  383. checkoutDate: this.checkoutDateActive
  384. }
  385. },
  386. update: (data) => _.cloneDeep(data.pages.checkConflicts),
  387. skip () {
  388. return this.mode === 'create' || this.isSaving || !this.isDirty
  389. }
  390. }
  391. }
  392. }
  393. </script>
  394. <style lang='scss'>
  395. .editor {
  396. background-color: mc('grey', '900') !important;
  397. min-height: 100vh;
  398. .application--wrap {
  399. background-color: mc('grey', '900');
  400. }
  401. &-title-input input {
  402. text-align: center;
  403. }
  404. }
  405. .atom-spinner.is-inline {
  406. display: inline-block;
  407. }
  408. </style>