0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Tampermonkey で gollum のプレビュー切り替えキーボードショートカットを変更する

Posted at

gollum というオープンソースの wiki にはキーボードショートカットがある。

On a Page
  - e: Edit the Page
In the Editor:
  - Ctrl/Cmd-S: Save
  - Ctrl-Shift-P: Toggle Preview

これが Karabiner-Elements の Emulation Modes - Emacs key bindings と相性が悪い。 Karabiner-Elements が Ctrl+P を キーの入力に書き換えるので、 Ctrl+Shift+P が ↑+P と解釈されてしまい、 gollum が用意したキーボードショートカットを呼び出せない。

特定 URL 内に閉じた話なので、 Tampermonkey で Karabiner-Elements と衝突しないキーボードショートカットを割り当てて解決した。

// ==UserScript==
// @name         localhost gollum preview hotkey Cmd+P
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http://localhost:4567/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // event.metaKey は keydown イベント時しか取得できない
    // https://stackoverflow.com/a/5500536/374851
    document.addEventListener('keydown', event => {
        if (event.metaKey && event.key === "p") {
          // https://github.com/gollum/gollum/blob/v5.0.1/lib/gollum/public/gollum/javascript/gollum.js.erb#L402-L405
          // Toggle Preview キーボードショートカットは最終的に以下のコードを実行している
          $('.tabnav-tab').not('.selected').click();

          // 今回割り当てた Cmd+P はブラウザの「印刷」メニューのショートカットキーと重複しているのでデフォルト挙動を無効化する
          event.preventDefault();
        }
    });
})();
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?