Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arbitrary cursor positioning #1

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/rich_text/controller.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class RichText.Controller

position

setPosition: (position) ->
selection = window.getSelection()
selection.removeAllRanges()
selection.selectAllChildren(@element)
selection.collapse(selection.anchorNode, position)

didTypeCharacter: (character) ->
@insertString(character)

Expand All @@ -39,21 +45,19 @@ class RichText.Controller
@render()

insertString: (string) ->
@text.insertTextAtPosition(new RichText.Text(string), @getPosition())
text = new RichText.Text(string)
position = @getPosition()

@text.insertTextAtPosition(text, position)
@render()
@setPosition(position + text.getLength())

backspace: ->
position = @getPosition()
@text.removeTextAtRange([position - 1, position]) if position > 0
@render()
if position = @getPosition()
@text.removeTextAtRange([position - 1, position])
@render()
@setPosition(position - 1)

render: ->
@element.innerHTML = ""
@element.appendChild(@renderer.render())
@updateSelection()

updateSelection: ->
selection = window.getSelection()
selection.removeAllRanges()
selection.selectAllChildren(@element)
selection.collapseToEnd()