msaltnet / T.Viewer

Cross Platform Tizen Log Viewer
https://blog.msalt.net/313
MIT License
15 stars 7 forks source link

[Feature] shortcut #61

Closed msaltnet closed 3 years ago

msaltnet commented 3 years ago

F2 toggle auto-scroll F3 toggle wrap F5 clear current tab ctr + space toggle listen state ctr + + font size up ctr + - font size down

msaltnet commented 3 years ago

Electron shortcut

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

Window event

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);
        })
    })
msaltnet commented 3 years ago

shortcut