mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-11-14 05:41:05 +08:00
6d1dbe1dbf
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
148 lines
4.4 KiB
CoffeeScript
148 lines
4.4 KiB
CoffeeScript
UndoManager = require "../src/undo-manager"
|
|
|
|
describe "UndoManager", ->
|
|
beforeEach ->
|
|
@undoManager = new UndoManager
|
|
|
|
afterEach ->
|
|
advanceClock(500)
|
|
|
|
it "Initializes empty", ->
|
|
expect(@undoManager._history.length).toBe 0
|
|
expect(@undoManager._markers.length).toBe 0
|
|
expect(@undoManager._historyIndex).toBe -1
|
|
expect(@undoManager._markerIndex).toBe -1
|
|
|
|
it "can push a history item onto the stack", ->
|
|
@undoManager.saveToHistory "A"
|
|
advanceClock(500)
|
|
expect(@undoManager._history[0]).toBe "A"
|
|
expect(@undoManager._history.length).toBe 1
|
|
expect(@undoManager.current()).toBe "A"
|
|
|
|
it "updates the historyIndex", ->
|
|
@undoManager.saveToHistory "A"
|
|
expect(@undoManager._historyIndex).toBe 0
|
|
|
|
it "updates the markerIndex", ->
|
|
@undoManager.saveToHistory "A"
|
|
advanceClock(500)
|
|
@undoManager.saveToHistory "AB"
|
|
@undoManager.saveToHistory "ABC"
|
|
advanceClock(500)
|
|
expect(@undoManager._markerIndex).toBe 1
|
|
expect(@undoManager._historyIndex).toBe 2
|
|
|
|
describe "when undoing", ->
|
|
beforeEach ->
|
|
@undoManager.saveToHistory "A"
|
|
advanceClock(500)
|
|
@undoManager.saveToHistory "AB"
|
|
@undoManager.saveToHistory "ABC"
|
|
|
|
it "returns the last item on the stack at the most recent marker", ->
|
|
expect(@undoManager.undo()).toBe "A"
|
|
|
|
it "doesn't change the size of the stack", ->
|
|
@undoManager.undo()
|
|
expect(@undoManager._history.length).toBe 3
|
|
|
|
it "set the historyIndex properly", ->
|
|
@undoManager.undo()
|
|
expect(@undoManager._historyIndex).toBe 0
|
|
|
|
it "set the markerIndex properly after a wait", ->
|
|
advanceClock(500)
|
|
@undoManager.undo()
|
|
expect(@undoManager._markerIndex).toBe 0
|
|
|
|
it "set the markerIndex properly when undo fires immediately", ->
|
|
@undoManager.undo()
|
|
expect(@undoManager._markerIndex).toBe 0
|
|
|
|
it "returns null when there's nothing to undo", ->
|
|
@undoManager.undo()
|
|
@undoManager.undo()
|
|
expect(@undoManager.undo()).toBe null
|
|
expect(@undoManager._markerIndex).toBe 0
|
|
|
|
describe "when redoing", ->
|
|
beforeEach ->
|
|
@undoManager.saveToHistory "X"
|
|
advanceClock(500)
|
|
@undoManager.saveToHistory "XY"
|
|
@undoManager.saveToHistory "XYZ"
|
|
|
|
it "returns the last item on the stack after a wait", ->
|
|
advanceClock(500)
|
|
@undoManager.undo()
|
|
advanceClock(500)
|
|
expect(@undoManager.redo()).toBe "XYZ"
|
|
|
|
it "returns the last item on the stack when fired immediately", ->
|
|
@undoManager.undo()
|
|
expect(@undoManager.redo()).toBe "XYZ"
|
|
|
|
it "doesn't change the size of the stack", ->
|
|
@undoManager.undo()
|
|
@undoManager.redo()
|
|
expect(@undoManager._history.length).toBe 3
|
|
|
|
it "set the historyIndex properly", ->
|
|
@undoManager.undo()
|
|
@undoManager.redo()
|
|
expect(@undoManager._historyIndex).toBe 2
|
|
|
|
it "set the markerIndex properly", ->
|
|
@undoManager.undo()
|
|
@undoManager.redo()
|
|
expect(@undoManager._markerIndex).toBe 1
|
|
|
|
it "returns null when there's nothing to redo", ->
|
|
expect(@undoManager.redo()).toBe null
|
|
expect(@undoManager.redo()).toBe null
|
|
expect(@undoManager._markerIndex).toBe 1
|
|
|
|
describe "when undoing and adding items", ->
|
|
beforeEach ->
|
|
@undoManager.saveToHistory "1"
|
|
advanceClock(500)
|
|
@undoManager.saveToHistory "12"
|
|
@undoManager.saveToHistory "123"
|
|
advanceClock(500)
|
|
@undoManager.saveToHistory "1234"
|
|
@undoManager.undo()
|
|
@undoManager.undo()
|
|
advanceClock(500)
|
|
@undoManager.saveToHistory "A"
|
|
advanceClock(500)
|
|
|
|
it "correctly sets the history", ->
|
|
expect(@undoManager._history).toEqual ["1", "A"]
|
|
|
|
it "correctly sets the length", ->
|
|
expect(@undoManager._history.length).toBe 2
|
|
|
|
it "puts the correct items on the stack", ->
|
|
@undoManager.undo()
|
|
expect(@undoManager.redo()).toBe "A"
|
|
|
|
it "sets the historyIndex correctly", ->
|
|
expect(@undoManager._historyIndex).toBe 1
|
|
|
|
describe "when the stack is full", ->
|
|
beforeEach ->
|
|
@undoManager._MAX_HISTORY_SIZE = 2
|
|
@undoManager.saveToHistory "A"
|
|
@undoManager.saveToHistory "AB"
|
|
@undoManager.saveToHistory "ABC"
|
|
@undoManager.saveToHistory "ABCD"
|
|
|
|
it "correctly sets the length", ->
|
|
expect(@undoManager._history.length).toBe 2
|
|
|
|
it "keeps the latest histories", ->
|
|
expect(@undoManager._history).toEqual ["ABC", "ABCD"]
|
|
|
|
it "updates the historyIndex", ->
|
|
expect(@undoManager._historyIndex).toBe 1
|