当前位置:首页 » 《我的小黑屋》 » 正文

vue3中使用codemirror6增加代码提示功能

11 人参与  2024年03月29日 13:00  分类 : 《我的小黑屋》  评论

点击全文阅读


1、安装依赖

// 安装codemirror、语言包、主题、自动补全

npm i codemirrornpm i @codemirror/lang-javascriptnpm i @codemirror/autocompletenpm 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、创建编辑器

<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;// 获取自定义提示内容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() || "")     },  });}// 获取当前编辑器中的内容字符串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为自定义内容,以@开头进行匹配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 = "";}// 外部输入内容,添加到编辑器当前光标(或选中内容)所在的位置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>


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/87624.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 祖母寿宴,侯府冒牌嫡女被打脸了(沈屿安秦秀婉)阅读 -
  • 《雕花锦年,昭都旧梦》(裴辞鹤昭都)完结版小说全文免费阅读_最新热门小说《雕花锦年,昭都旧梦》(裴辞鹤昭都) -
  • 郊区41号(许洛竹王云云)完整版免费阅读_最新全本小说郊区41号(许洛竹王云云) -
  • 负我情深几许(白诗茵陆司宴)完结版小说阅读_最热门小说排行榜负我情深几许白诗茵陆司宴 -
  • 九胞胎孕妇赖上我萱萱蓉蓉免费阅读全文_免费小说在线看九胞胎孕妇赖上我萱萱蓉蓉 -
  • 为保白月光,侯爷拿我抵了债(谢景安花田)小说完结版_完结版小说全文免费阅读为保白月光,侯爷拿我抵了债谢景安花田 -
  • 陆望程映川上官硕《我的阿爹是带攻略系统的替身》最新章节阅读_(我的阿爹是带攻略系统的替身)全章节免费在线阅读陆望程映川上官硕
  • 郑雅琴魏旭明免费阅读_郑雅琴魏旭明小说全文阅读笔趣阁
  • 头条热门小说《乔书意贺宴临(乔书意贺宴临)》乔书意贺宴临(全集完整小说大结局)全文阅读笔趣阁
  • 完结好看小说跨年夜,老婆初恋送儿子故意出车祸_沈月柔林瀚枫完结的小说免费阅读推荐
  • 热推《郑雅琴魏旭明》郑雅琴魏旭明~小说全文阅读~完本【已完结】笔趣阁
  • 《你的遗憾与我无关》宋怀川冯洛洛无弹窗小说免费阅读_免费小说大全《你的遗憾与我无关》宋怀川冯洛洛 -

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1