Skip to content

Command to insert text #333

Answered by duhaime
duhaime asked this question in Q&A
Discussion options

You must be logged in to vote

Aha, one can call the schema.text method from inside a custom command:

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 = '';

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by duhaime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant