Skip to content

Ensure that editor tabs have corresponding files #2388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/commons/application/ApplicationTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DashboardState } from '../../features/dashboard/DashboardTypes';
import { Grading } from '../../features/grading/GradingTypes';
import { PlaygroundState } from '../../features/playground/PlaygroundTypes';
import { PlaybackStatus, RecordingStatus } from '../../features/sourceRecorder/SourceRecorderTypes';
import { WORKSPACE_BASE_PATHS } from '../../pages/fileSystem/createInBrowserFileSystem';
import { Assessment } from '../assessment/AssessmentTypes';
import { FileSystemState } from '../fileSystem/FileSystemTypes';
import Constants from '../utils/Constants';
Expand Down Expand Up @@ -301,7 +302,7 @@ export const defaultWorkspaceManager: WorkspaceManagerState = {
usingSubst: false,
editorTabs: [
{
filePath: '/playground/program.js',
filePath: `${WORKSPACE_BASE_PATHS.playground}/program.js`,
value: defaultEditorValue,
highlightedLines: [],
breakpoints: []
Expand Down
55 changes: 51 additions & 4 deletions src/pages/fileSystem/createInBrowserFileSystem.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import { BFSRequire, configure } from 'browserfs';
import { ApiError } from 'browserfs/dist/node/core/api_error';
import { FSModule } from 'browserfs/dist/node/core/FS';
import { Store } from 'redux';

import { OverallState } from '../../commons/application/ApplicationTypes';
import { setInBrowserFileSystem } from '../../commons/fileSystem/FileSystemActions';
import { BASE_PLAYGROUND_FILE_PATH } from '../playground/Playground';
import { EditorTabState, WorkspaceManagerState } from '../../commons/workspace/WorkspaceTypes';

export const createInBrowserFileSystem = (store: Store) => {
export const WORKSPACE_BASE_PATHS: Record<keyof WorkspaceManagerState, string> = {
assessment: '/assessment',
githubAssessment: '/githubAssessment',
grading: '/grading',
playground: '/playground',
sicp: '/sicp',
sourcecast: '/sourcecast',
sourcereel: '/sourcereel'
};

export const createInBrowserFileSystem = (store: Store<OverallState>) => {
configure(
{
fs: 'MountableFileSystem',
options: {
[BASE_PLAYGROUND_FILE_PATH]: {
[WORKSPACE_BASE_PATHS.playground]: {
fs: 'IndexedDB',
options: {
storeName: 'playground'
Expand All @@ -22,7 +34,42 @@ export const createInBrowserFileSystem = (store: Store) => {
if (err) {
console.error(err);
}
store.dispatch(setInBrowserFileSystem(BFSRequire('fs')));
// Set the browser file system reference in the Redux store for retrieval
// in other parts of the codebase.
const fileSystem = BFSRequire('fs');
store.dispatch(setInBrowserFileSystem(fileSystem));
// Create files for editor tabs if they do not exist. This can happen when
// editor tabs are initialised from the Redux store defaults, as opposed to
// being created by the user.
const workspaceStates = store.getState().workspaces;
Object.entries(workspaceStates).forEach(([_workspaceLocation, workspaceState]) => {
const editorTabs = workspaceState.editorTabs;
createFilesForEditorTabs(fileSystem, editorTabs);
});
}
);
};

// An EditorTabWithFile is an editor tab with an associated file path.
type EditorTabWithFile = EditorTabState & {
filePath: string;
};

const createFilesForEditorTabs = (fileSystem: FSModule, editorTabs: EditorTabState[]) => {
const editorTabsWithFile = editorTabs.filter(
(editorTab): editorTab is EditorTabWithFile => editorTab.filePath !== undefined
);
editorTabsWithFile.forEach(editorTab => {
fileSystem.exists(editorTab.filePath, filePathExists => {
if (filePathExists) {
return;
}

fileSystem.writeFile(editorTab.filePath, editorTab.value, err => {
if (err) {
console.error(err);
}
});
});
});
};
7 changes: 3 additions & 4 deletions src/pages/playground/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ import {
Input,
SelectionRange
} from '../../features/sourceRecorder/SourceRecorderTypes';
import { WORKSPACE_BASE_PATHS } from '../fileSystem/createInBrowserFileSystem';

export type PlaygroundProps = OwnProps &
DispatchProps &
Expand Down Expand Up @@ -207,8 +208,6 @@ export async function handleHash(hash: string, props: PlaygroundProps) {
}
}

export const BASE_PLAYGROUND_FILE_PATH = '/playground';

const Playground: React.FC<PlaygroundProps> = ({ workspaceLocation = 'playground', ...props }) => {
const { isSicpEditor } = props;
const { isMobileBreakpoint } = useResponsive();
Expand Down Expand Up @@ -819,7 +818,7 @@ const Playground: React.FC<PlaygroundProps> = ({ workspaceLocation = 'playground
const editorContainerProps: NormalEditorContainerProps = {
..._.pick(props, 'editorSessionId', 'isEditorAutorun'),
editorVariant: 'normal',
baseFilePath: BASE_PLAYGROUND_FILE_PATH,
baseFilePath: WORKSPACE_BASE_PATHS.playground,
isFolderModeEnabled,
activeEditorTabIndex,
setActiveEditorTabIndex,
Expand Down Expand Up @@ -894,7 +893,7 @@ const Playground: React.FC<PlaygroundProps> = ({ workspaceLocation = 'playground
body: (
<FileSystemView
workspaceLocation="playground"
basePath={BASE_PLAYGROUND_FILE_PATH}
basePath={WORKSPACE_BASE_PATHS.playground}
/>
),
iconName: IconNames.FOLDER_CLOSE,
Expand Down