editor-ckeditor.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. placeholder: 'Type the page content here',
  65. wordCount: {
  66. onUpdate: stats => {
  67. this.stats = {
  68. characters: stats.characters,
  69. words: stats.words
  70. }
  71. }
  72. }
  73. })
  74. this.$refs.toolbarContainer.appendChild(this.editor.ui.view.toolbar.element)
  75. if (this.mode !== 'create') {
  76. this.editor.setData(this.$store.get('editor/content'))
  77. }
  78. this.editor.model.document.on('change:data', _.debounce(evt => {
  79. this.$store.set('editor/content', beautify(this.editor.getData(), { indent_size: 2, end_with_newline: true }))
  80. }, 300))
  81. this.$root.$on('editorInsert', opts => {
  82. switch (opts.kind) {
  83. case 'IMAGE':
  84. this.editor.execute('imageInsert', {
  85. source: opts.path
  86. })
  87. break
  88. case 'BINARY':
  89. this.editor.execute('link', opts.path, {
  90. linkIsDownloadable: true
  91. })
  92. break
  93. }
  94. })
  95. this.$root.$on('editorLinkToPage', opts => {
  96. this.insertLink()
  97. })
  98. // Handle save conflict
  99. this.$root.$on('saveConflict', () => {
  100. this.isConflict = true
  101. })
  102. this.$root.$on('overwriteEditorContent', () => {
  103. this.editor.setData(this.$store.get('editor/content'))
  104. })
  105. },
  106. beforeDestroy () {
  107. if (this.editor) {
  108. this.editor.destroy()
  109. this.editor = null
  110. }
  111. }
  112. }
  113. </script>
  114. <style lang="scss">
  115. $editor-height: calc(100vh - 64px - 24px);
  116. $editor-height-mobile: calc(100vh - 56px - 16px);
  117. .editor-ckeditor {
  118. background-color: mc('grey', '200');
  119. flex: 1 1 50%;
  120. display: flex;
  121. flex-flow: column nowrap;
  122. height: $editor-height;
  123. max-height: $editor-height;
  124. position: relative;
  125. @at-root .theme--dark & {
  126. background-color: mc('grey', '900');
  127. }
  128. @include until($tablet) {
  129. height: $editor-height-mobile;
  130. max-height: $editor-height-mobile;
  131. }
  132. &-sysbar {
  133. padding-left: 0;
  134. &-locale {
  135. background-color: rgba(255,255,255,.25);
  136. display:inline-flex;
  137. padding: 0 12px;
  138. height: 24px;
  139. width: 63px;
  140. justify-content: center;
  141. align-items: center;
  142. }
  143. }
  144. .ck.ck-toolbar {
  145. border: none;
  146. justify-content: center;
  147. background-color: mc('grey', '300');
  148. color: #FFF;
  149. }
  150. > .ck-editor__editable {
  151. background-color: mc('grey', '100');
  152. overflow-y: auto;
  153. overflow-x: hidden;
  154. padding: 2rem;
  155. box-shadow: 0 0 5px hsla(0, 0, 0, .1);
  156. margin: 1rem auto 0;
  157. width: calc(100vw - 256px - 16vw);
  158. min-height: calc(100vh - 64px - 24px - 1rem - 40px);
  159. border-radius: 5px;
  160. @at-root .theme--dark & {
  161. background-color: #303030;
  162. color: #FFF;
  163. }
  164. @include until($widescreen) {
  165. width: calc(100vw - 2rem);
  166. margin: 1rem 1rem 0 1rem;
  167. min-height: calc(100vh - 64px - 24px - 1rem - 40px);
  168. }
  169. @include until($tablet) {
  170. width: 100%;
  171. margin: 0;
  172. min-height: calc(100vh - 56px - 24px - 76px);
  173. }
  174. &.ck.ck-editor__editable:not(.ck-editor__nested-editable).ck-focused {
  175. border-color: #FFF;
  176. box-shadow: 0 0 10px rgba(mc('blue', '700'), .25);
  177. @at-root .theme--dark & {
  178. border-color: #444;
  179. border-bottom: none;
  180. box-shadow: 0 0 10px rgba(#000, .25);
  181. }
  182. }
  183. &.ck .ck-editor__nested-editable.ck-editor__nested-editable_focused,
  184. &.ck .ck-editor__nested-editable:focus,
  185. .ck-widget.table td.ck-editor__nested-editable.ck-editor__nested-editable_focused,
  186. .ck-widget.table th.ck-editor__nested-editable.ck-editor__nested-editable_focused {
  187. background-color: mc('grey', '100');
  188. @at-root .theme--dark & {
  189. background-color: mc('grey', '900');
  190. }
  191. }
  192. }
  193. }
  194. </style>