Skip to content
Snippets Groups Projects
Select Git revision
  • 91f9f0df2e5f846a1a7f29009e6c0da12cc08e5c
  • mui5-annotation-on-video-stable default
  • get_setter_canvasSizeInformations
  • fix-error-div-into-p
  • annotation-on-video-v2
  • detached
  • annotation-on-video-r17
  • mui5
  • mui5-react-18
  • jacob-test
  • annotation-on-video protected
  • master
  • test-antoinev1
  • 20-fetch-thumbnail-on-annotation
  • add-research-field
  • Save
  • add-plugin
  • 14-wip-no-seek-to
  • 14-bug-on-video-time-control
  • 9_wip_videotests
  • _upgrade_material_ui
  • latest-tetras-16
  • v3.3.0
  • v3.2.0
  • v3.1.1
  • v3.1.0
  • v3.0.0
  • v3.0.0-rc.7
  • v3.0.0-rc.6
  • v3.0.0-rc.5
  • v3.0.0-rc.4
  • v3.0.0-rc.3
  • v3.0.0-rc.2
  • v3.0.0-rc.1
  • v3.0.0-beta.10
  • v3.0.0-beta.9
  • v3.0.0-beta.8
  • v3.0.0-beta.7
  • v3.0.0-beta.6
  • v3.0.0-beta.5
  • v3.0.0-beta.3
41 results

SearchHit.js

Blame
  • SearchHit.js 4.43 KiB
    import React, { Component } from 'react';
    import PropTypes from 'prop-types';
    import clsx from 'clsx';
    import Button from '@material-ui/core/Button';
    import ListItem from '@material-ui/core/ListItem';
    import ListItemText from '@material-ui/core/ListItemText';
    import Typography from '@material-ui/core/Typography';
    import Chip from '@material-ui/core/Chip';
    import SanitizedHtml from '../containers/SanitizedHtml';
    import TruncatedHit from '../lib/TruncatedHit';
    import { ScrollTo } from './ScrollTo';
    
    /** */
    export class SearchHit extends Component {
      /** */
      constructor(props) {
        super(props);
    
        this.handleClick = this.handleClick.bind(this);
      }
    
      /** */
      handleClick() {
        const {
          annotationId, selectContentSearchAnnotation, windowId,
        } = this.props;
    
        selectContentSearchAnnotation(windowId, [annotationId]);
      }
    
      /** */
      render() {
        const {
          adjacent,
          annotation,
          annotationLabel,
          canvasLabel,
          classes,
          companionWindowId,
          containerRef,
          hit,
          focused,
          index,
          showDetails,
          selected,
          t,
        } = this.props;
    
        if (focused && !selected) return null;
    
        const truncatedHit = focused ? hit : hit && new TruncatedHit(hit);
        const truncated = hit && truncatedHit.before !== hit.before && truncatedHit.after !== hit.after;
        const canvasLabelHtmlId = `${companionWindowId}-${index}`;
    
        return (
          <ScrollTo
            containerRef={containerRef}
            offsetTop={96} // offset for the height of the form above
            scrollTo={selected}
          >
            <ListItem
              className={clsx(
                classes.listItem,
                {
                  [classes.adjacent]: adjacent,
                  [classes.selected]: selected,
                  [classes.focused]: focused,
                },
              )}
              button={!selected}
              component="li"
              onClick={this.handleClick}
              selected={selected}
            >
              <ListItemText primaryTypographyProps={{ variant: 'body1' }}>
                <Typography variant="subtitle2" className={classes.subtitle}>
                  <Chip component="span" label={index + 1} className={classes.hitCounter} />
                  <span id={canvasLabelHtmlId}>
                    {canvasLabel}
                  </span>
                </Typography>
                {annotationLabel && (
                  <Typography variant="subtitle2">{annotationLabel}</Typography>
                )}
                {hit && (
                  <>
                    <SanitizedHtml ruleSet="iiif" htmlString={truncatedHit.before} />
                    {' '}
                    <strong>
                      <SanitizedHtml ruleSet="iiif" htmlString={truncatedHit.match} />
                    </strong>
                    {' '}
                    <SanitizedHtml ruleSet="iiif" htmlString={truncatedHit.after} />
                    {' '}
                    { truncated && !focused && (
                      <Button className={classes.inlineButton} onClick={showDetails} color="secondary" size="small" aria-describedby={canvasLabelHtmlId}>
                        {t('more')}
                      </Button>
                    )}
                  </>
                )}
                {!hit && annotation && <SanitizedHtml ruleSet="iiif" htmlString={annotation.chars} />}
              </ListItemText>
            </ListItem>
          </ScrollTo>
        );
      }
    }
    
    SearchHit.propTypes = {
      adjacent: PropTypes.bool,
      annotation: PropTypes.shape({
        content: PropTypes.string,
      }),
      annotationId: PropTypes.string,
      annotationLabel: PropTypes.string,
      canvasLabel: PropTypes.string,
      classes: PropTypes.objectOf(PropTypes.string),
      companionWindowId: PropTypes.string,
      containerRef: PropTypes.oneOfType([
        PropTypes.func,
        PropTypes.shape({ current: PropTypes.instanceOf(Element) }),
      ]),
      focused: PropTypes.bool,
      hit: PropTypes.shape({
        after: PropTypes.string,
        before: PropTypes.string,
        match: PropTypes.string,
      }),
      index: PropTypes.number,
      selectContentSearchAnnotation: PropTypes.func,
      selected: PropTypes.bool,
      showDetails: PropTypes.func,
      t: PropTypes.func,
      windowId: PropTypes.string.isRequired, // eslint-disable-line react/no-unused-prop-types
    };
    
    SearchHit.defaultProps = {
      adjacent: false,
      annotation: undefined,
      annotationId: undefined,
      annotationLabel: undefined,
      canvasLabel: undefined,
      classes: {},
      companionWindowId: undefined,
      containerRef: undefined,
      focused: false,
      hit: undefined,
      index: undefined,
      selectContentSearchAnnotation: () => {},
      selected: false,
      showDetails: () => {},
      t: k => k,
    };