All files / src/components/fileOperator PdfPreview.tsx

66.66% Statements 8/12
100% Branches 4/4
25% Functions 1/4
66.66% Lines 8/12

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                        1x 4x   4x 3x 3x     1x                                                                                                                                                       1x                                 1x                                    
import React, { ReactElement } from "react";
import { LocalizationMap, Viewer } from "@react-pdf-viewer/core";
import { Worker } from '@react-pdf-viewer/core';
import '@react-pdf-viewer/core/lib/styles/index.css';
import '@react-pdf-viewer/default-layout/lib/styles/index.css';
import '@react-pdf-viewer/toolbar/lib/styles/index.css';
import zh_CN from '@react-pdf-viewer/locales/lib/zh_CN.json';
import { defaultLayoutPlugin, } from '@react-pdf-viewer/default-layout';
import { SearchIcon } from '@react-pdf-viewer/search';
import { MoreActionsPopover, ToolbarProps, ToolbarSlot } from '@react-pdf-viewer/toolbar';
import SearchSidebar from './SearchSidebar';
 
const PdfPreview = (props: { previewBlob: string }): ReactElement => {
  const { previewBlob } = props;
 
  if (previewBlob == null || previewBlob === '') {
    console.warn(`preview blob is [${previewBlob}], will not render pdf preview component`);
    return <></>;
  }
 
  const renderToolbar = (Toolbar: (props: ToolbarProps) => ReactElement): ReactElement => (
    <Toolbar>
      {(toolbarSlot: ToolbarSlot) => {
        const {
          CurrentPageInput,
          EnterFullScreen,
          GoToNextPage,
          GoToPreviousPage,
          NumberOfPages,
          Print,
          SwitchTheme,
          Zoom,
          ZoomIn,
          ZoomOut,
        } = toolbarSlot;
 
        return (
          <div className="rpv-toolbar" role="toolbar" aria-orientation="horizontal">
            <div className="rpv-toolbar__left">
              <div className="rpv-toolbar__item">
                <ZoomOut />
              </div>
              <div className="rpv-core__display--hidden rpv-core__display--block-small">
                <div className="rpv-toolbar__item">
                  <Zoom />
                </div>
              </div>
              <div className="rpv-toolbar__item">
                <ZoomIn />
              </div>
            </div>
            <div className="rpv-toolbar__center">
              <div className="rpv-core__display--hidden rpv-core__display--block-small">
                <div className="rpv-toolbar__item">
                  <GoToPreviousPage />
                </div>
              </div>
              <div className="rpv-toolbar__item">
                <CurrentPageInput />{' '}
                <span className="rpv-toolbar__label">
                  / <NumberOfPages />
                </span>
              </div>
              <div className="rpv-core__display--hidden rpv-core__display--block-small">
                <div className="rpv-toolbar__item">
                  <GoToNextPage />
                </div>
              </div>
            </div>
            <div className="rpv-toolbar__right">
              <div className="rpv-core__display--hidden rpv-core__display--block-medium">
                <div className="rpv-toolbar__item">
                  <SwitchTheme />
                </div>
              </div>
              <div className="rpv-core__display--hidden rpv-core__display--block-medium">
                <div className="rpv-toolbar__item">
                  <EnterFullScreen />
                </div>
              </div>
              <div className="rpv-core__display--hidden rpv-core__display--block-medium">
                <div className="rpv-toolbar__item">
                  <Print />
                </div>
              </div>
              <div className="rpv-toolbar__item">
                <MoreActionsPopover toolbarSlot={toolbarSlot} />
              </div>
            </div>
          </div>
        );
      }}
    </Toolbar>
  );
 
 
  const defaultLayoutPluginInstance = defaultLayoutPlugin({
    renderToolbar,
    sidebarTabs: (defaultTabs) =>
      [
        {
          content: (
            <SearchSidebar
              searchPluginInstance={
                defaultLayoutPluginInstance.toolbarPluginInstance.searchPluginInstance
              }
            />
          ),
          icon: <SearchIcon />,
          title: 'Search',
        },
      ].concat(defaultTabs),
  });
  return (
    <Worker workerUrl="/3rd/pdf.worker.min.js">
      <div
        style={{
          height: '100%',
        }}
      >
        <Viewer
          fileUrl={previewBlob as string} theme={{ theme: 'auto' }}
          plugins={[defaultLayoutPluginInstance]}
          localization={zh_CN as unknown as LocalizationMap}
        />
      </div>
    </Worker>
  );
};
 
export default PdfPreview;