-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathEditorInput.jsx
More file actions
198 lines (180 loc) · 6.54 KB
/
Copy pathEditorInput.jsx
File metadata and controls
198 lines (180 loc) · 6.54 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
import React, { createRef, useEffect, useRef, useState } from "react";
import { DragDropContext } from "@hello-pangea/dnd";
import { useDispatch, useSelector } from "react-redux";
import { TabPanel, Tabs } from "react-tabs";
import classNames from "classnames";
import { useMediaQuery } from "react-responsive";
import {
closeFile,
setFocussedFileIndex,
setOpenFiles,
} from "../../../redux/EditorSlice";
import Button from "../../Button/Button";
import EditorPanel from "../EditorPanel/EditorPanel";
import DraggableTab from "../DraggableTabs/DraggableTab";
import DroppableTabList from "../DraggableTabs/DroppableTabList";
import RunBar from "../../RunButton/RunBar";
import ErrorMessage from "../ErrorMessage/ErrorMessage";
import "../../../assets/stylesheets/EditorInput.scss";
import RunnerControls from "../../RunButton/RunnerControls";
import { MOBILE_MEDIA_QUERY } from "../../../utils/mediaQueryBreakpoints";
import CloseIcon from "../../../utils/CloseIcon";
import { useTranslation } from "react-i18next";
const EditorInput = () => {
const project = useSelector((state) => state.editor.project);
const openFiles = useSelector((state) => state.editor.openFiles);
const focussedFileIndices = useSelector(
(state) => state.editor.focussedFileIndices,
);
const dispatch = useDispatch();
const isMobile = useMediaQuery({ query: MOBILE_MEDIA_QUERY });
const readOnly = useSelector((state) => state.editor.readOnly);
const [numberOfComponents, setNumberOfComponents] = useState(
project?.components?.length,
);
const [fileNames, setFileNames] = useState();
const { t } = useTranslation();
const onDragStart = (input) => {
const { source } = input;
dispatch(
setFocussedFileIndex({
panelIndex: parseInt(source.droppableId),
fileIndex: source.index,
}),
);
};
const onDragEnd = (result) => {
const { source, destination } = result;
if (!destination) return;
let openFilesData = [...openFiles];
let oldPane = [...openFilesData[source.droppableId]];
const [removed] = oldPane.splice(source.index, 1);
openFilesData[source.droppableId] = [...oldPane];
let newPane = [...openFilesData[destination.droppableId]];
newPane.splice(destination.index, 0, removed);
openFilesData[destination.droppableId] = [...newPane];
dispatch(setOpenFiles(openFilesData));
dispatch(
setFocussedFileIndex({
panelIndex: parseInt(destination.droppableId),
fileIndex: destination.index,
}),
);
if (destination.droppableId !== source.droppableId) {
dispatch(
setFocussedFileIndex({
panelIndex: parseInt(source.droppableId),
fileIndex: Math.max(source.index - 1, 0),
}),
);
}
};
const closeFileTab = (e, fileName) => {
e.stopPropagation();
dispatch(closeFile(fileName));
};
let tabRefs = useRef(project?.components?.map(createRef));
useEffect(() => {
if (!project?.components) return;
setNumberOfComponents(project.components.length);
Array(project.components.length)
.fill()
.forEach((_, i) => {
tabRefs.current[i] = tabRefs.current[i] || React.createRef();
});
}, [project?.components]);
useEffect(() => {
const newFileNames = project.components.map(
(file) => `${file.name}.${file.extension}`,
);
if (newFileNames.join() !== fileNames?.join()) {
setFileNames(newFileNames);
}
}, [fileNames, project.components]);
useEffect(() => {
if (!fileNames) return;
focussedFileIndices.forEach((index, i) => {
const fileName = openFiles[i][index];
const componentIndex = fileNames.findIndex((name) => name === fileName);
const fileRef = tabRefs.current[componentIndex];
if (fileRef && fileRef.current) {
fileRef.current.parentElement.scrollIntoView();
}
});
}, [focussedFileIndices, openFiles, numberOfComponents, fileNames]);
if (!project || !project.components) {
return null;
}
return (
<DragDropContext
onDragStart={(input) => onDragStart(input)}
onDragEnd={(result) => onDragEnd(result)}
>
<div className="editor-input">
{openFiles.map((panel, panelIndex) => (
<Tabs
key={panelIndex}
selectedIndex={focussedFileIndices[panelIndex]}
onSelect={() => {}}
>
<div className="react-tabs__tab-container">
<DroppableTabList index={panelIndex}>
{panel.map((fileName, fileIndex) => (
<DraggableTab
key={fileIndex}
fileIndex={fileIndex}
panelIndex={panelIndex}
>
<span
className={classNames("react-tabs__tab-text", {
"react-tabs__tab-text--split": ![
"main.py",
"index.html",
].includes(fileName),
})}
ref={
tabRefs.current[
project?.components?.findIndex(
(file) =>
`${file.name}.${file.extension}` === fileName,
)
]
}
>
{fileName}
</span>
{!["main.py", "index.html"].includes(fileName) ? (
<Button
className="btn--tertiary react-tabs__tab-close-btn"
label={t("editorPanel.close")}
onClickHandler={(e) => closeFileTab(e, fileName)}
ButtonIcon={() => <CloseIcon scaleFactor={0.85} />}
/>
) : null}
</DraggableTab>
))}
</DroppableTabList>
{isMobile ? <RunnerControls skinny /> : null}
</div>
{panel.map((fileName, i) => (
<TabPanel key={i}>
{readOnly && (
<span className="editor-input__view-only-banner">
{t("editorPanel.viewOnly")}
</span>
)}
<EditorPanel
fileName={fileName.split(".")[0]}
extension={fileName.split(".").slice(1).join(".")}
/>
</TabPanel>
))}
<ErrorMessage />
{isMobile ? null : <RunBar />}
</Tabs>
))}
</div>
</DragDropContext>
);
};
export default EditorInput;