All files / src/UI/Canvas Canvas.tsx

100% Statements 37/37
61.11% Branches 11/18
100% Functions 5/5
100% Lines 28/28

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                              7x             7x 7x 7x 7x 7x           7x 9x 9x 9x 9x 9x     9x 9x 9x 9x 9x 9x 9x 9x 6x               9x 3x 3x     9x 9x 3x                 9x                          
/*
    Copyright (C) 2017 Red Hat, Inc.
 
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
 
            http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/
import React, {
  CSSProperties,
  FunctionComponent,
  SVGAttributes,
  useEffect,
  useMemo,
} from 'react';
import { CanvasTransforms } from './CanvasTransforms';
import { css } from '@patternfly/react-styles';
import styles from './Canvas.module.css';
import { useCanvas } from './CanvasContext';
import { useDimensions } from '../useDimensions';
 
export interface ICanvasProps extends SVGAttributes<SVGSVGElement> {
  isFilled?: boolean;
}
 
export const Canvas: FunctionComponent<ICanvasProps> = ({
  children,
  className,
  style,
  isFilled = true,
  ...props
}) => {
  const {
    initialWidth,
    initialHeight,
    setDimension,
    addRedrawListener,
    removeRedrawListener,
  } = useCanvas();
  const [dimensionsRef, dimensions, measure] = useDimensions();
  useEffect(() => {
    setDimension({
      width: dimensions.width,
      height: dimensions.height,
      offsetLeft: dimensions.left,
      offsetTop: dimensions.top,
    });
  }, [dimensions, setDimension]);
 
  useEffect(() => {
    addRedrawListener(measure);
    return () => removeRedrawListener(measure);
  }, [addRedrawListener, measure, removeRedrawListener]);
 
  const { allowPanning, isPanning, zoom, bindCanvas } = useCanvas();
  const svgStyle = useMemo(() => {
    return {
      cursor: allowPanning ? (isPanning ? 'grabbing' : 'grab') : undefined,
      userSelect: allowPanning && isPanning ? 'none' : 'auto',
      backgroundSize: `${30 * zoom}px ${30 * zoom}px`,
      width: initialWidth || '100%',
      height: initialHeight || '100%',
      ...style,
    } as CSSProperties;
  }, [allowPanning, initialHeight, initialWidth, isPanning, style, zoom]);
  return (
    <div ref={dimensionsRef} className={css(isFilled && styles.canvasWrapper)}>
      <svg
        {...props}
        className={css(styles.svg, allowPanning && styles.panning, className)}
        style={svgStyle}
        {...bindCanvas()}
      >
        <CanvasTransforms>{children}</CanvasTransforms>
      </svg>
    </div>
  );
};