All files / src/ui Search.js

37.5% Statements 21/56
0% Branches 0/22
62.5% Functions 10/16
38% Lines 19/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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 1701x 1x 1x 1x 1x 1x   1x 1x 2x 2x     1417x                                   118x     1012x                                                                                                                   353x                   118x                   1012x 506x 1012x             506x                                                                                                
import React, {Component} from 'react';
import PropTypes from 'prop-types/prop-types';
import Modal from 'react-modal';
import {Tab, Tabs, TabList, TabPanel} from 'react-tabs';
import 'react-tabs/style/react-tabs.less';
import {say, getBeginCursor, getEndCursor, playSound, WRAP} from '../utils';
 
export default (Editor, searchModes) => {
  const settings = searchModes.reduce((acc, searchMode, i) => {
    acc[i] = searchMode.setting;
    return acc;
  }, {});
 
  return class extends Component {
 
    static propTypes = {
      appElement: PropTypes.object.isRequired
    }
 
    state = {
      showSearchDialog: false,
      searchEngine: null,
      cursor: null,
      settings: settings,
      cmbState: null,
      firstTime: true
    }
 
    displayName = 'Search Component'
 
    componentDidMount() {
      Modal.setAppElement(this.props.appElement);
    }
 
    handleChangeSetting = i => setting => {
      this.setState({
        settings: {...this.state.settings, [i]: setting}, 
        searchEngine: i
      });
    }
 
    handleActivateSearch = (state, done, searchForward) => {
      this.setState({showSearchDialog: true});
      this.callback = done;
      this.setState({cmbState: state});
      this.setState({searchForward: searchForward});
    }
 
    handleCloseModal = () => {
      this.setState({showSearchDialog: false, firstTime: true});
      this.callback();
    }
 
    handleSearch = (forward, cmbState, overrideCur) => {
      console.log(overrideCur);
      if(this.state.searchEngine == null) {
        say("No search setting have been selected.");
        return;
      }
      var searchFrom = overrideCur || this.state.cursor, result;
      // keep searching until we find an unfocused node, or we run out of results
      while((result = searchModes[this.state.searchEngine].search(
        searchFrom,
        this.state.settings[this.state.searchEngine],
        this.cm,
        cmbState,
        forward))) {
        if(result && result.node.id !== cmbState.focusId) break;
        searchFrom = result.cursor;
      }
      if (result !== null) {
        const {node, cursor} = result;
        this.setState({cursor});
        return node;
      } else {
        if(overrideCur) return null; // if there's no wrapped match, give up
        playSound(WRAP);
        const wrappedStart = (forward? getBeginCursor : getEndCursor)(this.cm);
        return this.handleSearch(forward, cmbState, wrappedStart)
      }
    }
 
    handleKeyModal = e => {
      if (e.key === 'Enter' || e.key === 'Escape') { // enter or escape
        this.handleCloseModal();
        if (e.key === 'Escape') return; // don't initiate search
        this.state.searchForward();
        say("Searching for next match. Use PageUp and PageDown to search forwards and backwards");
      }
    }
 
    handleSetCursor = cursor => {
      this.setState({cursor});
    }
 
    // Override default: only allow tab switching via left/right, NOT up/down
    handleTab = (searchEngine, lastTabIdx, event) => {
      this.setState({firstTime: false});
      if(["ArrowDown", "ArrowUp"].includes(event.key)) return false;
      return this.setState({searchEngine});
    }
 
    handleSetCM = cm => this.cm = cm
 
    search = {
      onSearch: this.handleActivateSearch,
      search: this.handleSearch,
      setCursor: this.handleSetCursor,
      setCM: this.handleSetCM,
    }
 
    render() {
      const tabs = searchModes.map(({label}, i) => <Tab key={i}>{label}</Tab>);
      const tabPanels = searchModes.map(({component: SearchMode}, i) => (
        <TabPanel key={i}>
          <SearchMode setting={this.state.settings[i]}
                      firstTime={this.state.firstTime}
                      onChange={this.handleChangeSetting(i)}
                      cmbState={this.state.cmbState} />
        </TabPanel>
      ));
      return (
        <>
          <Editor {...this.props} search={this.search} />
 
          <Modal isOpen={this.state.showSearchDialog}
                 className="wrapper-modal">
            <div tabIndex="-1" className="react-modal" onKeyUp={this.handleKeyModal}
                 role="dialog" aria-label="Search Settings">
              <div className="modal-content">
                <div className="modal-header">
                  <button type="button" className="close" data-dismiss="modal"
                          aria-labelledby="searchTitle"
                          aria-describedby="searchTitle searchDescription"
                          onClick={this.handleCloseModal}>
                    &times;
                  </button>
                  <h5 id="searchTitle" className="modal-title" arial-level="1">
                    Search Settings
                  </h5>
                  <span id="searchDescription">
                    <i>What should <kbd>PgUp</kbd> and <kbd>PgDown</kbd> search for?</i>
                  </span>
                </div>
                <div className="modal-body">
                  <Tabs
                        onSelect={this.handleTab}
                        defaultFocus={true}>
                    <TabList>{tabs}</TabList>
                    {tabPanels}
                  </Tabs>
                </div>
                <div className="modal-footer">
                  <small className="form-text text-muted">
                    <div>
                      <kbd>&larr;</kbd>
                      <kbd>&rarr;</kbd> to change modes;
                      <kbd>&crarr;</kbd>
                      <kbd>esc</kbd> to close and find next;
                    </div>
                  </small>
                </div>
              </div>
            </div>
          </Modal>
        </>
      );
    }
  };
};