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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/modules/undo-manager.coffee
Expand Up @@ -7,6 +7,7 @@ class UndoManager
@DEFAULTS:
delay: 1000
maxStack: 100
userOnly: false

@hotkeys:
UNDO: { key: 'Z', metaKey: true }
Expand All @@ -31,9 +32,12 @@ class UndoManager
return false
)
)
@quill.on(@quill.constructor.events.TEXT_CHANGE, (delta, origin) =>
@quill.on(@quill.constructor.events.TEXT_CHANGE, (delta, source) =>
return if @ignoreChange
this.record(delta, @oldDelta)
if !@options.userOnly or source == Quill.sources.USER
this.record(delta, @oldDelta)
else
this._transform(delta)
@oldDelta = @quill.getContents()
)

Expand Down Expand Up @@ -90,13 +94,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
13 changes: 13 additions & 0 deletions test/unit/modules/undo-manager.coffee
Expand Up @@ -108,5 +108,18 @@ describe('UndoManager', ->
dom(@quill.root).trigger('keydown', Quill.Module.UndoManager.hotkeys.REDO)
expect(@quill.getContents()).toEqualDelta(changed)
)

it('api change transform', ->
@quill.getModule('undo-manager').options.userOnly = true
@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'))
)
)
)