All files / modules/70-pipeline/components/CanvasButtons CanvasButtons.tsx

100% Statements 39/39
81.48% Branches 22/27
100% Functions 7/7
100% Lines 39/39

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              102x 102x   102x 102x 102x   102x 102x 102x 102x 102x                     102x             48x 48x   2x 2x 2x 2x         48x   1x 1x 1x 1x 1x         48x   1x 1x 1x 1x 1x         48x   1x 1x   1x 1x 1x 1x           48x 48x                                                                             48x                            
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React, { useCallback } from 'react'
import { Layout, ButtonGroup, Button, ButtonVariation } from '@wings-software/uicore'
import type { DiagramEngine } from '@projectstorm/react-diagrams-core'
import cx from 'classnames'
import { useStrings } from 'framework/strings'
import css from './CanvasButtons.module.scss'
 
export enum CanvasButtonsActions {
  ZoomIn,
  ZoomOut,
  Reset,
  ZoomToFit
}
 
interface CanvasButtonsProps {
  engine: DiagramEngine
  className?: string
  callback?: (action: CanvasButtonsActions) => void
  tooltipPosition?: string
  layout?: 'horizontal' | 'vertical'
}
 
export function CanvasButtons({
  engine,
  callback,
  className = '',
  tooltipPosition = 'left',
  layout = 'vertical'
}: CanvasButtonsProps): React.ReactElement {
  const { getString } = useStrings()
  const zoomToFit = useCallback(
    e => {
      e.stopPropagation()
      engine.getModel().setZoomLevel(100)
      engine.zoomToFit()
      callback?.(CanvasButtonsActions.ZoomToFit)
    },
    [engine, callback]
  )
 
  const zoomReset = useCallback(
    e => {
      e.stopPropagation()
      engine.getModel().setZoomLevel(100)
      engine.getModel().setOffset(0, 0)
      engine.repaintCanvas()
      callback?.(CanvasButtonsActions.Reset)
    },
    [engine, callback]
  )
 
  const zoomIn = useCallback(
    e => {
      e.stopPropagation()
      const zoomLevel = engine.getModel().getZoomLevel()
      engine.getModel().setZoomLevel(zoomLevel + 20)
      engine.repaintCanvas()
      callback?.(CanvasButtonsActions.ZoomIn)
    },
    [engine, callback]
  )
 
  const zoomOut = useCallback(
    e => {
      e.stopPropagation()
      const zoomLevel = engine.getModel().getZoomLevel()
      // Minimum Zoom level should be 40
      Eif (zoomLevel >= 60) {
        engine.getModel().setZoomLevel(zoomLevel - 20)
        engine.repaintCanvas()
        callback?.(CanvasButtonsActions.ZoomOut)
      }
    },
    [engine, callback]
  )
 
  const renderButtons = () => (
    <>
      <ButtonGroup>
        <Button
          variation={ButtonVariation.TERTIARY}
          icon="canvas-position"
          tooltip={getString('canvasButtons.zoomToFit')}
          tooltipProps={{ position: tooltipPosition as any }}
          onClick={zoomToFit}
        />
      </ButtonGroup>
      <ButtonGroup>
        <Button
          variation={ButtonVariation.TERTIARY}
          icon="canvas-selector"
          tooltip={getString('reset')}
          onClick={zoomReset}
          tooltipProps={{ position: tooltipPosition as any }}
        />
      </ButtonGroup>
      <span className={layout === 'vertical' ? css.verticalButtons : ''}>
        <ButtonGroup>
          <Button
            variation={ButtonVariation.TERTIARY}
            icon="zoom-in"
            tooltip={getString('canvasButtons.zoomIn')}
            onClick={zoomIn}
            tooltipProps={{ position: tooltipPosition as any }}
          />
          <Button
            variation={ButtonVariation.TERTIARY}
            icon="zoom-out"
            tooltip={getString('canvasButtons.zoomOut')}
            onClick={zoomOut}
            tooltipProps={{ position: tooltipPosition as any }}
          />
        </ButtonGroup>
      </span>
    </>
  )
  return (
    <span className={cx(css.canvasButtons, className)}>
      {layout === 'horizontal' ? (
        <Layout.Horizontal spacing="medium" id="button-group">
          {renderButtons()}
        </Layout.Horizontal>
      ) : (
        <Layout.Vertical spacing="medium" id="button-group">
          {renderButtons()}
        </Layout.Vertical>
      )}
    </span>
  )
}