aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/editor/EditorButtons.tsx
blob: 34b647512df160a57dcce46a6f4cea83a222be00 (plain) (blame)
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
import type { Diagnostic } from '@codemirror/lint';
import CheckIcon from '@mui/icons-material/Check';
import ErrorIcon from '@mui/icons-material/Error';
import FormatListNumberedIcon from '@mui/icons-material/FormatListNumbered';
import FormatPaint from '@mui/icons-material/FormatPaint';
import InfoOutlinedIcon from '@mui/icons-material/InfoOutlined';
import RedoIcon from '@mui/icons-material/Redo';
import SearchIcon from '@mui/icons-material/Search';
import UndoIcon from '@mui/icons-material/Undo';
import WarningIcon from '@mui/icons-material/Warning';
import IconButton from '@mui/material/IconButton';
import Stack from '@mui/material/Stack';
import ToggleButton from '@mui/material/ToggleButton';
import ToggleButtonGroup from '@mui/material/ToggleButtonGroup';
import { observer } from 'mobx-react-lite';
import React from 'react';

import { useRootStore } from '../RootStore';

// Exhastive switch as proven by TypeScript.
// eslint-disable-next-line consistent-return
function getLintIcon(severity: Diagnostic['severity'] | undefined) {
  switch (severity) {
    case 'error':
      return <ErrorIcon fontSize="small" />;
    case 'warning':
      return <WarningIcon fontSize="small" />;
    case 'info':
      return <InfoOutlinedIcon fontSize="small" />;
    default:
      return <CheckIcon fontSize="small" />;
  }
}

function EditorButtons(): JSX.Element {
  const { editorStore } = useRootStore();

  return (
    <Stack direction="row" flexGrow={1}>
      <IconButton
        disabled={!editorStore.canUndo}
        onClick={() => editorStore.undo()}
        aria-label="Undo"
      >
        <UndoIcon fontSize="small" />
      </IconButton>
      <IconButton
        disabled={!editorStore.canRedo}
        onClick={() => editorStore.redo()}
        aria-label="Redo"
      >
        <RedoIcon fontSize="small" />
      </IconButton>
      <ToggleButtonGroup size="small" sx={{ mx: 1 }}>
        <ToggleButton
          selected={editorStore.showLineNumbers}
          onClick={() => editorStore.toggleLineNumbers()}
          aria-label="Show line numbers"
          value="show-line-numbers"
        >
          <FormatListNumberedIcon fontSize="small" />
        </ToggleButton>
        <ToggleButton
          selected={editorStore.searchPanel.state}
          onClick={() => editorStore.searchPanel.toggle()}
          aria-label="Show find/replace"
          value="show-search-panel"
        >
          <SearchIcon fontSize="small" />
        </ToggleButton>
        <ToggleButton
          selected={editorStore.lintPanel.state}
          onClick={() => editorStore.lintPanel.toggle()}
          aria-label="Show diagnostics panel"
          value="show-lint-panel"
        >
          {getLintIcon(editorStore.highestDiagnosticLevel)}
        </ToggleButton>
      </ToggleButtonGroup>
      <IconButton
        onClick={() => editorStore.formatText()}
        aria-label="Automatic format"
      >
        <FormatPaint fontSize="small" />
      </IconButton>
    </Stack>
  );
}

export default observer(EditorButtons);