editor-ckeditor.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. .contents {
  146. table {
  147. margin: inherit;
  148. }
  149. pre > code {
  150. background-color: unset;
  151. color: unset;
  152. padding: .15em;
  153. }
  154. }
  155. .ck.ck-toolbar {
  156. border: none;
  157. justify-content: center;
  158. background-color: mc('grey', '300');
  159. color: #FFF;
  160. }
  161. .ck.ck-toolbar__items {
  162. justify-content: center;
  163. }
  164. > .ck-editor__editable {
  165. background-color: mc('grey', '100');
  166. overflow-y: auto;
  167. overflow-x: hidden;
  168. padding: 2rem;
  169. box-shadow: 0 0 5px hsla(0, 0, 0, .1);
  170. margin: 1rem auto 0;
  171. width: calc(100vw - 256px - 16vw);
  172. min-height: calc(100vh - 64px - 24px - 1rem - 40px);
  173. border-radius: 5px;
  174. @at-root .theme--dark & {
  175. background-color: #303030;
  176. color: #FFF;
  177. }
  178. @include until($widescreen) {
  179. width: calc(100vw - 2rem);
  180. margin: 1rem 1rem 0 1rem;
  181. min-height: calc(100vh - 64px - 24px - 1rem - 40px);
  182. }
  183. @include until($tablet) {
  184. width: 100%;
  185. margin: 0;
  186. min-height: calc(100vh - 56px - 24px - 76px);
  187. }
  188. &.ck.ck-editor__editable:not(.ck-editor__nested-editable).ck-focused {
  189. border-color: #FFF;
  190. box-shadow: 0 0 10px rgba(mc('blue', '700'), .25);
  191. @at-root .theme--dark & {
  192. border-color: #444;
  193. border-bottom: none;
  194. box-shadow: 0 0 10px rgba(#000, .25);
  195. }
  196. }
  197. &.ck .ck-editor__nested-editable.ck-editor__nested-editable_focused,
  198. &.ck .ck-editor__nested-editable:focus,
  199. .ck-widget.table td.ck-editor__nested-editable.ck-editor__nested-editable_focused,
  200. .ck-widget.table th.ck-editor__nested-editable.ck-editor__nested-editable_focused {
  201. background-color: mc('grey', '100');
  202. @at-root .theme--dark & {
  203. background-color: mc('grey', '900');
  204. }
  205. }
  206. }
  207. }
  208. </style>