Skip to content

Commit

Permalink
feat(runtime): store to support file creation
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Sep 4, 2024
1 parent 81eac67 commit 88e4815
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/react/src/Panels/EditorPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface Props {
onEditorScroll?: OnEditorScroll;
onHelpClick?: () => void;
onFileSelect?: (value?: string) => void;
onFileCreate?: (value: string) => void;
}

export function EditorPanel({
Expand All @@ -46,6 +47,7 @@ export function EditorPanel({
onEditorScroll,
onHelpClick,
onFileSelect,
onFileCreate,
}: Props) {
const fileTreePanelRef = useRef<ImperativePanelHandle>(null);

Expand Down Expand Up @@ -81,6 +83,7 @@ export function EditorPanel({
files={files}
scope={fileTreeScope}
onFileSelect={onFileSelect}
onFileCreate={onFileCreate}
/>
</Panel>
<PanelResizeHandle
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/Panels/WorkspacePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ function EditorSection({ theme, tutorialStore, hasEditor }: PanelProps) {
helpAction={helpAction}
onHelpClick={lessonFullyLoaded ? onHelpClick : undefined}
onFileSelect={(filePath) => tutorialStore.setSelectedFile(filePath)}
onFileCreate={(filePath) => tutorialStore.addFile(filePath)}
selectedFile={selectedFile}
onEditorScroll={(position) => tutorialStore.setCurrentDocumentScrollPosition(position)}
onEditorChange={(update) => tutorialStore.setCurrentDocumentContent(update.content)}
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/core/FileTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface Props {
files: string[];
selectedFile?: string;
onFileSelect?: (filePath: string) => void;
onFileCreate?: (filePath: string) => void;
hideRoot: boolean;
scope?: string;
hiddenFiles?: Array<string | RegExp>;
Expand Down
9 changes: 9 additions & 0 deletions packages/runtime/src/store/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ export class EditorStore {
});
}

addFile(filePath: string) {
this.documents.setKey(filePath, {
filePath,
value: '',
loading: false,
});
this.setSelectedFile(filePath);
}

updateFile(filePath: string, content: string): boolean {
const documentState = this.documents.get()[filePath];

Expand Down
5 changes: 5 additions & 0 deletions packages/runtime/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ export class TutorialStore {
this._editorStore.setSelectedFile(filePath);
}

addFile(filePath: string) {
this._editorStore.addFile(filePath);
this._runner.updateFile(filePath, '');
}

updateFile(filePath: string, content: string) {
const hasChanged = this._editorStore.updateFile(filePath, content);

Expand Down

0 comments on commit 88e4815

Please sign in to comment.