reactjsace-editorreact-ace

React-ace - Get cursor position


I was checking ace editor docs & found that, editor.getCursorPosition() retrieves the current cursor position.

But how do I do this in react-ace. I'm not sure, how to get the editor instance & call getCursorPosition() function over it.

I checked the react-ace documentation as well but I couldn't find a way to do this.

Could anyone help on this?


Solution

  • Answering my own question here. My requirement was to know which line is being edited.

    I got access to the selection object with onSelectionChange props. Then I stored selection object in a state variable & called selection.getCursor() method in onChange function.

    sample code

          <TextEditor
            id={`editor_${index}`}
            isRequired={true}
            inputObject={inputObj}
            onSelectionChange={(selectionObj) => {
            setEditoreditorSelectionObject(selectionObj);
            }}
            onChange = {() => {
                const cursor = editorSelectionObject.getCursor();
                //do some operations with cursor
            }}
          />