Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Textarea
- constructor: (dom) ->
- @textarea = dom
- @value = @textarea.value
- toFrame: (text = '', before = '', after = '') =>
- selection = @getSelectionPosition()
- output = (
- @value.substr(0, selection.start) +
- '' + before + '' +
- text +
- '' + after + '' +
- @value.substr(selection.end)
- )
- @value = output
- @setSelection(selection.start + before.length, selection.end + before.length)
- @
- getSelectedLines: =>
- lines = (text) ->
- raw = text.match(/\n/g)
- (if raw? then raw else []).length
- selection = @getSelectionPosition()
- return {
- start: lines(@value.substr(0, selection.start)),
- end: lines(@value.substr(0, selection.end))
- }
- getAllLines: =>
- return (@value.match(/\n/g) || []).length + 1
- setSelection: (from, to) =>
- @textarea.focus()
- @textarea.selectionStart = from
- @textarea.selectionEnd = to
- @
- getSelectionText: =>
- cursor = @getSelectionPosition()
- return @value.substr(cursor.start, cursor.end - cursor.start)
- getSelectionPosition: =>
- [start, end] = [0, 0];
- if document.selection
- @textarea.focus()
- sel = document.selection.createRange()
- sel.moveStart 'character', -@textarea.value.length
- start = sel.text.length;
- sel.moveEnd 'character', -@textarea.value.length
- else if @textarea.selectionStart || @textarea.selectionStart is '0'
- start = @textarea.selectionStart;
- end = @textarea.selectionEnd;
- return {start: start, end: end}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement