New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Finding absolute start/end position of the node at a given position. #505
Comments
|
Thanks. This works for the simple example I provided, because the local position is the same as the absolute one, but I should have provided a more representative example. Given something like: From what I see, the solution you suggested would provide the local position within the Is finding such absolute positions an anti-pattern? Am I meant to record positions using local offsets and generate a transform for that later on for my use case? Thanks again for your help! |
So add |
Awesome, that did it, thanks a lot for your help! For reference if anyone needs to do this, here is roughly what my code looks like: const resolvedPos = doc.resolve(pos);
const parentInfo = resolvedPos.parent.childBefore(resolvedPos.parentOffset);
const linkNode = parentInfo.node;
const linkStartPos = parentInfo.offset;
const posInParent = resolvedPos.parentOffset;
const offsetInLink = posInParent - linkStartPos;
const linkFrom = pos - offsetInLink;
const linkTo = linkFrom + linkNode.nodeSize; Note that if the same link mark applies to a segment of text that contains another sub-mark (eg italic), more work needs to be done to find the boundaries of the link. |
Seeing that this is really a little awkward, I'm adding a const $pos = doc.resolve(pos)
const linkStart = pos - $pos.textOffset
const linkEnd = linkStart + $pos.parent.child($pos.index()).nodeSize |
Brilliant, thanks @marijnh! |
If you need the selection range of the paren node of the current selection you can do it this way:
|
Given the simple document
p("hello ", link("world"))
, is there a way using the API to find the start/end absolute positions (position 7/12) in the document of the link, given a position within it (egwo|rld
i.e. position 9)?The use case is to implement a link editor, so that when the user clicks on a link, I can determine the start and end position of the link so that it can be edited or removed. I've spent the last two 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 obvious in the docs, or else there may be a totally different way of going about solving this...
The text was updated successfully, but these errors were encountered: