editor.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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 editorStore from '../store/editor'
  64. /* global WIKI */
  65. WIKI.$store.registerModule('editor', editorStore)
  66. export default {
  67. i18nOptions: { namespaces: 'editor' },
  68. components: {
  69. AtomSpinner,
  70. StatusIndicator,
  71. editorApi: () => import(/* webpackChunkName: "editor-api", webpackMode: "lazy" */ './editor/editor-api.vue'),
  72. editorCode: () => import(/* webpackChunkName: "editor-code", webpackMode: "lazy" */ './editor/editor-code.vue'),
  73. editorCkeditor: () => import(/* webpackChunkName: "editor-ckeditor", webpackMode: "lazy" */ './editor/editor-ckeditor.vue'),
  74. editorMarkdown: () => import(/* webpackChunkName: "editor-markdown", webpackMode: "lazy" */ './editor/editor-markdown.vue'),
  75. editorRedirect: () => import(/* webpackChunkName: "editor-redirect", webpackMode: "lazy" */ './editor/editor-redirect.vue'),
  76. editorModalEditorselect: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-editorselect.vue'),
  77. editorModalProperties: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-properties.vue'),
  78. editorModalUnsaved: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-unsaved.vue'),
  79. editorModalMedia: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-media.vue'),
  80. editorModalBlocks: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-blocks.vue'),
  81. editorModalConflict: () => import(/* webpackChunkName: "editor-conflict", webpackMode: "lazy" */ './editor/editor-modal-conflict.vue')
  82. },
  83. props: {
  84. locale: {
  85. type: String,
  86. default: 'en'
  87. },
  88. path: {
  89. type: String,
  90. default: 'home'
  91. },
  92. title: {
  93. type: String,
  94. default: 'Untitled Page'
  95. },
  96. description: {
  97. type: String,
  98. default: ''
  99. },
  100. tags: {
  101. type: Array,
  102. default: () => ([])
  103. },
  104. isPublished: {
  105. type: Boolean,
  106. default: true
  107. },
  108. scriptCss: {
  109. type: String,
  110. default: ''
  111. },
  112. scriptJs: {
  113. type: String,
  114. default: ''
  115. },
  116. initEditor: {
  117. type: String,
  118. default: null
  119. },
  120. initMode: {
  121. type: String,
  122. default: 'create'
  123. },
  124. initContent: {
  125. type: String,
  126. default: null
  127. },
  128. pageId: {
  129. type: Number,
  130. default: 0
  131. },
  132. checkoutDate: {
  133. type: String,
  134. default: new Date().toISOString()
  135. },
  136. effectivePermissions: {
  137. type: String,
  138. default: ''
  139. }
  140. },
  141. data() {
  142. return {
  143. isSaving: false,
  144. isConflict: false,
  145. dialogProps: false,
  146. dialogProgress: false,
  147. dialogEditorSelector: false,
  148. dialogUnsaved: false,
  149. exitConfirmed: false,
  150. initContentParsed: '',
  151. savedState: {
  152. description: '',
  153. isPublished: false,
  154. publishEndDate: '',
  155. publishStartDate: '',
  156. tags: '',
  157. title: ''
  158. }
  159. }
  160. },
  161. computed: {
  162. currentEditor: sync('editor/editor'),
  163. activeModal: sync('editor/activeModal'),
  164. mode: get('editor/mode'),
  165. welcomeMode() { return this.mode === `create` && this.path === `home` },
  166. currentPageTitle: sync('page/title'),
  167. checkoutDateActive: sync('editor/checkoutDateActive'),
  168. isDirty () {
  169. return _.some([
  170. this.initContentParsed !== this.$store.get('editor/content'),
  171. this.locale !== this.$store.get('page/locale'),
  172. this.path !== this.$store.get('page/path'),
  173. this.savedState.title !== this.$store.get('page/title'),
  174. this.savedState.description !== this.$store.get('page/description'),
  175. this.savedState.tags !== this.$store.get('page/tags'),
  176. this.savedState.isPublished !== this.$store.get('page/isPublished')
  177. ], Boolean)
  178. }
  179. },
  180. watch: {
  181. currentEditor(newValue, oldValue) {
  182. if (newValue !== '' && this.mode === 'create') {
  183. _.delay(() => {
  184. this.dialogProps = true
  185. }, 500)
  186. }
  187. }
  188. },
  189. created() {
  190. this.$store.set('page/id', this.pageId)
  191. this.$store.set('page/description', this.description)
  192. this.$store.set('page/isPublished', this.isPublished)
  193. this.$store.set('page/locale', this.locale)
  194. this.$store.set('page/path', this.path)
  195. this.$store.set('page/tags', this.tags)
  196. this.$store.set('page/title', this.title)
  197. this.$store.set('page/scriptCss', this.scriptCss)
  198. this.$store.set('page/scriptJs', this.scriptJs)
  199. this.$store.set('page/mode', 'edit')
  200. this.setCurrentSavedState()
  201. this.checkoutDateActive = this.checkoutDate
  202. if (this.effectivePermissions) {
  203. this.$store.set('page/effectivePermissions', JSON.parse(Buffer.from(this.effectivePermissions, 'base64').toString()))
  204. }
  205. },
  206. mounted() {
  207. this.$store.set('editor/mode', this.initMode || 'create')
  208. this.initContentParsed = this.initContent ? Base64.decode(this.initContent) : ''
  209. this.$store.set('editor/content', this.initContentParsed)
  210. if (this.mode === 'create' && !this.initEditor) {
  211. _.delay(() => {
  212. this.dialogEditorSelector = true
  213. }, 500)
  214. } else {
  215. this.currentEditor = `editor${_.startCase(this.initEditor || 'markdown')}`
  216. }
  217. window.onbeforeunload = () => {
  218. if (!this.exitConfirmed && this.initContentParsed !== this.$store.get('editor/content')) {
  219. return this.$t('editor:unsavedWarning')
  220. } else {
  221. return undefined
  222. }
  223. }
  224. this.$root.$on('resetEditorConflict', () => {
  225. this.isConflict = false
  226. })
  227. // this.$store.set('editor/mode', 'edit')
  228. // this.currentEditor = `editorApi`
  229. },
  230. methods: {
  231. openPropsModal(name) {
  232. this.dialogProps = true
  233. },
  234. showProgressDialog(textKey) {
  235. this.dialogProgress = true
  236. },
  237. hideProgressDialog() {
  238. this.dialogProgress = false
  239. },
  240. openConflict() {
  241. this.$root.$emit('saveConflict')
  242. },
  243. async save({ rethrow = false, overwrite = false } = {}) {
  244. this.showProgressDialog('saving')
  245. this.isSaving = true
  246. const saveTimeoutHandle = setTimeout(() => {
  247. throw new Error('Save operation timed out.')
  248. }, 30000)
  249. try {
  250. if (this.$store.get('editor/mode') === 'create') {
  251. // --------------------------------------------
  252. // -> CREATE PAGE
  253. // --------------------------------------------
  254. let resp = await this.$apollo.mutate({
  255. mutation: gql`
  256. mutation (
  257. $content: String!
  258. $description: String!
  259. $editor: String!
  260. $isPrivate: Boolean!
  261. $isPublished: Boolean!
  262. $locale: String!
  263. $path: String!
  264. $publishEndDate: Date
  265. $publishStartDate: Date
  266. $scriptCss: String
  267. $scriptJs: String
  268. $tags: [String]!
  269. $title: String!
  270. ) {
  271. pages {
  272. create(
  273. content: $content
  274. description: $description
  275. editor: $editor
  276. isPrivate: $isPrivate
  277. isPublished: $isPublished
  278. locale: $locale
  279. path: $path
  280. publishEndDate: $publishEndDate
  281. publishStartDate: $publishStartDate
  282. scriptCss: $scriptCss
  283. scriptJs: $scriptJs
  284. tags: $tags
  285. title: $title
  286. ) {
  287. responseResult {
  288. succeeded
  289. errorCode
  290. slug
  291. message
  292. }
  293. page {
  294. id
  295. updatedAt
  296. }
  297. }
  298. }
  299. }
  300. `,
  301. variables: {
  302. content: this.$store.get('editor/content'),
  303. description: this.$store.get('page/description'),
  304. editor: this.$store.get('editor/editorKey'),
  305. locale: this.$store.get('page/locale'),
  306. isPrivate: false,
  307. isPublished: this.$store.get('page/isPublished'),
  308. path: this.$store.get('page/path'),
  309. publishEndDate: this.$store.get('page/publishEndDate') || '',
  310. publishStartDate: this.$store.get('page/publishStartDate') || '',
  311. scriptCss: this.$store.get('page/scriptCss'),
  312. scriptJs: this.$store.get('page/scriptJs'),
  313. tags: this.$store.get('page/tags'),
  314. title: this.$store.get('page/title')
  315. }
  316. })
  317. resp = _.get(resp, 'data.pages.create', {})
  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.createSuccess'),
  323. style: 'success',
  324. icon: 'check'
  325. })
  326. this.$store.set('editor/id', _.get(resp, 'page.id'))
  327. this.$store.set('editor/mode', 'update')
  328. this.exitConfirmed = true
  329. window.location.assign(`/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  330. } else {
  331. throw new Error(_.get(resp, 'responseResult.message'))
  332. }
  333. } else {
  334. // --------------------------------------------
  335. // -> UPDATE EXISTING PAGE
  336. // --------------------------------------------
  337. const conflictResp = await this.$apollo.query({
  338. query: gql`
  339. query ($id: Int!, $checkoutDate: Date!) {
  340. pages {
  341. checkConflicts(id: $id, checkoutDate: $checkoutDate)
  342. }
  343. }
  344. `,
  345. fetchPolicy: 'network-only',
  346. variables: {
  347. id: this.pageId,
  348. checkoutDate: this.checkoutDateActive
  349. }
  350. })
  351. if (_.get(conflictResp, 'data.pages.checkConflicts', false)) {
  352. this.$root.$emit('saveConflict')
  353. throw new Error(this.$t('editor:conflict.warning'))
  354. }
  355. let resp = await this.$apollo.mutate({
  356. mutation: gql`
  357. mutation (
  358. $id: Int!
  359. $content: String
  360. $description: String
  361. $editor: String
  362. $isPrivate: Boolean
  363. $isPublished: Boolean
  364. $locale: String
  365. $path: String
  366. $publishEndDate: Date
  367. $publishStartDate: Date
  368. $scriptCss: String
  369. $scriptJs: String
  370. $tags: [String]
  371. $title: String
  372. ) {
  373. pages {
  374. update(
  375. id: $id
  376. content: $content
  377. description: $description
  378. editor: $editor
  379. isPrivate: $isPrivate
  380. isPublished: $isPublished
  381. locale: $locale
  382. path: $path
  383. publishEndDate: $publishEndDate
  384. publishStartDate: $publishStartDate
  385. scriptCss: $scriptCss
  386. scriptJs: $scriptJs
  387. tags: $tags
  388. title: $title
  389. ) {
  390. responseResult {
  391. succeeded
  392. errorCode
  393. slug
  394. message
  395. }
  396. page {
  397. updatedAt
  398. }
  399. }
  400. }
  401. }
  402. `,
  403. variables: {
  404. id: this.$store.get('page/id'),
  405. content: this.$store.get('editor/content'),
  406. description: this.$store.get('page/description'),
  407. editor: this.$store.get('editor/editorKey'),
  408. locale: this.$store.get('page/locale'),
  409. isPrivate: false,
  410. isPublished: this.$store.get('page/isPublished'),
  411. path: this.$store.get('page/path'),
  412. publishEndDate: this.$store.get('page/publishEndDate') || '',
  413. publishStartDate: this.$store.get('page/publishStartDate') || '',
  414. scriptCss: this.$store.get('page/scriptCss'),
  415. scriptJs: this.$store.get('page/scriptJs'),
  416. tags: this.$store.get('page/tags'),
  417. title: this.$store.get('page/title')
  418. }
  419. })
  420. resp = _.get(resp, 'data.pages.update', {})
  421. if (_.get(resp, 'responseResult.succeeded')) {
  422. this.checkoutDateActive = _.get(resp, 'page.updatedAt', this.checkoutDateActive)
  423. this.isConflict = false
  424. this.$store.commit('showNotification', {
  425. message: this.$t('editor:save.updateSuccess'),
  426. style: 'success',
  427. icon: 'check'
  428. })
  429. if (this.locale !== this.$store.get('page/locale') || this.path !== this.$store.get('page/path')) {
  430. _.delay(() => {
  431. window.location.replace(`/e/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  432. }, 1000)
  433. }
  434. } else {
  435. throw new Error(_.get(resp, 'responseResult.message'))
  436. }
  437. }
  438. this.initContentParsed = this.$store.get('editor/content')
  439. this.setCurrentSavedState()
  440. } catch (err) {
  441. this.$store.commit('showNotification', {
  442. message: err.message,
  443. style: 'error',
  444. icon: 'warning'
  445. })
  446. if (rethrow === true) {
  447. clearTimeout(saveTimeoutHandle)
  448. this.isSaving = false
  449. this.hideProgressDialog()
  450. throw err
  451. }
  452. }
  453. clearTimeout(saveTimeoutHandle)
  454. this.isSaving = false
  455. this.hideProgressDialog()
  456. },
  457. async saveAndClose() {
  458. try {
  459. if (this.$store.get('editor/mode') === 'create') {
  460. await this.save()
  461. } else {
  462. await this.save({ rethrow: true })
  463. await this.exit()
  464. }
  465. } catch (err) {
  466. // Error is already handled
  467. }
  468. },
  469. async exit() {
  470. if (this.isDirty) {
  471. this.dialogUnsaved = true
  472. } else {
  473. this.exitGo()
  474. }
  475. },
  476. exitGo() {
  477. this.$store.commit(`loadingStart`, 'editor-close')
  478. this.currentEditor = ''
  479. this.exitConfirmed = true
  480. _.delay(() => {
  481. if (this.$store.get('editor/mode') === 'create') {
  482. window.location.assign(`/`)
  483. } else {
  484. window.location.assign(`/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  485. }
  486. }, 500)
  487. },
  488. setCurrentSavedState () {
  489. this.savedState = {
  490. description: this.$store.get('page/description'),
  491. isPublished: this.$store.get('page/isPublished'),
  492. publishEndDate: this.$store.get('page/publishEndDate') || '',
  493. publishStartDate: this.$store.get('page/publishStartDate') || '',
  494. tags: this.$store.get('page/tags'),
  495. title: this.$store.get('page/title')
  496. }
  497. }
  498. },
  499. apollo: {
  500. isConflict: {
  501. query: gql`
  502. query ($id: Int!, $checkoutDate: Date!) {
  503. pages {
  504. checkConflicts(id: $id, checkoutDate: $checkoutDate)
  505. }
  506. }
  507. `,
  508. fetchPolicy: 'network-only',
  509. pollInterval: 5000,
  510. variables () {
  511. return {
  512. id: this.pageId,
  513. checkoutDate: this.checkoutDateActive
  514. }
  515. },
  516. update: (data) => _.cloneDeep(data.pages.checkConflicts),
  517. skip () {
  518. return this.mode === 'create' || this.isSaving || !this.isDirty
  519. }
  520. }
  521. }
  522. }
  523. </script>
  524. <style lang='scss'>
  525. .editor {
  526. background-color: mc('grey', '900') !important;
  527. min-height: 100vh;
  528. .application--wrap {
  529. background-color: mc('grey', '900');
  530. }
  531. &-title-input input {
  532. text-align: center;
  533. }
  534. }
  535. .atom-spinner.is-inline {
  536. display: inline-block;
  537. }
  538. </style>