Skip to content

Commit

Permalink
fix: some stuff related to project changing
Browse files Browse the repository at this point in the history
  • Loading branch information
lajbel committed Sep 24, 2024
1 parent f202272 commit 2218afc
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/components/Config/ConfigDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ const ConfigDialog = () => {
KAPLAY Configuration are disabled in
examples mode. Try Projects (toolbox) {"->"}
{" "}
Reset Project
Create Project
</>
)}
</main>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Playground/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const Playground = () => {
const {
project,
getProjectMode,
resetProject,
createNewProject,
} = useProject();
const [loadingProject, setLoadingProject] = useState<boolean>(true);
const [loadingEditor, setLoadingEditor] = useState<boolean>(true);
Expand All @@ -32,7 +32,7 @@ const Playground = () => {
useEffect(() => {
if (project.files.size > 0) setLoadingProject(false);
else {
resetProject();
createNewProject();
}
}, [project]);

Expand Down
6 changes: 3 additions & 3 deletions src/components/Toolbar/Projects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { downloadBlob } from "../../util/download";
import ToolbarButton from "./ToolbarButton";

const Projects: FC = () => {
const { project, replaceProject, loadProject } = useProject();
const { project, replaceProject, createNewProject } = useProject();
const { update, run, showNotification } = useEditor();

const handleDownload = () => {
Expand All @@ -34,7 +34,7 @@ const Projects: FC = () => {
};

const handleProjectReset = () => {
loadProject("kaplay-unsaved-pj");
createNewProject();
update();
run();
};
Expand Down Expand Up @@ -72,7 +72,7 @@ const Projects: FC = () => {
<button
onClick={handleProjectReset}
>
Reset project
Create new project
</button>
</li>
</ul>
Expand Down
21 changes: 15 additions & 6 deletions src/stores/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { StateCreator } from "zustand";
import { defaultProject } from "../config/defaultProject";
import { useEditor } from "../hooks/useEditor";
import { useProject } from "../hooks/useProject";
import { debug } from "../util/logs";
import type { Asset, AssetsSlice } from "./storage/assets";
import type { File, FilesSlice } from "./storage/files";

Expand All @@ -18,8 +19,8 @@ export type Project = {
export interface ProjectSlice {
/** The current project */
project: Project;
/** Reset the project */
resetProject: () => void;
/** Creates a new project */
createNewProject: () => void;
/** Replace the project with a new project */
replaceProject: (project: Project) => void;
/** Set the project mode */
Expand Down Expand Up @@ -60,27 +61,33 @@ export const createProjectSlice: StateCreator<
kaplayConfig: {},
mode: "project",
},
resetProject: () => {
console.debug("Resetting project");
createNewProject: () => {
debug(0, "creating a new project");

const files = new Map();
const assets = new Map();

// Load default setup
get().loadDefaultSetup("project", files, assets);
debug(1, "New files for the new project", files, assets);

console.log("New files", files, assets);
useProject.persist.setOptions({
name: "Untitled Project",
});
useProject.persist.rehydrate();

set(() => ({
project: {
name: "Untitled Project",
name: "Project #" + get().getSavedProjects().length,
version: "2.0.0",
files: files,
assets: assets,
kaplayConfig: {},
mode: "project",
},
}));

useEditor.getState().update();
},
replaceProject: (project) => {
const { run, update } = useEditor.getState();
Expand Down Expand Up @@ -121,6 +128,8 @@ export const createProjectSlice: StateCreator<
});

useProject.persist.rehydrate();

localStorage.removeItem(`Untitled Project`);
},
isProjectSaved: (name: string) => {
return get().getSavedProjects().includes(`pj-${name}`);
Expand Down
9 changes: 8 additions & 1 deletion src/util/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useProject } from "../hooks/useProject";
import { debug } from "./logs";

// Wraps the game in an acceptable format for iFrame
export const wrapGame = (code: string) => `
Expand Down Expand Up @@ -34,6 +35,8 @@ const transformAssetUrl = (regex: RegExp, code: string) => {
const { project: { assets: resources } } = useProject.getState();

return code.replace(regex, (match, asset: string) => {
debug(0, "asset matched", asset);

// remove first / and last / from asset, also remove "assets" from asset
const normalizeAsset = asset.replace(/^\/|\/$/g, "").replace(
"assets/",
Expand All @@ -51,8 +54,12 @@ export const parseAssets = (code: string) => {
const regexLoad = /load\w+\(\s*"[^"]*",\s*"([^"]*)"\s*\)/g;
const regexComment = /\/\/\s*kaplay-transformation-asset\s*(.*)/g;

return transformAssetUrl(
const codeTransformed = transformAssetUrl(
regexLoad,
transformAssetUrl(regexComment, code),
);

debug(1, "code with assets", codeTransformed);

return codeTransformed;
};
2 changes: 1 addition & 1 deletion src/util/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 0: Normal log
// 1: Internal and long log
// 2: Ultra internal and long log
export const debug = (level: number = 0, ...msg: string[]) => {
export const debug = (level: number = 0, ...msg: any[]) => {
if (level === 0) {
console.debug(`%c${msg.join(" ")}`, "color: #ff00ff");
} else if (level === 1) {
Expand Down

0 comments on commit 2218afc

Please sign in to comment.