All files / src/components/fileOperator SearchSidebar.tsx

4% Statements 1/25
0% Branches 0/18
0% Functions 0/7
4% Lines 1/25

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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201                                            1x                                                                                                                                                                                                                                                                                                                                                                    
import * as React from 'react';
import { MinimalButton, Spinner, TextBox } from '@react-pdf-viewer/core';
import {
  Match,
  NextIcon,
  PreviousIcon,
  RenderSearchProps,
  SearchPlugin
} from '@react-pdf-viewer/search';
import { ReactElement } from "react";
 
enum SearchStatus {
  NotSearchedYet,
  Searching,
  FoundResults,
}
 
interface SearchSidebarProps {
  searchPluginInstance: SearchPlugin;
}
 
// eslint-disable-next-line react/prop-types
const SearchSidebar: React.FC<SearchSidebarProps> = ({ searchPluginInstance }): ReactElement => {
  const [searchStatus, setSearchStatus] = React.useState(SearchStatus.NotSearchedYet);
  const [matches, setMatches] = React.useState<Match[]>([]);
 
  // eslint-disable-next-line react/prop-types
  const { Search } = searchPluginInstance;
 
  const renderMatchSample = (match: Match): ReactElement => {
    //  match.startIndex    match.endIndex
    //      |                       |
    //      ▼                       ▼
    //  ....[_____props.keyword_____]....
 
    const wordsBefore = match.pageText.substr(match.startIndex - 20, 20);
    let words = wordsBefore.split(' ');
    words.shift();
    const begin = words.length === 0 ? wordsBefore : words.join(' ');
 
    const wordsAfter = match.pageText.substr(match.endIndex, 60);
    words = wordsAfter.split(' ');
    words.pop();
    const end = words.length === 0 ? wordsAfter : words.join(' ');
 
    return (
      <div>
        {begin}
        <span style={{ backgroundColor: 'rgb(255, 255, 0)' }}>
                    {match.pageText.substring(match.startIndex, match.endIndex)}
                </span>
        {end}
      </div>
    );
  };
 
  return (
    <Search>
      {(renderSearchProps: RenderSearchProps) => {
        const {
          currentMatch,
          keyword,
          setKeyword,
          jumpToMatch,
          jumpToNextMatch,
          jumpToPreviousMatch,
          search
        } =
          renderSearchProps;
 
        const handleSearchKeyDown = (e: React.KeyboardEvent): void => {
          if (e.key === 'Enter' && keyword) {
            setSearchStatus(SearchStatus.Searching);
            search().then((matches) => {
              setSearchStatus(SearchStatus.FoundResults);
              setMatches(matches);
            });
          }
        };
 
        return (
          <div
            style={{
              display: 'flex',
              flexDirection: 'column',
              height: '100%',
              overflow: 'hidden',
              width: '100%',
            }}
          >
            <div style={{ padding: '.5rem' }}>
              <div style={{ position: 'relative' }}>
                <TextBox
                  placeholder="Enter to search"
                  value={keyword}
                  onChange={setKeyword}
                  onKeyDown={handleSearchKeyDown}
                />
                {searchStatus === SearchStatus.Searching && (
                  <div
                    style={{
                      alignItems: 'center',
                      display: 'flex',
                      bottom: 0,
                      position: 'absolute',
                      right: '.5rem',
                      top: 0,
                    }}
                  >
                    <Spinner size="1.5rem" />
                  </div>
                )}
              </div>
            </div>
            {searchStatus === SearchStatus.FoundResults && (
              <>
                {matches.length === 0 && 'Not found'}
                {matches.length > 0 && (
                  <>
                    <div
                      style={{
                        alignItems: 'center',
                        display: 'flex',
                        padding: '.5rem',
                      }}
                    >
                      <div
                        style={{
                          color: 'rgba(0, 0, 0, .5)',
                          fontSize: '.8rem',
                          marginRight: '.5rem',
                        }}
                      >
                        Found {matches.length} results
                      </div>
                      <div style={{ marginLeft: 'auto', marginRight: '.5rem' }}>
                        <MinimalButton onClick={jumpToPreviousMatch}>
                          <PreviousIcon />
                        </MinimalButton>
                      </div>
                      <MinimalButton onClick={jumpToNextMatch}>
                        <NextIcon />
                      </MinimalButton>
                    </div>
                    <div
                      style={{
                        borderTop: '1px solid rgba(0, 0, 0, .2)',
                        flex: 1,
                        overflow: 'auto',
                        padding: '.5rem 1rem',
                      }}
                    >
                      {matches.map((match, index) => (
                        <div key={index} style={{ margin: '1rem 0' }}>
                          <div
                            style={{
                              display: 'flex',
                              justifyContent: 'space-between',
                              marginBottom: '.5rem',
                            }}
                          >
                            <div>#{index + 1}</div>
                            <div
                              style={{
                                color: 'rgba(0, 0, 0, .5)',
                                fontSize: '.8rem',
                                textAlign: 'right',
                              }}
                            >
                              Page {match.pageIndex + 1}
                            </div>
                          </div>
                          <div
                            style={{
                              backgroundColor:
                                currentMatch === index + 1 ? 'rgba(0, 0, 0, .1)' : '',
                              border: '1px solid rgba(0, 0, 0, .2)',
                              borderRadius: '.25rem',
                              cursor: 'pointer',
                              overflowWrap: 'break-word',
                              padding: '.5rem',
                            }}
                            onClick={() => jumpToMatch(index + 1)}
                          >
                            {renderMatchSample(match)}
                          </div>
                        </div>
                      ))}
                    </div>
                  </>
                )}
              </>
            )}
          </div>
        );
      }}
    </Search>
  );
};
 
export default SearchSidebar;