Skip to content
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

fix: can't change files again #34

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions src/components/Editor/MonacoEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type Props = {

const MonacoEditor: FC<Props> = (props) => {
const { updateFile, getFile } = useProject();
const { run, update, updateImageDecorations, getRuntime } = useEditor();
const { run, update, updateImageDecorations, getRuntime, setRuntime } =
useEditor();

const handleEditorBeforeMount = (monaco: Monaco) => {
configMonaco(monaco);
Expand All @@ -22,8 +23,7 @@ const MonacoEditor: FC<Props> = (props) => {
editor: editor.IStandaloneCodeEditor,
monaco: Monaco,
) => {
getRuntime().editor = editor;
getRuntime().monaco = monaco;
setRuntime({ editor, monaco });
const currentFile = getRuntime().currentFile;

props.onMount?.();
Expand Down
6 changes: 3 additions & 3 deletions src/components/FileTree/FileEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ const logoByKind = {

const FileEntry: FC<Props> = ({ file }) => {
const { removeFile } = useProject();
const { getRuntime } = useEditor();
const { getRuntime, setCurrentFile } = useEditor();

const handleClick: MouseEventHandler = () => {
getRuntime().currentFile = file.path;
setCurrentFile(file.path);
};

const handleDelete: MouseEventHandler = (e) => {
Expand All @@ -35,7 +35,7 @@ const FileEntry: FC<Props> = ({ file }) => {

if (confirm("Are you sure you want to remove this scene?")) {
removeFile(file.path);
getRuntime().currentFile = "main.js";
setCurrentFile("main.js");
}
};

Expand Down
22 changes: 21 additions & 1 deletion src/hooks/useEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type EditorStore = {
update: (value?: string) => void;
run: () => void;
getRuntime: () => EditorRuntime;
setRuntime: (runtime: Partial<EditorRuntime>) => void;
setCurrentFile: (currentFile: string) => void;
setTheme: (theme: string) => void;
updateImageDecorations: () => void;
showNotification: (message: string) => void;
Expand All @@ -34,7 +36,7 @@ type EditorStore = {
loadProject: (project: string) => void;
};

export const useEditor = create<EditorStore>((_set, get) => ({
export const useEditor = create<EditorStore>((set, get) => ({
runtime: {
editor: null,
monaco: null,
Expand All @@ -43,7 +45,25 @@ export const useEditor = create<EditorStore>((_set, get) => ({
iframe: createRef() as MutableRefObject<HTMLIFrameElement>,
isDefaultExample: false,
},
setRuntime: (runtime) => {
set((state) => ({
runtime: {
...state.runtime,
...runtime,
},
}));
},
getRuntime: () => get().runtime,
setCurrentFile: (currentFile) => {
set((state) => ({
runtime: {
...state.runtime,
currentFile,
},
}));

get().update();
},
update: (customValue?: string) => {
if (customValue) {
debug(0, "Editor value updated with custom value");
Expand Down