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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
/*
* SPDX-FileCopyrightText: 2024 The Refinery Authors <https://refinery.tools/>
*
* SPDX-License-Identifier: EPL-2.0
*/
import ChevronRightIcon from '@mui/icons-material/ChevronRight';
import ContentCopyIcon from '@mui/icons-material/ContentCopy';
import ContrastIcon from '@mui/icons-material/Contrast';
import DarkModeIcon from '@mui/icons-material/DarkMode';
import ImageIcon from '@mui/icons-material/Image';
import InsertDriveFileOutlinedIcon from '@mui/icons-material/InsertDriveFileOutlined';
import LightModeIcon from '@mui/icons-material/LightMode';
import SaveAltIcon from '@mui/icons-material/SaveAlt';
import ShapeLineIcon from '@mui/icons-material/ShapeLine';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import FormControlLabel from '@mui/material/FormControlLabel';
import Slider from '@mui/material/Slider';
import Stack from '@mui/material/Stack';
import Switch from '@mui/material/Switch';
import ToggleButton from '@mui/material/ToggleButton';
import ToggleButtonGroup from '@mui/material/ToggleButtonGroup';
import Typography from '@mui/material/Typography';
import { styled } from '@mui/material/styles';
import { observer } from 'mobx-react-lite';
import { useCallback } from 'react';
import { useRootStore } from '../../RootStoreProvider';
import getLogger from '../../utils/getLogger';
import type GraphStore from '../GraphStore';
import SlideInPanel from '../SlideInPanel';
import exportDiagram from './exportDiagram';
const log = getLogger('graph.ExportPanel');
const SwitchButtonGroup = styled(ToggleButtonGroup, {
name: 'ExportPanel-SwitchButtonGroup',
})(({ theme }) => ({
marginTop: theme.spacing(2),
marginInline: theme.spacing(2),
minWidth: '260px',
'.MuiToggleButton-root': {
width: '100%',
fontSize: '1rem',
lineHeight: '1.5',
},
'& svg': {
margin: '0 6px 0 0',
},
}));
const AutoThemeMessage = styled(Typography, {
name: 'ExportPanel-AutoThemeMessage',
})(({ theme }) => ({
width: '260px',
marginInline: theme.spacing(2),
}));
function getLabel(value: number): string {
return `${value}%`;
}
const marks = [100, 200, 300, 400].map((value) => ({
value,
label: (
<Stack direction="column" alignItems="center">
<ImageIcon sx={{ width: `${11 + (value / 100) * 3}px` }} />
<Typography variant="caption">{getLabel(value)}</Typography>
</Stack>
),
}));
function ExportPanel({
graph,
svgContainer,
dialog,
}: {
graph: GraphStore;
svgContainer: HTMLElement | undefined;
dialog: boolean;
}): JSX.Element {
const { exportSettingsStore } = useRootStore();
const icon = useCallback(
(show: boolean) =>
show && !dialog ? <ChevronRightIcon /> : <SaveAltIcon />,
[dialog],
);
const { format } = exportSettingsStore;
const emptyGraph = graph.semantics.nodes.length === 0;
const buttons = useCallback(
(close: () => void) => (
<>
<Button
color="inherit"
startIcon={<SaveAltIcon />}
disabled={emptyGraph}
onClick={() => {
exportDiagram(svgContainer, graph, exportSettingsStore, 'download')
.then(close)
.catch((error) => {
log.error('Failed to download diagram', error);
});
}}
>
Download
</Button>
{'write' in navigator.clipboard && format === 'png' && (
<Button
color="inherit"
startIcon={<ContentCopyIcon />}
disabled={emptyGraph}
onClick={() => {
exportDiagram(svgContainer, graph, exportSettingsStore, 'copy')
.then(close)
.catch((error) => {
log.error('Failed to copy diagram', error);
});
}}
>
Copy
</Button>
)}
</>
),
[svgContainer, graph, exportSettingsStore, format, emptyGraph],
);
return (
<SlideInPanel
anchor="right"
dialog={dialog}
title="Export diagram"
icon={icon}
iconLabel={`Export image\u2026`}
buttons={buttons}
>
<SwitchButtonGroup size="small" className="rounded">
<ToggleButton
value="svg"
selected={exportSettingsStore.format === 'svg'}
onClick={() => exportSettingsStore.setFormat('svg')}
>
<ShapeLineIcon fontSize="small" /> SVG
</ToggleButton>
<ToggleButton
value="pdf"
selected={exportSettingsStore.format === 'pdf'}
onClick={() => exportSettingsStore.setFormat('pdf')}
>
<InsertDriveFileOutlinedIcon fontSize="small" /> PDF
</ToggleButton>
<ToggleButton
value="png"
selected={exportSettingsStore.format === 'png'}
onClick={() => exportSettingsStore.setFormat('png')}
>
<ImageIcon fontSize="small" /> PNG
</ToggleButton>
</SwitchButtonGroup>
<SwitchButtonGroup size="small" className="rounded">
<ToggleButton
value="light"
selected={exportSettingsStore.theme === 'light'}
onClick={() => exportSettingsStore.setTheme('light')}
>
<LightModeIcon fontSize="small" /> Light
</ToggleButton>
<ToggleButton
value="dark"
selected={exportSettingsStore.theme === 'dark'}
onClick={() => exportSettingsStore.setTheme('dark')}
>
<DarkModeIcon fontSize="small" /> Dark
</ToggleButton>
{exportSettingsStore.canSetDynamicTheme && (
<ToggleButton
value="dynamic"
selected={exportSettingsStore.theme === 'dynamic'}
onClick={() => exportSettingsStore.setTheme('dynamic')}
>
<ContrastIcon fontSize="small" /> Auto
</ToggleButton>
)}
</SwitchButtonGroup>
{exportSettingsStore.canChangeTransparency && (
<FormControlLabel
control={
<Switch
checked={exportSettingsStore.transparent}
onClick={() => exportSettingsStore.toggleTransparent()}
/>
}
label="Transparent background"
/>
)}
{exportSettingsStore.canEmbedFonts && (
<FormControlLabel
control={
<Switch
checked={exportSettingsStore.embedFonts}
onClick={() => exportSettingsStore.toggleEmbedFonts()}
/>
}
label={
<Stack direction="column">
<Typography>Embed fonts</Typography>
<Typography variant="caption">
{exportSettingsStore.format === 'pdf' ? (
<>+20 kB fully embedded</>
) : (
<>+75 kB, only supported in browsers</>
)}
</Typography>
</Stack>
}
/>
)}
{exportSettingsStore.theme === 'dynamic' && (
<>
<AutoThemeMessage mt={2}>
For embedding into HTML directly
</AutoThemeMessage>
<AutoThemeMessage variant="caption" mt={1}>
Set <code>data-theme="dark"</code> on a containing element
to use a dark theme
</AutoThemeMessage>
</>
)}
{exportSettingsStore.canScale && (
<Box mx={4} mt={1} mb={2}>
<Slider
aria-label="Image scale"
value={exportSettingsStore.scale}
min={100}
max={400}
valueLabelFormat={getLabel}
getAriaValueText={getLabel}
step={50}
valueLabelDisplay="auto"
marks={marks}
onChange={(_, value) => {
if (typeof value === 'number') {
exportSettingsStore.setScale(value);
}
}}
/>
</Box>
)}
</SlideInPanel>
);
}
export default observer(ExportPanel);
|