Skip to content

Commit

Permalink
Extract a CursorsComponent containing all cursors
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Sobo committed Apr 22, 2014
1 parent 0ec6cbe commit 14bfa90
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
44 changes: 44 additions & 0 deletions src/cursors-component.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
React = require 'react'
{div} = require 'reactionary'
{debounce} = require 'underscore-plus'
SubscriberMixin = require './subscriber-mixin'
CursorComponent = require './cursor-component'


module.exports =
CursorsComponent = React.createClass
mixins: [SubscriberMixin]

cursorBlinkIntervalHandle: null

render: ->
{editor} = @props
{blinkCursorsOff} = @state

div className: 'cursors',
for selection in editor.getSelections() when editor.selectionIntersectsVisibleRowRange(selection)
CursorComponent(cursor: selection.cursor, blinkOff: blinkCursorsOff)

getInitialState: ->
blinkCursorsOff: false

componentDidMount: ->
{editor} = @props
@subscribe editor, 'cursors-moved', @pauseCursorBlinking
@startBlinkingCursors()

startBlinkingCursors: ->
@cursorBlinkIntervalHandle = setInterval(@toggleCursorBlink, @props.cursorBlinkPeriod / 2)

startBlinkingCursorsAfterDelay: null # Created lazily

stopBlinkingCursors: ->
clearInterval(@cursorBlinkIntervalHandle)
@setState(blinkCursorsOff: false)

toggleCursorBlink: -> @setState(blinkCursorsOff: not @state.blinkCursorsOff)

pauseCursorBlinking: ->
@stopBlinkingCursors()
@startBlinkingCursorsAfterDelay ?= debounce(@startBlinkingCursors, @props.cursorBlinkResumeDelay)
@startBlinkingCursorsAfterDelay()
38 changes: 4 additions & 34 deletions src/editor-scroll-view-component.coffee
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
React = require 'react'
ReactUpdates = require 'react/lib/ReactUpdates'
{div, span} = require 'reactionary'
{debounce, isEqual, multiplyString, pick} = require 'underscore-plus'
{div} = require 'reactionary'

InputComponent = require './input-component'
LinesComponent = require './lines-component'
CursorComponent = require './cursor-component'
CursorsComponent = require './cursors-component'
SelectionComponent = require './selection-component'
SubscriberMixin = require './subscriber-mixin'

module.exports =
EditorScrollViewComponent = React.createClass
mixins: [SubscriberMixin]

render: ->
{editor, fontSize, fontFamily, lineHeight, showIndentGuide} = @props
{editor, fontSize, fontFamily, lineHeight, showIndentGuide, cursorBlinkPeriod, cursorBlinkResumeDelay} = @props
{visibleRowRange, onInputFocused, onInputBlurred} = @props
contentStyle =
height: editor.getScrollHeight()
Expand All @@ -30,50 +26,24 @@ EditorScrollViewComponent = React.createClass
onBlur: onInputBlurred

div className: 'scroll-view-content', style: contentStyle, onMouseDown: @onMouseDown,
@renderCursors()
CursorsComponent({editor, cursorBlinkPeriod, cursorBlinkResumeDelay})
LinesComponent({ref: 'lines', editor, fontSize, fontFamily, lineHeight, visibleRowRange, showIndentGuide})
@renderUnderlayer()

renderCursors: ->
{editor} = @props
{blinkCursorsOff} = @state

for selection in editor.getSelections() when editor.selectionIntersectsVisibleRowRange(selection)
CursorComponent(cursor: selection.cursor, blinkOff: blinkCursorsOff)

renderUnderlayer: ->
{editor} = @props

div className: 'underlayer',
for selection in editor.getSelections() when editor.selectionIntersectsVisibleRowRange(selection)
SelectionComponent({selection})

getInitialState: ->
blinkCursorsOff: false

componentDidMount: ->
@getDOMNode().addEventListener 'overflowchanged', @updateModelDimensions
@subscribe @props.editor, 'cursors-moved', @pauseCursorBlinking
@updateModelDimensions()
@startBlinkingCursors()

focus: ->
@refs.input.focus()

startBlinkingCursors: ->
@cursorBlinkIntervalHandle = setInterval(@toggleCursorBlink, @props.cursorBlinkPeriod / 2)

stopBlinkingCursors: ->
clearInterval(@cursorBlinkIntervalHandle)
@setState(blinkCursorsOff: false)

toggleCursorBlink: -> @setState(blinkCursorsOff: not @state.blinkCursorsOff)

pauseCursorBlinking: ->
@stopBlinkingCursors()
@startBlinkingCursorsAfterDelay ?= debounce(@startBlinkingCursors, @props.cursorBlinkResumeDelay)
@startBlinkingCursorsAfterDelay()

getHiddenInputPosition: ->
{editor} = @props

Expand Down

0 comments on commit 14bfa90

Please sign in to comment.