feat(fe/pane_login): login on enter keydown

This commit is contained in:
hexxa 2022-02-11 13:18:50 +08:00 committed by Hexxa
parent 0cff932343
commit b06824ebf3
2 changed files with 74 additions and 1 deletions

View file

@ -0,0 +1,59 @@
import * as React from "react";
import { Map } from "immutable";
export interface Hotkey {
key: string;
ctrl?: boolean;
shift?: boolean;
alt?: boolean;
meta?: boolean;
repeat?: boolean;
ev?: KeyboardEvent;
}
export type HotkeyCb = (hotKey?: Hotkey) => void;
export class HotkeyHandler {
private keyMap: Map<string, HotkeyCb>;
constructor() {
this.keyMap = Map<string, HotkeyCb>();
}
getSign = (hk: Hotkey): string => {
let sign = hk.key;
sign = hk.ctrl != null && hk.ctrl ? `${sign}+ctrl` : sign;
sign = hk.shift != null && hk.shift ? `${sign}+shift` : sign;
sign = hk.alt != null && hk.alt ? `${sign}+alt` : sign;
sign = hk.meta != null && hk.meta ? `${sign}+meta` : sign;
sign = hk.repeat != null && hk.repeat ? `${sign}+repeat` : sign;
return sign;
};
add = (hk: Hotkey, handler: HotkeyCb) => {
const sign = this.getSign(hk);
this.keyMap = this.keyMap.set(sign, handler);
};
handle = (ev: KeyboardEvent) => {
const hotKey = {
key: ev.key,
ctrl: ev.ctrlKey,
shift: ev.shiftKey,
alt: ev.altKey,
meta: ev.metaKey,
repeat: ev.repeat,
};
const sign = this.getSign(hotKey);
if (this.keyMap.has(sign)) {
const handler = this.keyMap.get(sign);
handler(hotKey);
}
};
printMap = () => {
console.log(this.keyMap.toMap());
};
}

View file

@ -1,5 +1,5 @@
import * as React from "react"; import * as React from "react";
import { List } from "immutable"; import { List, Map } from "immutable";
import { ICoreState, MsgProps, UIProps } from "./core_state"; import { ICoreState, MsgProps, UIProps } from "./core_state";
import { Flexbox } from "./layout/flexbox"; import { Flexbox } from "./layout/flexbox";
@ -8,6 +8,7 @@ import { alertMsg } from "../common/env";
import { Quota, Preferences } from "../client"; import { Quota, Preferences } from "../client";
import { getErrMsg } from "../common/utils"; import { getErrMsg } from "../common/utils";
import { ctrlOn, ctrlOff, loadingCtrl } from "../common/controls"; import { ctrlOn, ctrlOff, loadingCtrl } from "../common/controls";
import { HotkeyHandler } from "../common/hotkeys";
export interface ExtInfo { export interface ExtInfo {
usedSpace: string; usedSpace: string;
@ -39,6 +40,8 @@ export interface State {
export class AuthPane extends React.Component<Props, State, {}> { export class AuthPane extends React.Component<Props, State, {}> {
private update: (updater: (prevState: ICoreState) => ICoreState) => void; private update: (updater: (prevState: ICoreState) => ICoreState) => void;
private hotkeyHandler: HotkeyHandler;
constructor(p: Props) { constructor(p: Props) {
super(p); super(p);
this.update = p.update; this.update = p.update;
@ -50,6 +53,17 @@ export class AuthPane extends React.Component<Props, State, {}> {
}; };
} }
componentDidMount(): void {
this.hotkeyHandler = new HotkeyHandler();
this.hotkeyHandler.add({ key: "Enter" }, this.login);
document.addEventListener("keyup", this.hotkeyHandler.handle);
}
componentWillUnmount() {
document.removeEventListener("keyup", this.hotkeyHandler.handle);
}
changeUser = (ev: React.ChangeEvent<HTMLInputElement>) => { changeUser = (ev: React.ChangeEvent<HTMLInputElement>) => {
this.setState({ user: ev.target.value }); this.setState({ user: ev.target.value });
}; };