2015-05-20 07:06:59 +08:00
_ = require 'underscore'
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'
2015-09-23 07:02:44 +08:00
{Utils,
DOMUtils,
DraftStore} = require 'nylas-exports'
ClipboardService = require './clipboard-service'
FloatingToolbarContainer = require './floating-toolbar-container'
2015-03-18 07:19:40 +08:00
2015-04-25 02:33:10 +08:00
class ContenteditableComponent extends React.Component
2015-09-23 07:02:44 +08:00
@displayName: "ContenteditableComponent"
@propTypes:
2015-03-06 08:00:56 +08:00
html: React.PropTypes.string
2015-03-18 07:19:40 +08:00
initialSelectionSnapshot: React.PropTypes.object
2015-03-06 08:00:56 +08:00
2015-09-23 07:40:33 +08:00
filters: React.PropTypes.array
2015-09-23 07:02:44 +08:00
footerElements: React.PropTypes.node
2015-05-20 07:12:39 +08:00
# Passes an absolute top coordinate to scroll to.
2015-09-23 07:02:44 +08:00
onChange: React.PropTypes.func.isRequired
onFilePaste: React.PropTypes.func
2015-07-31 09:29:38 +08:00
onScrollTo: React.PropTypes.func
onScrollToBottom: React.PropTypes.func
2015-05-20 07:12:39 +08:00
2015-09-23 07:02:44 +08:00
@defaultProps:
filters: []
2015-04-25 02:33:10 +08:00
constructor: (@props) ->
2015-09-23 07:02:44 +08:00
@innerState = {}
@_setupServices(@props)
_setupServices: (props) ->
@clipboardService = new ClipboardService
onFilePaste: props.onFilePaste
setInnerState: (innerState={}) ->
@innerState = _.extend @innerState, innerState
@refs["toolbarController"]?.componentWillReceiveInnerProps(innerState)
2015-02-19 06:24:16 +08:00
2015-04-25 02:33:10 +08:00
componentDidMount: =>
2015-03-28 01:12:56 +08:00
@_editableNode().addEventListener('contextmenu', @_onShowContextualMenu)
2015-03-03 07:33:58 +08:00
@_setupSelectionListeners()
2015-03-20 08:16:38 +08:00
@_setupGlobalMouseListener()
2015-02-19 06:24:16 +08:00
2015-03-21 01:23:50 +08:00
@_disposable = atom.commands.add '.contenteditable-container *', {
'core:focus-next': (event) =>
editableNode = @_editableNode()
2015-09-23 07:02:44 +08:00
range = DOMUtils.getRangeInScope(editableNode)
2015-03-21 01:23:50 +08:00
for extension in DraftStore.extensions()
extension.onFocusNext(editableNode, range, event) if extension.onFocusNext
2015-06-06 02:02:44 +08:00
2015-03-21 01:23:50 +08:00
'core:focus-previous': (event) =>
editableNode = @_editableNode()
2015-09-23 07:02:44 +08:00
range = DOMUtils.getRangeInScope(editableNode)
2015-03-21 01:23:50 +08:00
for extension in DraftStore.extensions()
extension.onFocusPrevious(editableNode, range, event) if extension.onFocusPrevious
2015-06-06 02:02:44 +08:00
}
2015-03-21 01:23:50 +08:00
2015-05-20 07:12:39 +08:00
@_cleanHTML()
2015-09-23 07:02:44 +08:00
@setInnerState editableNode: @_editableNode()
2015-05-20 07:12:39 +08:00
shouldComponentUpdate: (nextProps, nextState) ->
not Utils.isEqualReact(nextProps, @props) or
not Utils.isEqualReact(nextState, @state)
2015-04-25 02:33:10 +08:00
componentWillUnmount: =>
2015-03-28 01:12:56 +08:00
@_editableNode().removeEventListener('contextmenu', @_onShowContextualMenu)
2015-03-03 07:33:58 +08:00
@_teardownSelectionListeners()
2015-03-20 08:16:38 +08:00
@_teardownGlobalMouseListener()
2015-03-21 01:23:50 +08:00
@_disposable.dispose()
2015-03-03 07:33:58 +08:00
2015-04-25 02:33:10 +08:00
componentWillReceiveProps: (nextProps) =>
2015-09-23 07:02:44 +08:00
@_setupServices(nextProps)
2015-03-03 07:33:58 +08:00
if nextProps.initialSelectionSnapshot?
@_setSelectionSnapshot(nextProps.initialSelectionSnapshot)
2015-04-25 02:33:10 +08:00
componentDidUpdate: =>
2015-05-20 07:12:39 +08:00
@_cleanHTML()
2015-09-23 07:02:44 +08:00
2015-03-03 07:33:58 +08:00
@_restoreSelection()
2015-02-19 06:24:16 +08:00
2015-09-12 02:12:25 +08:00
editableNode = @_editableNode()
for extension in DraftStore.extensions()
2015-09-23 07:02:44 +08:00
extension.onComponentDidUpdate(@_editableNode()) if extension.onComponentDidUpdate
@setInnerState
links: editableNode.querySelectorAll("*[href]")
editableNode: editableNode
2015-09-12 02:12:25 +08:00
2015-04-25 02:33:10 +08:00
render: =>
2015-02-19 06:24:16 +08:00
<div className="contenteditable-container">
2015-09-23 07:02:44 +08:00
<FloatingToolbarContainer
ref="toolbarController"
onSaveUrl={@_onSaveUrl}
onDomMutator={@_onDomMutator} />
2015-02-19 06:24:16 +08:00
<div id="contenteditable"
ref="contenteditable"
contentEditable
2015-09-03 04:20:01 +08:00
spellCheck={false}
2015-03-03 07:33:58 +08:00
onBlur={@_onBlur}
2015-09-14 22:37:00 +08:00
onFocus={@_onFocus}
2015-06-24 06:21:25 +08:00
onClick={@_onClick}
2015-09-23 07:02:44 +08:00
onPaste={@clipboardService.onPaste}
2015-03-03 07:33:58 +08:00
onInput={@_onInput}
2015-08-20 04:26:11 +08:00
onKeyDown={@_onKeyDown}
2015-03-03 07:33:58 +08:00
dangerouslySetInnerHTML={@_dangerouslySetInnerHTML()}></div>
2015-09-23 07:02:44 +08:00
{@props.footerElements}
2015-02-19 06:24:16 +08:00
</div>
2015-04-25 02:33:10 +08:00
focus: =>
@_editableNode().focus()
2015-02-19 06:24:16 +08:00
2015-06-24 06:21:25 +08:00
selectEnd: =>
range = document.createRange()
range.selectNodeContents(@_editableNode())
range.collapse(false)
@_editableNode().focus()
selection = window.getSelection()
selection.removeAllRanges()
selection.addRange(range)
2015-09-23 07:02:44 +08:00
# When some other component (like the `FloatingToolbar` or some
# `DraftStoreExtension`) wants to mutate the DOM, it declares a
# `mutator` function. That mutator expects to be passed the latest DOM
# object (the `_editableNode()`) and will do mutations to it. Once those
# mutations are done, we need to be sure to notify that changes
# happened.
_onDomMutator: (mutator) =>
@_teardownSelectionListeners()
mutator(@_editableNode())
@_setupSelectionListeners()
@_onInput()
2015-06-24 06:21:25 +08:00
_onClick: (event) ->
# We handle mouseDown, mouseMove, mouseUp, but we want to stop propagation
# of `click` to make it clear that we've handled the event.
# Note: Related to composer-view#_onClickComposeBody
event.stopPropagation()
2015-08-20 04:26:11 +08:00
_onKeyDown: (event) =>
if event.key is "Tab"
@_onTabDown(event)
2015-09-23 07:02:44 +08:00
if event.key is "Backspace"
@_onBackspaceDown(event)
U = 85
if event.which is U and (event.metaKey or event.ctrlKey)
event.preventDefault()
document.execCommand("underline")
2015-08-20 04:26:11 +08:00
return
2015-09-23 07:02:44 +08:00
_onInput: (event) =>
2015-08-20 04:26:11 +08:00
return if @_ignoreInputChanges
@_ignoreInputChanges = true
2015-09-23 07:02:44 +08:00
@_resetInnerStateOnInput()
2015-05-16 01:45:18 +08:00
2015-08-20 04:26:11 +08:00
@_runCoreFilters()
2015-09-23 07:02:44 +08:00
@_runExtensionFilters(event)
2015-05-20 07:12:39 +08:00
2015-09-23 07:02:44 +08:00
@_normalize()
2015-05-20 07:12:39 +08:00
@_saveSelectionState()
2015-09-23 07:02:44 +08:00
@_saveNewHtml()
2015-08-20 04:26:11 +08:00
@_ignoreInputChanges = false
return
2015-09-23 07:02:44 +08:00
_resetInnerStateOnInput: ->
@_justCreatedList = false
@setInnerState dragging: false if @innerState.dragging
@setInnerState doubleDown: false if @innerState.doubleDown
2015-08-20 04:26:11 +08:00
_runCoreFilters: ->
@_createLists()
2015-09-23 07:02:44 +08:00
_runExtensionFilters: (event) ->
for extension in DraftStore.extensions()
extension.onInput(@_editableNode(), event) if extension.onInput
_saveNewHtml: ->
html = @_editableNode().innerHTML
for filter in @props.filters
html = filter.afterDisplay(html)
@props.onChange(target: {value: html})
2015-08-20 04:26:11 +08:00
# Determines if the user wants to add an ordered or unordered list.
_createLists: ->
# The `execCommand` will update the DOM and move the cursor. Since
# this is happening in the middle of an `_onInput` callback, we want
# the whole operation to look "atomic". As such we'll do any necessary
# DOM cleanup and fire the `exec` command with the listeners off, then
# re-enable at the end.
2015-09-23 07:02:44 +08:00
if @_resetListToText
@_resetListToText = false
return
2015-08-20 04:26:11 +08:00
updateDOM = (command) =>
@_teardownSelectionListeners()
document.execCommand(command)
selection = document.getSelection()
selection.anchorNode.parentElement.innerHTML = ""
@_setupSelectionListeners()
text = @_textContentAtCursor()
if (/^\d\.\s$/).test text
2015-09-23 07:02:44 +08:00
@_justCreatedList = text
2015-08-20 04:26:11 +08:00
updateDOM("insertOrderedList")
2015-09-01 07:55:01 +08:00
else if (/^[*-]\s$/).test text
2015-09-23 07:02:44 +08:00
@_justCreatedList = text
2015-08-20 04:26:11 +08:00
updateDOM("insertUnorderedList")
2015-09-23 07:02:44 +08:00
_onBackspaceDown: (event) ->
if document.getSelection()?.isCollapsed
if @_atStartOfList()
li = @_closestAtCursor("li")
list = @_closestAtCursor("ul, ol")
return unless li and list
event.preventDefault()
if list.querySelectorAll('li')?[0] is li # We're in first li
if @_justCreatedList
@_resetListToText = true
@_replaceFirstListItem(li, @_justCreatedList)
else
@_replaceFirstListItem(li, "")
else
document.execCommand("outdent")
_closestAtCursor: (selector) ->
selection = document.getSelection()
return unless selection?.isCollapsed
return selection.anchorNode?.closest(selector)
_replaceFirstListItem: (li, replaceWith) ->
@_teardownSelectionListeners()
list = li.closest("ul, ol")
if replaceWith.length is 0
replaceWith = replaceWith.replace /\s/g, " "
text = document.createElement("div")
text.innerHTML = "<br>"
else
replaceWith = replaceWith.replace /\s/g, " "
text = document.createElement("span")
text.innerHTML = "#{replaceWith}"
if list.querySelectorAll('li').length <= 1
# Delete the whole list and replace with text
list.parentNode.replaceChild(text, list)
else
# Delete the list item and prepend the text before the rest of the
# list
li.parentNode.removeChild(li)
list.parentNode.insertBefore(text, list)
child = text.childNodes[0] ? text
index = Math.max(replaceWith.length - 1, 0)
selection = document.getSelection()
selection.setBaseAndExtent(child, index, child, index)
@_setupSelectionListeners()
@_onInput()
2015-08-20 04:26:11 +08:00
_onTabDown: (event) ->
event.preventDefault()
selection = document.getSelection()
if selection?.isCollapsed
# https://developer.mozilla.org/en-US/docs/Web/API/Element/closest
2015-09-01 07:55:01 +08:00
if selection.anchorNode instanceof HTMLElement
anchorElement = selection.anchorNode
else
anchorElement = selection.anchorNode.parentElement
# Only Elements (not Text nodes) have the `closest` method
if anchorElement.closest("li")
2015-08-20 04:26:11 +08:00
if event.shiftKey
document.execCommand("outdent")
else
document.execCommand("indent")
return
2015-09-01 07:55:01 +08:00
else if event.shiftKey
2015-09-14 22:37:00 +08:00
if @_atTabChar()
@_removeLastCharacter()
else if @_atBeginning()
return # Don't stop propagation
2015-09-01 07:55:01 +08:00
else
document.execCommand("insertText", false, "\t")
else
if event.shiftKey
document.execCommand("insertText", false, "")
else
document.execCommand("insertText", false, "\t")
2015-09-14 22:37:00 +08:00
event.stopPropagation()
2015-09-01 07:55:01 +08:00
_selectionInText: (selection) ->
return false unless selection
return selection.isCollapsed and selection.anchorNode.nodeType is Node.TEXT_NODE and selection.anchorOffset > 0
_atTabChar: ->
selection = document.getSelection()
if @_selectionInText(selection)
return selection.anchorNode.textContent[selection.anchorOffset - 1] is "\t"
else return false
2015-09-23 07:02:44 +08:00
_atStartOfList: ->
selection = document.getSelection()
anchor = selection.anchorNode
return false if not selection.isCollapsed
return true if anchor?.nodeName is "LI"
return false if selection.anchorOffset > 0
li = anchor.closest("li")
return unless li
return DOMUtils.isFirstChild(li, anchor)
2015-09-14 22:37:00 +08:00
_atBeginning: ->
selection = document.getSelection()
return false if not selection.isCollapsed
return false if selection.anchorOffset > 0
el = @_editableNode()
return true if el.childNodes.length is 0
return true if selection.anchorNode is el
firstChild = el.childNodes[0]
return selection.anchorNode is firstChild
2015-09-01 07:55:01 +08:00
_removeLastCharacter: ->
selection = document.getSelection()
if @_selectionInText(selection)
node = selection.anchorNode
offset = selection.anchorOffset
@_teardownSelectionListeners()
selection.setBaseAndExtent(node, offset - 1, node, offset)
document.execCommand("delete")
@_setupSelectionListeners()
2015-08-20 04:26:11 +08:00
_textContentAtCursor: ->
selection = document.getSelection()
if selection.isCollapsed
2015-08-20 04:35:12 +08:00
return selection.anchorNode?.textContent
2015-08-20 04:26:11 +08:00
else return null
2015-05-20 07:12:39 +08:00
# This component works by re-rendering on every change and restoring the
# selection. This is also how standard React controlled inputs work too.
#
2015-09-12 02:12:25 +08:00
# Since the contents of the contenteditable are complex, nested DOM
2015-05-20 07:12:39 +08:00
# structures, a simple replacement of the DOM is not easy. There are a
# variety of edge cases that we need to correct for and prepare both the
# HTML and the selection to be serialized without error.
2015-09-23 07:02:44 +08:00
_normalize: ->
2015-05-20 07:12:39 +08:00
@_cleanHTML()
@_cleanSelection()
# We need to clean the HTML on input to fix several edge cases that
# arise when we go to save the selection state and restore it on the
# next render.
_cleanHTML: ->
return unless @_editableNode()
# One issue is that we need to pre-normalize the HTML so it looks the
# same after it gets re-inserted. If we key selection markers off of an
# non normalized DOM, then they won't match up when the HTML gets reset.
#
2015-05-16 01:45:18 +08:00
# The Node.normalize() method puts the specified node and all of its
# sub-tree into a "normalized" form. In a normalized sub-tree, no text
# nodes in the sub-tree are empty and there are no adjacent text
# nodes.
2015-05-20 07:12:39 +08:00
@_editableNode().normalize()
2015-09-23 07:02:44 +08:00
@_collapseAdjacentLists()
2015-05-20 07:12:39 +08:00
@_fixLeadingBRCondition()
# An issue arises from <br/> tags immediately inside of divs. In this
# case the cursor's anchor node will not be the <br/> tag, but rather
# the entire enclosing element. Sometimes, that enclosing element is the
# container wrapping all of the content. The browser has a native
# built-in feature that will automatically scroll the page to the bottom
# of the current element that the cursor is in if the cursor is off the
# screen. In the given case, that element is the whole div. The net
# effect is that the browser will scroll erroneously to the bottom of
# the whole content div, which is likely NOT where the cursor is or the
# user wants. The solution to this is to replace this particular case
# with <span></span> tags and place the cursor in there.
_fixLeadingBRCondition: ->
treeWalker = document.createTreeWalker @_editableNode()
while treeWalker.nextNode()
currentNode = treeWalker.currentNode
if @_hasLeadingBRCondition(currentNode)
newNode = document.createElement("div")
newNode.appendChild(document.createElement("br"))
currentNode.replaceChild(newNode, currentNode.childNodes[0])
return
_hasLeadingBRCondition: (node) ->
childNodes = node.childNodes
return childNodes.length >= 2 and childNodes[0].nodeName is "BR"
2015-09-23 07:02:44 +08:00
# If users ended up with two <ul> lists adjacent to each other, we
# collapse them into one. We leave adjacent <ol> lists intact in case
# the user wanted to restart the numbering sequence
_collapseAdjacentLists: ->
els = @_editableNode().querySelectorAll('ul')
# This mutates the DOM in place.
DOMUtils.collapseAdjacentElements(els)
2015-05-20 07:12:39 +08:00
# After an input, the selection can sometimes get itself into a state
# that either can't be restored properly, or will cause undersirable
# native behavior. This method, in combination with `_cleanHTML`, fixes
# each of those scenarios before we save and later restore the
# selection.
_cleanSelection: ->
selection = document.getSelection()
return unless selection.anchorNode? and selection.focusNode?
2015-03-21 01:23:50 +08:00
2015-05-20 07:12:39 +08:00
# The _unselectableNode case only is valid when it's at the very top
# (offset 0) of the node. If the offsets are > 0 that means we're
# trying to select somewhere within some sort of containing element.
# This is okay to do. The odd case only arises at the top of
# unselectable elements.
return if selection.anchorOffset > 0 or selection.focusOffset > 0
if selection.isCollapsed and @_unselectableNode(selection.focusNode)
@_teardownSelectionListeners()
treeWalker = document.createTreeWalker(selection.focusNode)
while treeWalker.nextNode()
currentNode = treeWalker.currentNode
if @_unselectableNode(currentNode)
selection.setBaseAndExtent(currentNode, 0, currentNode, 0)
break
@_setupSelectionListeners()
return
2015-03-21 01:23:50 +08:00
2015-05-20 07:12:39 +08:00
_unselectableNode: (node) ->
return true if not node
2015-09-23 07:02:44 +08:00
if node.nodeType is Node.TEXT_NODE and DOMUtils.isBlankTextNode(node)
2015-05-20 07:12:39 +08:00
return true
else if node.nodeType is Node.ELEMENT_NODE
child = node.firstChild
return true if not child
2015-09-23 07:02:44 +08:00
hasText = (child.nodeType is Node.TEXT_NODE and not DOMUtils.isBlankTextNode(node))
2015-05-20 07:12:39 +08:00
hasBr = (child.nodeType is Node.ELEMENT_NODE and node.nodeName is "BR")
return not hasText and not hasBr
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
2015-05-20 07:12:39 +08:00
else return false
2015-03-28 07:35:27 +08:00
2015-04-25 02:33:10 +08:00
_onBlur: (event) =>
2015-09-23 07:02:44 +08:00
# console.log "On Blur Contenteditable"
@setInnerState dragging: false
2015-03-03 07:33:58 +08:00
# The delay here is necessary to see if the blur was caused by us
# navigating to the toolbar and focusing on the set-url input.
_.delay =>
2015-09-23 07:02:44 +08:00
@setInnerState editableFocused: false
2015-03-03 07:33:58 +08:00
, 50
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
2015-09-14 22:37:00 +08:00
_onFocus: (event) =>
2015-09-23 07:02:44 +08:00
@setInnerState editableFocused: true
2015-09-14 22:37:00 +08:00
@props.onFocus?(event)
2015-04-29 06:32:15 +08:00
_editableNode: =>
React.findDOMNode(@refs.contenteditable)
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
2015-04-25 02:33:10 +08:00
_dangerouslySetInnerHTML: =>
2015-09-23 07:02:44 +08:00
html = @props.html
for filter in @props.filters
html = filter.beforeDisplay(html)
return __html: html
2015-03-03 07:33:58 +08:00
######### SELECTION MANAGEMENT ##########
#
# Saving and restoring a selection is difficult with React.
#
# React only handles Input and Textarea elements:
# https://github.com/facebook/react/blob/master/src/browser/ui/ReactInputSelection.js
# This is because they expose a very convenient `selectionStart` and
# `selectionEnd` integer.
#
# Contenteditable regions are trickier. They require the more
# sophisticated `Range` and `Selection` APIs.
#
# Range docs:
# http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html
#
# Selection API docs:
# http://www.w3.org/TR/selection-api/#dfn-range
#
# A Contenteditable region can have arbitrary html inside of it. This
# means that a selection start point can be some node (the `anchorNode`)
# and its end point can be a completely different node (the `focusNode`)
#
# When React re-renders, all of the DOM nodes may change. They may
# look exactly the same, but have different object references.
#
# This means that your old references to `anchorNode` and `focusNode`
# may be bad and no longer in scope or painted.
#
# In order to restore the selection properly we need to re-find the
# equivalent `anchorNode` and `focusNode`. Luckily we can use the
# `isEqualNode` method to get a shallow comparison of the nodes.
#
# Unfortunately it's possible for `isEqualNode` to match more than one
# node since two nodes may look very similar.
#
# To fix this we need to keep track of the original indices to determine
# which node is most likely the matching one.
# http://www.w3.org/TR/selection-api/#selectstart-event
2015-04-25 02:33:10 +08:00
_setupSelectionListeners: =>
2015-09-01 07:55:01 +08:00
@_ignoreInputChanges = false
2015-07-31 09:29:38 +08:00
document.addEventListener("selectionchange", @_saveSelectionState)
2015-03-03 07:33:58 +08:00
2015-04-25 02:33:10 +08:00
_teardownSelectionListeners: =>
2015-07-31 09:29:38 +08:00
document.removeEventListener("selectionchange", @_saveSelectionState)
2015-09-01 07:55:01 +08:00
@_ignoreInputChanges = true
2015-03-03 07:33:58 +08:00
2015-04-25 02:33:10 +08:00
getCurrentSelection: => _.clone(@_selection ? {})
getPreviousSelection: => _.clone(@_previousSelection ? {})
2015-03-03 07:33:58 +08:00
# Every time the cursor changes we need to preserve its location and
# state.
#
# We can't use React's `state` variable because cursor position is not
2015-03-18 07:19:40 +08:00
# naturally supported in the virtual DOM.
2015-03-03 07:33:58 +08:00
#
# We also need to make sure that node references are cloned so they
# don't change out from underneath us.
#
# We also need to keep references to the previous selection state in
# order for undo/redo to work properly.
#
# We need to be sure to deeply `cloneNode`. This is because sometimes
# our anchorNodes are divs with nested <br> tags. If we don't do a deep
# clone then when `isEqualNode` is run it will erroneously return false
2015-05-16 01:45:18 +08:00
# and our selection restoration will fail.
#
# The Selection API has the concept of an `anchorNode` and a
# `focusNode`. The `anchorNode` is where the selection started from and
# does not move. The `focusNode` is where the end of the selection
# currently is and may move. A "caret" is simply a selection whose
# anchorNode == focusNode and anchorOffset == focusOffset.
#
# An `anchorNode` is also known as a `startNode`, or `baseNode`. We use
# the alias `startNode` since I think it makes more intuitive sense.
#
# A `focusNode` is also known as an `endNode` or `focusNode`. I use the
# `endNode` alias since it makes more inuitive sense.
2015-05-20 07:12:39 +08:00
#
# When we restore the selection later, we need to find a node that looks
# the same as the one we saved (since they're different object
# references). Unfortunately there many be many nodes that "look" the
# same (match the `isEqualNode`) test. For example, say I have a bunch
# of lines with the TEXT_NODE "Foo". All of those will match
# `isEqualNode`. To fix this we assume there will be multiple matches
# and keep track of the index of the match. e.g. all "Foo" TEXT_NODEs
# may look alike, but I know I want the Nth "Foo" TEXT_NODE. We store
# this information in the `startNodeIndex` and `endNodeIndex` fields via
2015-09-23 07:02:44 +08:00
# the `DOMUtils.getNodeIndex` method.
2015-05-20 07:12:39 +08:00
_saveSelectionState: =>
2015-03-03 07:33:58 +08:00
selection = document.getSelection()
2015-09-23 07:02:44 +08:00
context = @_editableNode()
return if DOMUtils.isSameSelection(selection, @_selection, context)
2015-05-20 07:12:39 +08:00
return unless selection.anchorNode? and selection.focusNode?
2015-09-23 07:02:44 +08:00
return unless DOMUtils.selectionInScope(selection, context)
2015-03-03 07:33:58 +08:00
@_previousSelection = @_selection
2015-03-18 07:19:40 +08:00
2015-03-03 07:33:58 +08:00
@_selection =
2015-05-20 07:12:39 +08:00
startNode: selection.anchorNode.cloneNode(true)
2015-05-16 01:45:18 +08:00
startOffset: selection.anchorOffset
2015-09-23 07:02:44 +08:00
startNodeIndex: DOMUtils.getNodeIndex(context, selection.anchorNode)
2015-05-16 01:45:18 +08:00
endNode: selection.focusNode.cloneNode(true)
endOffset: selection.focusOffset
2015-09-23 07:02:44 +08:00
endNodeIndex: DOMUtils.getNodeIndex(context, selection.focusNode)
2015-03-18 07:19:40 +08:00
isCollapsed: selection.isCollapsed
2015-03-03 07:33:58 +08:00
2015-07-31 09:29:38 +08:00
@_ensureSelectionVisible(selection)
2015-03-03 07:33:58 +08:00
2015-09-23 07:02:44 +08:00
@setInnerState
selection: @_selection
editableFocused: true
2015-05-20 07:12:39 +08:00
2015-09-23 07:02:44 +08:00
return @_selection
2015-05-20 07:12:39 +08:00
2015-04-25 02:33:10 +08:00
_setSelectionSnapshot: (selection) =>
2015-03-18 07:19:40 +08:00
@_previousSelection = @_selection
@_selection = selection
2015-09-23 07:02:44 +08:00
@setInnerState
selection: @_selection
editableFocused: true
2015-05-20 07:12:39 +08:00
# When the selectionState gets set by a parent (e.g. undo-ing and
# redo-ing) we need to make sure it's visible to the user.
#
# Unfortunately, we can't use the native `scrollIntoView` because it
# naively scrolls the whole window and doesn't know not to scroll if
# it's already in view. There's a new native method called
# `scrollIntoViewIfNeeded`, but this only works when the scroll
# container is a direct parent of the requested element. In this case
# the scroll container may be many levels up.
_ensureSelectionVisible: (selection) ->
2015-07-31 09:29:38 +08:00
# If our parent supports scroll to bottom, check for that
2015-09-23 07:02:44 +08:00
if @props.onScrollToBottom and DOMUtils.atEndOfContent(selection, @_editableNode())
2015-07-31 09:29:38 +08:00
@props.onScrollToBottom()
# Don't bother computing client rects if no scroll method has been provided
else if @props.onScrollTo
2015-09-23 07:02:44 +08:00
rangeInScope = DOMUtils.getRangeInScope(@_editableNode())
2015-08-19 01:31:18 +08:00
return unless rangeInScope
rect = rangeInScope.getBoundingClientRect()
2015-09-23 07:02:44 +08:00
if DOMUtils.isEmptyBoudingRect(rect)
2015-07-31 09:29:38 +08:00
rect = @_getSelectionRectFromDOM(selection)
if rect
@props.onScrollTo({rect})
2015-09-23 07:02:44 +08:00
# The bounding client rect has changed
@setInnerState editableNode: @_editableNode()
2015-05-20 07:12:39 +08:00
2015-07-31 09:29:38 +08:00
_getSelectionRectFromDOM: (selection) ->
2015-05-20 07:12:39 +08:00
node = selection.anchorNode
if node.nodeType is Node.TEXT_NODE
r = document.createRange()
r.selectNodeContents(node)
2015-07-31 09:29:38 +08:00
return r.getBoundingClientRect()
2015-05-20 07:12:39 +08:00
else if node.nodeType is Node.ELEMENT_NODE
2015-07-31 09:29:38 +08:00
return node.getBoundingClientRect()
2015-05-20 07:12:39 +08:00
else
2015-07-31 09:29:38 +08:00
return null
2015-03-20 08:16:38 +08:00
# We use global listeners to determine whether or not dragging is
# happening. This is because dragging may stop outside the scope of
# this element. Note that the `dragstart` and `dragend` events don't
# detect text selection. They are for drag & drop.
2015-04-25 02:33:10 +08:00
_setupGlobalMouseListener: =>
2015-03-20 08:16:38 +08:00
@__onMouseDown = _.bind(@_onMouseDown, @)
@__onMouseMove = _.bind(@_onMouseMove, @)
@__onMouseUp = _.bind(@_onMouseUp, @)
window.addEventListener("mousedown", @__onMouseDown)
window.addEventListener("mouseup", @__onMouseUp)
2015-03-21 01:23:50 +08:00
2015-04-25 02:33:10 +08:00
_teardownGlobalMouseListener: =>
2015-03-20 08:16:38 +08:00
window.removeEventListener("mousedown", @__onMouseDown)
window.removeEventListener("mouseup", @__onMouseUp)
2015-04-25 02:33:10 +08:00
_onShowContextualMenu: (event) =>
2015-09-23 07:02:44 +08:00
@refs["toolbarController"]?.forceClose()
2015-03-28 01:12:56 +08:00
event.preventDefault()
selection = document.getSelection()
range = selection.getRangeAt(0)
2015-09-11 01:53:27 +08:00
# On Windows, right-clicking a word does not select it at the OS-level.
# We need to implement this behavior locally for the rest of the logic here.
if range.collapsed
DOMUtils.selectWordContainingRange(range)
range = selection.getRangeAt(0)
2015-03-28 01:12:56 +08:00
text = range.toString()
remote = require('remote')
clipboard = require('clipboard')
Menu = remote.require('menu')
MenuItem = remote.require('menu-item')
spellchecker = require('spellchecker')
2015-04-25 02:33:10 +08:00
apply = (newtext) =>
2015-03-28 01:12:56 +08:00
range.deleteContents()
node = document.createTextNode(newtext)
range.insertNode(node)
range.selectNode(node)
selection.removeAllRanges()
selection.addRange(range)
2015-09-03 04:20:01 +08:00
for extension in DraftStore.extensions()
if extension.onSubstitutionPerformed
extension.onSubstitutionPerformed(@_editableNode())
2015-03-28 01:12:56 +08:00
2015-09-09 08:52:26 +08:00
learnSpelling = =>
spellchecker.add(text)
for extension in DraftStore.extensions()
if extension.onLearnSpelling
extension.onLearnSpelling(@_editableNode(), text)
2015-04-25 02:33:10 +08:00
cut = =>
2015-03-28 01:12:56 +08:00
clipboard.writeText(text)
apply('')
2015-04-25 02:33:10 +08:00
copy = =>
2015-03-28 01:12:56 +08:00
clipboard.writeText(text)
2015-04-25 02:33:10 +08:00
paste = =>
2015-03-28 01:12:56 +08:00
apply(clipboard.readText())
menu = new Menu()
if spellchecker.isMisspelled(text)
corrections = spellchecker.getCorrectionsForMisspelling(text)
if corrections.length > 0
corrections.forEach (correction) ->
menu.append(new MenuItem({ label: correction, click:( -> apply(correction))}))
2015-09-09 08:52:26 +08:00
else
menu.append(new MenuItem({ label: 'No Guesses Found', enabled: false}))
menu.append(new MenuItem({ type: 'separator' }))
menu.append(new MenuItem({ label: 'Learn Spelling', click: learnSpelling}))
menu.append(new MenuItem({ type: 'separator' }))
2015-03-28 01:12:56 +08:00
menu.append(new MenuItem({ label: 'Cut', click:cut}))
menu.append(new MenuItem({ label: 'Copy', click:copy}))
menu.append(new MenuItem({ label: 'Paste', click:paste}))
menu.popup(remote.getCurrentWindow())
2015-05-16 01:56:40 +08:00
2015-04-25 02:33:10 +08:00
_onMouseDown: (event) =>
2015-03-20 08:16:38 +08:00
@_mouseDownEvent = event
@_mouseHasMoved = false
window.addEventListener("mousemove", @__onMouseMove)
2015-03-21 01:23:50 +08:00
2015-03-24 22:40:48 +08:00
# We can't use the native double click event because that only fires
# on the second up-stroke
if Date.now() - (@_lastMouseDown ? 0) < 250
@_onDoubleDown(event)
@_lastMouseDown = 0 # to prevent triple down
else
@_lastMouseDown = Date.now()
2015-04-25 02:33:10 +08:00
_onDoubleDown: (event) =>
2015-04-29 06:32:15 +08:00
editable = @_editableNode()
return unless editable?
2015-03-24 22:40:48 +08:00
if editable is event.target or editable.contains(event.target)
2015-09-23 07:02:44 +08:00
@setInnerState doubleDown: true
2015-03-24 22:40:48 +08:00
2015-04-25 02:33:10 +08:00
_onMouseMove: (event) =>
2015-03-20 08:16:38 +08:00
if not @_mouseHasMoved
@_onDragStart(@_mouseDownEvent)
@_mouseHasMoved = true
2015-03-21 01:23:50 +08:00
2015-04-25 02:33:10 +08:00
_onMouseUp: (event) =>
2015-03-20 08:16:38 +08:00
window.removeEventListener("mousemove", @__onMouseMove)
2015-03-21 01:23:50 +08:00
2015-09-23 07:02:44 +08:00
if @innerState.doubleDown
@setInnerState doubleDown: false
2015-03-24 22:40:48 +08:00
2015-03-20 08:16:38 +08:00
if @_mouseHasMoved
@_mouseHasMoved = false
@_onDragEnd(event)
2015-03-21 01:23:50 +08:00
editableNode = @_editableNode()
selection = document.getSelection()
2015-09-23 07:02:44 +08:00
return event unless DOMUtils.selectionInScope(selection, editableNode)
2015-03-21 01:23:50 +08:00
2015-09-23 07:02:44 +08:00
range = DOMUtils.getRangeInScope(editableNode)
2015-03-21 01:23:50 +08:00
if range
try
for extension in DraftStore.extensions()
extension.onMouseUp(editableNode, range, event) if extension.onMouseUp
catch e
console.log('DraftStore extension raised an error: '+e.toString())
2015-03-24 22:40:48 +08:00
2015-03-21 01:23:50 +08:00
event
2015-04-25 02:33:10 +08:00
_onDragStart: (event) =>
2015-04-29 06:32:15 +08:00
editable = @_editableNode()
return unless editable?
2015-03-20 08:16:38 +08:00
if editable is event.target or editable.contains(event.target)
2015-09-23 07:02:44 +08:00
@setInnerState dragging: true
2015-03-20 08:16:38 +08:00
2015-04-25 02:33:10 +08:00
_onDragEnd: (event) =>
2015-09-23 07:02:44 +08:00
if @innerState.dragging
@setInnerState dragging: false
2015-03-21 01:23:50 +08:00
return event
2015-03-03 07:33:58 +08:00
2015-05-16 01:45:18 +08:00
# We restore the Selection via the `setBaseAndExtent` property of the
# `Selection` API
#
# See http://w3c.github.io/selection-api/#widl-Selection-setBaseAndExtent-void-Node-anchorNode-unsigned-long-anchorOffset-Node-focusNode-unsigned-long-focusOffset
#
# Since the last time we saved the `@_selection`, the DOM may have
# completely changed due to a re-render. To the user it may look
# identical, but the newly rendered region may be comprised of
# completely new DOM nodes. Our old node references may not exist
# anymore. As such, we have the task of re-finding the nodes again and
# creating a new selection that matches as accurately as possible.
#
# There are multiple ways of setting a new selection with the Selection
# API. One very common one is to create a new Range object and then call
# `addRange` on a selection instance. This does NOT work for us because
# `Range` objects are direction-less. A Selection's start node (aka
# anchor node aka base node) can be "after" a selection's end node (aka
# focus node aka extent node).
2015-03-03 07:33:58 +08:00
#
# force - when set to true it will not care whether or not the selection
# is already in the box. Normally we only restore when the
# contenteditable is in focus
# collapse - Can either be "end" or "start". When we reset the
# selection, we'll collapse the range into a single caret
# position
2015-04-25 02:33:10 +08:00
_restoreSelection: ({force, collapse}={}) =>
2015-09-23 07:02:44 +08:00
return if @innerState.dragging
2015-03-03 07:33:58 +08:00
return if not @_selection?
return if document.activeElement isnt @_editableNode() and not force
return if not @_selection.startNode? or not @_selection.endNode?
2015-09-23 07:02:44 +08:00
editable = @_editableNode()
newStartNode = DOMUtils.findSimilarNodes(editable, @_selection.startNode)[@_selection.startNodeIndex]
newEndNode = DOMUtils.findSimilarNodes(editable, @_selection.endNode)[@_selection.endNodeIndex]
2015-05-16 01:45:18 +08:00
return if not newStartNode? or not newEndNode?
2015-03-03 07:33:58 +08:00
@_teardownSelectionListeners()
2015-05-16 01:45:18 +08:00
selection = document.getSelection()
selection.setBaseAndExtent(newStartNode,
@_selection.startOffset,
newEndNode,
@_selection.endOffset)
2015-07-31 09:29:38 +08:00
@_ensureSelectionVisible(selection)
2015-03-03 07:33:58 +08:00
@_setupSelectionListeners()
2015-04-25 02:33:10 +08:00
_getNodeIndex: (nodeToFind) =>
2015-09-23 07:02:44 +08:00
DOMUtils.findSimilarNodes(@_editableNode(), nodeToFind).indexOf nodeToFind
2015-03-03 07:33:58 +08:00
# This needs to be in the contenteditable area because we need to first
# restore the selection before calling the `execCommand`
#
# If the url is empty, that means we want to remove the url.
2015-04-25 02:33:10 +08:00
_onSaveUrl: (url, linkToModify) =>
2015-03-03 07:33:58 +08:00
if linkToModify?
2015-09-23 07:02:44 +08:00
linkToModify = DOMUtils.findSimilarNodes(@_editableNode(), linkToModify)?[0]?.childNodes[0]
return unless linkToModify?
return if linkToModify.getAttribute?('href').trim() is url.trim()
range =
anchorNode: linkToModify
anchorOffset: 0
focusNode: linkToModify
focusOffset: linkToModify.length
2015-03-03 07:33:58 +08:00
if url.trim().length is 0
2015-09-23 07:02:44 +08:00
@_execCommand ["unlink", false], range
else @_execCommand ["createLink", false, url], range
2015-03-03 07:33:58 +08:00
else
@_restoreSelection(force: true)
2015-09-23 07:02:44 +08:00
if not document.getSelection().isCollapsed
2015-03-03 07:33:58 +08:00
if url.trim().length is 0
2015-09-23 07:02:44 +08:00
@_execCommand ["unlink", false]
else @_execCommand ["createLink", false, url]
2015-03-03 07:33:58 +08:00
@_restoreSelection(force: true, collapse: "end")
2015-09-23 07:02:44 +08:00
_execCommand: (commandArgs=[], selectionRange={}) =>
{anchorNode, anchorOffset, focusNode, focusOffset} = selectionRange
@_teardownSelectionListeners()
if anchorNode and focusNode
selection = document.getSelection()
selection.setBaseAndExtent(anchorNode, anchorOffset, focusNode, focusOffset)
document.execCommand.apply(document, commandArgs)
@_setupSelectionListeners()
@_onInput()
2015-03-03 07:33:58 +08:00
2015-05-12 09:07:06 +08:00
module.exports = ContenteditableComponent