Vue2 集成 CodeMirror 实现公式编辑、块状文本编辑,TAG标签功能

这篇具有很好参考价值的文章主要介绍了Vue2 集成 CodeMirror 实现公式编辑、块状文本编辑,TAG标签功能。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

效果图

Vue2 集成 CodeMirror 实现公式编辑、块状文本编辑,TAG标签功能,# Vue,前端,vue

Vue2 集成 CodeMirror 实现公式编辑、块状文本编辑,TAG标签功能,# Vue,前端,vue

 安装codemirror依赖

本示例为Vue2项目,安装低版本的依赖

npm i codemirror@5.65.12
npm i vue-codemirror@4.0.6

实现

实现代码如下,里边涉及到的变量和函数自行替换即可,没有其他复杂逻辑。文章来源地址https://www.toymoban.com/news/detail-688678.html

<template>
  <div class="picker">
    <div class="code-edit">
      <div class="top-title">公式</div>
      <codemirror
              ref="codeEditor"
              v-model="formulaStr"
              :options="cmOptions"
              @input="codeMirrorChange"
      ></codemirror>

    </div>
    <el-button
            size="mini"
            icon="el-icon-setting"
            @click="insertContent('表单4', 'variable')"
    >添加变量
    </el-button
    >
    <el-button
            size="mini"
            icon="el-icon-setting"
            @click="insertContent('SUM', 'func')"
    >添加函数
    </el-button
    >
  </div>

</template>

<script>
import {codemirror} from "vue-codemirror";
import "codemirror/lib/codemirror.css";
import "codemirror/theme/idea.css";

