aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/editor/EditorButtons.tsx
blob: 50cd51dcd25eca199c27c7338d9c356276a2c3c2 (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
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
/*
 * 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 FileOpenIcon from '@mui/icons-material/FileOpen';
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 SaveIcon from '@mui/icons-material/Save';
import SaveAsIcon from '@mui/icons-material/SaveAs';
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 Tooltip from '@mui/material/Tooltip';
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}>
      <Tooltip title="Open">
        <IconButton
          disabled={editorStore === undefined}
          onClick={() => editorStore?.openFile()}
          color="inherit"
        >
          <FileOpenIcon fontSize="small" />
        </IconButton>
      </Tooltip>
      <Tooltip title="Save">
        <IconButton
          disabled={editorStore === undefined || !editorStore.unsavedChanges}
          onClick={() => editorStore?.saveFile()}
          color="inherit"
        >
          <SaveIcon fontSize="small" />
        </IconButton>
      </Tooltip>
      {'showSaveFilePicker' in window && (
        <Tooltip title={`Save as\u2026`}>
          <IconButton
            disabled={editorStore === undefined}
            onClick={() => editorStore?.saveFileAs()}
            color="inherit"
          >
            <SaveAsIcon fontSize="small" />
          </IconButton>
        </Tooltip>
      )}
      <Tooltip title="Undo">
        <IconButton
          disabled={editorStore === undefined || !editorStore.canUndo}
          onClick={() => editorStore?.undo()}
          color="inherit"
          sx={{ ml: 1 }}
        >
          <UndoIcon fontSize="small" />
        </IconButton>
      </Tooltip>
      <Tooltip title="Redo">
        <IconButton
          disabled={editorStore === undefined || !editorStore.canRedo}
          onClick={() => editorStore?.redo()}
          color="inherit"
        >
          <RedoIcon fontSize="small" />
        </IconButton>
      </Tooltip>
      <ToggleButtonGroup size="small" className="rounded" sx={{ mx: 1 }}>
        <Tooltip title="Line numbers">
          <ToggleButton
            selected={editorStore?.showLineNumbers ?? false}
            disabled={editorStore === undefined}
            onClick={() => editorStore?.toggleLineNumbers()}
            value="show-line-numbers"
          >
            <FormatListNumberedIcon fontSize="small" />
          </ToggleButton>
        </Tooltip>
        <Tooltip title="Color identifiers">
          <ToggleButton
            selected={editorStore?.colorIdentifiers ?? false}
            disabled={editorStore === undefined}
            onClick={() => editorStore?.toggleColorIdentifiers()}
            value="color-identifiers"
          >
            <LooksIcon fontSize="small" />
          </ToggleButton>
        </Tooltip>
        <Tooltip title="Find and replace">
          <ToggleButton
            selected={editorStore?.searchPanel?.state ?? false}
            disabled={editorStore === undefined}
            onClick={() => editorStore?.searchPanel?.toggle()}
            {...(editorStore !== undefined &&
              editorStore.searchPanel.state && {
                'aria-controls': editorStore.searchPanel.id,
              })}
            value="show-search-panel"
          >
            <SearchIcon fontSize="small" />
          </ToggleButton>
        </Tooltip>
        <Tooltip title="Diagnostics panel">
          <ToggleButton
            selected={editorStore?.lintPanel?.state ?? false}
            disabled={editorStore === undefined}
            onClick={() => editorStore?.lintPanel.toggle()}
            {...(editorStore !== undefined &&
              editorStore.lintPanel.state && {
                'aria-controls': editorStore.lintPanel.id,
              })}
            value="show-lint-panel"
          >
            {getLintIcon(editorStore?.delayedErrors?.highestDiagnosticLevel)}
          </ToggleButton>
        </Tooltip>
      </ToggleButtonGroup>
      <Tooltip title="Automatic format">
        <IconButton
          disabled={editorStore === undefined || !editorStore.opened}
          onClick={() => editorStore?.formatText()}
          color="inherit"
        >
          <FormatPaintIcon fontSize="small" />
        </IconButton>
      </Tooltip>
      <ConnectButton editorStore={editorStore} />
    </Stack>
  );
});