ProseMirror / prosemirror

The ProseMirror WYSIWYM editor
http://prosemirror.net/
MIT License
7.61k stars 336 forks source link

Node class could have new methods for finding nodes among its direct children content and its descendants #1477

Closed KevinVerre closed 2 months ago

KevinVerre commented 2 months ago

I would love it if the Node class had more methods. That way, we wouldn't have to write our own helper functions. We write these helper functions using node.descendants(). An example of a helper function that is useful is to take the current Node and return all of the children nodes or descendant nodes with a particular type.

function getDescendantsWithType(node: Node, targetType: NodeType): Node[] function getDirectChildrenWithType(node: Node, targetType: NodeType): Node[]

marijnh commented 2 months ago

Since these are simple enough to implement on top of descendants, and the precise form they take will be different per use case (sometimes you need just the node, sometimes the node and its position, sometimes its parent, etc), I feel it's fine to have people implement these as custom functions in their own codebase.