ResolvedPos Confusion

With the integer positions, it can get a bit confusing to follow the documentation, what is absolute position vs. what is a relative one. One important piece that I have trouble with right now is the behavior of the resolve method. The following is confusing to me:

  1. Start with blank document and type in 123456789
  2. Put the cursor right after the ‘1’
  3. resolve current positions:

rp = editor.doc.resolve(editor.selection.head) ResolvedPos {pos: 2, path: Array[6], depth: 1, parentOffset: 1}

parentOffset makes sense: one character after the beginning of the parent paragraph node.

  1. Now resolve the same position from the paragraph node instead of the editor.doc:

rp2 = rp.parent.resolve(editor.selection.head) ResolvedPos {pos: 2, path: Array[3], depth: 0, parentOffset: 2}

why is parentOffset == 2 here? The parent in both rp and rp2 is the same (the paragraph node), why is the offset different?

Thanks! Boris

You almost never (probably entirely never) should be resolving from anything but the root node. Resolving from something else expects the position to be relative to that node, so a position 2 units into the paragraph would have parentOffset 2, since positions are counted from the start of the reference node.

I see, ok thanks!