import * as React from "react"; import { createRoot, Root } from 'react-dom/client'; import Config from "../../config"; import KeybindDialogComponent from "./KeybindDialogComponent"; import { formatKey, Keybind, keybindEquals, keybindToString } from "../../../maze-utils/src/config"; export interface KeybindProps { option: string; } export interface KeybindState { keybind: Keybind; } let dialog; let root: Root; 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); root = createRoot(dialog); root.render( this.closeEditDialog(updateWith)} />); } closeEditDialog(updateWith: Keybind): void { root.unmount(); dialog.remove(); if (updateWith != null) this.setState({keybind: updateWith}); } unbind(): void { this.setState({keybind: null}); Config.config[this.props.option] = null; } } export default KeybindComponent;