-
Notifications
You must be signed in to change notification settings - Fork 752
Expand file tree
/
Copy pathsidebar-file-tree.tsx
More file actions
262 lines (236 loc) · 7.5 KB
/
sidebar-file-tree.tsx
File metadata and controls
262 lines (236 loc) · 7.5 KB
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
258
259
260
261
262
import * as React from 'react';
import {
Button,
ButtonGroup,
Classes,
Icon,
Menu,
MenuItem,
Tree,
TreeNodeInfo,
} from '@blueprintjs/core';
import { ContextMenu2, Tooltip2 } from '@blueprintjs/popover2';
import classNames from 'classnames';
import { observer } from 'mobx-react';
import { EditorId, PACKAGE_NAME } from '../../interfaces';
import { EditorPresence } from '../editor-mosaic';
import { AppState } from '../state';
import { isMainEntryPoint, isSupportedFile } from '../utils/editor-utils';
interface FileTreeProps {
appState: AppState;
}
interface FileTreeState {
action: 'add' | 'default';
}
// crazy idea: maybe save state should be tied to the editors
// and not to the filesystem
export const SidebarFileTree = observer(
class SidebarFileTree extends React.Component<FileTreeProps, FileTreeState> {
constructor(props: FileTreeProps) {
super(props);
this.state = {
action: 'default',
};
}
public render() {
const { editorMosaic } = this.props.appState;
const { files, focusedFile } = editorMosaic;
const fileList: TreeNodeInfo[] = Array.from(files)
.sort((a, b) => a[0].localeCompare(b[0]))
.map(([editorId, presence], index) => {
const visibilityIcon =
presence !== EditorPresence.Hidden ? 'eye-open' : 'eye-off';
const isInactive =
isMainEntryPoint(editorId) && this.getMainEntryPoint() !== editorId;
return {
isSelected: focusedFile === editorId,
id: index,
hasCaret: false,
icon: 'document',
label: (
<ContextMenu2
className="pointer"
style={isInactive ? { opacity: 0.5 } : undefined}
onClick={() => this.setFocusedFile(editorId)}
content={
<Menu>
<MenuItem
icon="redo"
text="Rename"
intent="primary"
onClick={() => this.renameEditor(editorId)}
/>
{isMainEntryPoint(editorId) && (
<MenuItem
icon="star"
text="Set as Main Entry Point"
intent="primary"
onClick={() => this.setMainEntryPoint(editorId)}
/>
)}
<MenuItem
disabled={isMainEntryPoint(editorId)}
icon="remove"
text="Delete"
intent="danger"
onClick={() => this.removeEditor(editorId)}
/>
</Menu>
}
>
{String(editorId)}
</ContextMenu2>
),
secondaryLabel: (
<ButtonGroup>
<Tooltip2
content="Toggle Visibility"
minimal={true}
hoverOpenDelay={1000}
>
<Button
minimal
onClick={() => this.toggleVisibility(editorId)}
>
<Icon icon={visibilityIcon} />
</Button>
</Tooltip2>
</ButtonGroup>
),
};
});
if (this.state.action === 'add') {
fileList.push({
id: 'add',
className: 'add-file-input',
icon: 'document',
label: (
<input
className={classNames(Classes.INPUT, Classes.FILL, Classes.SMALL)}
style={{ width: `100%`, padding: 0 }}
onKeyDown={(e) => {
if (e.key === 'Escape') {
e.currentTarget.blur();
} else if (e.key === 'Enter') {
this.createEditor(e.currentTarget.value as EditorId);
e.currentTarget.blur();
}
}}
id="new-file-input"
autoFocus
onBlur={() => {
this.setState({ action: 'default' });
}}
/>
),
});
}
const editorsTree: TreeNodeInfo[] = [
{
childNodes: fileList,
id: 'files',
hasCaret: false,
icon: 'folder-open',
isExpanded: true,
label: 'Editors',
secondaryLabel: (
<ButtonGroup minimal>
<Tooltip2
content="Add New File"
minimal={true}
hoverOpenDelay={1000}
>
<Button
small
icon="add"
onClick={() => this.setState({ action: 'add' })}
/>
</Tooltip2>
<Tooltip2
content="Reset Layout"
minimal={true}
hoverOpenDelay={1000}
>
<Button small icon="grid-view" onClick={this.resetLayout} />
</Tooltip2>
</ButtonGroup>
),
},
];
return (
<div className="fiddle-scrollbar">
<Tree contents={editorsTree} />
</div>
);
}
public toggleVisibility = (editorId: EditorId) => {
const { editorMosaic } = this.props.appState;
editorMosaic.toggle(editorId);
};
public setFocusedFile = (editorId: EditorId) => {
const { editorMosaic } = this.props.appState;
editorMosaic.setFocusedFile(editorId);
};
public renameEditor = async (editorId: EditorId) => {
const { appState } = this.props;
const visible =
appState.editorMosaic.files.get(editorId) === EditorPresence.Visible;
const id = (await appState.showInputDialog({
label: 'Enter New Filename',
ok: 'Rename',
placeholder: 'New Name',
})) as EditorId;
if (!id) return;
if (
id.endsWith('.json') &&
[PACKAGE_NAME, 'package-lock.json'].includes(id)
) {
await appState.showErrorDialog(
`Cannot add ${PACKAGE_NAME} or package-lock.json as custom files`,
);
return;
}
if (!isSupportedFile(id)) {
await appState.showErrorDialog(
`Invalid filename "${id}": Must be a file ending in .cjs, .js, .mjs, .html, .css, or .json`,
);
return;
}
try {
appState.editorMosaic.renameFile(editorId, id);
if (visible) appState.editorMosaic.show(id);
} catch (err) {
appState.showErrorDialog(err.message);
}
};
public setMainEntryPoint = (editorId: EditorId) => {
const { appState } = this.props;
try {
appState.editorMosaic.setMainEntryPoint(editorId);
} catch (err) {
appState.showErrorDialog(err.message);
}
};
public getMainEntryPoint = () => {
const { editorMosaic } = this.props.appState;
return editorMosaic.mainEntryPointFile();
};
public removeEditor = (editorId: EditorId) => {
const { editorMosaic } = this.props.appState;
editorMosaic.remove(editorId);
};
public createEditor = (editorId: EditorId) => {
const { appState } = this.props;
try {
appState.editorMosaic.addNewFile(editorId);
appState.editorMosaic.show(editorId);
} catch (err) {
appState.showErrorDialog(err.message);
}
};
public resetLayout = () => {
const { editorMosaic } = this.props.appState;
editorMosaic.resetLayout();
};
},
);