How to get the position(line, ch) from ResolvedPos?

I try to get the position(line, ch) from the state.selection(like the position codemirror provides) using the API or property, because i need the line and ch position to change our other internal model(not prosemirror model) with this position whenever prosemirror document is changed. So I implement this using selection and transation in dispatchTransaction.

dispatchTransaction(transaction) {
  const { $from, $to, ranges } = this.state.selection;
  const { state, transactions } = this.state.applyTransaction(transaction);

  transactions.forEach(tr => {
    if (tr.docChanged) {
      // ...
    }
  });
}

But I’ve spent the last three hours playing with all the prosemirror-model APIs I could find, but no luck extracting those positions… I feel like I must be missing something important.

Is there any obvious way to get the line and ch position of the state.selection based on view?

Thank you in advance! :slight_smile:

ProseMirror doesn’t really have the concept of lines, as the data is stored as one long sequence of characters (or presented as a DOM tree).

Depending on how you’re defining “lines”, perhaps you could do this?:

  1. Get the text content of the whole document (doc.textContent?).
  2. Split the text at the current cursor position.
  3. Split the first chunk on \n.
  4. Use the number of items as the current line number.
  5. Use the length of the last item as the current character offset in the current line.

Got it. I find the another way. Thanks!

Hey, can you please share how did you get what you wanted?