import type { FC } from 'react' import { Fragment, memo, useCallback, useState, } from 'react' import { RiZoomInLine, RiZoomOutLine, } from '@remixicon/react' import { useTranslation } from 'react-i18next' import { useReactFlow, useViewport, } from 'reactflow' import { useNodesSyncDraft, useWorkflowReadOnly, } from '../hooks' import { getKeyboardKeyNameBySystem, } from '../utils' import ShortcutsName from '../shortcuts-name' import TipPopup from './tip-popup' import cn from '@/utils/classnames' import { PortalToFollowElem, PortalToFollowElemContent, PortalToFollowElemTrigger, } from '@/app/components/base/portal-to-follow-elem' enum ZoomType { zoomIn = 'zoomIn', zoomOut = 'zoomOut', zoomToFit = 'zoomToFit', zoomTo25 = 'zoomTo25', zoomTo50 = 'zoomTo50', zoomTo75 = 'zoomTo75', zoomTo100 = 'zoomTo100', zoomTo200 = 'zoomTo200', } const ZoomInOut: FC = () => { const { t } = useTranslation() const { zoomIn, zoomOut, zoomTo, fitView, } = useReactFlow() const { zoom } = useViewport() const { handleSyncWorkflowDraft } = useNodesSyncDraft() const [open, setOpen] = useState(false) const { workflowReadOnly, getWorkflowReadOnly, } = useWorkflowReadOnly() const ZOOM_IN_OUT_OPTIONS = [ [ { key: ZoomType.zoomTo200, text: '200%', }, { key: ZoomType.zoomTo100, text: '100%', }, { key: ZoomType.zoomTo75, text: '75%', }, { key: ZoomType.zoomTo50, text: '50%', }, { key: ZoomType.zoomTo25, text: '25%', }, ], [ { key: ZoomType.zoomToFit, text: t('workflow.operator.zoomToFit'), }, ], ] const handleZoom = (type: string) => { if (workflowReadOnly) return if (type === ZoomType.zoomToFit) fitView() if (type === ZoomType.zoomTo25) zoomTo(0.25) if (type === ZoomType.zoomTo50) zoomTo(0.5) if (type === ZoomType.zoomTo75) zoomTo(0.75) if (type === ZoomType.zoomTo100) zoomTo(1) if (type === ZoomType.zoomTo200) zoomTo(2) handleSyncWorkflowDraft() } const handleTrigger = useCallback(() => { if (getWorkflowReadOnly()) return setOpen(v => !v) }, [getWorkflowReadOnly]) return (
{ e.stopPropagation() zoomOut() }} >
{parseFloat(`${zoom * 100}`).toFixed(0)}%
{ e.stopPropagation() zoomIn() }} >
{ ZOOM_IN_OUT_OPTIONS.map((options, i) => ( { i !== 0 && (
) }
{ options.map(option => (
handleZoom(option.key)} > {option.text} { option.key === ZoomType.zoomToFit && ( ) } { option.key === ZoomType.zoomTo50 && ( ) } { option.key === ZoomType.zoomTo100 && ( ) }
)) }
)) }
) } export default memo(ZoomInOut)