editor-code.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <template lang='pug'>
  2. .editor-code
  3. .editor-code-toolbar
  4. .editor-code-toolbar-group
  5. .editor-code-toolbar-item
  6. svg.icons.is-18(role='img')
  7. title Bold
  8. use(xlink:href='#fa-bold')
  9. .editor-code-toolbar-item
  10. svg.icons.is-18(role='img')
  11. title Italic
  12. use(xlink:href='#fa-italic')
  13. .editor-code-toolbar-item
  14. svg.icons.is-18(role='img')
  15. title Strikethrough
  16. use(xlink:href='#fa-strikethrough')
  17. .editor-code-toolbar-group
  18. .editor-code-toolbar-item.is-dropdown
  19. svg.icons.is-18(role='img')
  20. title Heading
  21. use(xlink:href='#fa-heading')
  22. .editor-code-toolbar-group
  23. .editor-code-toolbar-item
  24. svg.icons.is-18(role='img')
  25. title Unordered List
  26. use(xlink:href='#fa-list-ul')
  27. .editor-code-toolbar-item
  28. svg.icons.is-18(role='img')
  29. title Ordered List
  30. use(xlink:href='#fa-list-ol')
  31. .editor-code-toolbar-group
  32. .editor-code-toolbar-item
  33. svg.icons.is-18(role='img')
  34. title Link
  35. use(xlink:href='#fa-link')
  36. .editor-code-main
  37. .editor-code-editor
  38. .editor-code-editor-title Editor
  39. codemirror(ref='cm', v-model='code', :options='cmOptions', @ready="onCmReady")
  40. .editor-code-preview
  41. .editor-code-preview-title Preview
  42. </template>
  43. <script>
  44. import { codemirror } from 'vue-codemirror'
  45. import 'codemirror/lib/codemirror.css'
  46. // Theme
  47. import 'codemirror/theme/base16-dark.css'
  48. // Language
  49. import 'codemirror/mode/markdown/markdown.js'
  50. // Addons
  51. import 'codemirror/addon/selection/active-line.js'
  52. import 'codemirror/addon/display/fullscreen.js'
  53. import 'codemirror/addon/display/fullscreen.css'
  54. import 'codemirror/addon/selection/mark-selection.js'
  55. import 'codemirror/addon/scroll/annotatescrollbar.js'
  56. import 'codemirror/addon/search/matchesonscrollbar.js'
  57. import 'codemirror/addon/search/searchcursor.js'
  58. import 'codemirror/addon/search/match-highlighter.js'
  59. export default {
  60. components: {
  61. codemirror
  62. },
  63. data() {
  64. return {
  65. code: 'const a = 10',
  66. cmOptions: {
  67. tabSize: 2,
  68. mode: 'text/markdown',
  69. theme: 'base16-dark',
  70. lineNumbers: false,
  71. lineWrapping: true,
  72. line: true,
  73. styleActiveLine: true,
  74. highlightSelectionMatches: {
  75. annotateScrollbar: true
  76. },
  77. viewportMargin: 50,
  78. extraKeys: {
  79. 'F11'(cm) {
  80. cm.setOption('fullScreen', !cm.getOption('fullScreen'))
  81. },
  82. 'Esc'(cm) {
  83. if (cm.getOption('fullScreen')) cm.setOption('fullScreen', false)
  84. }
  85. }
  86. }
  87. }
  88. },
  89. computed: {
  90. cm() {
  91. return this.$refs.cm.codemirror
  92. }
  93. },
  94. methods: {
  95. onCmReady(cm) {
  96. cm.setSize(null, 'calc(100vh - 50px)')
  97. },
  98. onCmFocus(cm) {
  99. console.log('the editor is focus!', cm)
  100. },
  101. onCmCodeChange(newCode) {
  102. console.log('this is new code', newCode)
  103. this.code = newCode
  104. }
  105. }
  106. }
  107. </script>
  108. <style lang='scss'>
  109. .editor-code {
  110. &-main {
  111. display: flex;
  112. width: 100%;
  113. }
  114. &-editor {
  115. flex: 1 1 50%;
  116. display: block;
  117. min-height: calc(100vh - 50px);
  118. position: relative;
  119. &-title {
  120. background-color: mc('grey', '800');
  121. border-bottom-left-radius: 5px;
  122. display: inline-flex;
  123. height: 30px;
  124. justify-content: center;
  125. align-items: center;
  126. padding: 0 1rem;
  127. color: mc('grey', '500');
  128. position: absolute;
  129. top: 0;
  130. right: 0;
  131. z-index: 2;
  132. text-transform: uppercase;
  133. font-size: .7rem;
  134. }
  135. }
  136. &-preview {
  137. flex: 1 1 50%;
  138. background-color: mc('grey', '100');
  139. position: relative;
  140. &-title {
  141. background-color: mc('blue', '100');
  142. border-bottom-right-radius: 5px;
  143. display: inline-flex;
  144. height: 30px;
  145. justify-content: center;
  146. align-items: center;
  147. padding: 0 1rem;
  148. color: mc('blue', '800');
  149. position: absolute;
  150. top: 0;
  151. left: 0;
  152. z-index: 2;
  153. text-transform: uppercase;
  154. font-size: .7rem;
  155. }
  156. }
  157. &-toolbar {
  158. background-color: mc('blue', '700');
  159. background-image: linear-gradient(to bottom, mc('blue', '700') 0%, mc('blue','800') 100%);
  160. height: 50px;
  161. color: #FFF;
  162. display: flex;
  163. &-group {
  164. display: flex;
  165. + .editor-code-toolbar-group {
  166. border-left: 1px solid rgba(mc('blue', '900'), .75);
  167. }
  168. }
  169. &-item {
  170. width: 40px;
  171. height: 50px;
  172. display: flex;
  173. justify-content: center;
  174. align-items: center;
  175. transition: all .4s ease;
  176. cursor: pointer;
  177. &:first-child {
  178. padding-left: .5rem;
  179. }
  180. &:last-child {
  181. padding-right: .5rem;
  182. }
  183. &:hover {
  184. background-color: mc('blue', '600');
  185. }
  186. }
  187. svg {
  188. use {
  189. color: #FFF;
  190. }
  191. }
  192. }
  193. }
  194. .CodeMirror {
  195. height: auto;
  196. }
  197. .CodeMirror-focused .cm-matchhighlight {
  198. background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAFklEQVQI12NgYGBgkKzc8x9CMDAwAAAmhwSbidEoSQAAAABJRU5ErkJggg==);
  199. background-position: bottom;
  200. background-repeat: repeat-x;
  201. }
  202. .cm-matchhighlight {
  203. background-color: mc('grey', '800');
  204. }
  205. .CodeMirror-selection-highlight-scrollbar {
  206. background-color: mc('green', '600');
  207. }
  208. </style>