You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
6.2 KiB

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