广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >vue codemirror实现在线代码编译器效果
  • 293
分享到

vue codemirror实现在线代码编译器效果

2024-04-02 19:04:59 293人浏览 薄情痞子
摘要

前言 如果我们想在WEB端实现在线代码编译的效果,那么需要使用组件Vue-codemirror,他是将CodeMirror进行了再次封装 支持代码高亮 62种主题颜

前言

如果我们想在WEB端实现在线代码编译的效果,那么需要使用组件Vue-codemirror,他是将CodeMirror进行了再次封装

  • 支持代码高亮
  • 62种主题颜色,例如monokai等等
  • 支持JSON, sql, javascriptCSS,xml, html,yaml, markdown, python编辑模式,默认为 json
  • 支持快速搜索
  • 支持自动补全提示
  • 支持自动匹配括号

环境准备


npm install jshint
npm install jsonlint
npm install script-loader
npm install vue-codemirror

封装组件

我们可以在项目中的components中将vue-codemirror进行再次封装


<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的空格个数
        indentUnit: !this.cmIndentUnit ? 2 : this.cmIndentUnit,  // 一个块(编辑语言中的含义)应缩进多少个空格
        autocorrect: true,  // 自动更正
        spellcheck: true,  // 拼写检查
        lint: true,  // 检查格式
        lineNumbers: true,  //是否显示行数
        lineWrapping: true, //是否自动换行
        styleActiveLine: true,  //line选择是是否高亮
        keyMap: 'sublime',  // sublime编辑器效果
        matchBrackets: true,  //括号匹配
        autoCloseBrackets: true,  // 在键入时将自动关闭括号和引号
        matchTags: { bothTags: true },  // 将突出显示光标周围的标签
        foldGutter: true,  // 可将对象折叠,与下面的gutters一起使用
        gutters: [
          "CodeMirror-lint-markers",
          "CodeMirror-linenumbers",
          "CodeMirror-foldgutter"
        ],
        highlightSelectionMatches: {
          minChars: 2,
          style: "matchhighlight",
          showToken: true
        },
      },
      enableAutoFormatJson: this.autoFormatJson == null ? true : this.autoFormatJson,  // json编辑模式下,输入框失去焦点时是否自动格式化,true 开启, false 关闭
    }
  },
  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出错:" + 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();
    },
    // 失去焦点时处理函数
    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) {
        // 啥也不做
      }
    },
    // 按下键盘事件处理函数
    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 });
      }
    },
    // 按下鼠标时事件处理函数
    onMouseDown(event) {
      this.$refs.myCm.codemirror.closeHint();
    },
    // 黏贴事件处理函数
    OnPaste(event) {
      if (this.cmOptions.mode === "application/json") {
        try {
          this.editorValue = this.formatStrInJson(this.editorValue);
        } catch (e) {
          // 啥都不做
        }
      }
    },
  }
}
</script>

<style scoped>

</style>

此组件默认配置了json编译器,cmOptions中是代码编译器的配置项,需要额外的功能也可以去看官方文档配置
接下来看展示效果

可以看到我们输入了json格式的字符串,即使格式不正确,会给我们错误提示,并且也会给我们自动格式化

python编译器

我们封装的组件默认是json编译器,如果我们想使用其他语言,也很简单,只需要导入其他语言的mode


<template>
  <div>
    <el-button type="primary" icon="el-icon-circle-check-outline" @click="handleConfirm" round>
      点击保存
    </el-button>
    <el-button icon="el-icon-caret-right" type="info" @click="handleRunCode" round>
      在线运行
    </el-button>
  <code-editor
    :cmTheme="cmTheme"
    :cmMode="cmMode"
  >
  </code-editor>
  </div>
</template>

<script>
import codeEditor from '@/components/CodeEditor/index'
import 'codemirror/theme/monokai.css'  // 导入monokai主题
import 'codemirror/mode/python/python.js'  // 导入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>

效果如下

到此这篇关于vue codemirror实现在线代码编译器 的文章就介绍到这了,更多相关vue codemirror在线代码编译器 内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: vue codemirror实现在线代码编译器效果

