Skip to content

Commit

Permalink
fix: can't change files again (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
lajbel authored Oct 1, 2024
1 parent 17e6cd9 commit fbaa50f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
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

0 comments on commit fbaa50f

Please sign in to comment.