fix(composer): focus on text box when clicking in margins

This commit is contained in:
Evan Morikawa 2015-12-10 16:51:44 -05:00
parent 03908fbd84
commit 35e45032ec
3 changed files with 22 additions and 1 deletions

View file

@ -4,6 +4,7 @@ React = require 'react'
{File, {File,
Utils, Utils,
Actions, Actions,
DOMUtils,
DraftStore, DraftStore,
UndoManager, UndoManager,
ContactStore, ContactStore,
@ -174,6 +175,7 @@ class ComposerView extends React.Component
if @props.mode is "inline" if @props.mode is "inline"
<FocusTrackingRegion className={@_wrapClasses()} <FocusTrackingRegion className={@_wrapClasses()}
ref="composerWrap" ref="composerWrap"
onFocusIn={@_onFocusIn}
tabIndex="-1"> tabIndex="-1">
{@_renderComposer()} {@_renderComposer()}
</FocusTrackingRegion> </FocusTrackingRegion>
@ -486,6 +488,14 @@ class ComposerView extends React.Component
@refs[Fields.Body].selectEnd() @refs[Fields.Body].selectEnd()
@_mouseDownTarget = null @_mouseDownTarget = null
# When a user focuses the composer, it's possible that no input is
# initially focused. If this happens, we focus the contenteditable. If
# we didn't focus the contenteditable, the user may start typing and
# erroneously trigger keyboard shortcuts.
_onFocusIn: (event) =>
return if DOMUtils.closest(event.target, DOMUtils.inputTypes())
@refs[Fields.Body].selectEnd()
_onMouseMoveComposeBody: (event) => _onMouseMoveComposeBody: (event) =>
if @_mouseComposeBody is "down" then @_mouseComposeBody = "move" if @_mouseComposeBody is "down" then @_mouseComposeBody = "move"

View file

@ -11,13 +11,20 @@ class FocusTrackingRegion extends React.Component
@propTypes: @propTypes:
className: React.PropTypes.string className: React.PropTypes.string
children: React.PropTypes.any children: React.PropTypes.any
onFocusIn: React.PropTypes.func
onFocusOut: React.PropTypes.func
@defaultProps:
onFocusIn: ->
onFocusOut: ->
constructor: (@props) -> constructor: (@props) ->
@state = {focused: false} @state = {focused: false}
@_goingout = false @_goingout = false
@_in = => @_in = (args...) =>
@_goingout = false @_goingout = false
@props.onFocusIn(args...) if @state.focused is false
@setState(focused: true) @setState(focused: true)
@_out = => @_out = =>
@ -41,6 +48,7 @@ class FocusTrackingRegion extends React.Component
# when the element receiving focus does not support selection (like a # when the element receiving focus does not support selection (like a
# div with tabIndex=-1) # div with tabIndex=-1)
document.getSelection().empty() document.getSelection().empty()
@props.onFocusOut() if @state.focused is true
@setState(focused: false) @setState(focused: false)
@_goingout = false @_goingout = false
, 100 , 100

View file

@ -169,6 +169,9 @@ DOMUtils =
return unless li return unless li
return DOMUtils.isFirstChild(li, anchor) return DOMUtils.isFirstChild(li, anchor)
# Selectors for input types
inputTypes: -> "input, textarea, *[contenteditable]"
# https://developer.mozilla.org/en-US/docs/Web/API/Element/closest # https://developer.mozilla.org/en-US/docs/Web/API/Element/closest
# Only Elements (not Text nodes) have the `closest` method # Only Elements (not Text nodes) have the `closest` method
closest: (node, selector) -> closest: (node, selector) ->