# Apply decorations directly through an editor method?

**URL:** https://discuss.prosemirror.net/t/apply-decorations-directly-through-an-editor-method/2377
**Category:** Uncategorized
**Created:** [November 28, 2019, 10:51am UTC](https://discuss.prosemirror.net/t/apply-decorations-directly-through-an-editor-method/2377 "2019-11-28T10:51:05Z")
**Posts on this page:** 1
**Showing post:** 7

<div class="post-metadata">

### Author: ![ericksonc](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/ericksonc/32/1242_2.png) [@ericksonc](https://discuss.prosemirror.net/u/ericksonc)
#### Post date: [November 29, 2019, 6:18am UTC](https://discuss.prosemirror.net/t/apply-decorations-directly-through-an-editor-method/2377/7 "2019-11-29T06:18:15Z")

</div>

Thanks, now I’m starting to get it!

I managed to get it to work, code is below.

**Helper Function:**

```
function applyDecoration (state, view, fromPos, toPos) {
  let transaction = state.tr.setMeta("directDecoration", {fromPos: fromPos, toPos: toPos})
  view.dispatch(transaction)
}

```

**Plugin:**

```
let directDecoration= new Plugin({
  state: {
    init() {
      // No decorations set by default
    },
    apply(tr, value) {
      if (tr.getMeta("directDecoration")) {
        const {fromPos, toPos} = tr.getMeta("directDecoration")
        return DecorationSet.create(tr.doc, [
          Decoration.inline(fromPos, toPos, {style: "color: purple"})
        ])
      } else {
         // map "other" changes so our decoration "stays put" 
         // (e.g. user is typing so decoration's pos must change)
        return value.map(tr.mapping, tr.doc)
      }
    }
  },
  props: {
    decorations(state) {
      return directDecoration.getState(state)
    }
  }
})
```

---

_[View the full topic](https://discuss.prosemirror.net/t/apply-decorations-directly-through-an-editor-method/2377)._
