index.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <template>
  2. <div class="cl-code">
  3. <codemirror
  4. ref="code"
  5. v-model="value2"
  6. :options="options2"
  7. :style="{
  8. height,
  9. width
  10. }"
  11. />
  12. </div>
  13. </template>
  14. <script>
  15. import { codemirror } from "vue-codemirror";
  16. import beautifyJs from "js-beautify";
  17. import "codemirror/theme/cobalt.css";
  18. import "codemirror/lib/codemirror.css";
  19. import "codemirror/addon/hint/show-hint.css";
  20. import "codemirror/addon/hint/javascript-hint";
  21. import "codemirror/mode/javascript/javascript";
  22. export default {
  23. name: "cl-codemirror",
  24. components: {
  25. codemirror
  26. },
  27. props: {
  28. value: String,
  29. height: String,
  30. width: String,
  31. options: Object
  32. },
  33. data() {
  34. return {
  35. value2: ""
  36. };
  37. },
  38. watch: {
  39. value: {
  40. immediate: true,
  41. handler(val) {
  42. this.value2 = val || "";
  43. }
  44. },
  45. value2(val) {
  46. this.$emit("input", val);
  47. }
  48. },
  49. computed: {
  50. options2() {
  51. return {
  52. mode: "javascript",
  53. theme: "ambiance",
  54. styleActiveLine: true,
  55. lineNumbers: true,
  56. lineWrapping: true,
  57. indentUnit: 4,
  58. ...this.options
  59. };
  60. }
  61. },
  62. mounted() {
  63. this.$el.onkeydown = (e) => {
  64. let keyCode = e.keyCode || e.which || e.charCode;
  65. let altKey = e.altKey || e.metaKey;
  66. let shiftKey = e.shiftKey || e.metaKey;
  67. if (altKey && shiftKey && keyCode == 70) {
  68. this.setValue();
  69. }
  70. };
  71. this.setValue(this.value2);
  72. },
  73. methods: {
  74. setValue(val) {
  75. this.value2 = beautifyJs(val || this.value2);
  76. }
  77. }
  78. };
  79. </script>
  80. <style lang="scss">
  81. .cl-code {
  82. border-radius: 3px;
  83. border: 1px solid #dcdfe6;
  84. box-sizing: border-box;
  85. border-radius: 3px;
  86. }
  87. .CodeMirror {
  88. height: 100%;
  89. }
  90. .cm-s-ambiance * {
  91. font-family: "Consolas";
  92. font-size: 13px;
  93. }
  94. .cm-s-ambiance .cm-header {
  95. color: blue;
  96. }
  97. .cm-s-ambiance .cm-quote {
  98. color: #24c2c7;
  99. }
  100. .cm-s-ambiance .cm-keyword {
  101. color: #a626a4;
  102. }
  103. .cm-s-ambiance .cm-atom {
  104. color: #986801;
  105. }
  106. .cm-s-ambiance .cm-number {
  107. color: #986801;
  108. }
  109. .cm-s-ambiance .cm-def {
  110. color: #383a42;
  111. }
  112. .cm-s-ambiance .cm-variable {
  113. color: #4078f2;
  114. }
  115. .cm-s-ambiance .cm-variable-2 {
  116. color: #eed1b3;
  117. }
  118. .cm-s-ambiance .cm-variable-3,
  119. .cm-s-ambiance .cm-type {
  120. color: #faded3;
  121. }
  122. .cm-s-ambiance .cm-property {
  123. color: #333333;
  124. }
  125. .cm-s-ambiance .cm-operator {
  126. color: #0184bc;
  127. }
  128. .cm-s-ambiance .cm-comment {
  129. color: #555;
  130. font-style: italic;
  131. }
  132. .cm-s-ambiance .cm-string {
  133. color: #50a14f;
  134. }
  135. .cm-s-ambiance .cm-string-2 {
  136. color: #9d937c;
  137. }
  138. .cm-s-ambiance .cm-meta {
  139. color: #d2a8a1;
  140. }
  141. .cm-s-ambiance .cm-qualifier {
  142. color: yellow;
  143. }
  144. .cm-s-ambiance .cm-builtin {
  145. color: #9999cc;
  146. }
  147. .cm-s-ambiance .cm-bracket {
  148. color: #24c2c7;
  149. }
  150. .cm-s-ambiance .cm-tag {
  151. color: #fee4ff;
  152. }
  153. .cm-s-ambiance .cm-attribute {
  154. color: #9b859d;
  155. }
  156. .cm-s-ambiance .cm-hr {
  157. color: pink;
  158. }
  159. .cm-s-ambiance .cm-link {
  160. color: #f4c20b;
  161. }
  162. .cm-s-ambiance .cm-special {
  163. color: #ff9d00;
  164. }
  165. .cm-s-ambiance .cm-error {
  166. color: #af2018;
  167. }
  168. .cm-s-ambiance .CodeMirror-matchingbracket {
  169. color: #0f0;
  170. }
  171. .cm-s-ambiance .CodeMirror-nonmatchingbracket {
  172. color: #f22;
  173. }
  174. .cm-s-ambiance div.CodeMirror-selected {
  175. background: rgba(0, 0, 0, 0.15);
  176. }
  177. .cm-s-ambiance.CodeMirror-focused div.CodeMirror-selected {
  178. background: rgba(0, 0, 0, 0.1);
  179. }
  180. .cm-s-ambiance .CodeMirror-line::selection,
  181. .cm-s-ambiance .CodeMirror-line > span::selection,
  182. .cm-s-ambiance .CodeMirror-line > span > span::selection {
  183. background: rgba(0, 0, 0, 0.1);
  184. }
  185. .cm-s-ambiance .CodeMirror-line::-moz-selection,
  186. .cm-s-ambiance .CodeMirror-line > span::-moz-selection,
  187. .cm-s-ambiance .CodeMirror-line > span > span::-moz-selection {
  188. background: rgba(0, 0, 0, 0.1);
  189. }
  190. /* Editor styling */
  191. .cm-s-ambiance.CodeMirror {
  192. line-height: 1.4em;
  193. color: #383a42;
  194. background-color: #f7f7f7;
  195. }
  196. .cm-s-ambiance .CodeMirror-gutters {
  197. background: #f7f7f7;
  198. }
  199. .cm-s-ambiance .CodeMirror-linenumber {
  200. color: #666;
  201. padding: 0 5px;
  202. }
  203. .cm-s-ambiance .CodeMirror-guttermarker {
  204. color: #aaa;
  205. }
  206. .cm-s-ambiance .CodeMirror-guttermarker-subtle {
  207. color: #666;
  208. }
  209. .cm-s-ambiance .CodeMirror-cursor {
  210. border-left: 1px solid #999;
  211. margin-left: 2px;
  212. }
  213. .cm-s-ambiance .CodeMirror-activeline-background {
  214. background: none repeat scroll 0% 0% rgba(255, 255, 255, 0.031);
  215. }
  216. </style>