github.com/tilt-dev/tilt@v0.36.0/web/src/OverviewActionBarKeyboardShortcuts.tsx (about)

     1  import React, { Component } from "react"
     2  import { clearLogs } from "./ClearLogs"
     3  import LogStore from "./LogStore"
     4  import { isTargetEditable } from "./shortcut"
     5  import { ResourceName, UILink } from "./types"
     6  
     7  type Props = {
     8    logStore: LogStore
     9    resourceName: string
    10    endpoints?: UILink[]
    11    openEndpointUrl: (url: string) => void
    12  }
    13  
    14  const keyCodeOne = 49
    15  const keyCodeNine = 57
    16  
    17  /**
    18   * Sets up keyboard shortcuts that depend on the state of the current resource.
    19   */
    20  class OverviewActionBarKeyboardShortcuts extends Component<Props> {
    21    constructor(props: Props) {
    22      super(props)
    23      this.onKeydown = this.onKeydown.bind(this)
    24    }
    25  
    26    componentDidMount() {
    27      document.body.addEventListener("keydown", this.onKeydown)
    28    }
    29  
    30    componentWillUnmount() {
    31      document.body.removeEventListener("keydown", this.onKeydown)
    32    }
    33  
    34    onKeydown(e: KeyboardEvent) {
    35      if (isTargetEditable(e)) {
    36        return
    37      }
    38  
    39      if (e.altKey || e.isComposing) {
    40        return
    41      }
    42  
    43      if (e.ctrlKey || e.metaKey) {
    44        if (e.key === "Backspace" && !e.shiftKey) {
    45          const all = this.props.resourceName === ResourceName.all
    46          clearLogs(this.props.logStore, this.props.resourceName)
    47          e.preventDefault()
    48          return
    49        }
    50  
    51        return
    52      }
    53  
    54      if (e.keyCode >= keyCodeOne && e.keyCode <= keyCodeNine && e.shiftKey) {
    55        let endpointIndex = e.keyCode - keyCodeOne
    56        let endpoint = this.props.endpoints && this.props.endpoints[endpointIndex]
    57        if (!endpoint || !endpoint.url) {
    58          return
    59        }
    60  
    61        this.props.openEndpointUrl(endpoint.url)
    62        e.preventDefault()
    63        return
    64      }
    65    }
    66  
    67    render() {
    68      return <></>
    69    }
    70  }
    71  
    72  export default OverviewActionBarKeyboardShortcuts