Search code examples
codemirror

Inserting a new text at given cursor position


I am working on customizing the codemirror for my new language mode. As part of this new mode implementation, I am writing a new tool bar where user can select some text and say insert. This command should insert the text where user was typing just before clicking on tool bar.

I could not find any API level support to do so. If there is any other way can someone help me out on this?

Basically get the current cursor positio- line number and position at which cursor is currently present. May be a Position object

API for inserting a text, something like insertText("Text", PositionObject)


Solution

  • How about replaceSelection (http://codemirror.net/doc/manual.html#replaceSelection)?

    doc.replaceSelection(replacement: string, ?select: string) Replace the selection(s) with the given string. By default, the new selection ends up after the inserted text. The optional select argument can be used to change this—passing "around" will cause the new text to be selected, passing "start" will collapse the selection to the start of the inserted text.