From ac85bfdba7ad6041f1bebb504cd8090fb39b66d8 Mon Sep 17 00:00:00 2001 From: IRBorisov <8611739+IRBorisov@users.noreply.github.com> Date: Sun, 7 Apr 2024 21:31:38 +0300 Subject: [PATCH] Add image save function --- rsconcept/frontend/src/components/Icons.tsx | 1 + .../EditorTermGraph/EditorTermGraph.tsx | 15 +++++++++++++++ .../RSFormPage/EditorTermGraph/GraphToolbar.tsx | 10 +++++++++- rsconcept/frontend/src/utils/utils.tsx | 14 ++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/rsconcept/frontend/src/components/Icons.tsx b/rsconcept/frontend/src/components/Icons.tsx index 83bb9a38..87b4213c 100644 --- a/rsconcept/frontend/src/components/Icons.tsx +++ b/rsconcept/frontend/src/components/Icons.tsx @@ -25,6 +25,7 @@ export { FiBellOff as IconFollowOff } from 'react-icons/fi'; export { LuChevronDown as IconDropArrow } from 'react-icons/lu'; export { LuChevronUp as IconDropArrowUp } from 'react-icons/lu'; +export { LuImage as IconImage } from 'react-icons/lu'; export { TbColumns as IconList } from 'react-icons/tb'; export { TbColumnsOff as IconListOff } from 'react-icons/tb'; export { BiFontFamily as IconText } from 'react-icons/bi'; diff --git a/rsconcept/frontend/src/pages/RSFormPage/EditorTermGraph/EditorTermGraph.tsx b/rsconcept/frontend/src/pages/RSFormPage/EditorTermGraph/EditorTermGraph.tsx index e98954f7..4f755081 100644 --- a/rsconcept/frontend/src/pages/RSFormPage/EditorTermGraph/EditorTermGraph.tsx +++ b/rsconcept/frontend/src/pages/RSFormPage/EditorTermGraph/EditorTermGraph.tsx @@ -2,6 +2,7 @@ import clsx from 'clsx'; import { AnimatePresence } from 'framer-motion'; +import fileDownload from 'js-file-download'; import { useCallback, useLayoutEffect, useMemo, useRef, useState } from 'react'; import InfoConstituenta from '@/components/info/InfoConstituenta'; @@ -16,6 +17,7 @@ import { applyNodeSizing } from '@/models/miscellaneousAPI'; import { ConstituentaID, CstType } from '@/models/rsform'; import { colorBgGraphNode } from '@/styling/color'; import { PARAMETER, storage } from '@/utils/constants'; +import { convertBase64ToBlob } from '@/utils/utils'; import { useRSEdit } from '../RSEditContext'; import GraphSelectors from './GraphSelectors'; @@ -149,6 +151,18 @@ function EditorTermGraph({ onOpenEdit }: EditorTermGraphProps) { [setFilterParams] ); + const handleSaveImage = useCallback(() => { + if (!graphRef?.current) { + return; + } + const data = graphRef.current.exportCanvas(); + try { + fileDownload(convertBase64ToBlob(data), 'graph.png', 'data:image/png;base64'); + } catch (error) { + console.error(error); + } + }, [graphRef]); + function handleKeyDown(event: React.KeyboardEvent) { // Hotkeys implementation if (!controller.isContentEditable || controller.isProcessing) { @@ -235,6 +249,7 @@ function EditorTermGraph({ onOpenEdit }: EditorTermGraphProps) { onCreate={handleCreateCst} onDelete={handleDeleteCst} onResetViewpoint={() => setToggleResetView(prev => !prev)} + onSaveImage={handleSaveImage} toggleOrbit={() => setOrbit(prev => !prev)} toggleFoldDerived={handleFoldDerived} toggleNoText={() => diff --git a/rsconcept/frontend/src/pages/RSFormPage/EditorTermGraph/GraphToolbar.tsx b/rsconcept/frontend/src/pages/RSFormPage/EditorTermGraph/GraphToolbar.tsx index cae6509c..db042a0b 100644 --- a/rsconcept/frontend/src/pages/RSFormPage/EditorTermGraph/GraphToolbar.tsx +++ b/rsconcept/frontend/src/pages/RSFormPage/EditorTermGraph/GraphToolbar.tsx @@ -6,6 +6,7 @@ import { IconDestroy, IconFilter, IconFitImage, + IconImage, IconNewItem, IconRotate3D, IconText, @@ -31,6 +32,7 @@ interface GraphToolbarProps { onCreate: () => void; onDelete: () => void; onResetViewpoint: () => void; + onSaveImage: () => void; toggleFoldDerived: () => void; toggleNoText: () => void; @@ -48,7 +50,8 @@ function GraphToolbar({ showParamsDialog, onCreate, onDelete, - onResetViewpoint + onResetViewpoint, + onSaveImage }: GraphToolbarProps) { const controller = useRSEdit(); @@ -112,6 +115,11 @@ function GraphToolbar({ onClick={onDelete} /> ) : null} + } + title='Сохранить изображение' + onClick={onSaveImage} + />