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
|
|
|
fs = require 'fs-plus'
|
|
|
|
path = require 'path'
|
|
|
|
{Emitter, Disposable} = require 'event-kit'
|
|
|
|
|
|
|
|
# Extended: A singleton instance of this class available via `atom.styles`,
|
|
|
|
# which you can use to globally query and observe the set of active style
|
|
|
|
# sheets. The `StyleManager` doesn't add any style elements to the DOM on its
|
|
|
|
# own, but is instead subscribed to by individual `<atom-styles>` elements,
|
|
|
|
# which clone and attach style elements in different contexts.
|
2015-05-14 02:44:28 +08:00
|
|
|
#
|
|
|
|
# Section: Atom
|
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
|
|
|
module.exports =
|
|
|
|
class StyleManager
|
|
|
|
constructor: ->
|
|
|
|
@emitter = new Emitter
|
|
|
|
@styleElements = []
|
|
|
|
@styleElementsBySourcePath = {}
|
|
|
|
|
|
|
|
###
|
|
|
|
Section: Event Subscription
|
|
|
|
###
|
|
|
|
|
|
|
|
# Extended: Invoke `callback` for all current and future style elements.
|
|
|
|
#
|
|
|
|
# * `callback` {Function} that is called with style elements.
|
|
|
|
# * `styleElement` An `HTMLStyleElement` instance. The `.sheet` property
|
|
|
|
# will be null because this element isn't attached to the DOM. If you want
|
|
|
|
# to attach this element to the DOM, be sure to clone it first by calling
|
|
|
|
# `.cloneNode(true)` on it. The style element will also have the following
|
|
|
|
# non-standard properties:
|
|
|
|
# * `sourcePath` A {String} containing the path from which the style
|
|
|
|
# element was loaded.
|
|
|
|
# * `context` A {String} indicating the target context of the style
|
|
|
|
# element.
|
|
|
|
#
|
|
|
|
# Returns a {Disposable} on which `.dispose()` can be called to cancel the
|
|
|
|
# subscription.
|
|
|
|
observeStyleElements: (callback) ->
|
|
|
|
callback(styleElement) for styleElement in @getStyleElements()
|
|
|
|
@onDidAddStyleElement(callback)
|
|
|
|
|
|
|
|
# Extended: Invoke `callback` when a style element is added.
|
|
|
|
#
|
|
|
|
# * `callback` {Function} that is called with style elements.
|
|
|
|
# * `styleElement` An `HTMLStyleElement` instance. The `.sheet` property
|
|
|
|
# will be null because this element isn't attached to the DOM. If you want
|
|
|
|
# to attach this element to the DOM, be sure to clone it first by calling
|
|
|
|
# `.cloneNode(true)` on it. The style element will also have the following
|
|
|
|
# non-standard properties:
|
|
|
|
# * `sourcePath` A {String} containing the path from which the style
|
|
|
|
# element was loaded.
|
|
|
|
# * `context` A {String} indicating the target context of the style
|
|
|
|
# element.
|
|
|
|
#
|
|
|
|
# Returns a {Disposable} on which `.dispose()` can be called to cancel the
|
|
|
|
# subscription.
|
|
|
|
onDidAddStyleElement: (callback) ->
|
|
|
|
@emitter.on 'did-add-style-element', callback
|
|
|
|
|
|
|
|
# Extended: Invoke `callback` when a style element is removed.
|
|
|
|
#
|
|
|
|
# * `callback` {Function} that is called with style elements.
|
|
|
|
# * `styleElement` An `HTMLStyleElement` instance.
|
|
|
|
#
|
|
|
|
# Returns a {Disposable} on which `.dispose()` can be called to cancel the
|
|
|
|
# subscription.
|
|
|
|
onDidRemoveStyleElement: (callback) ->
|
|
|
|
@emitter.on 'did-remove-style-element', callback
|
|
|
|
|
|
|
|
# Extended: Invoke `callback` when an existing style element is updated.
|
|
|
|
#
|
|
|
|
# * `callback` {Function} that is called with style elements.
|
|
|
|
# * `styleElement` An `HTMLStyleElement` instance. The `.sheet` property
|
|
|
|
# will be null because this element isn't attached to the DOM. The style
|
|
|
|
# element will also have the following non-standard properties:
|
|
|
|
# * `sourcePath` A {String} containing the path from which the style
|
|
|
|
# element was loaded.
|
|
|
|
# * `context` A {String} indicating the target context of the style
|
|
|
|
# element.
|
|
|
|
#
|
|
|
|
# Returns a {Disposable} on which `.dispose()` can be called to cancel the
|
|
|
|
# subscription.
|
|
|
|
onDidUpdateStyleElement: (callback) ->
|
|
|
|
@emitter.on 'did-update-style-element', callback
|
|
|
|
|
|
|
|
###
|
|
|
|
Section: Reading Style Elements
|
|
|
|
###
|
|
|
|
|
|
|
|
# Extended: Get all loaded style elements.
|
|
|
|
getStyleElements: ->
|
|
|
|
@styleElements.slice()
|
|
|
|
|
|
|
|
addStyleSheet: (source, params) ->
|
|
|
|
sourcePath = params?.sourcePath
|
|
|
|
context = params?.context
|
|
|
|
priority = params?.priority
|
|
|
|
|
|
|
|
if sourcePath? and styleElement = @styleElementsBySourcePath[sourcePath]
|
|
|
|
updated = true
|
|
|
|
else
|
|
|
|
styleElement = document.createElement('style')
|
|
|
|
if sourcePath?
|
|
|
|
styleElement.sourcePath = sourcePath
|
|
|
|
styleElement.setAttribute('source-path', sourcePath)
|
|
|
|
|
|
|
|
if context?
|
|
|
|
styleElement.context = context
|
|
|
|
styleElement.setAttribute('context', context)
|
|
|
|
|
|
|
|
if priority?
|
|
|
|
styleElement.priority = priority
|
|
|
|
styleElement.setAttribute('priority', priority)
|
|
|
|
|
|
|
|
styleElement.textContent = source
|
|
|
|
|
|
|
|
if updated
|
|
|
|
@emitter.emit 'did-update-style-element', styleElement
|
|
|
|
else
|
|
|
|
@addStyleElement(styleElement)
|
|
|
|
|
|
|
|
new Disposable => @removeStyleElement(styleElement)
|
|
|
|
|
|
|
|
addStyleElement: (styleElement) ->
|
|
|
|
{sourcePath, priority} = styleElement
|
|
|
|
|
|
|
|
if priority?
|
|
|
|
for existingElement, index in @styleElements
|
|
|
|
if existingElement.priority > priority
|
|
|
|
insertIndex = index
|
|
|
|
break
|
|
|
|
|
|
|
|
insertIndex ?= @styleElements.length
|
|
|
|
|
|
|
|
@styleElements.splice(insertIndex, 0, styleElement)
|
|
|
|
@styleElementsBySourcePath[sourcePath] ?= styleElement if sourcePath?
|
|
|
|
@emitter.emit 'did-add-style-element', styleElement
|
|
|
|
|
|
|
|
removeStyleElement: (styleElement) ->
|
|
|
|
index = @styleElements.indexOf(styleElement)
|
|
|
|
unless index is -1
|
|
|
|
@styleElements.splice(index, 1)
|
|
|
|
delete @styleElementsBySourcePath[styleElement.sourcePath] if styleElement.sourcePath?
|
|
|
|
@emitter.emit 'did-remove-style-element', styleElement
|
|
|
|
|
|
|
|
getSnapshot: ->
|
|
|
|
@styleElements.slice()
|
|
|
|
|
|
|
|
restoreSnapshot: (styleElementsToRestore) ->
|
|
|
|
for styleElement in @getStyleElements()
|
|
|
|
@removeStyleElement(styleElement) unless styleElement in styleElementsToRestore
|
|
|
|
|
|
|
|
existingStyleElements = @getStyleElements()
|
|
|
|
for styleElement in styleElementsToRestore
|
|
|
|
@addStyleElement(styleElement) unless styleElement in existingStyleElements
|
|
|
|
|
|
|
|
###
|
|
|
|
Section: Paths
|
|
|
|
###
|
|
|
|
|
|
|
|
# Extended: Get the path of the user style sheet in `~/.atom`.
|
|
|
|
#
|
|
|
|
# Returns a {String}.
|
|
|
|
getUserStyleSheetPath: ->
|
|
|
|
stylesheetPath = fs.resolve(path.join(atom.getConfigDirPath(), 'styles'), ['css', 'less'])
|
|
|
|
if fs.isFileSync(stylesheetPath)
|
|
|
|
stylesheetPath
|
|
|
|
else
|
|
|
|
path.join(atom.getConfigDirPath(), 'styles.less')
|