2
0

editor-code.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <template lang='pug'>
  2. .editor-code
  3. .editor-code-main
  4. .editor-code-editor
  5. codemirror(ref='cm', v-model='code', :options='cmOptions', @ready='onCmReady', @input='onCmInput')
  6. </template>
  7. <script>
  8. import _ from 'lodash'
  9. // ========================================
  10. // IMPORTS
  11. // ========================================
  12. // Code Mirror
  13. import CodeMirror from 'codemirror'
  14. import 'codemirror/lib/codemirror.css'
  15. // Language
  16. import 'codemirror/mode/markdown/markdown.js'
  17. // Addons
  18. import 'codemirror/addon/selection/active-line.js'
  19. import 'codemirror/addon/display/fullscreen.js'
  20. import 'codemirror/addon/display/fullscreen.css'
  21. import 'codemirror/addon/selection/mark-selection.js'
  22. import 'codemirror/addon/scroll/annotatescrollbar.js'
  23. import 'codemirror/addon/search/matchesonscrollbar.js'
  24. import 'codemirror/addon/search/searchcursor.js'
  25. import 'codemirror/addon/search/match-highlighter.js'
  26. // ========================================
  27. // INIT
  28. // ========================================
  29. // Platform detection
  30. const CtrlKey = /Mac/.test(navigator.platform) ? 'Cmd' : 'Ctrl'
  31. // ========================================
  32. // Vue Component
  33. // ========================================
  34. export default {
  35. data() {
  36. return {
  37. code: '<h1>Title</h1>\n\n<p>Some text here</p>',
  38. cmOptions: {
  39. tabSize: 2,
  40. mode: 'text/html',
  41. theme: 'wikijs-dark',
  42. lineNumbers: true,
  43. lineWrapping: true,
  44. line: true,
  45. styleActiveLine: true,
  46. highlightSelectionMatches: {
  47. annotateScrollbar: true
  48. },
  49. viewportMargin: 50
  50. }
  51. }
  52. },
  53. computed: {
  54. cm() {
  55. return this.$refs.cm.codemirror
  56. },
  57. isMobile() {
  58. return this.$vuetify.breakpoint.smAndDown
  59. }
  60. },
  61. methods: {
  62. onCmReady(cm) {
  63. let self = this
  64. const keyBindings = {
  65. 'F11' (cm) {
  66. cm.setOption('fullScreen', !cm.getOption('fullScreen'))
  67. },
  68. 'Esc' (cm) {
  69. if (cm.getOption('fullScreen')) cm.setOption('fullScreen', false)
  70. }
  71. }
  72. _.set(keyBindings, `${CtrlKey}-S`, cm => {
  73. self.$parent.save()
  74. })
  75. cm.setSize(null, 'calc(100vh - 64px)')
  76. cm.setOption('extraKeys', keyBindings)
  77. this.onCmInput(this.code)
  78. },
  79. onCmInput: _.debounce(function (newContent) {
  80. this.$store.set('editor/content', newContent)
  81. }, 500)
  82. }
  83. }
  84. </script>
  85. <style lang='scss'>
  86. .editor-code {
  87. &-main {
  88. display: flex;
  89. width: 100%;
  90. }
  91. &-editor {
  92. background-color: darken(mc('grey', '900'), 4.5%);
  93. flex: 1 1 50%;
  94. display: block;
  95. height: calc(100vh - 96px);
  96. position: relative;
  97. &-title {
  98. background-color: mc('grey', '800');
  99. border-bottom-left-radius: 5px;
  100. display: inline-flex;
  101. height: 30px;
  102. justify-content: center;
  103. align-items: center;
  104. padding: 0 1rem;
  105. color: mc('grey', '500');
  106. position: absolute;
  107. top: 0;
  108. right: 0;
  109. z-index: 7;
  110. text-transform: uppercase;
  111. font-size: .7rem;
  112. cursor: pointer;
  113. @include until($tablet) {
  114. display: none;
  115. }
  116. }
  117. }
  118. // ==========================================
  119. // CODE MIRROR
  120. // ==========================================
  121. .CodeMirror {
  122. height: auto;
  123. .cm-header-1 {
  124. font-size: 1.5rem;
  125. }
  126. .cm-header-2 {
  127. font-size: 1.25rem;
  128. }
  129. .cm-header-3 {
  130. font-size: 1.15rem;
  131. }
  132. .cm-header-4 {
  133. font-size: 1.1rem;
  134. }
  135. .cm-header-5 {
  136. font-size: 1.05rem;
  137. }
  138. .cm-header-6 {
  139. font-size: 1.025rem;
  140. }
  141. }
  142. .CodeMirror-focused .cm-matchhighlight {
  143. background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAFklEQVQI12NgYGBgkKzc8x9CMDAwAAAmhwSbidEoSQAAAABJRU5ErkJggg==);
  144. background-position: bottom;
  145. background-repeat: repeat-x;
  146. }
  147. .cm-matchhighlight {
  148. background-color: mc('grey', '800');
  149. }
  150. .CodeMirror-selection-highlight-scrollbar {
  151. background-color: mc('green', '600');
  152. }
  153. .cm-s-wikijs-dark.CodeMirror {
  154. background: darken(mc('grey','900'), 3%);
  155. color: #e0e0e0;
  156. }
  157. .cm-s-wikijs-dark div.CodeMirror-selected {
  158. background: mc('blue','800');
  159. }
  160. .cm-s-wikijs-dark .cm-matchhighlight {
  161. background: mc('blue','800');
  162. }
  163. .cm-s-wikijs-dark .CodeMirror-line::selection, .cm-s-wikijs-dark .CodeMirror-line > span::selection, .cm-s-wikijs-dark .CodeMirror-line > span > span::selection {
  164. background: mc('red', '500');
  165. }
  166. .cm-s-wikijs-dark .CodeMirror-line::-moz-selection, .cm-s-wikijs-dark .CodeMirror-line > span::-moz-selection, .cm-s-wikijs-dark .CodeMirror-line > span > span::-moz-selection {
  167. background: mc('red', '500');
  168. }
  169. .cm-s-wikijs-dark .CodeMirror-gutters {
  170. background: darken(mc('grey','900'), 6%);
  171. border-right: 1px solid mc('grey','900');
  172. }
  173. .cm-s-wikijs-dark .CodeMirror-guttermarker {
  174. color: #ac4142;
  175. }
  176. .cm-s-wikijs-dark .CodeMirror-guttermarker-subtle {
  177. color: #505050;
  178. }
  179. .cm-s-wikijs-dark .CodeMirror-linenumber {
  180. color: mc('grey','800');
  181. }
  182. .cm-s-wikijs-dark .CodeMirror-cursor {
  183. border-left: 1px solid #b0b0b0;
  184. }
  185. .cm-s-wikijs-dark span.cm-comment {
  186. color: mc('orange','800');
  187. }
  188. .cm-s-wikijs-dark span.cm-atom {
  189. color: #aa759f;
  190. }
  191. .cm-s-wikijs-dark span.cm-number {
  192. color: #aa759f;
  193. }
  194. .cm-s-wikijs-dark span.cm-property, .cm-s-wikijs-dark span.cm-attribute {
  195. color: #90a959;
  196. }
  197. .cm-s-wikijs-dark span.cm-keyword {
  198. color: #ac4142;
  199. }
  200. .cm-s-wikijs-dark span.cm-string {
  201. color: #f4bf75;
  202. }
  203. .cm-s-wikijs-dark span.cm-variable {
  204. color: #90a959;
  205. }
  206. .cm-s-wikijs-dark span.cm-variable-2 {
  207. color: #6a9fb5;
  208. }
  209. .cm-s-wikijs-dark span.cm-def {
  210. color: #d28445;
  211. }
  212. .cm-s-wikijs-dark span.cm-bracket {
  213. color: #e0e0e0;
  214. }
  215. .cm-s-wikijs-dark span.cm-tag {
  216. color: #ac4142;
  217. }
  218. .cm-s-wikijs-dark span.cm-link {
  219. color: #aa759f;
  220. }
  221. .cm-s-wikijs-dark span.cm-error {
  222. background: #ac4142;
  223. color: #b0b0b0;
  224. }
  225. .cm-s-wikijs-dark .CodeMirror-activeline-background {
  226. background: mc('grey','900');
  227. }
  228. .cm-s-wikijs-dark .CodeMirror-matchingbracket {
  229. text-decoration: underline;
  230. color: white !important;
  231. }
  232. }
  233. </style>