# Inserted custom block nodes don't influence on selection

**URL:** https://discuss.prosemirror.net/t/inserted-custom-block-nodes-dont-influence-on-selection/1454
**Category:** Uncategorized
**Created:** [August 1, 2018, 11:57am UTC](https://discuss.prosemirror.net/t/inserted-custom-block-nodes-dont-influence-on-selection/1454 "2018-08-01T11:57:26Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![maximka777](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/maximka777/32/693_2.png) [@maximka777](https://discuss.prosemirror.net/u/maximka777)
#### Post date: [August 1, 2018, 11:57am UTC](https://discuss.prosemirror.net/t/inserted-custom-block-nodes-dont-influence-on-selection/1454/1 "2018-08-01T11:57:26Z")

</div>

I have such custom block node schema:

```
const imageNodeSpec = {
  attrs: {
    src: {},
    alt: {
      default: null
    }
  },
  inline: false,
  group: 'block',
  draggable: true,
  parseDOM: [{
    tag: 'div[image-with-description]',
    getAttrs(dom) {
      const image = dom.querySelector('img[src]');
      return {
        src: image.getAttribute('src'),
        alt: image.getAttribute('alt')
      };
    }
  }],
  toDOM(node) {
    return [
      'div',
      {
        class: 'image-with-description-container',
        'image-with-description': true
      },
      [
        'img',
        {
          ...node.attrs,
          class: 'image'
        },
      ],
      [
        'p',
        {
          class: 'image-description'
        },
        node.attrs.alt
      ]
    ];
  }
};

```

It’s an image with description under it.

Each insertion of this node is occurred at the beginning.

If editor is empty and we insert such nodes then **each time we will have position index 1**.

Insertion is occured this way:

```
        const tr = view.state.tr;
        if (!tr.selection.empty) {
          tr.deleteSelection();
        }
        view.dispatch(tr.replaceSelectionWith(type.createAndFill({
          ...attributes,
          src: image.url
        })));
```

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)
#### Post date: [August 1, 2018, 12:11pm UTC](https://discuss.prosemirror.net/t/inserted-custom-block-nodes-dont-influence-on-selection/1454/2 "2018-08-01T12:11:06Z")

</div>

Since your schema probably doesn’t allow a completely empty document, what does the document look like after you run that command? And where did you expect the selection to be?

---

<div class="post-metadata">

### Author: ![maximka777](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/maximka777/32/693_2.png) [@maximka777](https://discuss.prosemirror.net/u/maximka777)
#### Post date: [August 1, 2018, 12:23pm UTC](https://discuss.prosemirror.net/t/inserted-custom-block-nodes-dont-influence-on-selection/1454/3 "2018-08-01T12:23:36Z")

</div>

After I inserted my custom node I have cursor after inserted element, but in state of editor it’s before inserted element. I can increment it on 1 in debug mode (when I insert second node, if I insert 3rd node I should increment on 2 and so on) and new node will be inserted in correct place.

I don’t know how to show my document so there is screenshot 🙂

 ![31](https://discuss.prosemirror.net/uploads/secondsite/original/1X/781b69baf87718b756a8b493e3b844f6137e55da.png)

There are two custom nodes in a paragraph element.

And each time selection position will be 1 if I insert new nodes.

---

<div class="post-metadata">

### Author: ![maximka777](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/maximka777/32/693_2.png) [@maximka777](https://discuss.prosemirror.net/u/maximka777)
#### Post date: [August 1, 2018, 1:46pm UTC](https://discuss.prosemirror.net/t/inserted-custom-block-nodes-dont-influence-on-selection/1454/4 "2018-08-01T13:46:11Z")

</div>

I think I found the problem: when I click on dropdown item in menu to upload image it focuses on the first element in editor and inserts here new node (sometimes replaces the 1st node).

**Updated** : No, I made mistake, it’s still not working.

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)
#### Post date: [August 2, 2018, 7:18am UTC](https://discuss.prosemirror.net/t/inserted-custom-block-nodes-dont-influence-on-selection/1454/5 "2018-08-02T07:18:15Z")

</div>

How come the images are rendered inside a paragraph in the DOM, though they are block nodes? That seems suspicious.

To look at the actual document, you can run `view.state.doc.toString()`.

---

<div class="post-metadata">

### Author: ![maximka777](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/maximka777/32/693_2.png) [@maximka777](https://discuss.prosemirror.net/u/maximka777)
#### Post date: [August 2, 2018, 7:44am UTC](https://discuss.prosemirror.net/t/inserted-custom-block-nodes-dont-influence-on-selection/1454/6 "2018-08-02T07:44:28Z")

</div>

It’s a document with two custom nodes:

`"doc(image, image, paragraph)"`

`image` - name of my custom node.

The last paragraph it’s an empty line.

---

<div class="post-metadata">

### Author: ![maximka777](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/maximka777/32/693_2.png) [@maximka777](https://discuss.prosemirror.net/u/maximka777)
#### Post date: [August 2, 2018, 7:55am UTC](https://discuss.prosemirror.net/t/inserted-custom-block-nodes-dont-influence-on-selection/1454/7 "2018-08-02T07:55:01Z")

</div>

I think the problem was in my application it was refreshing the editor and changing cursor position. I fixed it and nodes are inserted correctly now.

Thanks for your answers 🙂
