aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/editor/EditorButtons.tsx
blob: f45139096d6d6fa1774046a842532bae37e1c27d (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
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
/*
 * SPDX-FileCopyrightText: 2021-2024 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import type { Diagnostic } from '@codemirror/lint';
import CancelIcon from '@mui/icons-material/Cancel';
import CheckIcon from '@mui/icons-material/Check';
import FormatListNumberedIcon from '@mui/icons-material/FormatListNumbered';
import FormatPaintIcon from '@mui/icons-material/FormatPaint';
import InfoOutlinedIcon from '@mui/icons-material/InfoOutlined';
import LooksIcon from '@mui/icons-material/Looks';
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 ConnectButton from './ConnectButton';
import type EditorStore from './EditorStore';

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

export default observer(function EditorButtons({
  editorStore,
}: {
  editorStore: EditorStore | undefined;
}): JSX.Element {
  return (
    <Stack direction="row" flexGrow={1}>
      <IconButton
        disabled={editorStore === undefined || !editorStore.canUndo}
        onClick={() => editorStore?.undo()}
        aria-label="Undo"
        color="inherit"
      >
        <UndoIcon fontSize="small" />
      </IconButton>
      <IconButton
        disabled={editorStore === undefined || !editorStore.canRedo}
        onClick={() => editorStore?.redo()}
        aria-label="Redo"
        color="inherit"
      >
        <RedoIcon fontSize="small" />
      </IconButton>
      <ToggleButtonGroup size="small" className="rounded" sx={{ mx: 1 }}>
        <ToggleButton
          selected={editorStore?.showLineNumbers ?? false}
          disabled={editorStore === undefined}
          onClick={() => editorStore?.toggleLineNumbers()}
          aria-label="Show line numbers"
          value="show-line-numbers"
        >
          <FormatListNumberedIcon fontSize="small" />
        </ToggleButton>
        <ToggleButton
          selected={editorStore?.colorIdentifiers ?? false}
          disabled={editorStore === undefined}
          onClick={() => editorStore?.toggleColorIdentifiers()}
          aria-label="Color identifiers"
          value="color-identifiers"
        >
          <LooksIcon fontSize="small" />
        </ToggleButton>
        <ToggleButton
          selected={editorStore?.searchPanel?.state ?? false}
          disabled={editorStore === undefined}
          onClick={() => editorStore?.searchPanel?.toggle()}
          aria-label="Show find/replace"
          {...(editorStore !== undefined &&
            editorStore.searchPanel.state && {
              'aria-controls': editorStore.searchPanel.id,
            })}
          value="show-search-panel"
        >
          <SearchIcon fontSize="small" />
        </ToggleButton>
        <ToggleButton
          selected={editorStore?.lintPanel?.state ?? false}
          disabled={editorStore === undefined}
          onClick={() => editorStore?.lintPanel.toggle()}
          aria-label="Show diagnostics panel"
          {...(editorStore !== undefined &&
            editorStore.lintPanel.state && {
              'aria-controls': editorStore.lintPanel.id,
            })}
          value="show-lint-panel"
        >
          {getLintIcon(editorStore?.delayedErrors?.highestDiagnosticLevel)}
        </ToggleButton>
      </ToggleButtonGroup>
      <IconButton
        disabled={editorStore === undefined || !editorStore.opened}
        onClick={() => editorStore?.formatText()}
        aria-label="Automatic format"
        color="inherit"
      >
        <FormatPaintIcon fontSize="small" />
      </IconButton>
      <ConnectButton editorStore={editorStore} />
    </Stack>
  );
});