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.

429 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='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 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. await this.save({ rethrow: true })
  340. await this.exit()
  341. } catch (err) {
  342. // Error is already handled
  343. }
  344. },
  345. async exit() {
  346. if (this.isDirty) {
  347. this.dialogUnsaved = true
  348. } else {
  349. this.exitGo()
  350. }
  351. },
  352. exitGo() {
  353. this.$store.commit(`loadingStart`, 'editor-close')
  354. this.currentEditor = ''
  355. this.exitConfirmed = true
  356. _.delay(() => {
  357. if (this.$store.get('editor/mode') === 'create') {
  358. window.location.assign(`/`)
  359. } else {
  360. window.location.assign(`/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  361. }
  362. }, 500)
  363. }
  364. },
  365. apollo: {
  366. isConflict: {
  367. query: gql`
  368. query ($id: Int!, $checkoutDate: Date!) {
  369. pages {
  370. checkConflicts(id: $id, checkoutDate: $checkoutDate)
  371. }
  372. }
  373. `,
  374. fetchPolicy: 'network-only',
  375. pollInterval: 5000,
  376. variables () {
  377. return {
  378. id: this.pageId,
  379. checkoutDate: this.checkoutDateActive
  380. }
  381. },
  382. update: (data) => _.cloneDeep(data.pages.checkConflicts),
  383. skip () {
  384. return this.mode === 'create' || this.isSaving || !this.isDirty
  385. }
  386. }
  387. }
  388. }
  389. </script>
  390. <style lang='scss'>
  391. .editor {
  392. background-color: mc('grey', '900') !important;
  393. min-height: 100vh;
  394. .application--wrap {
  395. background-color: mc('grey', '900');
  396. }
  397. &-title-input input {
  398. text-align: center;
  399. }
  400. }
  401. .atom-spinner.is-inline {
  402. display: inline-block;
  403. }
  404. </style>