forked from processing/p5.js-web-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSidebar.jsx
148 lines (137 loc) · 4.57 KB
/
Sidebar.jsx
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
import React, { useRef, useEffect } from 'react';
import classNames from 'classnames';
import { useTranslation } from 'react-i18next';
import { useDispatch, useSelector } from 'react-redux';
import {
closeProjectOptions,
collapseSidebar,
newFile,
newFolder,
openProjectOptions,
openUploadFileModal
} from '../actions/ide';
import { selectRootFile } from '../selectors/files';
import { getAuthenticated, selectCanEditSketch } from '../selectors/users';
import ConnectedFileNode from './FileNode';
import { PlusIcon } from '../../../common/icons';
import { FileDrawer } from './Editor/MobileEditor';
// TODO: use a generic Dropdown UI component
export default function SideBar() {
const { t } = useTranslation();
const dispatch = useDispatch();
const rootFile = useSelector(selectRootFile);
const ide = useSelector((state) => state.ide);
const projectOptionsVisible = useSelector(
(state) => state.ide.projectOptionsVisible
);
const isExpanded = useSelector((state) => state.ide.sidebarIsExpanded);
const canEditProject = useSelector(selectCanEditSketch);
const isAuthenticated = useSelector(getAuthenticated);
const sidebarOptionsRef = useRef(null);
useEffect(() => {
function handleClickOutside(event) {
if (
projectOptionsVisible &&
sidebarOptionsRef.current &&
!sidebarOptionsRef.current.contains(event.target)
) {
setTimeout(() => dispatch(closeProjectOptions()), 300);
}
}
if (projectOptionsVisible) {
document.addEventListener('mousedown', handleClickOutside);
} else {
document.removeEventListener('mousedown', handleClickOutside);
}
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [projectOptionsVisible, dispatch]);
const toggleProjectOptions = (e) => {
e.preventDefault();
if (projectOptionsVisible) {
dispatch(closeProjectOptions());
} else {
dispatch(openProjectOptions());
}
};
const sidebarClass = classNames({
sidebar: true,
'sidebar--contracted': !isExpanded,
'sidebar--project-options': projectOptionsVisible,
'sidebar--cant-edit': !canEditProject
});
return (
<FileDrawer>
{ide.sidebarIsExpanded && (
<button
data-backdrop="filedrawer"
onClick={() => {
dispatch(collapseSidebar());
dispatch(closeProjectOptions());
}}
/>
)}
<section className={sidebarClass}>
<header
className="sidebar__header"
onContextMenu={toggleProjectOptions}
>
<h3 className="sidebar__title">
<span>{t('Sidebar.Title')}</span>
</h3>
<div className="sidebar__icons" ref={sidebarOptionsRef}>
<button
aria-label={t('Sidebar.ToggleARIA')}
className="sidebar__add"
tabIndex="0"
onClick={toggleProjectOptions}
>
<PlusIcon focusable="false" aria-hidden="true" />
</button>
{projectOptionsVisible && (
<ul className="sidebar__project-options">
<li>
<button
aria-label={t('Sidebar.AddFolderARIA')}
onClick={() => {
dispatch(newFolder(rootFile.id));
setTimeout(() => dispatch(closeProjectOptions()), 300);
}}
>
{t('Sidebar.AddFolder')}
</button>
</li>
<li>
<button
aria-label={t('Sidebar.AddFileARIA')}
onClick={() => {
dispatch(newFile(rootFile.id));
setTimeout(() => dispatch(closeProjectOptions()), 300);
}}
>
{t('Sidebar.AddFile')}
</button>
</li>
{isAuthenticated && (
<li>
<button
aria-label={t('Sidebar.UploadFileARIA')}
onClick={() => {
dispatch(openUploadFileModal(rootFile.id));
setTimeout(() => dispatch(closeProjectOptions()), 300);
}}
>
{t('Sidebar.UploadFile')}
</button>
</li>
)}
</ul>
)}
</div>
</header>
<ConnectedFileNode id={rootFile.id} canEdit={canEditProject} />
</section>
</FileDrawer>
);
}