Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform the undo/redo stacks against non-user initiated changes #413

Merged
merged 4 commits into from
Jul 17, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/modules/undo-manager.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ class UndoManager
)
@quill.on(@quill.constructor.events.TEXT_CHANGE, (delta, origin) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rename origin to source? That's the more common name in other parts of the codebase.

return if @ignoreChange
this.record(delta, @oldDelta)
if origin == 'user'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change 'user' to Quill.sources.USER?

this.record(delta, @oldDelta)
else
this._transform(delta)
@oldDelta = @quill.getContents()
)

Expand Down Expand Up @@ -90,13 +93,22 @@ class UndoManager
change = @stack[source].pop()
@lastRecorded = 0
@ignoreChange = true
@quill.updateContents(change[source], 'user')
@quill.updateContents(change[source], Quill.sources.USER)
@ignoreChange = false
index = this._getLastChangeIndex(change[source])
@quill.setSelection(index, index)
@oldDelta = @quill.getContents()
@stack[dest].push(change)

_transform: (delta) ->
@oldDelta = delta.transform(@oldDelta, true)
for change in @stack.undo
change.undo = delta.transform(change.undo, true)
change.redo = delta.transform(change.redo, true)
for change in @stack.redo
change.undo = delta.transform(change.undo, true)
change.redo = delta.transform(change.redo, true)


Quill.registerModule('undo-manager', UndoManager)
module.exports = UndoManager
28 changes: 20 additions & 8 deletions test/unit/modules/undo-manager.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('UndoManager', ->
describe('undo/redo', ->
_.each(tests, (test, name) ->
it(name, ->
@quill.updateContents(test.delta)
@quill.updateContents(test.delta, Quill.sources.USER)
changed = @quill.getContents()
expect(changed).not.toEqualDelta(@original)
@undoManager.undo()
Expand All @@ -65,9 +65,9 @@ describe('UndoManager', ->

it('merge changes', ->
expect(@undoManager.stack.undo.length).toEqual(0)
@quill.updateContents(new Quill.Delta().retain(12).insert('e'))
@quill.updateContents(new Quill.Delta().retain(12).insert('e'), Quill.sources.USER)
expect(@undoManager.stack.undo.length).toEqual(1)
@quill.updateContents(new Quill.Delta().retain(13).insert('s'))
@quill.updateContents(new Quill.Delta().retain(13).insert('s'), Quill.sources.USER)
expect(@undoManager.stack.undo.length).toEqual(1)
@undoManager.undo()
expect(@quill.getContents()).toEqual(@original)
Expand All @@ -76,21 +76,21 @@ describe('UndoManager', ->

it('dont merge changes', (done) ->
expect(@undoManager.stack.undo.length).toEqual(0)
@quill.updateContents(new Quill.Delta().retain(12).insert('e'))
@quill.updateContents(new Quill.Delta().retain(12).insert('e'), Quill.sources.USER)
expect(@undoManager.stack.undo.length).toEqual(1)
setTimeout( =>
@quill.updateContents(new Quill.Delta().retain(13).insert('s'))
@quill.updateContents(new Quill.Delta().retain(13).insert('s'), Quill.sources.USER)
expect(@undoManager.stack.undo.length).toEqual(2)
done()
, @undoManager.options.delay * 1.25)
)

it('multiple undos', (done) ->
expect(@undoManager.stack.undo.length).toEqual(0)
@quill.updateContents(new Quill.Delta().retain(12).insert('e'))
@quill.updateContents(new Quill.Delta().retain(12).insert('e'), Quill.sources.USER)
contents = @quill.getContents()
setTimeout( =>
@quill.updateContents(new Quill.Delta().retain(13).insert('s'))
@quill.updateContents(new Quill.Delta().retain(13).insert('s'), Quill.sources.USER)
@undoManager.undo()
expect(@quill.getContents()).toEqual(contents)
@undoManager.undo()
Expand All @@ -100,13 +100,25 @@ describe('UndoManager', ->
)

it('hotkeys', ->
@quill.updateContents(new Quill.Delta().insert('A'))
@quill.updateContents(new Quill.Delta().insert('A'), Quill.sources.USER)
changed = @quill.getContents()
expect(changed).not.toEqualDelta(@original)
dom(@quill.root).trigger('keydown', Quill.Module.UndoManager.hotkeys.UNDO)
expect(@quill.getContents()).toEqualDelta(@original)
dom(@quill.root).trigger('keydown', Quill.Module.UndoManager.hotkeys.REDO)
expect(@quill.getContents()).toEqualDelta(changed)
)

it('api change transform', ->
@quill.updateContents(new Quill.Delta().retain(12).insert('es'), Quill.sources.USER)
@quill.updateContents(new Quill.Delta().retain(4).delete(5), Quill.sources.API)
@quill.updateContents(new Quill.Delta().retain(9).insert('!'), Quill.sources.USER)
expect(@undoManager.stack.undo.length).toEqual(1)
expect(@quill.getContents()).toEqual(new Quill.Delta().insert('The foxes!\n'))
@undoManager.undo()
expect(@quill.getContents()).toEqual(new Quill.Delta().insert('The fox\n'))
@undoManager.redo()
expect(@quill.getContents()).toEqual(new Quill.Delta().insert('The foxes!\n'))
)
)
)