-
I'm trying to use the prosemirror command interface to insert text with a custom mark set. I see that prosemirror has Is there a way to insert text with custom marks using the prosemirror command interface? |
Beta Was this translation helpful? Give feedback.
Answered by
duhaime
Feb 1, 2022
Replies: 1 comment
-
Aha, one can call the export const InsertText = createCmdKey();
const pluginText = () => async (ctx) => {
await ctx.wait(CommandsReady);
const commandManager = ctx.get(commandsCtx);
const schema = ctx.get(schemaCtx);
commandManager.create(InsertText, () => {
return (state, dispatch) => {
if (!dispatch) return true;
const { tr, selection } = state;
const { from, to } = selection;
const schema = ctx.get(schemaCtx)
// if there's a selection, store the selected text in `text`
const slice = state.doc.cut(from, to);
// handle case that there's no node between from and to
let text = '';
try {
text = slice.textBetween(from, to);
} catch (err) {}
// make the text to be inserted -- NB: you can insert marks after 'wheee'
const node = schema.text('wheee');
if (!node) {
return true;
}
dispatch(tr.replaceSelectionWith(node).scrollIntoView())
}
});
}; Then one needs to call <div onClick={() => { ctx.get(commandsCtx).call(InsertText) }}>insert text</div> |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
duhaime
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Aha, one can call the
schema.text
method from inside a custom command: