How to Insert text to current cursor position using custom toolbar

Open
#209 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
markdown, react, typescript
Domain
frontend

Research direction

Start at the custom toolbar's execute callback and inspect the CodeMirror view.state selection and dispatch change handling shown in the issue. Confirm how the current cursor position is represented and verify that the requested image-markdown string is inserted there with the selection updated afterward.

Written by the indexing model from the issue text.

Description

How can i insert a specific string at the current cursor position using the custom toolbar feature?

Is theme a method to get current cursor position and insert text? thank you!

this is my code, and it doesn't work properly.

please give me some advice, Thank you!

    execute: (mirror: ReactCodeMirrorRef) => {
      const { view, state, editor } = mirror
      if (!view || !state || !editor) {
        return
      }
      // NOTE: how to get current position of cursor?
      const lineInfo = view.state.doc.lineAt(view.state.selection.main.from)
      console.log(lineInfo)
      const insert = `![image title](image url)`
      view.dispatch({
        changes: {
          from: lineInfo.from,
          to: lineInfo.from + insert.length,
          insert,
        },
        selection: { anchor: lineInfo.from },
      })
    },

Dominant language
TypeScript
Stars
382
Forks
37
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from uiwjs/react-markdown-editor

All issues in uiwjs/react-markdown-editor

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.