Wrap selection in inline node and remove again

Hey all,

are there any commands or functions that help with wrapping a text selection in an inline node? wrapIn seems to only work with block nodes. I also need to remove the inline node again. Are there any helpers for that operation?

My Schema (simplified):

  • doc => someblock*
  • someblock => (text|someinline)*
  • someinline => text*

So far I came up with this code for wrapping the selection in an inline node (there are some checks of selection beforehand which i omitted)

function wrapInInlineNode(state, tr, schema, type) {
  const sliceJson = state.selection.content().toJSON();
  sliceJson.content[0].content = [{ type, content: sliceJson.content[0].content }];
  state.selection.replace(tr, Slice.fromJSON(schema, sliceJson));

No, there is no utility for this. Inline nodes with content aren’t commonly used, because editing on their boundaries tends to get messy.

Going through JSON to manipulate documents should not be necessary. Manually building up a ReplaceAround step that doesn’t touch the content of the node is preferable in general, because it will treat the operation as an actual wrap, rather than a replacement of the entire covered range (allowing positions inside it to be mapped properly).

Thank you for the very quick response. I will look into ReplaceAround step.