commit
3244c3c35d
10 changed files with 77 additions and 102 deletions
@ -0,0 +1,44 @@ |
||||
import messages from '../content/messages'; |
||||
|
||||
export default class ContentInputComponent { |
||||
constructor(target) { |
||||
this.pressed = {}; |
||||
|
||||
target.addEventListener('keypress', this.onKeyPress.bind(this)); |
||||
target.addEventListener('keydown', this.onKeyDown.bind(this)); |
||||
target.addEventListener('keyup', this.onKeyUp.bind(this)); |
||||
} |
||||
|
||||
onKeyPress(e) { |
||||
this.capture(e); |
||||
} |
||||
|
||||
onKeyDown(e) { |
||||
this.capture(e); |
||||
} |
||||
|
||||
onKeyUp(e) { |
||||
this.pressed[e.key] = false; |
||||
} |
||||
|
||||
capture(e) { |
||||
if (this.pressed[e.key]) { |
||||
return; |
||||
} |
||||
this.pressed[e.key] = true; |
||||
|
||||
if (e.target instanceof HTMLInputElement || |
||||
e.target instanceof HTMLTextAreaElement || |
||||
e.target instanceof HTMLSelectElement) { |
||||
if (e.key === 'Escape' && e.target.blur) { |
||||
e.target.blur(); |
||||
} |
||||
return; |
||||
} |
||||
browser.runtime.sendMessage({ |
||||
type: messages.KEYDOWN, |
||||
key: e.key, |
||||
ctrl: e.ctrlKey |
||||
}); |
||||
} |
||||
} |
@ -1,21 +0,0 @@ |
||||
const asKeymapChars = (keys) => { |
||||
return keys.map((k) => { |
||||
let c = String.fromCharCode(k.code); |
||||
if (k.ctrl) { |
||||
return '<C-' + c.toUpperCase() + '>'; |
||||
} |
||||
return c; |
||||
}).join(''); |
||||
}; |
||||
|
||||
const asCaretChars = (keys) => { |
||||
return keys.map((k) => { |
||||
let c = String.fromCharCode(k.code); |
||||
if (k.ctrl) { |
||||
return '^' + c.toUpperCase(); |
||||
} |
||||
return c; |
||||
}).join(''); |
||||
}; |
||||
|
||||
export { asKeymapChars, asCaretChars }; |
@ -1,31 +0,0 @@ |
||||
import { expect } from "chai"; |
||||
import * as keys from '../../src/shared/keys'; |
||||
|
||||
describe("keys", () => { |
||||
const KEYMAP = { |
||||
'g<C-X>GG': [], |
||||
'gg': { type: 'scroll.top' }, |
||||
}; |
||||
|
||||
const g = 'g'.charCodeAt(0); |
||||
const G = 'G'.charCodeAt(0); |
||||
const x = 'x'.charCodeAt(0); |
||||
|
||||
describe('#asKeymapChars', () => { |
||||
let keySequence = [ |
||||
{ code: g }, |
||||
{ code: x, ctrl: true }, |
||||
{ code: G } |
||||
]; |
||||
expect(keys.asKeymapChars(keySequence)).to.equal('g<C-X>G'); |
||||
}); |
||||
|
||||
describe('#asCaretChars', () => { |
||||
let keySequence = [ |
||||
{ code: g }, |
||||
{ code: x, ctrl: true }, |
||||
{ code: G } |
||||
]; |
||||
expect(keys.asCaretChars(keySequence)).to.equal('g^XG'); |
||||
}); |
||||
}); |
Reference in new issue