vue-codemirror icon indicating copy to clipboard operation
vue-codemirror copied to clipboard

How to force .blur() and .focus()

Open caiokawasaki opened this issue 6 years ago • 6 comments

After a few functions I need to force my JavaScript to .blur() and .focus() the code editor. How to achieve this?

caiokawasaki avatar Feb 27 '19 19:02 caiokawasaki

Have you had a look at the https://codemirror.net/doc/manual.html#addon_autorefresh plugin?

lino avatar Mar 18 '19 21:03 lino

my cm appears using v-if, I got it to focus using:

cmOptions: {
    tabSize: 4,
    mode: 'text/x-mysql',
    lineNumbers: false,
    line: false,
    placeholder: 'Code',
    autofocus: true,
  },

mike-seekwell avatar Oct 22 '19 15:10 mike-seekwell

In a case when you need to focus with JavaScript use codemirror property to access the actual editor and call focus. Let's say you have the following:

<codemirror ref="cm" v-model="code" :options="cmOptions" @changes="onChange" />

Now you can get the element via $refs.cm, then access the editor (.codemirror) and call focus:

this.$refs.cm.codemirror.focus()

lana-k avatar May 19 '21 21:05 lana-k

I need .focus and .blur too. the component CodeMirror(6) doesn't have these methods, but the codemirror api itself does.

so this line: this.$refs.cm.codemirror.focus() doesn't work.

cokuna-pavelkosolapov avatar Aug 22 '22 09:08 cokuna-pavelkosolapov

+1 those methods are not available to force focus.

jblyberg avatar Dec 29 '22 17:12 jblyberg

For who use codemirror v6 and wants to call methods from state or view instance.You can get instance from ready callback.

<codemirror v-model="code" @ready="handleReady" />
const editorView = shallowRef()
const editorState = shallowRef()
const handleReady = ({ view, state }) => {
  editorView.value = view
  editorState.value = state
}
// Now you can call focus methods.
editorView.value.focus()

gui117 avatar Apr 04 '23 03:04 gui117