set cursor position in v6

I wonder what is the command to set cursor position in v6.
Thanks.

editor.dispatch({selection: {anchor: N, head: N}})

1 Like

I did this in two steps

  1. Figure out current cursor position

viewUpdate.view.state.selection.ranges[0].from

  1. Set cursor to the line I want
const newPosition = viewUpdate.view.state.selection.ranges[0].from - 2;

viewUpdate.view.dispatch({
    selection: {
      anchor: newPosition,
      head: newPosition,
    },
  });

not sure if you think this is a decent solution also @marijn

hello, marijn. I pass the following code, the editor does not set the cursor。The highlighting of the focused row is normal, but the cursor display is not normal

view?.dispatch({
  selection: { anchor: pos, head: pos },
  effects: EditorView.scrollIntoView(targetLine.from, {
    y: 'center',
  }),
});

The style of the focused cursor through the click event。The above code lacks automatic new animation-name.

Could it be that the editor is simply not focused? The cursor won’t be visible then.

1 Like

hello,marijin.I recently used codemirror6 to implement sql editor, but after setting the zoom in and zoom out property of css, the cursor position will be incorrect, is there a good solution? Looking forward to your reply

CSS transforms on the editor are not supported. See issue 324.