How to wrap a text selection inside inline node a move cursor to the end

I have a paragraph node and math_inline node. I would like to select of piece of text inside my paragraph and wrap it inside math_inline node. I tried to use wrapIn command but without any success. I would like to use the same command to simply insert math_inline command in the current cursor position. What is the best way to achieve that?

	name: "math_inline",
	group: "inline math",
	content: "text*", // important!
	inline: true, // important!
	atom: true, // important!
	code: true,

EDIT: I did that with the following piece of code:

				() =>
				({ chain, state, tr }) => {
					let { $from, $to } = state.selection;
					let index = $from.index();
					if (!$from.parent.canReplaceWith(index, index, this.type)) {
						return false;

					let content = state.doc.cut(


					return chain()
						.command(({ tr }) => {
									content ? state.schema.text(content) : undefined

							tr.setSelection(NodeSelection.create(tr.doc, $from.pos));

							return true;

Is that OK? Another problem is that this way the cursor is positioned at the beginning od the math block while I would like to move it to the very end. How can I do that?

You can use textBetween as a more convenient replacement of the cut/textContent thing.

It seems you should be able to compute the end position of your node (start position + 2 (for start/end tokens) + content length). You shouldn’t use a resolved position into a previous document in NodeSelection.create anyway, so resolve your node end position in tr.doc, don’t use $from there.

1 Like