6.1.1 • Published 2 years ago

vue-codemirror v6.1.1

Weekly downloads
28,190
License
MIT
Repository
github
Last release
2 years ago

vue-codemirror

vue   GitHub stars   npm   Test Codecov   license

CodeMirror(6) component for Vue(3).

vue-codemirror v5/v6 has been released. This is a new version based on CodeMirror@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.

This example 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-html
yarn add @codemirror/lang-json
yarn 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 your package.json

e.g.

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

Local component

<template>
  <codemirror
    v-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 } from 'vue'
  import { Codemirror } from 'vue-codemirror'
  import { javascript } from '@codemirror/lang-javascript'
  import { oneDark } from '@codemirror/theme-one-dark'

  export default defineComponent({
    components: {
      Codemirror
    },
    setup() {
      const code = ref(`console.log('Hello, world!')`)
      const extensions = [javascript(), oneDark]

      // Codemirror EditorView instance ref
      const view = shallowRef()
      const handleReady = (payload) => {
        view.value = payload.view
      }

      // Status is available at all times via Codemirror EditorView
      const getCodemirrorStates = () => {
        const state = view.value.state
        const ranges = state.selection.ranges
        const selected = ranges.reduce((r, range) => r + range.to - range.from, 0)
        const cursor = ranges[0].anchor
        const length = state.doc.length
        const lines = 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'
import VueCodemirror from 'vue-codemirror'

const app = createApp()

app.use(VueCodemirror, {
  // optional default global options
  autofocus: 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{}
phrasesCodemirror internationalization phrases.Object{}
autoDestroyAuto destroy the CodeMirror instance before the component unmount.Booleantrue
extensionsPassed to CodeMirror EditorState.create({ extensions })Extension[]
selectionPassed to CodeMirror EditorState.create({ selection })EditorSelection-
rootPassed to CodeMirror new 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 edirot component mounted.(payload: { view: EditorView; state: EditorState; container: HTMLDivElement })

Basic Setup

The basic-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 empty
  extensions: []
})

Changelog

Detailed changes for each release are documented in the release notes.

License

Licensed under the MIT License.

1yongsh-fexaas-fexyvue-ferama-commonvue-fun-onlinepso-packege@jundell/vue-cmsmhhs-editoruseinsider-design-kitmaterials-editorace-pb-core2page-builder-coreprocess-editorprocess-editor-proprocess-editor-singletoolbox-layoutjpaas-formac-components-dynamicappup-componentsappup-components-dynamicvue_gmtyql-flowchartadonuxt-testecinc-cloud-appsogaws-testspaceone-dschapter-presenter-sdksalesforce-query-editornstl-common-filexvue-fesalesforce-query-editor-app@obpkg/obpkgttp-logic-sharing@wf-core/coresalesforce-query-editor-testshallow-coverageripples-aws-odmsripples-aws-svmsripples-psc-odmsripples-psc-svmsmillions-pcsparrow-bird-clientunicloud-router-iview-adminframework-flowvisible-easilyvue-bpmn-designereasis@eipgrid/s1@infinitebrahmanuniverse/nolb-vue-coqgb-cformjoymo-libilead-draggableilead-draggable2pds-formmakingpui9-components@everything-registry/sub-chunk-3076vuepress-plugin-preview-edit2ggg3@andriepu/flooemergency-disposalbi4paperless-uipaiuipaintbrush-uip5-vue-mirrornti-code-toolsnti-easydt-plusnpm-traiding-vuenoahv-componentportalman-web-componentpackage-yysd-uinuxt-content-try-itqc-onlinecodeqingnang-form-generatorpass-web-hundunyun@capibar/explorerzm-components-libzmz-ui3zny-vue-designer@dingdaoos/lucid-ui-playground@dockite/field-code@dockite/field-jsonbochum-adminbugmaker-zybpmn-modeler-antdvbpmn-vue-middolbpm-design@cloedu/vuetify-jsonschema-form@clearui/vuebri-componentsbri-vtoolsbuyerportalman-web-component@cool-vue/admin@culm/studio-anonymous@culm/studio-rendercbim-uicbim-ui-bakcgwxinfomationcompoentsceres-common-vue@gatship/gs-components-3
6.1.0

2 years ago

6.1.1

2 years ago

5.1.0

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

6.0.2

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

5.0.0-beta.1

2 years ago

4.0.6

5 years ago

4.0.5

6 years ago

4.0.4

6 years ago

4.0.3

6 years ago

4.0.2

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.1.8

6 years ago

3.1.7

7 years ago

3.1.6

7 years ago

3.1.4

7 years ago

3.1.2

7 years ago

3.1.0

7 years ago

3.0.8

7 years ago

3.0.7

7 years ago

3.0.6

7 years ago

3.0.5

7 years ago

3.0.4

7 years ago

3.0.3

7 years ago

3.0.2

7 years ago

3.0.0

7 years ago

2.9.9

7 years ago

2.1.8

7 years ago

2.1.7

7 years ago

2.1.6

7 years ago

2.1.5

7 years ago

2.1.2

7 years ago

2.1.0

8 years ago

2.0.0

8 years ago

1.3.6

8 years ago

1.3.4

8 years ago

1.3.2

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.4

8 years ago

1.1.2

8 years ago

1.1.0

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago