vue?codemirror實(shí)現(xiàn)在線代碼編譯器效果
前言
如果我們想在Web端實(shí)現(xiàn)在線代碼編譯的效果,那么需要使用組件vue-codemirror
,他是將CodeMirror
進(jìn)行了再次封裝
- 支持代碼高亮
- 62種主題顏色,例如monokai等等
- 支持json, sql, javascript,css,xml, html,yaml, markdown, python編輯模式,默認(rèn)為 json
- 支持快速搜索
- 支持自動(dòng)補(bǔ)全提示
- 支持自動(dòng)匹配括號(hào)
環(huán)境準(zhǔn)備
npm install jshint npm install jsonlint npm install script-loader npm install vue-codemirror
封裝組件
我們可以在項(xiàng)目中的components
中將vue-codemirror
進(jìn)行再次封裝
<template> <codemirror ref="myCm" v-model="editorValue" :options="cmOptions" @changes="onCmCodeChanges" @blur="onCmBlur" @keydown.native="onKeyDown" @mousedown.native="onMouseDown" @paste.native="OnPaste" > </codemirror> </template> <script> import { codemirror } from "vue-codemirror"; import 'codemirror/keymap/sublime' import "codemirror/mode/javascript/javascript.js"; import "codemirror/mode/xml/xml.js"; import "codemirror/mode/htmlmixed/htmlmixed.js"; import "codemirror/mode/css/css.js"; import "codemirror/mode/yaml/yaml.js"; import "codemirror/mode/sql/sql.js"; import "codemirror/mode/python/python.js"; import "codemirror/mode/markdown/markdown.js"; import "codemirror/addon/hint/show-hint.css"; import "codemirror/addon/hint/show-hint.js"; import "codemirror/addon/hint/javascript-hint.js"; import "codemirror/addon/hint/xml-hint.js"; import "codemirror/addon/hint/css-hint.js"; import "codemirror/addon/hint/html-hint.js"; import "codemirror/addon/hint/sql-hint.js"; import "codemirror/addon/hint/anyword-hint.js"; import "codemirror/addon/lint/lint.css"; import "codemirror/addon/lint/lint.js"; import "codemirror/addon/lint/json-lint"; import 'codemirror/addon/selection/active-line' import "codemirror/addon/hint/show-hint.js"; import "codemirror/addon/hint/anyword-hint.js"; require("script-loader!jsonlint"); import "codemirror/addon/lint/javascript-lint.js"; import "codemirror/addon/fold/foldcode.js"; import "codemirror/addon/fold/foldgutter.js"; import "codemirror/addon/fold/foldgutter.css"; import "codemirror/addon/fold/brace-fold.js"; import "codemirror/addon/fold/xml-fold.js"; import "codemirror/addon/fold/comment-fold.js"; import "codemirror/addon/fold/markdown-fold.js"; import "codemirror/addon/fold/indent-fold.js"; import "codemirror/addon/edit/closebrackets.js"; import "codemirror/addon/edit/closetag.js"; import "codemirror/addon/edit/matchtags.js"; import "codemirror/addon/edit/matchbrackets.js"; import "codemirror/addon/selection/active-line.js"; import "codemirror/addon/search/jump-to-line.js"; import "codemirror/addon/dialog/dialog.js"; import "codemirror/addon/dialog/dialog.css"; import "codemirror/addon/search/searchcursor.js"; import "codemirror/addon/search/search.js"; import "codemirror/addon/display/autorefresh.js"; import "codemirror/addon/selection/mark-selection.js"; import "codemirror/addon/search/match-highlighter.js"; export default { name: "index", components: {codemirror}, props: ["cmTheme", "cmMode", "cmIndentUnit", "autoFormatJson"], data() { return { editorValue: '{}', cmOptions: { theme: !this.cmTheme || this.cmTheme === "default" ? "default" : this.cmTheme, // 主題 mode: !this.cmMode || this.cmMode === "default" ? "application/json" : this.cmMode, // 代碼格式 tabSize: 4, // tab的空格個(gè)數(shù) indentUnit: !this.cmIndentUnit ? 2 : this.cmIndentUnit, // 一個(gè)塊(編輯語(yǔ)言中的含義)應(yīng)縮進(jìn)多少個(gè)空格 autocorrect: true, // 自動(dòng)更正 spellcheck: true, // 拼寫(xiě)檢查 lint: true, // 檢查格式 lineNumbers: true, //是否顯示行數(shù) lineWrapping: true, //是否自動(dòng)換行 styleActiveLine: true, //line選擇是是否高亮 keyMap: 'sublime', // sublime編輯器效果 matchBrackets: true, //括號(hào)匹配 autoCloseBrackets: true, // 在鍵入時(shí)將自動(dòng)關(guān)閉括號(hào)和引號(hào) matchTags: { bothTags: true }, // 將突出顯示光標(biāo)周?chē)臉?biāo)簽 foldGutter: true, // 可將對(duì)象折疊,與下面的gutters一起使用 gutters: [ "CodeMirror-lint-markers", "CodeMirror-linenumbers", "CodeMirror-foldgutter" ], highlightSelectionMatches: { minChars: 2, style: "matchhighlight", showToken: true }, }, enableAutoFormatJson: this.autoFormatJson == null ? true : this.autoFormatJson, // json編輯模式下,輸入框失去焦點(diǎn)時(shí)是否自動(dòng)格式化,true 開(kāi)啟, false 關(guān)閉 } }, created() { try { if (!this.editorValue) { this.cmOptions.lint = false; return; } if (this.cmOptions.mode === "application/json") { if (!this.enableAutoFormatJson) { return; } this.editorValue = this.formatStrInJson(this.editorValue); } } catch (e) { console.log("初始化codemirror出錯(cuò):" + e); } }, methods: { resetLint() { if (!this.$refs.myCm.codemirror.getValue()) { this.$nextTick(() => { this.$refs.myCm.codemirror.setOption("lint", false); }); return; } this.$refs.myCm.codemirror.setOption("lint", false); this.$nextTick(() => { this.$refs.myCm.codemirror.setOption("lint", true); }); }, // 格式化字符串為json格式字符串 formatStrInJson(strValue) { return JSON.stringify( JSON.parse(strValue), null, this.cmIndentUnit ); }, onCmCodeChanges(cm, changes) { this.editorValue = cm.getValue(); this.resetLint(); }, // 失去焦點(diǎn)時(shí)處理函數(shù) onCmBlur(cm, event) { try { let editorValue = cm.getValue(); if (this.cmOptions.mode === "application/json" && editorValue) { if (!this.enableAutoFormatJson) { return; } this.editorValue = this.formatStrInJson(editorValue); } } catch (e) { // 啥也不做 } }, // 按下鍵盤(pán)事件處理函數(shù) onKeyDown(event) { const keyCode = event.keyCode || event.which || event.charCode; const keyCombination = event.ctrlKey || event.altKey || event.metaKey; if (!keyCombination && keyCode > 64 && keyCode < 123) { this.$refs.myCm.codemirror.showHint({ completeSingle: false }); } }, // 按下鼠標(biāo)時(shí)事件處理函數(shù) onMouseDown(event) { this.$refs.myCm.codemirror.closeHint(); }, // 黏貼事件處理函數(shù) OnPaste(event) { if (this.cmOptions.mode === "application/json") { try { this.editorValue = this.formatStrInJson(this.editorValue); } catch (e) { // 啥都不做 } } }, } } </script> <style scoped> </style>
此組件默認(rèn)配置了json編譯器,cmOptions
中是代碼編譯器的配置項(xiàng),需要額外的功能也可以去看官方文檔配置
接下來(lái)看展示效果
可以看到我們輸入了json格式的字符串,即使格式不正確,會(huì)給我們錯(cuò)誤提示,并且也會(huì)給我們自動(dòng)格式化
python編譯器
我們封裝的組件默認(rèn)是json編譯器,如果我們想使用其他語(yǔ)言,也很簡(jiǎn)單,只需要導(dǎo)入其他語(yǔ)言的mode
<template> <div> <el-button type="primary" icon="el-icon-circle-check-outline" @click="handleConfirm" round> 點(diǎn)擊保存 </el-button> <el-button icon="el-icon-caret-right" type="info" @click="handleRunCode" round> 在線運(yùn)行 </el-button> <code-editor :cmTheme="cmTheme" :cmMode="cmMode" > </code-editor> </div> </template> <script> import codeEditor from '@/components/CodeEditor/index' import 'codemirror/theme/monokai.css' // 導(dǎo)入monokai主題 import 'codemirror/mode/python/python.js' // 導(dǎo)入python export default { name: "index", components: { codeEditor }, data() { return { cmTheme: "monokai", cmMode: "python", } }, methods: { handleConfirm() {}, handleRunCode() {} } } </script> <style> .CodeMirror { position: relative; height: 100vh; overflow: hidden; margin-top: 10px; } </style> <style lang="scss" scoped> </style>
效果如下
到此這篇關(guān)于vue codemirror實(shí)現(xiàn)在線代碼編譯器 的文章就介紹到這了,更多相關(guān)vue codemirror在線代碼編譯器 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue實(shí)現(xiàn)codemirror代碼編輯器中的SQL代碼格式化功能
- 在vue項(xiàng)目中使用codemirror插件實(shí)現(xiàn)代碼編輯器功能
- 在vue里使用codemirror遇到的問(wèn)題
- vue使用codemirror的兩種用法
- vue中使用codemirror的實(shí)例詳解
- 前端插件庫(kù)之vue3使用vue-codemirror插件的步驟和實(shí)例
- CodeMirror實(shí)現(xiàn)代碼對(duì)比功能(插件react vue)
- Vue進(jìn)階之CodeMirror的應(yīng)用小結(jié)
- vue3中如何使用codemirror6增加代碼提示功能
相關(guān)文章
vue如何設(shè)置定時(shí)器和清理定時(shí)器
這篇文章主要介紹了vue如何設(shè)置定時(shí)器和清理定時(shí)器,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05vue調(diào)試工具vue-devtools安裝及使用方法
本文主要介紹 vue的調(diào)試工具 vue-devtools 的安裝和使用,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-11-11在vue項(xiàng)目中 實(shí)現(xiàn)定義全局變量 全局函數(shù)操作
這篇文章主要介紹了在vue項(xiàng)目中 實(shí)現(xiàn)定義全局變量 全局函數(shù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10vue中定時(shí)器setInterval的使用及說(shuō)明
這篇文章主要介紹了vue中定時(shí)器setInterval的使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03vue項(xiàng)目如何實(shí)現(xiàn)ip和localhost同時(shí)訪問(wèn)
這篇文章主要介紹了vue項(xiàng)目如何實(shí)現(xiàn)ip和localhost同時(shí)訪問(wèn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10vue實(shí)現(xiàn)長(zhǎng)圖垂直居上 vue實(shí)現(xiàn)短圖垂直居中
這篇文章主要為大家詳細(xì)介紹了vue彈性布局實(shí)現(xiàn)長(zhǎng)圖垂直居上,vue實(shí)現(xiàn)短圖垂直居中,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10使用Bootstrap + Vue.js實(shí)現(xiàn)表格的動(dòng)態(tài)展示、新增和刪除功能
這篇文章主要介紹了使用Bootstrap + Vue.js實(shí)現(xiàn)表格的動(dòng)態(tài)展示、新增和刪除功能,需要的朋友可以參考下2017-11-11解決vue.js 數(shù)據(jù)渲染成功仍報(bào)錯(cuò)的問(wèn)題
今天小編就為大家分享一篇解決vue.js 數(shù)據(jù)渲染成功仍報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08