Mailspring/src/undo-manager.coffee
Evan Morikawa 6d1dbe1dbf fix(composer): support Chinese & others - handle composition events
Summary:
ignores composition event commands until they're done. We then simply
update the new state after that happens.

Some additional refactoring:

- The <Contenteditable /> prop is 'value' instead of 'html' to make it
  look more like a standard React controlled input
- Removed `filters` prop and `footerElements` prop from Contenteditable.
  These could easily be moved into the composer (where they belong).
- Moved contenteditable and a few of its helper classes into their own
  folder.
- Moved `UndoManager` up out of the `flux` folder into `src`. Currently
  undo/redo is only in the composer when all contenteditables should have
  the basic funcionality. Will refactor this later.
- Fix tests

Test Plan: manual

Reviewers: bengotow

Reviewed By: bengotow

Differential Revision: https://phab.nylas.com/D2211
2015-10-30 20:03:33 -04:00

51 lines
1.3 KiB
CoffeeScript

_ = require 'underscore'
module.exports =
class UndoManager
constructor: ->
@_historyIndex = -1
@_markerIndex = -1
@_history = []
@_markers = []
@_MAX_HISTORY_SIZE = 1000
current: ->
return @_history[@_historyIndex]
undo: ->
@__saveHistoryMarker()
if @_historyIndex > 0
@_markerIndex -= 1
@_historyIndex = @_markers[@_markerIndex]
return @_history[@_historyIndex]
else return null
redo: ->
@__saveHistoryMarker()
if @_historyIndex < (@_history.length - 1)
@_markerIndex += 1
@_historyIndex = @_markers[@_markerIndex]
return @_history[@_historyIndex]
else return null
saveToHistory: (historyItem) =>
if not _.isEqual((_.last(@_history) ? {}), historyItem)
@_historyIndex += 1
@_history.length = @_historyIndex
@_history.push(historyItem)
@_saveHistoryMarker()
while @_history.length > @_MAX_HISTORY_SIZE
@_history.shift()
@_historyIndex -= 1
__saveHistoryMarker: =>
if @_markers[@_markerIndex] isnt @_historyIndex
@_markerIndex += 1
@_markers.length = @_markerIndex
@_markers.push(@_historyIndex)
while @_markers.length > @_MAX_HISTORY_SIZE
@_markers.shift()
@_markerIndex -= 1
_saveHistoryMarker: _.debounce(UndoManager::__saveHistoryMarker, 300)