From 3b93f3d71ba1aba699d0a3560dab77aa1503e46a Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 23 Jan 2015 10:26:00 -0700 Subject: [PATCH] Blink cursors based on presenter state Signed-off-by: Max Brunsfeld --- spec/text-editor-component-spec.coffee | 5 ++++ src/cursors-component.coffee | 41 ++------------------------ src/text-editor-component.coffee | 26 +++++++++------- 3 files changed, 23 insertions(+), 49 deletions(-) diff --git a/spec/text-editor-component-spec.coffee b/spec/text-editor-component-spec.coffee index ad5a4e18c..0f6aee2ad 100644 --- a/spec/text-editor-component-spec.coffee +++ b/spec/text-editor-component-spec.coffee @@ -776,18 +776,23 @@ describe "TextEditorComponent", -> cursorsNode = componentNode.querySelector('.cursors') expect(cursorsNode.classList.contains('blink-off')).toBe false + advanceClock(component.props.cursorBlinkPeriod / 2) + nextAnimationFrame() expect(cursorsNode.classList.contains('blink-off')).toBe true advanceClock(component.props.cursorBlinkPeriod / 2) + nextAnimationFrame() expect(cursorsNode.classList.contains('blink-off')).toBe false # Stop blinking after moving the cursor editor.moveRight() + nextAnimationFrame() expect(cursorsNode.classList.contains('blink-off')).toBe false advanceClock(component.props.cursorBlinkResumeDelay) advanceClock(component.props.cursorBlinkPeriod / 2) + nextAnimationFrame() expect(cursorsNode.classList.contains('blink-off')).toBe true it "does not render cursors that are associated with non-empty selections", -> diff --git a/src/cursors-component.coffee b/src/cursors-component.coffee index f5dada022..21211f9a3 100644 --- a/src/cursors-component.coffee +++ b/src/cursors-component.coffee @@ -7,51 +7,14 @@ CursorComponent = require './cursor-component' module.exports = CursorsComponent = React.createClass displayName: 'CursorsComponent' - mixins: [SubscriberMixin] - - cursorBlinkIntervalHandle: null render: -> - {presenter, defaultCharWidth} = @props - {blinkOff} = @state + {presenter} = @props className = 'cursors' - className += ' blink-off' if blinkOff + className += ' blink-off' if presenter?.state.content.blinkCursorsOff div {className}, if presenter? for key, pixelRect of presenter.state.content.cursors CursorComponent({key, pixelRect}) - - getInitialState: -> - blinkOff: false - - componentDidMount: -> - @startBlinkingCursors() - - componentWillUnmount: -> - @stopBlinkingCursors() - - componentWillUpdate: (newProps) -> - cursorsMoved = @props.cursorPixelRects? and - isEqualForProperties(newProps, @props, 'defaultCharWidth', 'scopedCharacterWidthsChangeCount') and - not isEqual(newProps.cursorPixelRects, @props.cursorPixelRects) - - @pauseCursorBlinking() if cursorsMoved - - startBlinkingCursors: -> - @toggleCursorBlinkHandle = setInterval(@toggleCursorBlink, @props.cursorBlinkPeriod / 2) if @isMounted() - - startBlinkingCursorsAfterDelay: null # Created lazily - - stopBlinkingCursors: -> - clearInterval(@toggleCursorBlinkHandle) - - toggleCursorBlink: -> - @setState(blinkOff: not @state.blinkOff) - - pauseCursorBlinking: -> - @state.blinkOff = false - @stopBlinkingCursors() - @startBlinkingCursorsAfterDelay ?= debounce(@startBlinkingCursors, @props.cursorBlinkResumeDelay) - @startBlinkingCursorsAfterDelay() diff --git a/src/text-editor-component.coffee b/src/text-editor-component.coffee index d25713447..241b9d581 100644 --- a/src/text-editor-component.coffee +++ b/src/text-editor-component.coffee @@ -226,16 +226,22 @@ TextEditorComponent = React.createClass @performedInitialMeasurement = true @updatesPaused = false - {editor, lineOverdrawMargin} = @props - @presenter ?= new TextEditorPresenter - model: editor - clientHeight: editor.getHeight() - clientWidth: editor.getWidth() - scrollTop: editor.getScrollTop() - scrollLeft: editor.getScrollLeft() - lineHeight: editor.getLineHeightInPixels() - baseCharacterWidth: editor.getDefaultCharWidth() - lineOverdrawMargin: lineOverdrawMargin + {editor, lineOverdrawMargin, cursorBlinkPeriod, cursorBlinkResumeDelay} = @props + + + unless @presenter? + @presenter = new TextEditorPresenter + model: editor + clientHeight: editor.getHeight() + clientWidth: editor.getWidth() + scrollTop: editor.getScrollTop() + scrollLeft: editor.getScrollLeft() + lineHeight: editor.getLineHeightInPixels() + baseCharacterWidth: editor.getDefaultCharWidth() + lineOverdrawMargin: lineOverdrawMargin + cursorBlinkPeriod: cursorBlinkPeriod + cursorBlinkResumeDelay: cursorBlinkResumeDelay + @presenter.onDidUpdateState(@requestUpdate) @forceUpdate() if @canUpdate()