Skip to content

Commit b90a7a2

Browse files
authored
Ensure that editor tabs have corresponding files (#2388)
* Create files that do not exist for editor tabs * Consolidate workspace base paths into a single location For better cohesion.
1 parent 329c8d2 commit b90a7a2

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

src/commons/application/ApplicationTypes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { DashboardState } from '../../features/dashboard/DashboardTypes';
66
import { Grading } from '../../features/grading/GradingTypes';
77
import { PlaygroundState } from '../../features/playground/PlaygroundTypes';
88
import { PlaybackStatus, RecordingStatus } from '../../features/sourceRecorder/SourceRecorderTypes';
9+
import { WORKSPACE_BASE_PATHS } from '../../pages/fileSystem/createInBrowserFileSystem';
910
import { Assessment } from '../assessment/AssessmentTypes';
1011
import { FileSystemState } from '../fileSystem/FileSystemTypes';
1112
import Constants from '../utils/Constants';
@@ -301,7 +302,7 @@ export const defaultWorkspaceManager: WorkspaceManagerState = {
301302
usingSubst: false,
302303
editorTabs: [
303304
{
304-
filePath: '/playground/program.js',
305+
filePath: `${WORKSPACE_BASE_PATHS.playground}/program.js`,
305306
value: defaultEditorValue,
306307
highlightedLines: [],
307308
breakpoints: []
Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
import { BFSRequire, configure } from 'browserfs';
22
import { ApiError } from 'browserfs/dist/node/core/api_error';
3+
import { FSModule } from 'browserfs/dist/node/core/FS';
34
import { Store } from 'redux';
45

6+
import { OverallState } from '../../commons/application/ApplicationTypes';
57
import { setInBrowserFileSystem } from '../../commons/fileSystem/FileSystemActions';
6-
import { BASE_PLAYGROUND_FILE_PATH } from '../playground/Playground';
8+
import { EditorTabState, WorkspaceManagerState } from '../../commons/workspace/WorkspaceTypes';
79

8-
export const createInBrowserFileSystem = (store: Store) => {
10+
export const WORKSPACE_BASE_PATHS: Record<keyof WorkspaceManagerState, string> = {
11+
assessment: '/assessment',
12+
githubAssessment: '/githubAssessment',
13+
grading: '/grading',
14+
playground: '/playground',
15+
sicp: '/sicp',
16+
sourcecast: '/sourcecast',
17+
sourcereel: '/sourcereel'
18+
};
19+
20+
export const createInBrowserFileSystem = (store: Store<OverallState>) => {
921
configure(
1022
{
1123
fs: 'MountableFileSystem',
1224
options: {
13-
[BASE_PLAYGROUND_FILE_PATH]: {
25+
[WORKSPACE_BASE_PATHS.playground]: {
1426
fs: 'IndexedDB',
1527
options: {
1628
storeName: 'playground'
@@ -22,7 +34,42 @@ export const createInBrowserFileSystem = (store: Store) => {
2234
if (err) {
2335
console.error(err);
2436
}
25-
store.dispatch(setInBrowserFileSystem(BFSRequire('fs')));
37+
// Set the browser file system reference in the Redux store for retrieval
38+
// in other parts of the codebase.
39+
const fileSystem = BFSRequire('fs');
40+
store.dispatch(setInBrowserFileSystem(fileSystem));
41+
// Create files for editor tabs if they do not exist. This can happen when
42+
// editor tabs are initialised from the Redux store defaults, as opposed to
43+
// being created by the user.
44+
const workspaceStates = store.getState().workspaces;
45+
Object.entries(workspaceStates).forEach(([_workspaceLocation, workspaceState]) => {
46+
const editorTabs = workspaceState.editorTabs;
47+
createFilesForEditorTabs(fileSystem, editorTabs);
48+
});
2649
}
2750
);
2851
};
52+
53+
// An EditorTabWithFile is an editor tab with an associated file path.
54+
type EditorTabWithFile = EditorTabState & {
55+
filePath: string;
56+
};
57+
58+
const createFilesForEditorTabs = (fileSystem: FSModule, editorTabs: EditorTabState[]) => {
59+
const editorTabsWithFile = editorTabs.filter(
60+
(editorTab): editorTab is EditorTabWithFile => editorTab.filePath !== undefined
61+
);
62+
editorTabsWithFile.forEach(editorTab => {
63+
fileSystem.exists(editorTab.filePath, filePathExists => {
64+
if (filePathExists) {
65+
return;
66+
}
67+
68+
fileSystem.writeFile(editorTab.filePath, editorTab.value, err => {
69+
if (err) {
70+
console.error(err);
71+
}
72+
});
73+
});
74+
});
75+
};

src/pages/playground/Playground.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ import {
115115
Input,
116116
SelectionRange
117117
} from '../../features/sourceRecorder/SourceRecorderTypes';
118+
import { WORKSPACE_BASE_PATHS } from '../fileSystem/createInBrowserFileSystem';
118119

119120
export type PlaygroundProps = OwnProps &
120121
DispatchProps &
@@ -207,8 +208,6 @@ export async function handleHash(hash: string, props: PlaygroundProps) {
207208
}
208209
}
209210

210-
export const BASE_PLAYGROUND_FILE_PATH = '/playground';
211-
212211
const Playground: React.FC<PlaygroundProps> = ({ workspaceLocation = 'playground', ...props }) => {
213212
const { isSicpEditor } = props;
214213
const { isMobileBreakpoint } = useResponsive();
@@ -820,7 +819,7 @@ const Playground: React.FC<PlaygroundProps> = ({ workspaceLocation = 'playground
820819
const editorContainerProps: NormalEditorContainerProps = {
821820
..._.pick(props, 'editorSessionId', 'isEditorAutorun'),
822821
editorVariant: 'normal',
823-
baseFilePath: BASE_PLAYGROUND_FILE_PATH,
822+
baseFilePath: WORKSPACE_BASE_PATHS.playground,
824823
isFolderModeEnabled,
825824
activeEditorTabIndex,
826825
setActiveEditorTabIndex,
@@ -895,7 +894,7 @@ const Playground: React.FC<PlaygroundProps> = ({ workspaceLocation = 'playground
895894
body: (
896895
<FileSystemView
897896
workspaceLocation="playground"
898-
basePath={BASE_PLAYGROUND_FILE_PATH}
897+
basePath={WORKSPACE_BASE_PATHS.playground}
899898
/>
900899
),
901900
iconName: IconNames.FOLDER_CLOSE,

0 commit comments

Comments
 (0)