2017-04-16 04:14:58 +09:00
|
|
|
import * as Mousetrap from 'mousetrap';
|
2017-04-17 18:40:55 +09:00
|
|
|
import {Config, Account} from '../main/config';
|
2017-04-16 02:27:06 +09:00
|
|
|
import * as Ipc from './ipc';
|
|
|
|
import log from './log';
|
|
|
|
|
2017-04-16 04:14:58 +09:00
|
|
|
function scrollable() {
|
|
|
|
const scrollable = document.querySelector('.scrollable');
|
|
|
|
if (!scrollable) {
|
|
|
|
log.error('Scrollable element was not found!');
|
|
|
|
return {scrollTop: 0};
|
|
|
|
}
|
|
|
|
return scrollable;
|
|
|
|
}
|
|
|
|
|
2017-04-19 16:39:51 +09:00
|
|
|
function navigateTo(host: string, path: string) {
|
|
|
|
const url = `https://${host}${path}`;
|
|
|
|
if (window.location.href === url) {
|
|
|
|
log.info('Current URL is already', url);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const link = document.querySelector(`a[href="${path}"]`);
|
|
|
|
if (link) {
|
|
|
|
log.info('Click link by shortcut', path);
|
|
|
|
(link as HTMLAnchorElement).click();
|
|
|
|
} else {
|
|
|
|
log.info('Force navigation by shortcut', path);
|
|
|
|
window.location.href = url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-16 04:14:58 +09:00
|
|
|
const ShortcutActions = {
|
|
|
|
'scroll-top': () => {
|
|
|
|
scrollable().scrollTop = 0;
|
|
|
|
},
|
|
|
|
'scroll-bottom': () => {
|
|
|
|
scrollable().scrollTop = document.body.scrollHeight;
|
|
|
|
},
|
|
|
|
'scroll-down': () => {
|
|
|
|
scrollable().scrollTop += window.innerHeight / 3;
|
|
|
|
},
|
|
|
|
'scroll-up': () => {
|
|
|
|
scrollable().scrollTop -= window.innerHeight / 3;
|
|
|
|
},
|
2017-04-19 17:07:31 +09:00
|
|
|
'next-account': () => {
|
|
|
|
Ipc.send('mstdn:next-account');
|
|
|
|
},
|
|
|
|
'prev-account': () => {
|
|
|
|
Ipc.send('mstdn:prev-account');
|
|
|
|
},
|
2017-04-16 12:44:53 +09:00
|
|
|
} as {[action: string]: () => void};
|
2017-04-16 02:27:06 +09:00
|
|
|
|
2017-04-16 04:14:58 +09:00
|
|
|
function setupKeybinds(keybinds: {[key: string]: string}, host: string) {
|
|
|
|
for (const key in keybinds) {
|
|
|
|
const action = keybinds[key];
|
|
|
|
if (action.startsWith('/')) {
|
|
|
|
Mousetrap.bind(key, e => {
|
|
|
|
e.preventDefault();
|
2017-04-19 16:39:51 +09:00
|
|
|
navigateTo(host, action);
|
2017-04-16 04:14:58 +09:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const func = ShortcutActions[action];
|
2017-04-18 15:56:46 +09:00
|
|
|
if (!func) {
|
2017-04-16 04:14:58 +09:00
|
|
|
log.error('Unknown shortcut action:', action);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Mousetrap.bind(key, e => {
|
|
|
|
log.info('Shortcut:', action);
|
|
|
|
e.preventDefault();
|
|
|
|
func();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-16 02:27:06 +09:00
|
|
|
|
2017-04-17 18:40:55 +09:00
|
|
|
let config: Config | null = null;
|
|
|
|
|
2017-04-18 20:29:07 +09:00
|
|
|
Ipc.on('mstdn:config', (c: Config, a: Account) => {
|
2017-04-17 18:40:55 +09:00
|
|
|
config = c;
|
2017-04-19 16:39:51 +09:00
|
|
|
setupKeybinds(config.keymaps, a.host);
|
2017-04-16 04:14:58 +09:00
|
|
|
});
|