diff --git a/src/components/App.jsx b/src/components/App.jsx index b6685db..b043f1c 100644 --- a/src/components/App.jsx +++ b/src/components/App.jsx @@ -74,56 +74,47 @@ export default class App extends React.Component { }) - const keyCodes = { - "esc": 27, - "?": 191, - "o": 79, - "e": 69, - "s": 83, - "d": 68, - "i": 73, - "m": 77, - } - const shortcuts = [ { - keyCode: keyCodes["?"], + key: "?", handler: () => { this.toggleModal("shortcuts"); } }, { - keyCode: keyCodes["o"], + key: "o", handler: () => { this.toggleModal("open"); } }, { - keyCode: keyCodes["e"], + key: "e", handler: () => { this.toggleModal("export"); } }, { - keyCode: keyCodes["d"], + key: "d", handler: () => { this.toggleModal("sources"); } }, { - keyCode: keyCodes["s"], + key: "s", handler: () => { this.toggleModal("settings"); } }, { - keyCode: keyCodes["i"], + key: "i", handler: () => { - this.setMapState("inspect"); + this.setMapState( + this.state.mapState === "map" ? "inspect" : "map" + ); } }, { - keyCode: keyCodes["m"], + key: "m", handler: () => { document.querySelector(".mapboxgl-canvas").focus(); } @@ -131,16 +122,17 @@ export default class App extends React.Component { ] document.body.addEventListener("keyup", (e) => { - if(e.keyCode === keyCodes["esc"]) { + if(e.key === "Escape") { e.target.blur(); document.body.focus(); } - else if(document.activeElement === document.body) { + else if(this.state.isOpen.shortcuts || document.activeElement === document.body) { const shortcut = shortcuts.find((shortcut) => { - return (shortcut.keyCode === e.keyCode) + return (shortcut.key === e.key) }) if(shortcut) { + this.setModal("shortcuts", false); shortcut.handler(e); } } @@ -488,17 +480,21 @@ export default class App extends React.Component { this.setState({ selectedLayerIndex: idx }) } - toggleModal(modalName) { + setModal(modalName, value) { + if(modalName === 'survey' && value === false) { + localStorage.setItem('survey', ''); + } + this.setState({ isOpen: { ...this.state.isOpen, - [modalName]: !this.state.isOpen[modalName] + [modalName]: value } }) + } - if(modalName === 'survey') { - localStorage.setItem('survey', ''); - } + toggleModal(modalName) { + this.setModal(modalName, !this.state.isOpen[modalName]); } render() { @@ -553,6 +549,7 @@ export default class App extends React.Component { const modals =
this.shortcutEl = el} isOpen={this.state.isOpen.shortcuts} onOpenToggle={this.toggleModal.bind(this, 'shortcuts')} />