scniro / react-codemirror2

Codemirror integrated components for React
MIT License
1.66k stars 193 forks source link

Insert string at the current cursor position #166

Closed AienTech closed 4 years ago

AienTech commented 4 years ago

Hi there,

is there a way to insert a string at the current cursor position?

So I have a toolbar (a component) and there I have some buttons prepared for the user to add some pre-defined commands (like bold, underline, etc.) when clicking on each of them, a different type of strings should be inserted into the editor, where the cursor is set. how is this achievable with current configurations? is there any workaround?

many thanks

scniro commented 4 years ago

@AienTech I am a lot shorter on time these days as when I started this project. Codemirror & React APIs are moving to quickly for me to keep atop of for the day-to-day. I am looking for a co-maintainer of this project. Please contact me directly if you are interested. Thank you for understanding.

AienTech commented 4 years ago

Hi @scniro , thanks for the message. I currently moved to another library, so, unfortunately, I can't help with the maintenance.

jyxt commented 4 years ago

You can insert at cursor position by using

const doc = editor.getDoc()
const cursor = doc.getCursor()
doc.replaceRange('text', cursor)

You can get editor reference from onChange function arguments (editor, data, value)

mesizm commented 2 years ago

I have tried to in this way but it is not working for me https://stackoverflow.com/q/71999281/7715614 please share some ideas about this issue