How to get node selection at node plugin?

Hello everyone!

I made a custom node that is basically a span with several custom attributes in the tag. I need to get the NodeSelection object from the selection (of $from and $to) so i could get the tag attributed at the beggining and the end of the selection. Tried to use Selection.near but is still returning me a TextSelection. The schema has the selectable: true defined.

state: {

        init(config, editorState) {
          return DecorationSet.empty;
        },
        apply(transaction, decorationSet) {

          const { docChanged, curSelection } = transaction;

          if (!docChanged) {
            console.log("node = ", Selection.near(curSelection.$from))
          }

          return decorationSet;
        },
      },

Any ideas of how i could accomplish this?

You can use the NodeSelection constructor.

Thanks for the suggestion, i tried it, but i still get a TextType at the node property of the NodeSelection with the attribute attrs empty. I tried also using tr.doc.nodeAt() with the same result. What i need is the same node object returned by HandleClickOn where the attrs property is filled with the values in the tag . Any other suggestion of how i could achieve this?

Thanks to all in advance!

but i still get a TextType at the node property of the NodeSelection

That suggests that the position you’re using is not in fact pointing at the node you want. Possibly it’s pointing at the start of its content instead, and you need the position before it

Thanks for the comment, that made me use & adapt the function used by HandleClickOn to detect the node selectClickedNode Not sure if it’s the best solution but looks like it works.

Thanks again for the tips and the guide to the solution of the problem!