Skip to content

Commit

Permalink
Rename to removeHotkeys to indicate that an array is returned, and ac…
Browse files Browse the repository at this point in the history
…tually do what it says on the tin
  • Loading branch information
thomsbg authored and jhchen committed Nov 10, 2015
1 parent f67dd2b commit dec48b7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
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
14 changes: 10 additions & 4 deletions src/modules/keyboard.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ class Keyboard
@hotkeys[which].push(hotkey)
)

removeHotkey: (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.toUpperCase().charCodeAt(0)
for handler in @hotkeys[which] || []
return handler.callback if _.isEqual(hotkey, _.omit(handler, 'callback'))
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()
Expand Down
41 changes: 32 additions & 9 deletions test/unit/modules/keyboard.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,42 @@ describe('Keyboard', ->
expect(dom($('.ql-size').get(0)).value()).toBe(size)
)

it('removeHotkey by name', ->
fn = @quill.getModule('keyboard').removeHotkey('BOLD')
expect(typeof fn).toBe('function');
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('removeHotkey by number', ->
fn = @quill.getModule('keyboard').removeHotkey(13)
expect(typeof fn).toBe('function');
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('removeHotkey by object', ->
fn = @quill.getModule('keyboard').removeHotkey({ key: 'B', metaKey: true })
expect(typeof fn).toBe('function');
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 dec48b7

Please sign in to comment.