I am writing an unwrap command that will remove the top most container block. I tried to use lift in many different way but I couldn’t properly remove the container.
I ended up with this code, which works except for restoring the selection:
export function unwrap(type) {
return (state, dispatch, view) => {
const tr = state.tr
const $pos = state.selection.$anchor
for (let d = $pos.depth; d > 0; d--) {
const node = $pos.node(d)
if (node.type === type) {
const from = $pos.before(d)
const to = $pos.after(d)
if (dispatch) {
dispatch(
tr
.replaceWith(from, to, node.content.content)
.setSelection(
state.selection.map(tr.doc, tr.mapping),
)
.scrollIntoView(),
)
}
return true
}
}
return false
}
}
After applying this command, I end up with the selection/cursor after the block that was replaced.
Don’t replace the whole range to unwrap something. Use Transaction.lift or build up a ReplaceAroundStep that only deletes the relevant open/close tokens.