Node positions


I’m new to ProseMirror, currently experimenting with the APIs. I set out to do the simple task of programmatically deleting all content from the editor. So I thought I’d do: Pos([0], 0), endpos).apply();

where endpos would be the very last position of the document. I found it hard to figure out what the end position is. I can traverse the structure by calling doing something like this:

() => { let path = []; let current = editor.doc; while (current.lastChild != null) { path.push(current.size -1); } return new Pos(path.splice(0, path.length-1), path[path.length-1]); }

So in this particular case, this seems to work because the last recursively obtained child will be a text node and its size will be the length of the text. Is that the right approach? In general ‘size’ sometimes refers to the number of children of a node and sometimes to the length of its textual content. But how can I tell which?

Thanks! Boris

You can just take the start and end offset into the top-level node: Pos([], 0), new Pos([], pm.doc.size)).apply()


Thanks! Actually it didn’t occur to me that the second argument to Pos (the offset into the target node) could be an index to a child node rather than an offset in a text string. That makes sense.

But that brings me again to my other question: how do I know if the ‘size’ property represents the length of the text content of a node or its children count? A single character is not a “Node”, so I cannot think of the characters of a text node as its children. I have to somehow go by node type, or what else?

This seems like a beautifully designed library. I hope I can develop a good understanding of it and contribute to the community!

Cheers, Boris

It’s kind of the same thing, except that conceptually text nodes count as one node per character. I.e. in non-textblocks, the offsets are purely between child nodes. But in texblock nodes, they can also point between the characters of the text nodes.