Skip to content

Commit

Permalink
ショートカットキーとしてCommandキーを使えるようにする (#1865)
Browse files Browse the repository at this point in the history
* HotkeyComboブランド型を導入

* 忘れ物

* cmdを使えるようにする

* resolve conflict

* Update src/plugins/hotkeyPlugin.ts

* コメントを変更

---------

Co-authored-by: y-chan <[email protected]>
  • Loading branch information
Hiroshiba and y-chan authored Feb 29, 2024
1 parent 5184f03 commit cc2bceb
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/plugins/hotkeyPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export const useHotkeyManager = () => {

type Editor = "talk" | "song";

type BindingKey = string & { __brand: "BindingKey" }; // BindingKey専用のブランド型

/**
* ショートカットキーの処理を登録するための型。
*/
Expand All @@ -50,16 +52,17 @@ export type HotkeyAction = {

export type HotkeysJs = {
(
key: string,
key: BindingKey,
options: {
scope: string;
},
callback: (e: KeyboardEvent) => void
): void;
unbind: (key: string, scope: string) => void;
unbind: (key: BindingKey, scope: string) => void;
setScope: (scope: string) => void;
};

// デフォルトはテキストボックス内でショートカットキー無効なので有効にする
hotkeys.filter = () => {
return true;
};
Expand Down Expand Up @@ -269,8 +272,19 @@ export class HotkeyManager {
}
}

const combinationToBindingKey = (combination: HotkeyCombination) => {
return combination.toLowerCase().replaceAll(" ", "+");
/** hotkeys-js用のキーに変換する */
const combinationToBindingKey = (
combination: HotkeyCombination
): BindingKey => {
// MetaキーはCommandキーとして扱う
// NOTE: hotkeys-jsにはWinキーが無く、Commandキーとして扱われている
// NOTE: Metaキーは以前採用していたmousetrapがそうだった名残り
const bindingKey = combination
.toLowerCase()
.split(" ")
.map((key) => (key === "meta" ? "command" : key))
.join("+");
return bindingKey as BindingKey;
};

export const hotkeyPlugin: Plugin = {
Expand Down

0 comments on commit cc2bceb

Please sign in to comment.