codemirror-editor-vue3

rennzhangMIT长期未更新

把 CodeMirror 6 封装成 Vue3 组件,让前端项目能快速接入带语法高亮和代码编辑能力的编辑器,省去手动绑定生命周期的麻烦。

CodeMirror component for Vue3

v2.8.0发布于 2024-09-20 03:21 UTC250star57fork同步于 2026-11-07 07:32 UTC

怎么上手

由 README 提炼,执行前请确认
npm install codemirror-editor-vue3 codemirror@^5 -S
硬件要求按 README 说明

README 未说明

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

基于 CodeMirror 5 的 Vue3 代码编辑器组件,开箱即用支持多语言高亮

把 CodeMirror 6 封装成 Vue3 组件,让前端项目能快速接入带语法高亮和代码编辑能力的编辑器,省去手动绑定生命周期的麻烦。

核心能力

  • 封装 CodeMirror 5 为 Vue3 组件,支持 v-model 双向绑定
  • 支持官方语言模式,另加日志输出展示模式
  • 提供 border、width、height、placeholder 等布局属性
  • 支持 merge 合并模式,也可用作 diff 对比
  • 透传 CodeMirror 原生事件如 change、focus、scroll

容易踩的坑

必须同时安装 codemirror@^5,不能装 6.x 版本;语言模式、主题、placeholder 等需自行 import 对应 codemirror 文件;TypeScript 项目需额外安装 @types/codemirror;官方不建议全局注册组件,否则模板类型提示会失效

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v2.8.0
  1. v2.8.0最新正式版2024-09-20 · 2024-09-20 03:21 UTC

    Please refer to CHANGELOG.md for details.

  2. v2.8.0-beta.0pre-release2024-09-20 · 2024-09-20 03:00 UTC
    查看更新说明

    Please refer to CHANGELOG.md for details.

  3. v2.7.12024-09-05 · 2024-09-05 07:55 UTC
    查看更新说明

    Please refer to CHANGELOG.md for details.

  4. v2.7.02024-06-13 · 2024-06-13 09:18 UTC
    查看更新说明

    Please refer to CHANGELOG.md for details.

  5. v2.6.12024-06-13 · 2024-06-13 08:04 UTC
    查看更新说明

    Please refer to CHANGELOG.md for details.

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    GitHub stars npm downloads GitHub issues GitHub forks GitHub last commit license

    Introduction

    简体中文

    The codemirror component of vue3. This component is developed based on Codemirror 5 and only vue3 is supported.

    In addition to the officially supported modes, the log output presentation mode is added, out of the box, but not necessarily suitable for all scenarios.

    For complete documentation and more cases, please check codemirror-editor-vue3 docs.

    Install

    npm install codemirror-editor-vue3 codemirror@^5 -S
    
    yarn add codemirror-editor-vue3 codemirror@">=5.64.0 <6"
    
    pnpm i codemirror-editor-vue3 codemirror@^5 -S
    

    If your project requires Typescript support, you will also need to install the '@types/codemirror' dependency.

    npm install @types/codemirror -D
    

    Register global component

    Do not recommend global registration components, which will result in the type of prompt on the template that cannot be properly obtained.

    main.js:

    import { createApp } from "vue";
    import App from "./App.vue";
    import { InstallCodeMirror } from "codemirror-editor-vue3";
    
    const app = createApp(App);
    app.use(InstallCodeMirror);
    app.mount("#app");
    

    The global registered component name is Codemirror or you can customize a component name, for example:

    app.use(InstallCodeMirror, { componentName: "customName" });
    

    Use in components

    <template>
      <Codemirror
        v-model:value="code"
        :options="cmOptions"
        border
        placeholder="test placeholder"
        :height="200"
        @change="change"
      />
    </template>
    
    <script>
    import Codemirror from "codemirror-editor-vue3";
    
    // placeholder
    import "codemirror/addon/display/placeholder.js";
    
    // language
    import "codemirror/mode/javascript/javascript.js";
    // placeholder
    import "codemirror/addon/display/placeholder.js";
    // theme
    import "codemirror/theme/dracula.css";
    
    import { ref } from "vue";
    export default {
      components: { Codemirror },
      setup() {
        const code = ref(`
    var i = 0;
    for (; i < 9; i++) {
      console.log(i);
      // more statements
    }`);
    
        return {
          code,
          cmOptions: {
            mode: "text/javascript", // Language mode
            theme: "dracula", // Theme
          },
        };
      },
    };
    </script>
    

    Language highlighting

    You can click on the following link to view corresponding language cases

    More cases are gradually being added, and you can also refer to document to achieve more language modes.

    Component Props

    namedescriptiontypedefault
    value(v-model)Editor contentstring""
    optionsConfiguration options of codemirror5EditorConfigurationDEFAULT_OPTIONS
    placeholderEditor placeholder content to introduce codemirror related filesstring""
    borderWhether to display editor bordersbooleanfalse
    widthwidthstring100%
    heightheightstring100%
    original-styleUsing the original style, disable the second modification of the style for this component (but does not affect width, height, and border) booleanfalse
    KeepCursorInEndAlways keep the mouse position on the last linebooleanfalse
    mergemerge mode, can also be used as diff patternbooleanfalse

    Events

    Component Events

    The following three are only the events encapsulated by this component. Please refer to more events Codemirror Events

    event namedescriptionparams
    changevalue or instance changes(value: string, cm: Editor) => void
    inputinput(value: string) => void
    readyThe Codemirror component is mounted(cm: Editor) => void;

    Codemirror Events

    The following events are official events of Codemirror5. You can refer to the official documents for details Codemirror Event,You can use this component to bind events directly through components, for example:

    <Codemirror
      v-model:value="code"
      :options="{ mode: 'text/x-vue', theme: 'default' }"
      border
      placeholder="test-placeholder"
      :height="200"
      @change="onChange"
      @blur="onBlur"
      @focus="onFocus"
      @scroll="onScroll"
    />
    

    All event names are as follows:

    • changes
    • scroll
    • beforeChange
    • cursorActivity
    • keyHandled
    • inputRead
    • electricInput
    • beforeSelectionChange
    • viewportChange
    • swapDoc
    • gutterClick
    • gutterContextMenu
    • focus
    • blur
    • refresh
    • optionChange
    • scrollCursorIntoView
    • update