1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| <template> <div class="codePanel"><textarea ref="codemirror"></textarea></div> </template>
<script> import CodeMirror from "codemirror"; import "codemirror/lib/codemirror.css"; import "codemirror/mode/javascript/javascript.js"; import "codemirror/theme/idea.css"; import "codemirror/keymap/sublime.js"; import "codemirror/addon/dialog/dialog.js"; import "codemirror/addon/dialog/dialog.css"; import "codemirror/addon/search/search"; import "codemirror/addon/search/searchcursor.js"; import "codemirror/addon/fold/foldgutter.css"; import "codemirror/addon/fold/foldcode.js"; import "codemirror/addon/fold/foldgutter.js"; import "codemirror/addon/fold/brace-fold.js"; import "codemirror/addon/fold/comment-fold.js"; import "codemirror/addon/display/fullscreen.css"; import "codemirror/addon/display/fullscreen.js"; import "codemirror/addon/edit/matchbrackets.js"; import "codemirror/addon/edit/closebrackets.js"; import "codemirror/addon/hint/show-hint.css"; import "codemirror/addon/hint/show-hint.js"; import "codemirror/addon/hint/anyword-hint.js"; import "codemirror/addon/comment/comment.js"; import "codemirror/addon/lint/lint.css"; import "codemirror/addon/lint/lint.js"; import "codemirror/addon/lint/json-lint.js"; import jsonlint from "jsonlint-mod"; window.jsonlint = jsonlint;
let codemirror; export default { name: "JSON编辑器", data: () => ({ code: "", }), mounted() { codemirror = markRaw( CodeMirror.fromTextArea(this.$refs.codemirror, { mode: "application/json", indentUnit: 2, smartIndent: true, styleActiveLine: true, lineNumbers: true, theme: "idea", keyMap: "sublime", lineWrapping: true, foldGutter: true, gutters: [ "CodeMirror-linenumbers", "CodeMirror-foldgutter", "CodeMirror-lint-markers", ], lint: true,
fullScreen: false,
matchBrackets: true, autoCloseBrackets: true,
extraKeys: { F11: (cm) => { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, Esc: (cm) => { if (cm.getOption("fullScreen")) { cm.setOption("fullScreen", false); } }, }, }) ); codemirror.on("change", (cm) => { this.code = cm.getValue(); }); }, beforeUnmount() { this.destroy(); }, methods: { destroy() { const element = codemirror.doc.cm.getWrapperElement(); element && element.remove && element.remove(); }, }, }; </script>
|