Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request slab#413 from voxmedia/transform-undo-manager
Browse files Browse the repository at this point in the history
Transform the undo/redo stacks against non-user initiated changes
  • Loading branch information
jhchen committed Jul 17, 2015
2 parents ea8e31c + 06a44b7 commit b0b25c5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/modules/undo-manager.coffee
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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'))
)
)
)

0 comments on commit b0b25c5

Please sign in to comment.