1
0
mirror of https://github.com/rhysd/Mstdn.git synced 2025-01-21 20:52:11 +01:00
Mstdn/renderer/index.ts

64 lines
1.8 KiB
TypeScript
Raw Normal View History

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;
}
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-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 => {
const url = `https://${host}${action}`;
log.info('URL Shortcut:', url);
e.preventDefault();
2017-04-19 00:45:53 +09:00
if (window.location.href !== url) {
window.location.href = url;
}
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;
Ipc.on('mstdn:config', (c: Config, a: Account) => {
2017-04-17 18:40:55 +09:00
config = c;
const host = a.host;
2017-04-16 04:14:58 +09:00
setupKeybinds(config.keymaps, host);
});