# codemirror-editor-vue3

> CodeMirror component for Vue3

Latest version **2.8.0** (published 2024-09-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install codemirror-editor-vue3
pnpm add codemirror-editor-vue3
yarn add codemirror-editor-vue3
bun add codemirror-editor-vue3
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.8.0 |
| Published | 2024-09-20 |
| First published | 2021-08-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 93.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 250 |
| Author | RennCheung |
| Maintainers | zhangren |
| Keywords | codemirror-editor-vue3, vue3 codemirror, codemirror, vue3, log mode, log |

## Links

- npm: https://www.npmjs.com/package/codemirror-editor-vue3
- Repository: https://github.com/rennzhang/codemirror-editor-vue3
- Homepage: https://rennzhang.github.io/codemirror-editor-vue3/
- Issues: https://github.com/rennzhang/codemirror-editor-vue3/issues
- npm.io page: https://npm.io/package/codemirror-editor-vue3

## Dependencies (2)

- [codemirror](https://npm.io/package/codemirror.md) ^5
- [diff-match-patch](https://npm.io/package/diff-match-patch.md) ^1.0.5

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 2.8.0 (latest) — 2024-09-20
- 2.8.0-beta.0 (beta) — 2024-09-20
- 2.7.1 — 2024-09-06
- 2.7.0 — 2024-06-13
- 2.6.1 — 2024-06-13
- 2.6.0 — 2024-06-11
- 2.6.0-beta.1 — 2024-06-11
- 2.6.0-beta.0 — 2024-06-11
- 2.5.8 — 2024-04-07
- 2.5.0-beta.7 — 2024-03-29
- 2.5.0-beta.6 — 2024-03-28
- 2.5.0-beta.5 — 2024-03-28
- 2.5.0-beta.4 — 2024-03-28
- 2.5.0-beta.3 — 2024-03-28
- 2.5.0-beta.2 — 2024-03-28
- … 100 more at https://npm.io/package/codemirror-editor-vue3/versions

## README

[![GitHub stars](https://img.shields.io/github/stars/RennZhang/codemirror-editor-vue3)](https://github.com/RennZhang/codemirror-editor-vue3/stargazers)
[![npm downloads](https://img.shields.io/npm/dt/codemirror-editor-vue3)](https://www.npmjs.com/package/codemirror-editor-vue3)
[![GitHub issues](https://img.shields.io/github/issues/RennZhang/codemirror-editor-vue3)](https://github.com/RennZhang/codemirror-editor-vue3/issues)
[![GitHub forks](https://img.shields.io/github/forks/RennZhang/codemirror-editor-vue3)](https://github.com/RennZhang/codemirror-editor-vue3/network)
[![GitHub last commit](https://img.shields.io/github/last-commit/RennZhang/codemirror-editor-vue3)](https://github.com/RennZhang/codemirror-editor-vue3)
[![license](https://img.shields.io/github/license/RennZhang/codemirror-editor-vue3)](https://github.com/RennZhang/codemirror-editor-vue3)

# Introduction

[简体中文](https://rennzhang.github.io/codemirror-editor-vue3/zh-CN/guide/getting-started)

The codemirror component of vue3. This component is developed based on [Codemirror 5](http://codemirror.net/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](https://RennZhang.github.io/codemirror-editor-vue3/).

## Install

```bash
npm install codemirror-editor-vue3 codemirror@^5 -S
```

```bash
yarn add codemirror-editor-vue3 codemirror@">=5.64.0 <6"
```

```bash
pnpm i codemirror-editor-vue3 codemirror@^5 -S
```

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

```bash
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:`

```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:

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

## Use in components

```vue
<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

- [javascript](https://rennzhang.github.io/codemirror-editor-vue3/example?lang=javascript)
- [json](https://rennzhang.github.io/codemirror-editor-vue3/example?lang=json)
- [css](https://rennzhang.github.io/codemirror-editor-vue3/example?lang=css)
- [html](https://rennzhang.github.io/codemirror-editor-vue3/example?lang=html)
- [apl](https://rennzhang.github.io/codemirror-editor-vue3/example?lang=apl)
- [yaml](https://rennzhang.github.io/codemirror-editor-vue3/example?lang=yaml)

More cases are gradually being added, and you can also refer to
[document](https://rennzhang.github.io/codemirror-editor-vue3/guide/lang) to achieve more language modes.

## Component Props

[cm_config_url]: https://codemirror.net/doc/manual.html#config
[cm_editor_type_url]: https://codemirror.net/doc/manual.html#config
[default_options_url]: https://github.com/RennZhang/codemirror-editor-vue3/blob/main/packages/src/config/index.ts#L68

| name                |                                                                description                                                                | type                                      |                default                 |
| ------------------- | :---------------------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------- | :------------------------------------: |
| **value(v-model)**  |                                                              Editor content                                                               | `string`                                  |                   ""                   |
| **options**         |                                           [Configuration options of codemirror5][cm_config_url]                                           | [EditorConfiguration][cm_editor_type_url] | [DEFAULT_OPTIONS][default_options_url] |
| **placeholder**     |                                     Editor placeholder content to introduce codemirror related files                                      | `string`                                  |                   ""                   |
| **border**          |                                                     Whether to display editor borders                                                     | `boolean`                                 |                `false`                 |
| **width**           |                                                                   width                                                                   | `string`                                  |                `100%  `                |
| **height**          |                                                                  height                                                                   | `string`                                  |                `100%  `                |
| **original-style**  | Using the original style, disable the second modification of the style for this component (but does not affect width, height, and border) | ` boolean`                                |                `false`                 |
| **KeepCursorInEnd** |                                              Always keep the mouse position on the last line                                              | `boolean`                                 |                `false`                 |
| **merge**           |                                               merge mode, can also be used as diff pattern                                                | `boolean`                                 |                `false`                 |

## Events

### Component Events

> The following three are only the events encapsulated by this component. Please refer to more events
> [Codemirror Events](./events#codemirror-events)

| event name |             description             | params                                |
| ---------- | :---------------------------------: | :------------------------------------ |
| `change`   |      value or instance changes      | `(value: string, cm: Editor) => void` |
| `input`    |                input                | `(value: string) => void`             |
| `ready`    | The 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](https://codemirror.net/doc/manual.html#events)，You can use this component to bind events directly
through components, for example：

```vue {8-10}
<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`

---
_Source: https://npm.io/package/codemirror-editor-vue3 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
