Closed msaltnet closed 3 years ago
There are three types, local, global, window event.
ctrl + q
auto-scroll
ctrl + w
wrap
ctrl + e
clear current tab
ctrl + space
listen
ctrl + +
font size up
ctrl + -
font size down
Use Mousetrap
.
npm install mousetrap
import Mousetrap from "mousetrap";
const keyEventList = ['q', 'w', 'e', 'space', 'plus', 'minus'];
Mousetrap.bind('ctrl+q', function() {
console.log('control q');
this.onKeyEvent('q');
return false;
});
onKeyEvent(event) {
if (!keyEventList.includes(event))
return;
for (let i = 0; i < this.tabs.length; i++) {
if (this.tabs[i].id == this.currentItem) {
this.tabs[i].keyEvent[event] = !this.tabs[i].keyEvent[event];
return;
}
}
}
Test code
describe('onKeyEvent', () => {
it('should change correct keyEvent props when called', () => {
const vm = shallowMount(App).vm;
vm.tabs = [
{ id: 'mango', keyEvent: { q: false, w: false, e: false, space: true, plus: true, minus: true } },
{ id: 'banana', keyEvent: { q: true, w: true, e: true, space: false, plus: false, minus: false } }
];
vm.currentItem = 'mango';
vm.onKeyEvent('w');
expect(vm.tabs[0].keyEvent.w).toEqual(true);
vm.onKeyEvent('plus');
expect(vm.tabs[0].keyEvent.plus).toEqual(false);
vm.currentItem = 'banana';
vm.onKeyEvent('minus');
expect(vm.tabs[0].keyEvent.minus).toEqual(true);
vm.onKeyEvent('q');
expect(vm.tabs[0].keyEvent.q).toEqual(false);
})
it('should not change keyEvent props when called with invaild event', () => {
const vm = shallowMount(App).vm;
vm.tabs = [
{ id: 'mango', keyEvent: { q: false, w: false, e: false, space: true, plus: true, minus: true } },
{ id: 'banana', keyEvent: { q: true, w: true, e: true, space: false, plus: false, minus: false } }
];
vm.currentItem = 'mango';
vm.onKeyEvent('wq');
expect(vm.tabs[0].keyEvent.w).toEqual(false);
vm.onKeyEvent('plusa');
expect(vm.tabs[0].keyEvent.plus).toEqual(true);
})
})
F2
toggle auto-scrollF3
toggle wrapF5
clear current tabctr + space
toggle listen statectr + +
font size upctr + -
font size down