import * as React from "react"; import * as ReactDOM from "react-dom"; import Config from "../../config"; import { Keybind } from "../../types"; import KeybindDialogComponent from "./KeybindDialogComponent"; import { keybindEquals, keybindToString, formatKey } from "../../utils/configUtils"; export interface KeybindProps { option: string; } export interface KeybindState { keybind: Keybind; } let dialog; class KeybindComponent extends React.Component { constructor(props: KeybindProps) { super(props); this.state = {keybind: Config.config[this.props.option]}; } render(): React.ReactElement { return( <>
this.openEditDialog()}> {this.state.keybind?.ctrl &&
Ctrl
} {this.state.keybind?.ctrl && +} {this.state.keybind?.alt &&
Alt
} {this.state.keybind?.alt && +} {this.state.keybind?.shift &&
Shift
} {this.state.keybind?.shift && +} {this.state.keybind?.key != null &&
{formatKey(this.state.keybind.key)}
} {this.state.keybind == null && {chrome.i18n.getMessage("notSet")}}
{this.state.keybind != null &&
this.unbind()}> {chrome.i18n.getMessage("unbind")}
} ); } equals(other: Keybind): boolean { return keybindEquals(this.state.keybind, other); } toString(): string { return keybindToString(this.state.keybind); } openEditDialog(): void { dialog = parent.document.createElement("div"); dialog.id = "keybind-dialog"; parent.document.body.prepend(dialog); ReactDOM.render( this.closeEditDialog(updateWith)} />, dialog); } closeEditDialog(updateWith: Keybind): void { ReactDOM.unmountComponentAtNode(dialog); dialog.remove(); if (updateWith != null) this.setState({keybind: updateWith}); } unbind(): void { this.setState({keybind: null}); Config.config[this.props.option] = null; } } export default KeybindComponent;