Skip to content

Commit

Permalink
Merge pull request #453 from voxmedia/remove-hotkey
Browse files Browse the repository at this point in the history
Add removeHotkeys to keyboard module, closes #110
  • Loading branch information
jhchen committed Aug 17, 2015
2 parents 03c9416 + 79eb7f9 commit 10544f7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config/grunt/dist.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports = (grunt) ->
modifier: 'modern'
include: [
'difference', 'intersection', 'last'
'all', 'each', 'find', 'invoke', 'map', 'reduce'
'all', 'each', 'find', 'invoke', 'map', 'reduce', 'partition',
'bind', 'defer', 'partial'
'clone', 'extend', 'defaults', 'omit', 'values'
'isElement', 'isEqual', 'isFunction', 'isNumber', 'isObject', 'isString'
Expand Down
13 changes: 13 additions & 0 deletions src/modules/keyboard.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ class Keyboard
@hotkeys[which].push(hotkey)
)

removeHotkeys: (hotkey, callback) ->
hotkey = if _.isString(hotkey) then hotkey.toUpperCase() else hotkey
hotkey = if Keyboard.hotkeys[hotkey] then Keyboard.hotkeys[hotkey] else hotkey
hotkey = if _.isObject(hotkey) then hotkey else { key: hotkey }
which = if _.isNumber(hotkey.key) then hotkey.key else hotkey.key.charCodeAt(0)
@hotkeys[which] ?= []
[removed, kept] = _.partition(@hotkeys[which], (handler) ->
_.isEqual(hotkey, _.omit(handler, 'callback')) and
(!callback or callback == handler.callback)
)
@hotkeys[which] = kept
return _.map(removed, 'callback')

toggleFormat: (range, format) ->
if range.isCollapsed()
delta = @quill.getContents(Math.max(0, range.start-1), range.end)
Expand Down
38 changes: 38 additions & 0 deletions test/unit/modules/keyboard.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,43 @@ describe('Keyboard', ->
expect(dom($('.ql-bold').get(0)).hasClass('ql-active')).toBe(true)
expect(dom($('.ql-size').get(0)).value()).toBe(size)
)

it('removeHotkeys by name', ->
counter = 0
fn = -> counter += 1
keyboard = @quill.getModule('keyboard')
keyboard.addHotkey('S', fn)
dom(@quill.root).trigger('keydown', { key: 'S' })
expect(counter).toBe(1)
result = keyboard.removeHotkeys('S', fn)
expect(result.length).toBe(1)
expect(result[0]).toBe(fn);
dom(@quill.root).trigger('keydown', { key: 'S' })
expect(counter).toBe(1)
)

it('removeHotkeys by object', ->
counter = 0
fn = -> counter += 1
keyboard = @quill.getModule('keyboard')
keyboard.addHotkey({ key: 'S', metaKey: true }, fn)
dom(@quill.root).trigger('keydown', { key: 'S', metaKey: true })
result = keyboard.removeHotkeys({ key: 'S', metaKey: true })
expect(result.length).toBe(1)
expect(result[0]).toBe(fn)
dom(@quill.root).trigger('keydown', { key: 'S', metaKey: true })
expect(counter).toBe(1)
)

it('removeHotKeys only the specified callback', ->
fn = ->
anotherFn = ->
keyboard = @quill.getModule('keyboard')
keyboard.addHotkey({ key: 'S', metaKey: true }, fn)
keyboard.addHotkey({ key: 'S', metaKey: true }, anotherFn)
result = keyboard.removeHotkeys({ key: 'S', metaKey: true }, fn)
expect(result.length).toBe(1)
expect(result[0]).toBe(fn)
)
)
)

0 comments on commit 10544f7

Please sign in to comment.