editor.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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-main
  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. editorModalDrawio: () => import(/* webpackChunkName: "editor", webpackMode: "eager" */ './editor/editor-modal-drawio.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. scriptCss: {
  110. type: String,
  111. default: ''
  112. },
  113. publishStartDate: {
  114. type: String,
  115. default: ''
  116. },
  117. publishEndDate: {
  118. type: String,
  119. default: ''
  120. },
  121. scriptJs: {
  122. type: String,
  123. default: ''
  124. },
  125. initEditor: {
  126. type: String,
  127. default: null
  128. },
  129. initMode: {
  130. type: String,
  131. default: 'create'
  132. },
  133. initContent: {
  134. type: String,
  135. default: null
  136. },
  137. pageId: {
  138. type: Number,
  139. default: 0
  140. },
  141. tocOptions: {
  142. type: String,
  143. default: ''
  144. },
  145. checkoutDate: {
  146. type: String,
  147. default: new Date().toISOString()
  148. },
  149. effectivePermissions: {
  150. type: String,
  151. default: ''
  152. }
  153. },
  154. data() {
  155. return {
  156. isSaving: false,
  157. isConflict: false,
  158. dialogProps: false,
  159. dialogProgress: false,
  160. dialogEditorSelector: false,
  161. dialogUnsaved: false,
  162. exitConfirmed: false,
  163. initContentParsed: '',
  164. savedState: {
  165. description: '',
  166. isPublished: false,
  167. publishEndDate: '',
  168. publishStartDate: '',
  169. tags: '',
  170. title: '',
  171. css: '',
  172. js: '',
  173. tocDepth: 0,
  174. useDefaultTocDepth: false
  175. }
  176. }
  177. },
  178. computed: {
  179. currentEditor: sync('editor/editor'),
  180. activeModal: sync('editor/activeModal'),
  181. mode: get('editor/mode'),
  182. welcomeMode() { return this.mode === `create` && this.path === `home` },
  183. currentPageTitle: sync('page/title'),
  184. checkoutDateActive: sync('editor/checkoutDateActive'),
  185. currentStyling: get('page/scriptCss'),
  186. isDirty () {
  187. return _.some([
  188. this.initContentParsed !== this.$store.get('editor/content'),
  189. this.locale !== this.$store.get('page/locale'),
  190. this.path !== this.$store.get('page/path'),
  191. this.savedState.title !== this.$store.get('page/title'),
  192. this.savedState.description !== this.$store.get('page/description'),
  193. this.savedState.tocDepth !== this.$store.get('page/tocDepth@min') + (this.$store.get('page/tocDepth@max') * 10),
  194. this.savedState.useDefaultTocDepth !== this.$store.get('page/useDefaultTocDepth'),
  195. this.savedState.tags !== this.$store.get('page/tags'),
  196. this.savedState.isPublished !== this.$store.get('page/isPublished'),
  197. this.savedState.publishStartDate !== this.$store.get('page/publishStartDate'),
  198. this.savedState.publishEndDate !== this.$store.get('page/publishEndDate'),
  199. this.savedState.css !== this.$store.get('page/scriptCss'),
  200. this.savedState.js !== this.$store.get('page/scriptJs')
  201. ], Boolean)
  202. }
  203. },
  204. watch: {
  205. currentEditor(newValue, oldValue) {
  206. if (newValue !== '' && this.mode === 'create') {
  207. _.delay(() => {
  208. this.dialogProps = true
  209. }, 500)
  210. }
  211. },
  212. currentStyling(newValue) {
  213. this.injectCustomCss(newValue)
  214. }
  215. },
  216. created() {
  217. this.$store.set('page/id', this.pageId)
  218. this.$store.set('page/description', this.description)
  219. this.$store.set('page/isPublished', this.isPublished)
  220. this.$store.set('page/publishStartDate', this.publishStartDate)
  221. this.$store.set('page/publishEndDate', this.publishEndDate)
  222. this.$store.set('page/locale', this.locale)
  223. this.$store.set('page/path', this.path)
  224. this.$store.set('page/tags', this.tags)
  225. this.$store.set('page/title', this.title)
  226. this.$store.set('page/scriptCss', this.scriptCss)
  227. this.$store.set('page/scriptJs', this.scriptJs)
  228. this.$store.set('page/mode', 'edit')
  229. const tocOptions = JSON.parse(Buffer.from(this.tocOptions, 'base64').toString())
  230. this.$store.set('page/tocDepth', {
  231. min: tocOptions.min,
  232. max: tocOptions.max
  233. })
  234. this.$store.set('page/useDefaultTocDepth', tocOptions.useDefault)
  235. this.setCurrentSavedState()
  236. this.checkoutDateActive = this.checkoutDate
  237. if (this.effectivePermissions) {
  238. this.$store.set('page/effectivePermissions', JSON.parse(Buffer.from(this.effectivePermissions, 'base64').toString()))
  239. }
  240. },
  241. mounted() {
  242. this.$store.set('editor/mode', this.initMode || 'create')
  243. this.initContentParsed = this.initContent ? Base64.decode(this.initContent) : ''
  244. this.$store.set('editor/content', this.initContentParsed)
  245. if (this.mode === 'create' && !this.initEditor) {
  246. _.delay(() => {
  247. this.dialogEditorSelector = true
  248. }, 500)
  249. } else {
  250. this.currentEditor = `editor${_.startCase(this.initEditor || 'markdown')}`
  251. }
  252. window.onbeforeunload = () => {
  253. if (!this.exitConfirmed && this.initContentParsed !== this.$store.get('editor/content')) {
  254. return this.$t('editor:unsavedWarning')
  255. } else {
  256. return undefined
  257. }
  258. }
  259. this.$root.$on('resetEditorConflict', () => {
  260. this.isConflict = false
  261. })
  262. // this.$store.set('editor/mode', 'edit')
  263. // this.currentEditor = `editorApi`
  264. },
  265. methods: {
  266. openPropsModal(name) {
  267. this.dialogProps = true
  268. },
  269. showProgressDialog(textKey) {
  270. this.dialogProgress = true
  271. },
  272. hideProgressDialog() {
  273. this.dialogProgress = false
  274. },
  275. openConflict() {
  276. this.$root.$emit('saveConflict')
  277. },
  278. async save({ rethrow = false, overwrite = false } = {}) {
  279. this.showProgressDialog('saving')
  280. this.isSaving = true
  281. const saveTimeoutHandle = setTimeout(() => {
  282. throw new Error('Save operation timed out.')
  283. }, 30000)
  284. try {
  285. if (this.$store.get('editor/mode') === 'create') {
  286. // --------------------------------------------
  287. // -> CREATE PAGE
  288. // --------------------------------------------
  289. let resp = await this.$apollo.mutate({
  290. mutation: gql`
  291. mutation (
  292. $content: String!
  293. $description: String!
  294. $editor: String!
  295. $isPrivate: Boolean!
  296. $isPublished: Boolean!
  297. $locale: String!
  298. $path: String!
  299. $publishEndDate: Date
  300. $publishStartDate: Date
  301. $scriptCss: String
  302. $scriptJs: String
  303. $tocDepth: RangeInput
  304. $useDefaultTocDepth: Boolean
  305. $tags: [String]!
  306. $title: String!
  307. ) {
  308. pages {
  309. create(
  310. content: $content
  311. description: $description
  312. editor: $editor
  313. isPrivate: $isPrivate
  314. isPublished: $isPublished
  315. locale: $locale
  316. path: $path
  317. publishEndDate: $publishEndDate
  318. publishStartDate: $publishStartDate
  319. scriptCss: $scriptCss
  320. scriptJs: $scriptJs
  321. tocDepth: $tocDepth
  322. useDefaultTocDepth: $useDefaultTocDepth
  323. tags: $tags
  324. title: $title
  325. ) {
  326. responseResult {
  327. succeeded
  328. errorCode
  329. slug
  330. message
  331. }
  332. page {
  333. id
  334. updatedAt
  335. }
  336. }
  337. }
  338. }
  339. `,
  340. variables: {
  341. content: this.$store.get('editor/content'),
  342. description: this.$store.get('page/description'),
  343. editor: this.$store.get('editor/editorKey'),
  344. locale: this.$store.get('page/locale'),
  345. isPrivate: false,
  346. isPublished: this.$store.get('page/isPublished'),
  347. path: this.$store.get('page/path'),
  348. publishEndDate: this.$store.get('page/publishEndDate') || '',
  349. publishStartDate: this.$store.get('page/publishStartDate') || '',
  350. scriptCss: this.$store.get('page/scriptCss'),
  351. scriptJs: this.$store.get('page/scriptJs'),
  352. tocDepth: this.$store.get('page/tocDepth'),
  353. useDefaultTocDepth: this.$store.get('page/useDefaultTocDepth'),
  354. tags: this.$store.get('page/tags'),
  355. title: this.$store.get('page/title')
  356. }
  357. })
  358. resp = _.get(resp, 'data.pages.create', {})
  359. if (_.get(resp, 'responseResult.succeeded')) {
  360. this.checkoutDateActive = _.get(resp, 'page.updatedAt', this.checkoutDateActive)
  361. this.isConflict = false
  362. this.$store.commit('showNotification', {
  363. message: this.$t('editor:save.createSuccess'),
  364. style: 'success',
  365. icon: 'check'
  366. })
  367. this.$store.set('editor/id', _.get(resp, 'page.id'))
  368. this.$store.set('editor/mode', 'update')
  369. this.exitConfirmed = true
  370. window.location.assign(`/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  371. } else {
  372. throw new Error(_.get(resp, 'responseResult.message'))
  373. }
  374. } else {
  375. // --------------------------------------------
  376. // -> UPDATE EXISTING PAGE
  377. // --------------------------------------------
  378. const conflictResp = await this.$apollo.query({
  379. query: gql`
  380. query ($id: Int!, $checkoutDate: Date!) {
  381. pages {
  382. checkConflicts(id: $id, checkoutDate: $checkoutDate)
  383. }
  384. }
  385. `,
  386. fetchPolicy: 'network-only',
  387. variables: {
  388. id: this.pageId,
  389. checkoutDate: this.checkoutDateActive
  390. }
  391. })
  392. if (_.get(conflictResp, 'data.pages.checkConflicts', false)) {
  393. this.$root.$emit('saveConflict')
  394. throw new Error(this.$t('editor:conflict.warning'))
  395. }
  396. let resp = await this.$apollo.mutate({
  397. mutation: gql`
  398. mutation (
  399. $id: Int!
  400. $content: String
  401. $description: String
  402. $editor: String
  403. $isPrivate: Boolean
  404. $isPublished: Boolean
  405. $locale: String
  406. $path: String
  407. $publishEndDate: Date
  408. $publishStartDate: Date
  409. $scriptCss: String
  410. $scriptJs: String
  411. $tocDepth: RangeInput
  412. $useDefaultTocDepth: Boolean
  413. $tags: [String]
  414. $title: String
  415. ) {
  416. pages {
  417. update(
  418. id: $id
  419. content: $content
  420. description: $description
  421. editor: $editor
  422. isPrivate: $isPrivate
  423. isPublished: $isPublished
  424. locale: $locale
  425. path: $path
  426. publishEndDate: $publishEndDate
  427. publishStartDate: $publishStartDate
  428. scriptCss: $scriptCss
  429. scriptJs: $scriptJs
  430. tocDepth: $tocDepth
  431. useDefaultTocDepth: $useDefaultTocDepth
  432. tags: $tags
  433. title: $title
  434. ) {
  435. responseResult {
  436. succeeded
  437. errorCode
  438. slug
  439. message
  440. }
  441. page {
  442. updatedAt
  443. }
  444. }
  445. }
  446. }
  447. `,
  448. variables: {
  449. id: this.$store.get('page/id'),
  450. content: this.$store.get('editor/content'),
  451. description: this.$store.get('page/description'),
  452. editor: this.$store.get('editor/editorKey'),
  453. locale: this.$store.get('page/locale'),
  454. isPrivate: false,
  455. isPublished: this.$store.get('page/isPublished'),
  456. path: this.$store.get('page/path'),
  457. publishEndDate: this.$store.get('page/publishEndDate') || '',
  458. publishStartDate: this.$store.get('page/publishStartDate') || '',
  459. scriptCss: this.$store.get('page/scriptCss'),
  460. scriptJs: this.$store.get('page/scriptJs'),
  461. tocDepth: this.$store.get('page/tocDepth'),
  462. useDefaultTocDepth: this.$store.get('page/useDefaultTocDepth'),
  463. tags: this.$store.get('page/tags'),
  464. title: this.$store.get('page/title')
  465. }
  466. })
  467. resp = _.get(resp, 'data.pages.update', {})
  468. if (_.get(resp, 'responseResult.succeeded')) {
  469. this.checkoutDateActive = _.get(resp, 'page.updatedAt', this.checkoutDateActive)
  470. this.isConflict = false
  471. this.$store.commit('showNotification', {
  472. message: this.$t('editor:save.updateSuccess'),
  473. style: 'success',
  474. icon: 'check'
  475. })
  476. if (this.locale !== this.$store.get('page/locale') || this.path !== this.$store.get('page/path')) {
  477. _.delay(() => {
  478. window.location.replace(`/e/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  479. }, 1000)
  480. }
  481. } else {
  482. throw new Error(_.get(resp, 'responseResult.message'))
  483. }
  484. }
  485. this.initContentParsed = this.$store.get('editor/content')
  486. this.setCurrentSavedState()
  487. } catch (err) {
  488. this.$store.commit('showNotification', {
  489. message: err.message,
  490. style: 'error',
  491. icon: 'warning'
  492. })
  493. if (rethrow === true) {
  494. clearTimeout(saveTimeoutHandle)
  495. this.isSaving = false
  496. this.hideProgressDialog()
  497. throw err
  498. }
  499. }
  500. clearTimeout(saveTimeoutHandle)
  501. this.isSaving = false
  502. this.hideProgressDialog()
  503. },
  504. async saveAndClose() {
  505. try {
  506. if (this.$store.get('editor/mode') === 'create') {
  507. await this.save()
  508. } else {
  509. await this.save({ rethrow: true })
  510. await this.exit()
  511. }
  512. } catch (err) {
  513. // Error is already handled
  514. }
  515. },
  516. async exit() {
  517. if (this.isDirty) {
  518. this.dialogUnsaved = true
  519. } else {
  520. this.exitGo()
  521. }
  522. },
  523. exitGo() {
  524. this.$store.commit(`loadingStart`, 'editor-close')
  525. this.currentEditor = ''
  526. this.exitConfirmed = true
  527. _.delay(() => {
  528. if (this.$store.get('editor/mode') === 'create') {
  529. window.location.assign(`/`)
  530. } else {
  531. window.location.assign(`/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  532. }
  533. }, 500)
  534. },
  535. setCurrentSavedState () {
  536. this.savedState = {
  537. description: this.$store.get('page/description'),
  538. isPublished: this.$store.get('page/isPublished'),
  539. publishEndDate: this.$store.get('page/publishEndDate') || '',
  540. publishStartDate: this.$store.get('page/publishStartDate') || '',
  541. tags: this.$store.get('page/tags'),
  542. title: this.$store.get('page/title'),
  543. css: this.$store.get('page/scriptCss'),
  544. js: this.$store.get('page/scriptJs'),
  545. tocDepth: this.$store.get('page/tocDepth@min') + (this.$store.get('page/tocDepth@max') * 10),
  546. useDefaultTocDepth: this.$store.get('page/useDefaultTocDepth')
  547. }
  548. },
  549. injectCustomCss: _.debounce(css => {
  550. const oldStyl = document.querySelector('#editor-script-css')
  551. if (oldStyl) {
  552. document.head.removeChild(oldStyl)
  553. }
  554. if (!_.isEmpty(css)) {
  555. const styl = document.createElement('style')
  556. styl.type = 'text/css'
  557. styl.id = 'editor-script-css'
  558. document.head.appendChild(styl)
  559. styl.appendChild(document.createTextNode(css))
  560. }
  561. }, 1000)
  562. },
  563. apollo: {
  564. isConflict: {
  565. query: gql`
  566. query ($id: Int!, $checkoutDate: Date!) {
  567. pages {
  568. checkConflicts(id: $id, checkoutDate: $checkoutDate)
  569. }
  570. }
  571. `,
  572. fetchPolicy: 'network-only',
  573. pollInterval: 5000,
  574. variables () {
  575. return {
  576. id: this.pageId,
  577. checkoutDate: this.checkoutDateActive
  578. }
  579. },
  580. update: (data) => _.cloneDeep(data.pages.checkConflicts),
  581. skip () {
  582. return this.mode === 'create' || this.isSaving || !this.isDirty
  583. }
  584. }
  585. }
  586. }
  587. </script>
  588. <style lang='scss'>
  589. .editor {
  590. background-color: mc('grey', '900') !important;
  591. min-height: 100vh;
  592. .application--wrap {
  593. background-color: mc('grey', '900');
  594. }
  595. &-title-input input {
  596. text-align: center;
  597. }
  598. }
  599. .atom-spinner.is-inline {
  600. display: inline-block;
  601. }
  602. </style>