Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

@codemirror code editor component for@vuejs

License

NotificationsYou must be signed in to change notification settings

surmon-china/vue-codemirror

Repository files navigation

vue npm GitHub stars Test Codecov license

CodeMirror(6) component forVue(3).

vue-codemirror v5/v6 has been released. This is a new version based onCodeMirror@6 and is available to Vue3 only. Since CodeMirror@6 is developed with native ES Modules, the new version will no longer support direct browser references to UMD modules. In short, the new version is⚠️completely NOT compatible with previous versions. If you wish to continue using Vue2 or a lower version of CodeMirror, please refer to the legacy versions below.

Thisexample site contains most of what you want.

Legacy version


Documentation

CodeMirror packages


Usage

Install

yarn add codemirror vue-codemirror
npm install codemirror vue-codemirror --save

Depending on your actual needs, you may need to install more CodeMirror packages

# CodeMirror languages...yarn add @codemirror/lang-htmlyarn add @codemirror/lang-jsonyarn add @codemirror/lang-javascript# CodeMirror themes...yarn add @codemirror/theme-one-dark# more CodeMirror packages...

If you need import API/interface from codemirror, you need to make codemirror explicitly dependent in yourpackage.json

e.g.

"dependencies": {"@codemirror/state":"6.x"}
import{EditorState}from'@codemirror/state'

Local component

<template>  <codemirrorv-model="code"placeholder="Code goes here...":style="{ height: '400px' }":autofocus="true":indent-with-tab="true":tab-size="2":extensions="extensions"@ready="handleReady"@change="log('change', $event)"@focus="log('focus', $event)"@blur="log('blur', $event)"  /></template><script>import {defineComponent,ref,shallowRef }from'vue'import {Codemirror }from'vue-codemirror'import {javascript }from'@codemirror/lang-javascript'import {oneDark }from'@codemirror/theme-one-dark'exportdefaultdefineComponent({    components: {      Codemirror    },setup() {constcode=ref(`console.log('Hello, world!')`)constextensions= [javascript(), oneDark]// Codemirror EditorView instance refconstview=shallowRef()consthandleReady= (payload)=> {view.value=payload.view      }// Status is available at all times via Codemirror EditorViewconstgetCodemirrorStates= ()=> {conststate=view.value.stateconstranges=state.selection.rangesconstselected=ranges.reduce((r,range)=> r+range.to-range.from,0)constcursor= ranges[0].anchorconstlength=state.doc.lengthconstlines=state.doc.lines// more state info ...// return ...      }return {        code,        extensions,        handleReady,        log:console.log      }    }  })</script>

Global component

import{createApp}from'vue'import{basicSetup}from'codemirror'importVueCodemirrorfrom'vue-codemirror'constapp=createApp()app.use(VueCodemirror,{// optional default global optionsautofocus:true,disabled:false,indentWithTab:true,tabSize:2,placeholder:'Code goes here...',extensions:[basicSetup]// ...})

Component Props

propdescriptiontypedefault
modelValueThe input values accepted by the component also support two-way binding.String''
autofocusFocus editor immediately after mounted.Booleanfalse
disabledDisable input behavior and disable change state.Booleanfalse
indentWithTabBind keyboard Tab key event.Booleantrue
tabSizeSpecify the indent when the Tab key is pressed.Number2
placeholderDisplay when empty.String''
styleThe CSS style object that acts on the CodeMirror itself.Object{}
phrasesCodemirrorinternationalization phrases.Object{}
autoDestroyAuto destroy the CodeMirror instance before the component unmount.Booleantrue
extensionsPassed to CodeMirrorEditorState.create({ extensions })Extension[]
selectionPassed to CodeMirrorEditorState.create({ selection })EditorSelection-
rootPassed to CodeMirrornew EditorView({ root })ShadowRoot | Document-

Component Events

eventdescriptionparams
update:modelValueOnly when the CodeMirror content (doc) has changed.(value: string, viewUpdate: ViewUpdate)
changedittoditto
updateWhen any state of CodeMirror changes.(viewUpdate: ViewUpdate)
focusWhen CodeMirror focused.(viewUpdate: ViewUpdate)
blurWhen CodeMirror blurred.(viewUpdate: ViewUpdate)
readyWhen editor component mounted.(payload: { view: EditorView; state: EditorState; container: HTMLDivElement })

Basic Setup

Thebasic-setup extension is integrated by default in the vue-codemirror configuration and is intended as a handy helper to quickly set up CodeMirror without having to install and import a lot of standalone packages. If you want to override the default behavior of the config, just pass the empty array when installing the component globally.

app.use(VueCodemirror,{// keep the global default extensions emptyextensions:[]})

Changelog

Detailed changes for each release are documented in therelease notes.

License

Licensed under theMIT License.

Contributors15


[8]ページ先頭

©2009-2025 Movatter.jp