Node position & node decoration

I seen lot of topics here for the relatively same question and also tried to make sense of the documentation but I’m pretty lost :slightly_frowning_face:

My use case is I need to decorate a specific node that I know the path (for instance the 1st paragraph from root) and I tried so many thing but I always getting out of range position.

There is my current experimentation:

const root = doc.child(0) // The very first child is a root node
const pNode = root.child(0) //  For my test I have only one paragraph with one letter text

// None of the following works
  { class: 'decoration' }

const position = pNode.resolve(0)
  { class: 'decoration' }

// Now I just tried the following, no error but no class rendered :'(
const position = pNode.resolve(0)
  position.pos + pNode.content.size,
  { class: 'decoration' }

// The plugin
new Plugin<{ decorations: DecorationSet | undefined }>({
  key: pluginKey,
  state: {
    init: () => ({
      decorations: undefined
    apply: (transaction, prev) => {
      if (transaction.docChanged) {
        return { decorations: DecorationSet.create(getDecorations()) }
      return prev
  props: {
    decorations (state) {'this.getState(state).decorations', this.getState(state).decorations)
      return this.getState(state).decorations

I’m sorry if my question looks dumb, I think I understand position indexing and what this implies but I do not get how I can have a correct node decoration here :slightly_frowning_face:

Any help would be greatly appreciated and maybe some extra example in the documentation could be super helpful for anyone in the same situation.

You’ll have to track offsets as your descend into the document. A node doesn’t know its document position, so calling resolve on some inner node will not do what you seem to think it does (it resolves the position in the node’s content, but n.resolve(p).pos always equals p).

Oh now I understand why I am getting those results. Yes it is looking like keeping a offset counter with node.nodeSize is helping a lot.

I have weir behavior with widget (does not display a widget in my empty paragraph) but I think I’m gonna figure out.