editor-code.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. v-menu(offset-y, open-on-hover)
  19. .editor-code-toolbar-item.is-dropdown(slot='activator')
  20. svg.icons.is-18(role='img')
  21. title Heading
  22. use(xlink:href='#fa-heading')
  23. v-list
  24. v-list-tile(v-for='(n, idx) in 6', @click='', :key='idx')
  25. v-list-tile-action: v-icon format_size
  26. v-list-tile-title Heading {{n}}
  27. .editor-code-toolbar-item
  28. svg.icons.is-18(role='img')
  29. title Blockquote
  30. use(xlink:href='#fa-quote-left')
  31. .editor-code-toolbar-group
  32. .editor-code-toolbar-item
  33. svg.icons.is-18(role='img')
  34. title Unordered List
  35. use(xlink:href='#fa-list-ul')
  36. .editor-code-toolbar-item
  37. svg.icons.is-18(role='img')
  38. title Ordered List
  39. use(xlink:href='#fa-list-ol')
  40. .editor-code-toolbar-group
  41. .editor-code-toolbar-item
  42. svg.icons.is-18(role='img')
  43. title Link
  44. use(xlink:href='#fa-link')
  45. .editor-code-toolbar-group
  46. .editor-code-toolbar-item
  47. svg.icons.is-18(role='img')
  48. title Inline Code
  49. use(xlink:href='#fa-terminal')
  50. .editor-code-toolbar-item
  51. svg.icons.is-18(role='img')
  52. title Code Block
  53. use(xlink:href='#fa-code')
  54. .editor-code-toolbar-group
  55. .editor-code-toolbar-item
  56. svg.icons.is-18(role='img')
  57. title Horizontal Bar
  58. use(xlink:href='#fa-minus')
  59. .editor-code-main
  60. .editor-code-editor
  61. .editor-code-editor-title Editor
  62. codemirror(ref='cm', v-model='code', :options='cmOptions', @ready="onCmReady")
  63. .editor-code-preview
  64. .editor-code-preview-title Preview
  65. v-speed-dial(:open-on-hover='true', direction='top', transition='slide-y-reverse-transition', :fixed='true', :right='!isMobile', :left='isMobile', :bottom='true')
  66. v-btn(color='blue', fab, dark, slot='activator')
  67. v-icon add_circle
  68. v-icon close
  69. v-btn(color='teal', fab, dark): v-icon image
  70. v-btn(color='pink', fab, dark): v-icon insert_drive_file
  71. v-btn(color='red', fab, dark): v-icon play_circle_outline
  72. v-btn(color='purple', fab, dark): v-icon multiline_chart
  73. v-btn(color='indigo', fab, dark): v-icon functions
  74. v-speed-dial(:open-on-hover='true', :direction='saveMenuDirection', transition='slide-x-reverse-transition', :fixed='true', :right='true', :top='!isMobile', :bottom='isMobile')
  75. v-btn(color='blue', fab, dark, slot='activator')
  76. v-icon more_horiz
  77. v-icon close
  78. v-btn(color='blue-grey', fab, dark): v-icon sort_by_alpha
  79. v-btn(color='green', fab, dark): v-icon save
  80. v-btn(color='red', fab, dark, small): v-icon not_interested
  81. v-btn(color='orange', fab, dark, small): v-icon vpn_lock
  82. v-btn(color='indigo', fab, dark, small): v-icon restore
  83. v-btn(color='brown', fab, dark, small): v-icon archive
  84. </template>
  85. <script>
  86. import { codemirror } from 'vue-codemirror'
  87. import 'codemirror/lib/codemirror.css'
  88. // Theme
  89. import 'codemirror/theme/base16-dark.css'
  90. // Language
  91. import 'codemirror/mode/markdown/markdown.js'
  92. // Addons
  93. import 'codemirror/addon/selection/active-line.js'
  94. import 'codemirror/addon/display/fullscreen.js'
  95. import 'codemirror/addon/display/fullscreen.css'
  96. import 'codemirror/addon/selection/mark-selection.js'
  97. import 'codemirror/addon/scroll/annotatescrollbar.js'
  98. import 'codemirror/addon/search/matchesonscrollbar.js'
  99. import 'codemirror/addon/search/searchcursor.js'
  100. import 'codemirror/addon/search/match-highlighter.js'
  101. export default {
  102. components: {
  103. codemirror
  104. },
  105. data() {
  106. return {
  107. code: 'const a = 10',
  108. cmOptions: {
  109. tabSize: 2,
  110. mode: 'text/markdown',
  111. theme: 'base16-dark',
  112. lineNumbers: true,
  113. lineWrapping: true,
  114. line: true,
  115. styleActiveLine: true,
  116. highlightSelectionMatches: {
  117. annotateScrollbar: true
  118. },
  119. viewportMargin: 50,
  120. extraKeys: {
  121. 'F11'(cm) {
  122. cm.setOption('fullScreen', !cm.getOption('fullScreen'))
  123. },
  124. 'Esc'(cm) {
  125. if (cm.getOption('fullScreen')) cm.setOption('fullScreen', false)
  126. }
  127. }
  128. }
  129. }
  130. },
  131. computed: {
  132. cm() {
  133. return this.$refs.cm.codemirror
  134. },
  135. isMobile() {
  136. return this.$vuetify.breakpoint.smAndDown
  137. },
  138. saveMenuDirection() {
  139. return this.isMobile ? 'top' : 'bottom'
  140. }
  141. },
  142. methods: {
  143. onCmReady(cm) {
  144. cm.setSize(null, 'calc(100vh - 50px)')
  145. },
  146. onCmFocus(cm) {
  147. console.log('the editor is focus!', cm)
  148. },
  149. onCmCodeChange(newCode) {
  150. console.log('this is new code', newCode)
  151. this.code = newCode
  152. }
  153. }
  154. }
  155. </script>
  156. <style lang='scss'>
  157. .editor-code {
  158. &-main {
  159. display: flex;
  160. width: 100%;
  161. }
  162. &-editor {
  163. flex: 1 1 50%;
  164. display: block;
  165. min-height: calc(100vh - 50px);
  166. position: relative;
  167. &-title {
  168. background-color: mc('grey', '800');
  169. border-bottom-left-radius: 5px;
  170. display: inline-flex;
  171. height: 30px;
  172. justify-content: center;
  173. align-items: center;
  174. padding: 0 1rem;
  175. color: mc('grey', '500');
  176. position: absolute;
  177. top: 0;
  178. right: 0;
  179. z-index: 2;
  180. text-transform: uppercase;
  181. font-size: .7rem;
  182. @include until($tablet) {
  183. display: none;
  184. }
  185. }
  186. }
  187. &-preview {
  188. flex: 1 1 50%;
  189. background-color: mc('grey', '100');
  190. position: relative;
  191. padding: 30px 1rem 1rem 1rem;
  192. @include until($tablet) {
  193. display: none;
  194. }
  195. &-title {
  196. background-color: mc('blue', '100');
  197. border-bottom-right-radius: 5px;
  198. display: inline-flex;
  199. height: 30px;
  200. justify-content: center;
  201. align-items: center;
  202. padding: 0 1rem;
  203. color: mc('blue', '800');
  204. position: absolute;
  205. top: 0;
  206. left: 0;
  207. z-index: 2;
  208. text-transform: uppercase;
  209. font-size: .7rem;
  210. }
  211. }
  212. &-toolbar {
  213. background-color: mc('blue', '700');
  214. background-image: linear-gradient(to bottom, mc('blue', '700') 0%, mc('blue','800') 100%);
  215. height: 50px;
  216. color: #FFF;
  217. display: flex;
  218. @include until($tablet) {
  219. justify-content: center;
  220. }
  221. &-group {
  222. display: flex;
  223. + .editor-code-toolbar-group {
  224. border-left: 1px solid rgba(mc('blue', '900'), .75);
  225. }
  226. }
  227. &-item {
  228. width: 40px;
  229. height: 50px;
  230. display: flex;
  231. justify-content: center;
  232. align-items: center;
  233. transition: all .4s ease;
  234. cursor: pointer;
  235. &:first-child {
  236. padding-left: .5rem;
  237. }
  238. &:last-child {
  239. padding-right: .5rem;
  240. }
  241. &:hover {
  242. background-color: mc('blue', '600');
  243. }
  244. @include until($tablet) {
  245. width: 35px;
  246. }
  247. }
  248. svg {
  249. use {
  250. color: #FFF;
  251. }
  252. }
  253. }
  254. }
  255. .CodeMirror {
  256. height: auto;
  257. }
  258. .CodeMirror-focused .cm-matchhighlight {
  259. background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAFklEQVQI12NgYGBgkKzc8x9CMDAwAAAmhwSbidEoSQAAAABJRU5ErkJggg==);
  260. background-position: bottom;
  261. background-repeat: repeat-x;
  262. }
  263. .cm-matchhighlight {
  264. background-color: mc('grey', '800');
  265. }
  266. .CodeMirror-selection-highlight-scrollbar {
  267. background-color: mc('green', '600');
  268. }
  269. </style>