Mailspring/internal_packages/composer/lib/composer-participants.cjsx

237 lines
7.4 KiB
Plaintext
Raw Normal View History

fix(drafts): Various improvements and fixes to drafts, draft state management Summary: This diff contains a few major changes: 1. Scribe is no longer used for the text editor. It's just a plain contenteditable region. The toolbar items (bold, italic, underline) still work. Scribe was causing React inconcistency issues in the following scenario: - View thread with draft, edit draft - Move to another thread - Move back to thread with draft - Move to another thread. Notice that one or more messages from thread with draft are still there. There may be a way to fix this, but I tried for hours and there are Github Issues open on it's repository asking for React compatibility, so it may be fixed soon. For now contenteditable is working great. 2. Action.saveDraft() is no longer debounced in the DraftStore. Instead, firing that action causes the save to happen immediately, and the DraftStoreProxy has a new "DraftChangeSet" class which is responsbile for batching saves as the user interacts with the ComposerView. There are a couple big wins here: - In the future, we may want to be able to call Action.saveDraft() in other situations and it should behave like a normal action. We may also want to expose the DraftStoreProxy as an easy way of backing interactive draft UI. - Previously, when you added a contact to To/CC/BCC, this happened: <input> -> Action.saveDraft -> (delay!!) -> Database -> DraftStore -> DraftStoreProxy -> View Updates Increasing the delay to something reasonable like 200msec meant there was 200msec of lag before you saw the new view state. To fix this, I created a new class called DraftChangeSet which is responsible for accumulating changes as they're made and firing Action.saveDraft. "Adding" a change to the change set also causes the Draft provided by the DraftStoreProxy to change immediately (the changes are a temporary layer on top of the database object). This means no delay while changes are being applied. There's a better explanation in the source! This diff includes a few minor fixes as well: 1. Draft.state is gone—use Message.object = draft instead 2. String model attributes should never be null 3. Pre-send checks that can cancel draft send 4. Put the entire curl history and task queue into feedback reports 5. Cache localIds for extra speed 6. Move us up to latest React Test Plan: No new tests - once we lock down this new design I'll write tests for the DraftChangeSet Reviewers: evan Reviewed By: evan Differential Revision: https://review.inboxapp.com/D1125
2015-02-04 08:24:31 +08:00
React = require 'react/addons'
_ = require 'underscore-plus'
{CompositeDisposable} = require 'event-kit'
{Contact, ContactStore} = require 'inbox-exports'
ComposerParticipant = require './composer-participant.cjsx'
module.exports =
ComposerParticipants = React.createClass
getInitialState: ->
completions: []
selectedIndex: 0
currentEmail: ""
componentDidMount: ->
input = @refs.autocomplete.getDOMNode()
check = (fn) -> (event) ->
# Wrapper to guard against events triggering on the wrong element
fn(event) if event.target == input
@subscriptions = new CompositeDisposable()
@subscriptions.add atom.commands.add '.autocomplete',
'participants:move-up': (event) =>
@_onShiftSelectedIndex(-1)
event.preventDefault()
'participants:move-down': (event) =>
@_onShiftSelectedIndex(1)
event.preventDefault()
@subscriptions.add atom.commands.add '.autocomplete-with-suggestion',
'participants:add-suggestion': check @_onAddSuggestion
@subscriptions.add atom.commands.add '.autocomplete-no-suggestions',
'participants:add-raw-email': check @_onAddRawEmail
@subscriptions.add atom.commands.add '.autocomplete-empty',
'participants:remove': check @_onRemoveParticipant
@subscriptions.add atom.commands.add '.autocomplete',
'participants:cancel': check @_onParticipantsCancel
componentWillUnmount: ->
@subscriptions?.dispose()
componentDidUpdate: ->
input = @refs.autocomplete.getDOMNode()
# Absolutely place the completions field under the input
comp = @refs.completions.getDOMNode()
comp.style.top = input.offsetHeight + input.offsetTop + 6 + "px"
# Measure the width of the text in the input
measure = @refs.measure.getDOMNode()
measure.innerText = @_getInputValue()
measure.style.color = 'red'
measure.style.top = input.offsetTop + "px"
measure.style.left = input.offsetLeft + "px"
width = measure.offsetWidth
input.style.width = "calc(4px + #{width}px)"
render: ->
<span className={@_containerClasses()}
onClick={@_focusOnInput}>
<div className="participants-label">{"#{@props.placeholder}:"}</div>
<ul className="participants">
{@_currentParticipants()}
<span className={@state.focus and "hasFocus" or ""}>
<input name="add"
type="text"
ref="autocomplete"
onBlur={@_onBlur}
onFocus={@_onFocus}
onChange={@_onChange}
disabled={@props.disabled}
tabIndex={@props.tabIndex}
value={@state.currentEmail} />
<span ref="measure" style={
position: 'absolute'
visibility: 'hidden'
}/>
</span>
</ul>
<ul className="completions" ref='completions' style={@_completionsDisplay()}>
{@state.completions.map (p, i) =>
# Add a `seen` class if this participant is already in this field.
# We use CSS to grey it out.
# Add a `selected` class for the current selection.
# We use this instead of :hover so we can update selection with
# either mouse or keyboard.
classes = (_.compact [
p.email in _.pluck(@props.participants, 'email') and "seen",
(i+1) == @state.selectedIndex and 'selected'
]).join " "
<li
onMouseOver={=> @setState {selectedIndex: i+1}}
onMouseOut={=> @setState {selectedIndex: 0}}
onMouseDown={=> @_onMouseDown(p)}
onMouseUp={=> @_onMouseUp(p)}
key={"li-#{p.id}"}
className={classes}
><ComposerParticipant key={p.id} participant={p}/></li>}
</ul>
</span>
_currentParticipants: ->
@props.participants?.map (participant) =>
<li key={"participant-li-#{participant.id}"}
className={@_participantHoverClass(participant)}>
<ComposerParticipant key={"participant-#{participant.id}"}
participant={participant}
onRemove={@props.participantFunctions.remove}/>
</li>
_participantHoverClass: (participant) ->
React.addons.classSet
"hover": @_selected()?.email is participant.email
_containerClasses: ->
React.addons.classSet
"autocomplete": true
"increase-css-specificity": true
"autocomplete-empty": @state.currentEmail.trim().length is 0
"autocomplete-no-suggestions": @_noSuggestions()
"autocomplete-with-suggestion": @state.completions.length > 0
"autocomplete-looks-like-raw-email": @_looksLikeRawEmail()
_noSuggestions: ->
@state.completions.length is 0 and @state.currentEmail.trim().length > 0
_onBlur: ->
if @_cancelBlur then return
@_onAddRawEmail() if @_looksLikeRawEmail()
@setState
focus: false
selectedIndex: 0
_onParticipantsCancel: ->
@setState focus: false
@_clearSuggestions()
@refs.autocomplete.getDOMNode().blur()
_onFocus: ->
@_reloadSuggestions()
@setState focus: true
_onMouseDown: ->
@_cancelBlur = true
_onMouseUp: (participant) ->
@_cancelBlur = false
if participant?
@_addParticipant(participant)
# since the controlled input hasn't re-rendered yet, but we're
# going to fire a focus
@refs.autocomplete.getDOMNode().value = ""
@_focusOnInput()
_completionsDisplay: ->
if @state.completions.length > 0 and @state.focus
display: "initial"
else
display: "none"
_focusOnInput: ->
@refs.autocomplete.getDOMNode().focus()
_selected: ->
if @state.selectedIndex > 0 and @state.selectedIndex <= @state.completions.length
@state.completions[@state.selectedIndex - 1]
else
undefined
_onChange: (event) ->
@_reloadSuggestions()
_looksLikeRawEmail: ->
emailIsh = /.+@.+\..+/.test(@state.currentEmail.trim())
@state.completions.length is 0 and emailIsh
_onShiftSelectedIndex: (count) ->
newIndex = @state.selectedIndex + count
mod = @state.completions.length + 1
if (newIndex < 1)
newIndex = mod - (1 - (newIndex % mod))
else
if newIndex % mod is 0
newIndex = 1
else
newIndex = newIndex % mod
@setState
selectedIndex: newIndex
_onAddSuggestion: ->
participant = @_selected()
@_addParticipant(participant) if participant
_onAddRawEmail: ->
participants = (ContactStore.searchContacts(@_getInputValue()) ? [])
if participants[0]
@_addParticipant(participants[0])
else
newParticipant = new Contact(email: @_getInputValue())
@_addParticipant(newParticipant)
_addParticipant: (participant) ->
return if participant.email in _.pluck(@props.participants, 'email')
@props.participantFunctions.add participant
@_clearSuggestions()
_onRemoveParticipant: ->
if @props.participants.length > 0
@_removeParticipant _.last(@props.participants)
_removeParticipant: (participant) ->
@props.participantFunctions.remove participant
_clearSuggestions: ->
@setState
completions: []
selectedIndex: 0
currentEmail: ""
_reloadSuggestions: ->
val = @_getInputValue()
if val.length is 0 then completions = []
else completions = ContactStore.searchContacts val
@setState
completions: completions
currentEmail: val
selectedIndex: 1
_getInputValue: ->
(@refs.autocomplete.getDOMNode().value ? "").trimLeft()