123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- <template>
- <div class="cl-code">
- <codemirror
- ref="code"
- v-model="value2"
- :options="options2"
- :style="{
- height,
- width
- }"
- />
- </div>
- </template>
- <script>
- import { codemirror } from "vue-codemirror";
- import beautifyJs from "js-beautify";
- import "codemirror/theme/cobalt.css";
- import "codemirror/lib/codemirror.css";
- import "codemirror/addon/hint/show-hint.css";
- import "codemirror/addon/hint/javascript-hint";
- import "codemirror/mode/javascript/javascript";
- export default {
- name: "cl-codemirror",
- components: {
- codemirror
- },
- props: {
- value: String,
- height: String,
- width: String,
- options: Object
- },
- data() {
- return {
- value2: ""
- };
- },
- watch: {
- value: {
- immediate: true,
- handler(val) {
- this.value2 = val || "";
- }
- },
- value2(val) {
- this.$emit("input", val);
- }
- },
- computed: {
- options2() {
- return {
- mode: "javascript",
- theme: "ambiance",
- styleActiveLine: true,
- lineNumbers: true,
- lineWrapping: true,
- indentUnit: 4,
- ...this.options
- };
- }
- },
- mounted() {
- this.$el.onkeydown = (e) => {
- let keyCode = e.keyCode || e.which || e.charCode;
- let altKey = e.altKey || e.metaKey;
- let shiftKey = e.shiftKey || e.metaKey;
- if (altKey && shiftKey && keyCode == 70) {
- this.setValue();
- }
- };
- this.setValue(this.value2);
- },
- methods: {
- setValue(val) {
- this.value2 = beautifyJs(val || this.value2);
- }
- }
- };
- </script>
- <style lang="scss">
- .cl-code {
- border-radius: 3px;
- border: 1px solid #dcdfe6;
- box-sizing: border-box;
- border-radius: 3px;
- }
- .CodeMirror {
- height: 100%;
- }
- .cm-s-ambiance * {
- font-family: "Consolas";
- font-size: 13px;
- }
- .cm-s-ambiance .cm-header {
- color: blue;
- }
- .cm-s-ambiance .cm-quote {
- color: #24c2c7;
- }
- .cm-s-ambiance .cm-keyword {
- color: #a626a4;
- }
- .cm-s-ambiance .cm-atom {
- color: #986801;
- }
- .cm-s-ambiance .cm-number {
- color: #986801;
- }
- .cm-s-ambiance .cm-def {
- color: #383a42;
- }
- .cm-s-ambiance .cm-variable {
- color: #4078f2;
- }
- .cm-s-ambiance .cm-variable-2 {
- color: #eed1b3;
- }
- .cm-s-ambiance .cm-variable-3,
- .cm-s-ambiance .cm-type {
- color: #faded3;
- }
- .cm-s-ambiance .cm-property {
- color: #333333;
- }
- .cm-s-ambiance .cm-operator {
- color: #0184bc;
- }
- .cm-s-ambiance .cm-comment {
- color: #555;
- font-style: italic;
- }
- .cm-s-ambiance .cm-string {
- color: #50a14f;
- }
- .cm-s-ambiance .cm-string-2 {
- color: #9d937c;
- }
- .cm-s-ambiance .cm-meta {
- color: #d2a8a1;
- }
- .cm-s-ambiance .cm-qualifier {
- color: yellow;
- }
- .cm-s-ambiance .cm-builtin {
- color: #9999cc;
- }
- .cm-s-ambiance .cm-bracket {
- color: #24c2c7;
- }
- .cm-s-ambiance .cm-tag {
- color: #fee4ff;
- }
- .cm-s-ambiance .cm-attribute {
- color: #9b859d;
- }
- .cm-s-ambiance .cm-hr {
- color: pink;
- }
- .cm-s-ambiance .cm-link {
- color: #f4c20b;
- }
- .cm-s-ambiance .cm-special {
- color: #ff9d00;
- }
- .cm-s-ambiance .cm-error {
- color: #af2018;
- }
- .cm-s-ambiance .CodeMirror-matchingbracket {
- color: #0f0;
- }
- .cm-s-ambiance .CodeMirror-nonmatchingbracket {
- color: #f22;
- }
- .cm-s-ambiance div.CodeMirror-selected {
- background: rgba(0, 0, 0, 0.15);
- }
- .cm-s-ambiance.CodeMirror-focused div.CodeMirror-selected {
- background: rgba(0, 0, 0, 0.1);
- }
- .cm-s-ambiance .CodeMirror-line::selection,
- .cm-s-ambiance .CodeMirror-line > span::selection,
- .cm-s-ambiance .CodeMirror-line > span > span::selection {
- background: rgba(0, 0, 0, 0.1);
- }
- .cm-s-ambiance .CodeMirror-line::-moz-selection,
- .cm-s-ambiance .CodeMirror-line > span::-moz-selection,
- .cm-s-ambiance .CodeMirror-line > span > span::-moz-selection {
- background: rgba(0, 0, 0, 0.1);
- }
- /* Editor styling */
- .cm-s-ambiance.CodeMirror {
- line-height: 1.4em;
- color: #383a42;
- background-color: #f7f7f7;
- }
- .cm-s-ambiance .CodeMirror-gutters {
- background: #f7f7f7;
- }
- .cm-s-ambiance .CodeMirror-linenumber {
- color: #666;
- padding: 0 5px;
- }
- .cm-s-ambiance .CodeMirror-guttermarker {
- color: #aaa;
- }
- .cm-s-ambiance .CodeMirror-guttermarker-subtle {
- color: #666;
- }
- .cm-s-ambiance .CodeMirror-cursor {
- border-left: 1px solid #999;
- margin-left: 2px;
- }
- .cm-s-ambiance .CodeMirror-activeline-background {
- background: none repeat scroll 0% 0% rgba(255, 255, 255, 0.031);
- }
- </style>
|