Trix: Is there a way to get selected text?

Created on 27 Jun 2016  路  1Comment  路  Source: basecamp/trix

I need the selected text for specific range say [10, 22], is there a way to get it?

Most helpful comment

You can use Document#getStringAtRange or Document#getDocumentAtRange:

range = element.editor.getSelectedRange() // [297, 316]
element.editor.getDocument().getStringAtRange(range) // "Different By Design\n"
// equivalent to
element.editor.getDocument().getDocumentAtRange(range).toString() // "Different By Design\n"

A Document always ends with a \n so you may want to trim it off.

>All comments

You can use Document#getStringAtRange or Document#getDocumentAtRange:

range = element.editor.getSelectedRange() // [297, 316]
element.editor.getDocument().getStringAtRange(range) // "Different By Design\n"
// equivalent to
element.editor.getDocument().getDocumentAtRange(range).toString() // "Different By Design\n"

A Document always ends with a \n so you may want to trim it off.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benzkji picture benzkji  路  3Comments

adamdebono picture adamdebono  路  3Comments

divyenduz picture divyenduz  路  4Comments

pars0097 picture pars0097  路  4Comments

plainspace picture plainspace  路  4Comments