editor-ckeditor.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template lang='pug'>
  2. .editor-ckeditor
  3. div(ref='toolbarContainer')
  4. div.contents(ref='editor')
  5. v-system-bar.editor-ckeditor-sysbar(dark, status, color='grey darken-3')
  6. .caption.editor-ckeditor-sysbar-locale {{locale.toUpperCase()}}
  7. .caption.px-3 /{{path}}
  8. template(v-if='$vuetify.breakpoint.mdAndUp')
  9. v-spacer
  10. .caption Visual Editor
  11. v-spacer
  12. .caption {{$t('editor:ckeditor.stats', { chars: stats.characters, words: stats.words })}}
  13. editor-conflict(v-model='isConflict', v-if='isConflict')
  14. page-selector(mode='select', v-model='insertLinkDialog', :open-handler='insertLinkHandler', :path='path', :locale='locale')
  15. </template>
  16. <script>
  17. import _ from 'lodash'
  18. import { get, sync } from 'vuex-pathify'
  19. import DecoupledEditor from '@requarks/ckeditor5'
  20. import EditorConflict from './ckeditor/conflict.vue'
  21. import { html as beautify } from 'js-beautify/js/lib/beautifier.min.js'
  22. /* global siteLangs */
  23. export default {
  24. components: {
  25. EditorConflict
  26. },
  27. props: {
  28. save: {
  29. type: Function,
  30. default: () => {}
  31. }
  32. },
  33. data() {
  34. return {
  35. editor: null,
  36. stats: {
  37. characters: 0,
  38. words: 0
  39. },
  40. content: '',
  41. isConflict: false,
  42. insertLinkDialog: false
  43. }
  44. },
  45. computed: {
  46. isMobile() {
  47. return this.$vuetify.breakpoint.smAndDown
  48. },
  49. locale: get('page/locale'),
  50. path: get('page/path'),
  51. activeModal: sync('editor/activeModal')
  52. },
  53. methods: {
  54. insertLink () {
  55. this.insertLinkDialog = true
  56. },
  57. insertLinkHandler ({ locale, path }) {
  58. this.editor.execute('link', siteLangs.length > 0 ? `/${locale}/${path}` : `/${path}`)
  59. }
  60. },
  61. async mounted () {
  62. this.$store.set('editor/editorKey', 'ckeditor')
  63. this.editor = await DecoupledEditor.create(this.$refs.editor, {
  64. language: this.locale,
  65. placeholder: 'Type the page content here',
  66. wordCount: {
  67. onUpdate: stats => {
  68. this.stats = {
  69. characters: stats.characters,
  70. words: stats.words
  71. }
  72. }
  73. }
  74. })
  75. this.$refs.toolbarContainer.appendChild(this.editor.ui.view.toolbar.element)
  76. if (this.mode !== 'create') {
  77. this.editor.setData(this.$store.get('editor/content'))
  78. }
  79. this.editor.model.document.on('change:data', _.debounce(evt => {
  80. this.$store.set('editor/content', beautify(this.editor.getData(), { indent_size: 2, end_with_newline: true }))
  81. }, 300))
  82. this.$root.$on('editorInsert', opts => {
  83. switch (opts.kind) {
  84. case 'IMAGE':
  85. this.editor.execute('imageInsert', {
  86. source: opts.path
  87. })
  88. break
  89. case 'BINARY':
  90. this.editor.execute('link', opts.path, {
  91. linkIsDownloadable: true
  92. })
  93. break
  94. }
  95. })
  96. this.$root.$on('editorLinkToPage', opts => {
  97. this.insertLink()
  98. })
  99. // Handle save conflict
  100. this.$root.$on('saveConflict', () => {
  101. this.isConflict = true
  102. })
  103. this.$root.$on('overwriteEditorContent', () => {
  104. this.editor.setData(this.$store.get('editor/content'))
  105. })
  106. },
  107. beforeDestroy () {
  108. if (this.editor) {
  109. this.editor.destroy()
  110. this.editor = null
  111. }
  112. }
  113. }
  114. </script>
  115. <style lang="scss">
  116. $editor-height: calc(100vh - 64px - 24px);
  117. $editor-height-mobile: calc(100vh - 56px - 16px);
  118. .editor-ckeditor {
  119. background-color: mc('grey', '200');
  120. flex: 1 1 50%;
  121. display: flex;
  122. flex-flow: column nowrap;
  123. height: $editor-height;
  124. max-height: $editor-height;
  125. position: relative;
  126. @at-root .theme--dark & {
  127. background-color: mc('grey', '900');
  128. }
  129. @include until($tablet) {
  130. height: $editor-height-mobile;
  131. max-height: $editor-height-mobile;
  132. }
  133. &-sysbar {
  134. padding-left: 0;
  135. &-locale {
  136. background-color: rgba(255,255,255,.25);
  137. display:inline-flex;
  138. padding: 0 12px;
  139. height: 24px;
  140. width: 63px;
  141. justify-content: center;
  142. align-items: center;
  143. }
  144. }
  145. .ck.ck-toolbar {
  146. border: none;
  147. justify-content: center;
  148. background-color: mc('grey', '300');
  149. color: #FFF;
  150. }
  151. > .ck-editor__editable {
  152. background-color: mc('grey', '100');
  153. overflow-y: auto;
  154. overflow-x: hidden;
  155. padding: 2rem;
  156. box-shadow: 0 0 5px hsla(0, 0, 0, .1);
  157. margin: 1rem auto 0;
  158. width: calc(100vw - 256px - 16vw);
  159. min-height: calc(100vh - 64px - 24px - 1rem - 40px);
  160. border-radius: 5px;
  161. @at-root .theme--dark & {
  162. background-color: #303030;
  163. color: #FFF;
  164. }
  165. @include until($widescreen) {
  166. width: calc(100vw - 2rem);
  167. margin: 1rem 1rem 0 1rem;
  168. min-height: calc(100vh - 64px - 24px - 1rem - 40px);
  169. }
  170. @include until($tablet) {
  171. width: 100%;
  172. margin: 0;
  173. min-height: calc(100vh - 56px - 24px - 76px);
  174. }
  175. &.ck.ck-editor__editable:not(.ck-editor__nested-editable).ck-focused {
  176. border-color: #FFF;
  177. box-shadow: 0 0 10px rgba(mc('blue', '700'), .25);
  178. @at-root .theme--dark & {
  179. border-color: #444;
  180. border-bottom: none;
  181. box-shadow: 0 0 10px rgba(#000, .25);
  182. }
  183. }
  184. &.ck .ck-editor__nested-editable.ck-editor__nested-editable_focused,
  185. &.ck .ck-editor__nested-editable:focus,
  186. .ck-widget.table td.ck-editor__nested-editable.ck-editor__nested-editable_focused,
  187. .ck-widget.table th.ck-editor__nested-editable.ck-editor__nested-editable_focused {
  188. background-color: mc('grey', '100');
  189. @at-root .theme--dark & {
  190. background-color: mc('grey', '900');
  191. }
  192. }
  193. }
  194. }
  195. </style>