export default {
  components: {codemirror},
  data() {
    return {
      cmOptions: {
        // 语言及语法模式
        mode: 'text/javascript',
        // 主题
        theme: "idea",
        // 显示函数
        line: true,
        lineNumbers: false,
        // 软换行
        lineWrapping: true,
        // tab宽度
        tabSize: 4,
      },
      lang: 'javascript',
      formulaStr: "表单 表单1 * 表单11*表单12+SUM(1,2) AVG(99,21) IF()",
    };
  },
  computed: {
    editor() {
      return this.$refs.codeEditor.codemirror;
    }
  },
  mounted() {
    this.focus(this.formulaStr)
    this.autoMarkText()
  },
  methods: {
    codeMirrorChange() {
      //获取 editor 的内容
      console.log("content1: " + this.formulaStr);
      console.log("content2: " + JSON.stringify(this.editor.getValue()));
    },
    addFormula(content, type) {
      this.insertContent(content, type)
    },
    /**
     * editor 中的对内容进行处理
     * @param value
     * @param type variable | func,variable为表单变量,需标记,func 为函数,也需要做标记
     */
    insertContent(value, type) {
      const from = this.editor.getCursor();
      if (type === 'variable') {
        this.editor.replaceSelection(value);
        const to = this.editor.getCursor();
        this.markText(from, to, value, 'cm-field');
      } else if (type === 'func') {
        this.editor.replaceSelection(`${value}()`);
        const to = this.editor.getCursor();
        this.markText(from, {line: to.line, ch: to.ch - 2}, value, 'cm-func');
        this.editor.setCursor({line: to.line, ch: to.ch - 1});
      } else if (typeof value === 'string') {
        this.editor.replaceSelection(value);
      }
      this.editor.focus();
    },

    autoMarkText() {
      if (this.formulaStr) {
        this.autoMark(this.formulaStr);
        this.focus(this.formulaStr);
      }
    },
    focus(value) {
      this.editor.setCursor({
        line: 0,
        ch: value ? value.length : 0
      });
      this.editor.focus()
    },
    markText(from, to, label, className) {
      if (className === void 0) {
        className = "cm-func";
      }
      let text = document.createElement("span");
      text.className = className;
      text.innerText = label;
      this.editor.markText(from, to, {
        atomic: true,
        replacedWith: text,
      });
    },
    /**
     * 解析 editor 的内容,分别对表单变量和函数进行标记
     */
    autoMark() {
      const editor = this.editor;
      const lines = editor.lineCount();
      for (let line = 0; line < lines; line++) {
        const content = editor.getLine(line);
        // 标记函数调用,匹配一个或多个连续的大写字母,后面可以有任意数量的空白字符,再紧跟一个左括号
        content.replace(/([A-Z]+)\s*\(/g, (_, func, pos) => {
          this.markText({line: line, ch: pos}, {line: line, ch: pos + func.length}, func, 'cm-func');
          return _;
        });
        // 标记表单变量,这应该是动态获取,自行替换即可
        let vars = ["表单", "表单1", "表单11", "表单12"];
        vars.forEach(v => {
          let from = 0;
          let idx = -1;
          while (~(idx = content.indexOf(v, from))) {
            this.markText({line: line, ch: idx}, {line: line, ch: idx + v.length}, v, 'cm-field');
            from = idx + v.length;
          }
        });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.picker {
  height: 525px;
  text-align: left;
  width: 50%;
  margin: 0 auto;
  .code-edit {
    height: 240px;
    border-radius: 6px;
    border: 1px solid #e8e9eb;
  }
}
.top-title {
  background-color: #fafafa;
  height: 30px;
  vertical-align: center;
  line-height: 30px;
  padding-left: 10px;
  border-radius: 4px 4px 0 0;
  border-bottom: none;
}
/deep/ .CodeMirror {
  height: 200px !important;
  /*表单变量样式*/
  .cm-field {
    background: #007bff;
    padding: 3px 5px;
    border-radius: 3px;
    color: #fff;
    margin: 0 1px;
  }
  /*函数样式*/
  .cm-func {
    font-weight: bold;
    color: #ae4597;
    line-height: 14px;
    margin: 0 1px;
    padding: 0 2px;
  }
  .CodeMirror-scroll {
    width: 100%;
  }
}

</style>

到了这里,关于Vue2 集成 CodeMirror 实现公式编辑、块状文本编辑,TAG标签功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • vue-codemirror实现sql和json线上编辑器

    实习小计01 今天老大让写一个线上编辑器,写的是sql和json两个编译器,sql的编译器要有提示, 老大扔过来两个选择,1:vue-codemirror;2:monaco-editor,一个前端小白,只会vue,所以果断选那个我有点沾边的(也就是vue-codemirror),到网上查了一下文档,照着文档大致做出来的,

    2023年04月08日
    浏览(26)
  • VUE2.0集成 Markdown 编辑器

    Markdown编辑器的使用 这是一款基于Vue的markdown编辑器。既可以用来编辑Markdown语法,又可以用来解析 效果图,mavonEditor实现了Markdown集成 Markdown是一种标记语言,相较于word文档更加清晰方便,适合进行笔记等。将Markdown集成进入自己项目之后,就可以在项目中使用的Markdown编辑器

    2024年02月10日
    浏览(71)
  • vue-codemirror实现一个前端代码在线编辑器,可处理 HTML,VUE,JS,CSS代码在线编辑

    先找个目录创建一个vue项目 例如 我们想要项目叫 editor 在终端执行 2和3其实都可以 但个人建议 最近还是2会更稳定一些 在终端执行 引入依赖包 然后在项目src目录下创建 utils 文件夹 里面创建一个setting.js 参考代码如下 然后 这边 调用组件的代码 因为项目也刚创 我直接写 s

    2024年02月15日
    浏览(39)
  • vue2+wangEditor5富文本编辑器(图片视频上传)并加锚链接

    官网:https://www.wangeditor.com/v5/installation.html#npm 1、安装使用 安装 在main.js中引入样式 在使用编辑器的页面引入js 模板 js 到这一步编辑完就可以正常显示了 2、上传图片、视频 1)上传到后台接口的可直接按照文档这个配置就行接口返回格式也要可文档上一致 2)自定义上传(一

    2024年02月12日
    浏览(31)
  • vue2使用 tinymce富文本编辑器-图片上传、粘贴图片上传致服务器

    1.安装tinymce富文本编辑器插件 npm i tinymce npm i @tinymce/tinymce-vue 2.创建Editor.js文件 封装组件 以便使用 3.汉化包 (我放在public/tynymce/langs文件夹里) 4.vue组件中使用 本次记录重点在于 上传图片方法 需要区分工具栏中图片上传方法 以及 粘贴进去的图片也需要走上传方法。两个方

    2024年02月09日
    浏览(40)
  • vue2+wangEditor5富文本编辑器(图片视频自定义上传七牛云/服务器)

    1、安装使用 安装 在main.js中引入样式 在使用编辑器的页面引入js 模板 js  到这一步编辑器就可以正常显示了 2、上传图片、视频 上传到后台接口的可直接按照文档这个配置就行接口返回格式也要可文档上一致    2)自定义上传(一般上传到别的服务器上,我这边是上传到七

    2024年02月11日
    浏览(44)
  • 代码编辑器实践之vue-codemirror使用

    程序员用到 IDE 次数比较频繁,比如 vscode 、 idea 等,这些都是市场上比较流行的代码编辑器,拥有非常全面的功能。但是有时候在项目开发上也会用到代码编辑器,比如复杂的 ArrayObject 输入,或者需要用到用户交互的代码逻辑,或者需要用到json、yaml格式文件时的校验等等。

    2024年02月13日
    浏览(43)
  • vue3代码编辑器组件codemirror-editor-vue3

    官方文档:https://github.com/RennCheung/codemirror-editor-vue3 国内镜像:https://renncheung.github.io/codemirror-editor-vue3/zh-CN/guide/getting-started 参考文档:https://codemirror.net/5/mode/index.html 点击参考文档,选择语言并点击在文章最后找到mode的格式 在配置项中修改mode,并引入对应语言js 如使用pyt

    2024年02月14日
    浏览(26)
  • Vue3项目中在线编辑组件,codemirror-editor-vue3

    #安装 #组件中使用 #如果想要更换不同的主题色, 只需要在组件中引入你要使用的主题色的css文件,然后配置theme的名字为引入的css文件名称。 https://codemirror.net/5/theme/   #效果  

    2024年02月05日
    浏览(24)
  • vue代码编辑器vue-codemirror的简单使用更改样式和切换主题等

    可以查看官网演示:vue-codemirror | Homepage 支持的语言mode:CodeMirror: Language Modes  支持的主题样式:CodeMirror: Theme Demo  开始安装和使用:  基础使用 注册全局组件 注册局部组件 使用组件 实现的效果: 编辑器默认高度是300px,如果想更改高度和字体大小:添加样式  更改主题的

    2024年02月13日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包