editor.vue 20 KB

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