Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting Text At Cursor Location With Quill.js

I am trying to add a custom piece of functionality ("module") to quill.js and cannot seem to do it. Here is what I need:

If want to add a button that inserts a template replacement variable... say something like {{company}} at the location of the cursor in the editor, is that currently possible with the API - I thought I could do it using insertText but I cant seem to get it to work.

Thanks

like image 528
josephmisiti Avatar asked May 14 '14 17:05

josephmisiti


2 Answers

What I ended up doing in a very similar setup:

let mergeFieldText = '{{company}}';
var selection = this._quill.getSelection(true);
this._quill.insertText(selection.index, mergeFieldText);
like image 56
Tikall Avatar answered Sep 20 '22 16:09

Tikall


You should be able to do this with insertText but you might need to use getSelection to get the cursor location. The object returned by getSelection will have an index and length key. Adding the button and necessary click handler will be up to the implementor. Note focus should be returned back to the editor before calling getSelection with focus or simply passing true to getSelection.

like image 27
jhchen Avatar answered Sep 18 '22 16:09

jhchen