vue3中如何使用codemirror6增加代碼提示功能
1、安裝依賴(lài)
// 安裝codemirror、語(yǔ)言包、主題、自動(dòng)補(bǔ)全
npm i codemirror npm i @codemirror/lang-javascript npm i @codemirror/autocomplete npm i @codemirror/theme-one-dark
本人安裝的版本是
"dependencies": { "@codemirror/autocomplete": "^6.0.0", "@codemirror/lang-javascript": "^6.0.2", "@codemirror/theme-one-dark": "^6.0.0", "codemirror": "^6.0.1", ... },
2、創(chuàng)建編輯器
<template> <el-select placeholder="請(qǐng)選擇分組" v-model="group" clearable @change="insertGroup" > <el-option v-for="dict in groupList :key="dict.id" :label="dict.dgName + '(' + dict.dgCode + ')'" :value="dict.dgCode" ></el-option> </el-select> <el-button @click="codeBeauty" style="margin-bottom: 0.5rem">代碼格式化</el-button> <div id="coder"></div> <el-button type="primary" @click="submitForm" v-if="!testFlag">確 定</el-button> </template>
<style scoped> #coder{ margin-top: 10px; width: 100%; } </style>
<script setup name="Command"> import { javascript } from "@codemirror/lang-javascript"; import { oneDark } from "@codemirror/theme-one-dark"; import { basicSetup, EditorView } from "codemirror"; import { autocompletion } from "@codemirror/autocomplete"; const { proxy } = getCurrentInstance(); const allKeyList = ref([]); const groupList = ref([]); const group = ref(""); const data = reactive({ form: {}, }); const { form } = toRefs(data); let editor = null; // 獲取自定義提示內(nèi)容 function getCommandList() { groupList.value = [ { id: '1', label: '分組1', value: 'group1' }, { id: '2', label: '分組2', value: 'group2' }, ]; allKeyList.value = [ { label: "@match", type: "keyword", apply: "match", detail: "match" }, { label: "@hello", type: "variable", apply: "hello", detail: "hellodetail" }, { label: "@magic", type: "text", apply: "??*.?.*??", detail: "macro" }, ]; } // 代碼美化 function codeBeauty() { editor.dispatch({ changes: { from: 0, to: editor.state.doc.length, insert:js_beautify(getCommanContent() || "") }, }); } // 獲取當(dāng)前編輯器中的內(nèi)容字符串 function getCommanContent() { let str = "" editor.state.doc.children.forEach((el,index) => { str += el.text.join("\n") + "\n" }) return str.slice(0,-1); } // 初始化編輯器 function initCodeContent(){ setTimeout(() => { if(!editor) { editor = new EditorView({ doc: "Press Ctrl-Space in here...\n", extensions: [ basicSetup, javascript(), oneDark, autocompletion({ override: [myCompletions] }), // EditorView.updateListener.of((v) => { // console.log(v.state.doc.toString()) // }), ], parent: document.getElementById("coder"), options: { lineNumbers: true, line: true, //ctrl-space喚起智能提示 extraKeys: { Ctrl: "autocomplete", }, //括號(hào)匹配 matchBrackets: true, }, }); } editor.dispatch({ changes: { from: 0, to: editor.state.doc.length, insert: form.value.commandContent || "Press Ctrl-Space in here...\n" }, }); }, 500); } // 自定義的代碼不全,options為自定義內(nèi)容,以@開(kāi)頭進(jìn)行匹配 function myCompletions(context) { let word = context.matchBefore(/@\w*/); if (!word && !context.explicit) return null; return { from: word.from ? word.from : context.pos, options: allKeyList.value, }; } // 選擇分組添加到編輯其中 function insertGroup() { insertCommandContant(group.value); group.value = ""; } // 外部輸入內(nèi)容,添加到編輯器當(dāng)前光標(biāo)(或選中內(nèi)容)所在的位置 function insertCommandContant(insertContent) { editor.dispatch({ changes: { from: editor.state.selection.ranges[0].from, to: editor.state.selection.ranges[0].to, insert: insertContent }, }); } /** 提交按鈕 */ function submitForm() { proxy.$modal.loading("正在保存,請(qǐng)稍候..."); form.value.commandContent = getCommanContent(); addForm(form.value).then((response) => { proxy.$modal.msgSuccess("新增成功"); proxy.$modal.closeLoading(); }).catch((err) => {proxy.$modal.closeLoading();}); } getCommandList(); initCodeContent(); </script>
總結(jié)
到此這篇關(guān)于vue3中如何使用codemirror6增加代碼提示功能的文章就介紹到這了,更多相關(guān)vue3 codemirror6增加代碼提示內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+element搭建后臺(tái)小總結(jié) el-dropdown下拉功能
這篇文章主要為大家詳細(xì)介紹了vue+element搭建后臺(tái)小總結(jié),el-dropdown下拉功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Element UI 上傳組件實(shí)現(xiàn)文件上傳并附帶額外參數(shù)功能
在使用 ElementUI 的上傳組件 el-upload 實(shí)現(xiàn)文件上傳功能時(shí),如果單文件上傳是比較簡(jiǎn)單的,但是在實(shí)際需求中,往往會(huì)在上傳文件時(shí)伴隨著一些其他參數(shù),怎么操作呢,下面通過(guò)示例代碼講解感興趣的朋友一起看看吧2023-08-08vue中對(duì)象的賦值Object.assign({}, row)方式
這篇文章主要介紹了vue中對(duì)象的賦值Object.assign({}, row)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Vue+Echarts實(shí)現(xiàn)柱狀折線(xiàn)圖
這篇文章主要為大家詳細(xì)介紹了Vue+Echarts實(shí)現(xiàn)柱狀折線(xiàn)圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Element-ui?Layout布局(Row和Col組件)的實(shí)現(xiàn)
我們?cè)趯?shí)際開(kāi)發(fā)中遇到一些布局的時(shí)候會(huì)用到Layout布局,本文就詳細(xì)的介紹了Element-ui?Layout布局(Row和Col組件)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2021-12-12vue3中實(shí)現(xiàn)雙向數(shù)據(jù)綁定的方法
Vue3中,雙向數(shù)據(jù)綁定主要通過(guò)v-model指令實(shí)現(xiàn),v-model是一個(gè)語(yǔ)法糖,結(jié)合了v-bind和v-on指令來(lái)實(shí)現(xiàn)數(shù)據(jù)的雙向綁定,它在內(nèi)部做了綁定數(shù)據(jù)和監(jiān)聽(tīng)輸入事件兩件事,感興趣的朋友跟隨小編一起看看吧2024-12-12Vue3+Element-Plus使用Table預(yù)覽圖片發(fā)生元素遮擋的解決方法
這篇文章主要介紹了Vue3+Element-Plus使用Table預(yù)覽圖片發(fā)生元素遮擋的問(wèn)題分析和解決方法,文中通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家解決問(wèn)題有一定的幫助,需要的朋友可以參考下2024-04-04Vue ElementUI this.$confirm async await封
這篇文章主要介紹了Vue ElementUI this.$confirm async await封裝方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09vue element插件this.$confirm用法及說(shuō)明(取消也可以發(fā)請(qǐng)求)
這篇文章主要介紹了vue element插件this.$confirm用法及說(shuō)明(取消也可以發(fā)請(qǐng)求),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10