Mapping ranges in a Decoration

Looking at the example on decorations, I see that ranges are mapped in two places:

  1. When creating the StateEffect (using mapPos):
     const addUnderline = StateEffect.define<{from: number, to: number}>({
       map: ({from, to}, change) => ({from: change.mapPos(from), to: change.mapPos(to)})
     })
    
  2. When applying the StateEffect:
     update(underlines, tr) {
       underlines = underlines.map(tr.changes)`
    

This seems redundant – what am I missing? I (think) I understand why the second mapping occurs, but the first mapping seems unnecessary, since I assume dispatching this transactions will cause the second mapping to be performed.

Thanks!

Bryan

The first describes how to map the effect, which is indeed not used when a transaction is applied in the most simple way, but does take effect when transaction specs are combined or undoable effects are rebased by the undo history.

The second, which looks like you’re talking about state fields, and has nothing to do with StateEffect, is mapping an actual decoration set, which is a different thing from a state effect.

Ok, thanks for the clarification! I appreciate it.