本文链接: https://www.lsjlt.com/news/161301.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • vue codemirror实现在线代码编译器效果
    前言 如果我们想在Web端实现在线代码编译的效果,那么需要使用组件vue-codemirror,他是将CodeMirror进行了再次封装 支持代码高亮 62种主题颜...
    99+
    2022-11-12
  • vue codemirror如何实现在线代码编译器
    vue codemirror如何实现在线代码编译器,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。前言如果我们想在Web端实现在线代码编译的效果,那么需...
    99+
    2023-06-22
  • 在线代码编辑器CodeMirror的定位是什么
    在线代码编辑器CodeMirror的定位是什么,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。一款“Online Source Editor”,基于Javascr...
    99+
    2023-06-17
  • 如何使用vue codemirror插件实现代码编辑器功能
    本篇内容主要讲解“如何使用vue codemirror插件实现代码编辑器功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用vue codemirror插件实现代码编辑器功能”吧!1、使用...
    99+
    2023-07-04
  • 在vue项目中怎么使用codemirror插件实现代码编辑器功能
    这篇文章主要为大家展示了“在vue项目中怎么使用codemirror插件实现代码编辑器功能”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“在vue项目中怎么使用c...
    99+
    2022-10-19
  • vue中如何使用codemirror插件实现代码编辑器功能
    这篇文章主要介绍“vue中如何使用codemirror插件实现代码编辑器功能”,在日常操作中,相信很多人在vue中如何使用codemirror插件实现代码编辑器功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答...
    99+
    2023-06-20
  • Vue编译器实现代码生成方法介绍
    这里将讨论如何根据JavaScript AST生成渲染函数的代码,即代码生成。代码生成本质上是字符串拼接的艺术。需要访问JavaScript AST中的节点,为每一种类型的节点生成相...
    99+
    2023-01-05
    Vue代码生成 Vue代码生成方案
  • vue实现轮播图效果的代码
    这篇文章主要介绍“vue实现轮播图效果的代码”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“vue实现轮播图效果的代码”文章能帮助大家解决问题。1.原理:v-on:click="prev&q...
    99+
    2023-06-27
  • 怎么用JS实现代码编译器
    本篇内容介绍了“怎么用JS实现代码编译器”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、前言对于前端同学...
    99+
    2022-10-19
  • vue实现左右滑动效果实例代码
    前言 个人实际开发中用到的效果问题总结出来便于自己以后开发查看调用,如果也适用其他人请随意拿走勿喷就行! vue.js是现在流行的js框架之一,vue 是一套用于构建用户界面的渐进式...
    99+
    2022-11-12
  • Android粒子线条效果实现过程与代码
    目录前言需求效果图主要问题粒子逻辑控制新增点粒子生命周期粒子趋向于手指粒子连线前言 很久没写代码了,忙工作、忙朋友、人也懒了,最近重新调整自己,对技术还是要有热情,要热情的话还是用自...
    99+
    2023-02-09
    Android粒子线条 Android粒子线条效果 Android粒子效果
  • Android实现代码画虚线边框背景效果
    实现如下边框效果: 虚线画效果,可以使用Android中的xml来做。下面话不多说,直接上代码: <RelativeLayout android:id="...
    99+
    2022-06-06
    边框背景 Android
  • codemirror6实现在线代码编辑器使用详解
    目录背景介绍codemirror5、codemirror6对比codemirror6 核心包vue3+codemirror6实现简易在线代码编辑器安装依赖创建编辑器效果截图主题获取、...
    99+
    2023-01-08
    codemirror 代码编辑器 codemirror 在线编辑器
  • 怎么在vue中利用monaco实现一个代码高亮效果
    怎么在vue中利用monaco实现一个代码高亮效果?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。为什么要使用VueVue是一款友好的、多用途且高性能的JavaS...
    99+
    2023-06-06
  • Vue实现轮播图效果的代码怎么写
    今天小编给大家分享一下Vue实现轮播图效果的代码怎么写的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。Vue 过渡Vue 的过...
    99+
    2023-07-04
  • vue实现无缝轮播效果的示例代码
    小编给大家分享一下vue实现无缝轮播效果的示例代码,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!具体内容如下首先创建两个vue组件Sweiper.vue和Swei...
    99+
    2023-06-15
  • 在Python反编译中批量pyc转 py的实现代码
    什么是pyc文件 pyc是一种二进制文件,是由py文件经过编译后,生成的文件,是一种byte code,py文件变成pyc文件后,加载的速度有所提高,而且pyc是一种跨平台的字节码,...
    99+
    2022-11-13
  • vue实现下拉框二级联动效果的实例代码
    1、实现效果 2、后端返回的数据格式 "list": [ { "id": "1178214681118568449", "title...
    99+
    2022-11-12
  • Vue+Echart实现利用率表盘效果的示例代码
    目录效果演示组件使用方式效果演示 组件 里面对应两个图片资源,panelBackground_red.png 和 panelBackground_green.png,请前往百度网...
    99+
    2023-05-18
    Vue Echart实现利用率表盘效果 Vue Echart利用率表盘效果 Vue Echart表盘 Vue Echart
  • vue实现在线预览office文件的示例代码
    最近在做电子档案,后端提供了文件的华为云的oss链接。已经实现了点击下载文件的功能。但是呢,他们又希望常规的文件,可以直接点击预览,不需要下载。 按道理说,做文件的在线预览,买个第三...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作