What is being selected?

I’ve spent some time trying to resolve how to correctly use a resolved position to reliabliy replace a node(and all it’s children). Rather than writing a wall of text, or posting a bunch of code, I’ve create a diagram that shows my current understanding and the results of calling Resolve after the users clicks on the editor. I have two questions:

  • Is my understanding of positions (in the diagram) correct?
  • How can I use resolvedpos to select and replace the parent row?

The position of a node is the position directly in front of its opening token (so the template node is at 0, the row node at 1, etc). How to go from a resolved pos to a node position depends very much on where that resolved pos points. If it’s inside the node you’re interested in, $pos.before() would do it. If it points directly before it, just use $pos.pos.

I know that in the onClickHandler there is both a position (pos) parameter and a nodePos parameter.

What the heck is a nodePosition, and how would I use it?

pos is the position nearest to the click nodePos is the position before the node that is being clicked. If you for example click on the right side of an image, pos will be the position after the image, and nodePos will be the position before it.

Hmmm.

If I understand your reply correctly then:

nodePos == resolve(pos).before()

I think where I’m getting lost is in determing when a position is “in” a node…I mean I assume that a position (with the exception of 0 and docsize from the root) is always in ‘some’ node…but what one?!

No. If it was that simple nodePos wouldn’t be passed as a separate value. In the case above of clicking on an image, neither of the positions would be inside the image so this does not hold.

The parent property of ResolvedPos holds the node that the position points directly into.

My code ‘seems’ to be working correctly at this point. I am still lacking confidence in it as I am still lacking confidence in my understanding of how to correctly work with positions (assuming there is a ‘correct’ way…

Given this fairly simple layout:

 0      1      2      3       4      5       6       7      8      9      10     11      12      13       14
  <tem>  <row>  <col>  </col>  <col>  </col>  </row>  <row>  <col>  </col>  <col>  </col>  </row>  </tem>

I would assume that position 10, if the user could click on it, would have the row node as the parent, a depth of 2 and rp.node(2) would also return the row. Does that sound/look correct?