Selection.js
Selection.js provides a clean API to access the selection in a <textarea
>. It is powerful and
easy to use. It helps developers to deal with editors or the like.
Installation
Install with npm:
$ npm install selection.js --save
API
var Selection = require('selection.js');
new Selection(document.getElementById('textarea'));
new Selection(document.getElementsByTagName('textarea'));
new Selection($('textarea'));
Selection#cursor()
Get current cursor position, return [start, end]:
sel.cursor();
Set cursor position (or select text):
sel.cursor(1);
sel.cursor(1, 4);
sel.cursor([1, 4]);
Selection#text()
Get current selected text:
sel.text();
Replace current selected text:
// will select replaced text
// word ... [replaced text] word ...
sel.text('replaced text');
Selection#append(text)
Insert text after current selection
sel.append('append text');
Selection#prepend(text)
Insert text before current selection:
sel.prepend('prepend text');
Selection#line()
Get current line text:
sel.line()
Selection#surround()
Get surround word:
sel.surround();
sel.surround(3); // get surroud 3 characters.
Selection#insertText(text, cursorStart, cursorEnd)
Insert text at the give cursor:
sel.insertText('text', 3, 4);
License
New BSD