From d27a152d3ac76319611cdcfba038e3a36c56846d Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 16 Apr 2017 04:14:58 +0900 Subject: [PATCH] enable key shortcuts --- README.md | 2 +- main/config.ts | 12 +++++++++- main/log.ts | 1 + renderer/index.ts | 57 +++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 66 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4fd3b28..e2b00ed 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Web-based Desktop Client for [Mastodon][] Features: - [x] Small window on your menubar (or isolated window) -- [ ] Desktop notification +- [x] Desktop notification - [ ] Customizable shortcut keybinds - [ ] Multi-account diff --git a/main/config.ts b/main/config.ts index eef2f8f..21a3eac 100644 --- a/main/config.ts +++ b/main/config.ts @@ -34,7 +34,17 @@ function makeDefaultConfig(): Config { host: '', default_page: '/web/timelines/home', }], - keymaps: {}, + keymaps: { + j: 'scroll-down', + k: 'scroll-up', + i: 'scroll-top', + m: 'scroll-bottom', + 1: '/web/timelines/home', + 2: '/web/notifications', + 3: '/web/timelines/public/local', + 4: '/web/timelines/public', + 5: '/web/getting-started' + }, }; } diff --git a/main/log.ts b/main/log.ts index 29e118f..b7b24e9 100644 --- a/main/log.ts +++ b/main/log.ts @@ -5,5 +5,6 @@ if (process.env.NODE_ENV === 'development') { } else { log.setLevel('info'); } +log.setLevel('debug'); export default log; diff --git a/renderer/index.ts b/renderer/index.ts index b5b4b9a..0d7c4e8 100644 --- a/renderer/index.ts +++ b/renderer/index.ts @@ -1,10 +1,59 @@ -// import * as Mousetrap from 'mousetrap'; +import * as Mousetrap from 'mousetrap'; import {Config} from '../main/config'; import * as Ipc from './ipc'; import log from './log'; +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; + }, +} as {[action: string]: () => void} + +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(); + window.location.href = url; + }); + } else { + const func = ShortcutActions[action]; + if (func === undefined) { + log.error('Unknown shortcut action:', action); + continue; + } + Mousetrap.bind(key, e => { + log.info('Shortcut:', action); + e.preventDefault(); + func(); + }); + } + } +} + Ipc.on('mstdn:config', (config: Config) => { - log.info('FOOOOOO', config); + // TODO: Temporary. It should be fixed on supporting multi-account. + const host = config.accounts[0].host; + setupKeybinds(config.keymaps, host); }); - -