editor-ckeditor.vue 5.7 KB

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