All files / src/ui Toolbar.js

30.36% Statements 17/56
12.9% Branches 4/31
25% Functions 4/16
34% Lines 17/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 1531x 1x 1x 1x 1x 1x 1x 1x     12x 3x 3x                                                                                                                                                                 10x 10x                       10x 10x 10x                         18x                                                              
import React, {Component} from 'react';
import PropTypes from 'prop-types/prop-types';
import classNames from 'classnames';
import PrimitiveList from './PrimitiveList';
import PrimitiveBlock from './PrimitiveBlock';
import {PrimitiveGroup} from '../parsers/primitives';
import CodeMirror from 'codemirror';
import './Toolbar.less';
 
export default class Toolbar extends Component {
  constructor(props) {
    super(props);
    this.handleFocusPrimitive = this.handleFocusPrimitive.bind(this);
  }
 
  static propTypes = {
    primitives: PropTypes.instanceOf(PrimitiveGroup),
    languageId: PropTypes.string, // used to find the .blocks-language-{languageId} CSS class
    blockMode: PropTypes.bool,
  }
 
  static defaultProps = {
    primitives: null,
    blockMode: false
  }
 
  state = {
    search: '',
    selectedPrimitive: null,
  }
 
  changeSearch = (event) => {
    this.setState({search: event.target.value});
  }
 
  clearSearch = () => {
    this.setState({search: ''});
  }
 
  next() {
    let primitives = this.getPrimitives();
    if (primitives.length == 0) return; // Nothing to select.
    let i = this.getSelectedPrimitiveIndex(primitives);
    // If nothing is selected, select the first primitive, otherwise select the next.
    if (i === primitives.length - 1) i -= 1;
    if (i === null) i = -1;
    this.selectPrimitive(primitives[i + 1]);
  }
 
  prev() {
    let primitives = this.getPrimitives();
    if (primitives.length == 0) return; // Nothing to select.
    let i = this.getSelectedPrimitiveIndex(primitives);
    // If the first primitive is selected, keep it, otherwise select the previous.
    if (i === 0) i = 1;
    this.selectPrimitive(i === null ? null : primitives[i - 1]);
  }
 
  handleFocusPrimitive(selectedPrimitive) {
    this.setState({selectedPrimitive: selectedPrimitive});
  }
 
  selectPrimitive(selectedPrimitive) {
    if (selectedPrimitive?.element) {      
      selectedPrimitive.element.focus(); // will trigger handleFocusPrimitive
    } else {
      this.setState({selectedPrimitive: selectedPrimitive});
    }
  }
 
  // NOTE(DS26GTE): this is just so onBlur has a non-null value
  handleBlurPrimitive() {}
 
  handleKeyDown = event => {
    switch(CodeMirror.keyName(event)) {
    case 'Esc':
      event.target.blur();
      return;
    case 'Down':
      event.preventDefault();
      this.next();
      return;
    case 'Up':
      event.preventDefault();
      this.prev();
      return;
    default:
      event.stopPropagation();
      return;
    }
  }
 
  getPrimitives() {
    Eif (this.props.primitives) {
      return this.props.primitives.filter(this.state.search).primitives;
    } else {
      return [];
    }
  }
 
  getSelectedPrimitiveIndex(primitives) {
    const idx = primitives.findIndex(p => p === this.state.selectedPrimitive);
    return (idx == -1)? null : idx;
  }
 
  render() {
    let primitives = this.getPrimitives();
    const selected = this.state.selectedPrimitive;
    return (
      <div className={classNames('blocks-ui Toolbar', {'has-selected':!!selected})}>
        <div className="search-box" role="search">
          <label className="screenreader-only" htmlFor="search_box">
            <h2>Search Functions</h2>
          </label>
          <input type="search"
            id="search_box"
            placeholder="Search functions"
            disabled={!this.props.blockMode}
            className="form-control"
            value={this.state.search}
            onKeyDown={this.handleKeyDown}
            ref={(elt) => { this.primitiveSearch = elt; }} 
            onChange={this.changeSearch} />
          {this.state.search ?
            <button 
              aria-label="clear text" 
              className="glyphicon glyphicon-remove" 
              onClick={this.clearSearch} />
            : null}
        </div>
        <div className="primitives-box" tabIndex="-1">
          <PrimitiveList
            primitives={primitives}
            onFocus={this.handleFocusPrimitive}
            onBlur={this.handleBlurPrimitive}
            onKeyDown={this.handleKeyDown}
            selected={selected && selected.name}
            searchString={this.state.search}
            />
        </div>
        <div className={classNames('selected-primitive', `blocks-language-${this.props.languageId}`)}>
          <div className="block-header">Block</div>
          {selected? 
            (<PrimitiveBlock 
                primitive={selected} 
                id={primitives.findIndex(p => p.name === selected.name)} 
            />) 
            : ""}
        </div>
      </div>
    );
  }
}