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.

600 lines
20 KiB

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='$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. $isPrivate: Boolean!
  282. $isPublished: Boolean!
  283. $locale: String!
  284. $path: String!
  285. $publishEndDate: Date
  286. $publishStartDate: Date
  287. $scriptCss: String
  288. $scriptJs: String
  289. $tags: [String]!
  290. $title: String!
  291. ) {
  292. pages {
  293. create(
  294. content: $content
  295. description: $description
  296. editor: $editor
  297. isPrivate: $isPrivate
  298. isPublished: $isPublished
  299. locale: $locale
  300. path: $path
  301. publishEndDate: $publishEndDate
  302. publishStartDate: $publishStartDate
  303. scriptCss: $scriptCss
  304. scriptJs: $scriptJs
  305. tags: $tags
  306. title: $title
  307. ) {
  308. responseResult {
  309. succeeded
  310. errorCode
  311. slug
  312. message
  313. }
  314. page {
  315. id
  316. updatedAt
  317. }
  318. }
  319. }
  320. }
  321. `,
  322. variables: {
  323. content: this.$store.get('editor/content'),
  324. description: this.$store.get('page/description'),
  325. editor: this.$store.get('editor/editorKey'),
  326. locale: this.$store.get('page/locale'),
  327. isPrivate: false,
  328. isPublished: this.$store.get('page/isPublished'),
  329. path: this.$store.get('page/path'),
  330. publishEndDate: this.$store.get('page/publishEndDate') || '',
  331. publishStartDate: this.$store.get('page/publishStartDate') || '',
  332. scriptCss: this.$store.get('page/scriptCss'),
  333. scriptJs: this.$store.get('page/scriptJs'),
  334. tags: this.$store.get('page/tags'),
  335. title: this.$store.get('page/title')
  336. }
  337. })
  338. resp = _.get(resp, 'data.pages.create', {})
  339. if (_.get(resp, 'responseResult.succeeded')) {
  340. this.checkoutDateActive = _.get(resp, 'page.updatedAt', this.checkoutDateActive)
  341. this.isConflict = false
  342. this.$store.commit('showNotification', {
  343. message: this.$t('editor:save.createSuccess'),
  344. style: 'success',
  345. icon: 'check'
  346. })
  347. this.$store.set('editor/id', _.get(resp, 'page.id'))
  348. this.$store.set('editor/mode', 'update')
  349. this.exitConfirmed = true
  350. window.location.assign(`/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  351. } else {
  352. throw new Error(_.get(resp, 'responseResult.message'))
  353. }
  354. } else {
  355. // --------------------------------------------
  356. // -> UPDATE EXISTING PAGE
  357. // --------------------------------------------
  358. const conflictResp = await this.$apollo.query({
  359. query: gql`
  360. query ($id: Int!, $checkoutDate: Date!) {
  361. pages {
  362. checkConflicts(id: $id, checkoutDate: $checkoutDate)
  363. }
  364. }
  365. `,
  366. fetchPolicy: 'network-only',
  367. variables: {
  368. id: this.pageId,
  369. checkoutDate: this.checkoutDateActive
  370. }
  371. })
  372. if (_.get(conflictResp, 'data.pages.checkConflicts', false)) {
  373. this.$root.$emit('saveConflict')
  374. throw new Error(this.$t('editor:conflict.warning'))
  375. }
  376. let resp = await this.$apollo.mutate({
  377. mutation: gql`
  378. mutation (
  379. $id: Int!
  380. $content: String
  381. $description: String
  382. $editor: String
  383. $isPrivate: Boolean
  384. $isPublished: Boolean
  385. $locale: String
  386. $path: String
  387. $publishEndDate: Date
  388. $publishStartDate: Date
  389. $scriptCss: String
  390. $scriptJs: String
  391. $tags: [String]
  392. $title: String
  393. ) {
  394. pages {
  395. update(
  396. id: $id
  397. content: $content
  398. description: $description
  399. editor: $editor
  400. isPrivate: $isPrivate
  401. isPublished: $isPublished
  402. locale: $locale
  403. path: $path
  404. publishEndDate: $publishEndDate
  405. publishStartDate: $publishStartDate
  406. scriptCss: $scriptCss
  407. scriptJs: $scriptJs
  408. tags: $tags
  409. title: $title
  410. ) {
  411. responseResult {
  412. succeeded
  413. errorCode
  414. slug
  415. message
  416. }
  417. page {
  418. updatedAt
  419. }
  420. }
  421. }
  422. }
  423. `,
  424. variables: {
  425. id: this.$store.get('page/id'),
  426. content: this.$store.get('editor/content'),
  427. description: this.$store.get('page/description'),
  428. editor: this.$store.get('editor/editorKey'),
  429. locale: this.$store.get('page/locale'),
  430. isPrivate: false,
  431. isPublished: this.$store.get('page/isPublished'),
  432. path: this.$store.get('page/path'),
  433. publishEndDate: this.$store.get('page/publishEndDate') || '',
  434. publishStartDate: this.$store.get('page/publishStartDate') || '',
  435. scriptCss: this.$store.get('page/scriptCss'),
  436. scriptJs: this.$store.get('page/scriptJs'),
  437. tags: this.$store.get('page/tags'),
  438. title: this.$store.get('page/title')
  439. }
  440. })
  441. resp = _.get(resp, 'data.pages.update', {})
  442. if (_.get(resp, 'responseResult.succeeded')) {
  443. this.checkoutDateActive = _.get(resp, 'page.updatedAt', this.checkoutDateActive)
  444. this.isConflict = false
  445. this.$store.commit('showNotification', {
  446. message: this.$t('editor:save.updateSuccess'),
  447. style: 'success',
  448. icon: 'check'
  449. })
  450. if (this.locale !== this.$store.get('page/locale') || this.path !== this.$store.get('page/path')) {
  451. _.delay(() => {
  452. window.location.replace(`/e/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  453. }, 1000)
  454. }
  455. } else {
  456. throw new Error(_.get(resp, 'responseResult.message'))
  457. }
  458. }
  459. this.initContentParsed = this.$store.get('editor/content')
  460. this.setCurrentSavedState()
  461. } catch (err) {
  462. this.$store.commit('showNotification', {
  463. message: err.message,
  464. style: 'error',
  465. icon: 'warning'
  466. })
  467. if (rethrow === true) {
  468. clearTimeout(saveTimeoutHandle)
  469. this.isSaving = false
  470. this.hideProgressDialog()
  471. throw err
  472. }
  473. }
  474. clearTimeout(saveTimeoutHandle)
  475. this.isSaving = false
  476. this.hideProgressDialog()
  477. },
  478. async saveAndClose() {
  479. try {
  480. if (this.$store.get('editor/mode') === 'create') {
  481. await this.save()
  482. } else {
  483. await this.save({ rethrow: true })
  484. await this.exit()
  485. }
  486. } catch (err) {
  487. // Error is already handled
  488. }
  489. },
  490. async exit() {
  491. if (this.isDirty) {
  492. this.dialogUnsaved = true
  493. } else {
  494. this.exitGo()
  495. }
  496. },
  497. exitGo() {
  498. this.$store.commit(`loadingStart`, 'editor-close')
  499. this.currentEditor = ''
  500. this.exitConfirmed = true
  501. _.delay(() => {
  502. if (this.$store.get('editor/mode') === 'create') {
  503. window.location.assign(`/`)
  504. } else {
  505. window.location.assign(`/${this.$store.get('page/locale')}/${this.$store.get('page/path')}`)
  506. }
  507. }, 500)
  508. },
  509. setCurrentSavedState () {
  510. this.savedState = {
  511. description: this.$store.get('page/description'),
  512. isPublished: this.$store.get('page/isPublished'),
  513. publishEndDate: this.$store.get('page/publishEndDate') || '',
  514. publishStartDate: this.$store.get('page/publishStartDate') || '',
  515. tags: this.$store.get('page/tags'),
  516. title: this.$store.get('page/title'),
  517. css: this.$store.get('page/scriptCss'),
  518. js: this.$store.get('page/scriptJs')
  519. }
  520. },
  521. injectCustomCss: _.debounce(css => {
  522. const oldStyl = document.querySelector('#editor-script-css')
  523. if (oldStyl) {
  524. document.head.removeChild(oldStyl)
  525. }
  526. if (!_.isEmpty(css)) {
  527. const styl = document.createElement('style')
  528. styl.type = 'text/css'
  529. styl.id = 'editor-script-css'
  530. document.head.appendChild(styl)
  531. styl.appendChild(document.createTextNode(css))
  532. }
  533. }, 1000)
  534. },
  535. apollo: {
  536. isConflict: {
  537. query: gql`
  538. query ($id: Int!, $checkoutDate: Date!) {
  539. pages {
  540. checkConflicts(id: $id, checkoutDate: $checkoutDate)
  541. }
  542. }
  543. `,
  544. fetchPolicy: 'network-only',
  545. pollInterval: 5000,
  546. variables () {
  547. return {
  548. id: this.pageId,
  549. checkoutDate: this.checkoutDateActive
  550. }
  551. },
  552. update: (data) => _.cloneDeep(data.pages.checkConflicts),
  553. skip () {
  554. return this.mode === 'create' || this.isSaving || !this.isDirty
  555. }
  556. }
  557. }
  558. }
  559. </script>
  560. <style lang='scss'>
  561. .editor {
  562. background-color: mc('grey', '900') !important;
  563. min-height: 100vh;
  564. .application--wrap {
  565. background-color: mc('grey', '900');
  566. }
  567. &-title-input input {
  568. text-align: center;
  569. }
  570. }
  571. .atom-spinner.is-inline {
  572. display: inline-block;
  573. }
  574. </style>