ConceptPortal-public/rsconcept/frontend/src/pages/OssPage/EditorOssGraph/ToolbarOssGraph.tsx

188 lines
5.8 KiB
TypeScript
Raw Normal View History

'use client';
2024-07-21 15:19:57 +03:00
import clsx from 'clsx';
import { useMemo } from 'react';
2024-07-21 15:19:57 +03:00
2024-07-26 21:09:16 +03:00
import {
IconAnimation,
IconAnimationOff,
IconDestroy,
IconEdit2,
IconExecute,
2024-07-26 21:09:16 +03:00
IconFitImage,
IconGrid,
IconImage,
IconLineStraight,
IconLineWave,
IconNewItem,
IconReset,
IconSave
} from '@/components/Icons';
2024-07-21 15:19:57 +03:00
import BadgeHelp from '@/components/info/BadgeHelp';
import MiniButton from '@/components/ui/MiniButton';
import { HelpTopic } from '@/models/miscellaneous';
import { OperationType } from '@/models/oss';
2024-07-21 15:19:57 +03:00
import { PARAMETER } from '@/utils/constants';
2024-07-23 23:04:21 +03:00
import { prepareTooltip } from '@/utils/labels';
2024-07-21 15:19:57 +03:00
import { useOssEdit } from '../OssEditContext';
interface ToolbarOssGraphProps {
2024-07-23 23:04:21 +03:00
isModified: boolean;
2024-07-24 18:11:39 +03:00
showGrid: boolean;
2024-07-26 21:09:16 +03:00
edgeAnimate: boolean;
edgeStraight: boolean;
2024-07-21 15:19:57 +03:00
onCreate: () => void;
2024-07-23 23:04:21 +03:00
onDelete: () => void;
onEdit: () => void;
onExecute: () => void;
2024-07-23 23:04:21 +03:00
onFitView: () => void;
2024-07-24 18:11:39 +03:00
onSaveImage: () => void;
2024-07-23 23:04:21 +03:00
onSavePositions: () => void;
onResetPositions: () => void;
2024-07-24 18:11:39 +03:00
toggleShowGrid: () => void;
2024-07-26 21:09:16 +03:00
toggleEdgeAnimate: () => void;
toggleEdgeStraight: () => void;
2024-07-21 15:19:57 +03:00
}
2024-07-23 23:04:21 +03:00
function ToolbarOssGraph({
isModified,
2024-07-24 18:11:39 +03:00
showGrid,
2024-07-26 21:09:16 +03:00
edgeAnimate,
edgeStraight,
2024-07-23 23:04:21 +03:00
onCreate,
onDelete,
onEdit,
onExecute,
2024-07-23 23:04:21 +03:00
onFitView,
2024-07-24 18:11:39 +03:00
onSaveImage,
2024-07-23 23:04:21 +03:00
onSavePositions,
2024-07-24 18:11:39 +03:00
onResetPositions,
2024-07-26 21:09:16 +03:00
toggleShowGrid,
toggleEdgeAnimate,
toggleEdgeStraight
2024-07-23 23:04:21 +03:00
}: ToolbarOssGraphProps) {
2024-07-21 15:19:57 +03:00
const controller = useOssEdit();
const selectedOperation = useMemo(
() => controller.schema?.operationByID.get(controller.selected[0]),
[controller.selected, controller.schema]
);
const readyForSynthesis = useMemo(() => {
if (!selectedOperation || selectedOperation.operation_type !== OperationType.SYNTHESIS) {
return false;
}
if (!controller.schema || selectedOperation.result) {
return false;
}
const argumentIDs = controller.schema.graph.expandInputs([selectedOperation.id]);
if (!argumentIDs || argumentIDs.length < 2) {
return false;
}
const argumentOperations = argumentIDs.map(id => controller.schema!.operationByID.get(id)!);
if (argumentOperations.some(item => item.result === null)) {
return false;
}
return true;
}, [selectedOperation, controller.schema]);
2024-07-21 15:19:57 +03:00
return (
2024-07-26 21:09:16 +03:00
<div className='flex flex-col items-center'>
<div className='cc-icons'>
<MiniButton
title='Сбросить изменения'
icon={<IconReset size='1.25rem' className='icon-primary' />}
disabled={!isModified}
onClick={onResetPositions}
/>
2024-07-23 23:04:21 +03:00
<MiniButton
2024-07-26 21:09:16 +03:00
icon={<IconFitImage size='1.25rem' className='icon-primary' />}
title='Сбросить вид'
onClick={onFitView}
2024-07-23 23:04:21 +03:00
/>
<MiniButton
2024-07-26 21:09:16 +03:00
title={showGrid ? 'Скрыть сетку' : 'Отобразить сетку'}
icon={
showGrid ? (
<IconGrid size='1.25rem' className='icon-green' />
) : (
<IconGrid size='1.25rem' className='icon-primary' />
)
}
onClick={toggleShowGrid}
2024-07-23 23:04:21 +03:00
/>
2024-07-21 15:19:57 +03:00
<MiniButton
2024-07-26 21:09:16 +03:00
title={edgeStraight ? 'Связи: прямые' : 'Связи: безье'}
icon={
edgeStraight ? (
<IconLineStraight size='1.25rem' className='icon-primary' />
) : (
<IconLineWave size='1.25rem' className='icon-primary' />
)
}
onClick={toggleEdgeStraight}
/>
<MiniButton
title={edgeAnimate ? 'Анимация: вкл' : 'Анимация: выкл'}
icon={
edgeAnimate ? (
<IconAnimation size='1.25rem' className='icon-primary' />
) : (
<IconAnimationOff size='1.25rem' className='icon-primary' />
)
}
onClick={toggleEdgeAnimate}
2024-07-21 15:19:57 +03:00
/>
2024-07-23 23:04:21 +03:00
<MiniButton
2024-07-26 21:09:16 +03:00
icon={<IconImage size='1.25rem' className='icon-primary' />}
title='Сохранить изображение'
onClick={onSaveImage}
2024-07-23 23:04:21 +03:00
/>
2024-07-26 21:09:16 +03:00
<BadgeHelp
topic={HelpTopic.UI_OSS_GRAPH}
className={clsx(PARAMETER.TOOLTIP_WIDTH, 'sm:max-w-[40rem]')}
offset={4}
/>
</div>
{controller.isMutable ? (
<div className='cc-icons'>
<MiniButton
titleHtml={prepareTooltip('Сохранить изменения', 'Ctrl + S')}
icon={<IconSave size='1.25rem' className='icon-primary' />}
disabled={controller.isProcessing || !isModified}
onClick={onSavePositions}
/>
<MiniButton
titleHtml={prepareTooltip('Новая операция', 'Ctrl + Q')}
2024-07-26 21:09:16 +03:00
icon={<IconNewItem size='1.25rem' className='icon-green' />}
disabled={controller.isProcessing}
onClick={onCreate}
/>
<MiniButton
title='Выполнить операцию'
icon={<IconExecute size='1.25rem' className='icon-green' />}
disabled={controller.isProcessing || controller.selected.length !== 1 || !readyForSynthesis}
onClick={onExecute}
/>
<MiniButton
titleHtml={prepareTooltip('Редактировать выбранную', 'Двойной клик')}
icon={<IconEdit2 size='1.25rem' className='icon-primary' />}
disabled={controller.selected.length !== 1 || controller.isProcessing}
onClick={onEdit}
/>
<MiniButton
titleHtml={prepareTooltip('Удалить выбранную', 'Delete')}
2024-07-26 21:09:16 +03:00
icon={<IconDestroy size='1.25rem' className='icon-red' />}
disabled={controller.selected.length !== 1 || controller.isProcessing}
onClick={onDelete}
/>
</div>
2024-07-23 23:04:21 +03:00
) : null}
2024-07-21 15:19:57 +03:00
</div>
);
}
//IconExecute
2024-07-21 15:19:57 +03:00
export default ToolbarOssGraph;