vue3中如何使用codemirror6增加代碼提示功能
1、安裝依賴
// 安裝codemirror、語言包、主題、自動(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="請選擇分組"
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",
},
//括號匹配
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)容,以@開頭進(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("正在保存,請稍候...");
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項(xiàng)目中l(wèi)oadsh庫常用方法說明
這篇文章主要介紹了vue項(xiàng)目中l(wèi)oadsh庫常用方法說明,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
vue判斷內(nèi)容是否滑動(dòng)到底部的三種方式
這篇文章主要介紹了vue判斷內(nèi)容是否滑動(dòng)到底部的三種方式,文中通過代碼示例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-04-04
Vue3中defineProps設(shè)置默認(rèn)值的方法實(shí)現(xiàn)
Vue3中我們經(jīng)常需要使用defineProps來定義組件的屬性,本文主要介紹了Vue3中defineProps設(shè)置默認(rèn)值的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
vue?element-plus圖片預(yù)覽實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于vue?element-plus圖片預(yù)覽實(shí)現(xiàn)的相關(guān)資料,最近的項(xiàng)目中有圖片預(yù)覽的場景,也是踩了一些坑,在這里總結(jié)一下,需要的朋友可以參考下2023-07-07
vue實(shí)現(xiàn)鼠標(biāo)滑動(dòng)預(yù)覽視頻封面組件示例詳解
這篇文章主要為大家介紹了vue實(shí)現(xiàn)鼠標(biāo)滑動(dòng)預(yù)覽視頻封面組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
vue+Ant?Design進(jìn)度條滑塊與input聯(lián)動(dòng)效果實(shí)現(xiàn)
最近接到這樣一個(gè)需求滑塊進(jìn)度與輸入框?yàn)橐恢?默認(rèn)值為80,最小不能小于30,最大為100,怎么實(shí)現(xiàn)這個(gè)聯(lián)動(dòng)效果呢,下面小編給大家分享下基于vue+Ant?Design進(jìn)度條滑塊與input聯(lián)動(dòng)效果的實(shí)現(xiàn),感興趣的朋友跟隨小編一起看看吧2022-12-12
vue項(xiàng)目中封裝echarts的優(yōu)雅方式分享
在實(shí)際項(xiàng)目開發(fā)中,我們會(huì)經(jīng)常與圖表打交道,比如?訂單數(shù)量表、商品銷量表、會(huì)員數(shù)量表等等,它可能是以折線圖、柱狀圖、餅狀圖等等的方式來展現(xiàn),下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目中封裝echarts的優(yōu)雅方式的相關(guān)資料,需要的朋友可以參考下2022-03-03

