editor.vue 20 KB

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