Skip to content

Commit

Permalink
chore: stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
lajbel committed Nov 23, 2024
1 parent 4d3916b commit c4723eb
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ KAPLAYGROUND is a web based editor to create, edit and export KAPLAY projects.
- Export & Improt projects
- Autosaved project in local
- Snapable editor

```
git submodule update --init --recursive
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "2.0.0",
"bin": "scripts/cli.js",
"scripts": {
"install": "git submodule update --init --recursive && cd kaplay && pnpm i",
"install": "cd kaplay && pnpm i",
"dev": "vite dev",
"start": "vite dev",
"build": "vite build",
Expand Down
13 changes: 12 additions & 1 deletion src/components/Playground/GameView.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { type FC } from "react";
import { type FC, useEffect } from "react";
import { useEditor } from "../../hooks/useEditor";

type GameViewProps = {
onLoad?: () => void;
};

export const GameView: FC<GameViewProps> = ({ onLoad }) => {
const { setRuntime } = useEditor();

useEffect(() => {
const iframe = document.getElementById(
"game-view",
) as HTMLIFrameElement;

setRuntime({ iframe: iframe });
}, []);

return (
<iframe
id="game-view"
Expand Down
41 changes: 38 additions & 3 deletions src/components/Toolbar/Projects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import ToolbarButton from "./ToolbarButton";

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

const handleDownload = () => {
const projectLocal = localStorage.getItem(project.id);
Expand All @@ -28,6 +28,21 @@ const Projects: FC = () => {
showNotification("Exporting the project, check downloads...");
};

const handleExportHTML = () => {
const projectCode = getRuntime().iframe?.srcdoc;

if (!projectCode) {
showNotification("No project to export... Remember to save!");
return;
}

const blob = new Blob([projectCode], {
type: "text/html",
});

downloadBlob(blob, `${project.name}.html`);
};

const handleProjectUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (!file) return;
Expand Down Expand Up @@ -65,12 +80,18 @@ const Projects: FC = () => {
reader.readAsText(file);
};

const handleProjectReset = () => {
const handleNewProject = () => {
createNewProject("pj");
update();
run();
};

const handleNewExample = () => {
createNewProject("ex");
update();
run();
};

return (
<div className="dropdown dropdown-end flex-grow-0 flex-shrink-0 basis-24 h-full">
<ToolbarButton
Expand All @@ -90,6 +111,13 @@ const Projects: FC = () => {
Export project
</button>
</li>
<li>
<button
onClick={handleExportHTML}
>
Export project as .html
</button>
</li>
<li>
<label>
<input
Expand All @@ -103,11 +131,18 @@ const Projects: FC = () => {
</li>
<li>
<button
onClick={handleProjectReset}
onClick={handleNewProject}
>
Create new project
</button>
</li>
<li>
<button
onClick={handleNewExample}
>
Create new example
</button>
</li>
</ul>
</div>
);
Expand Down
8 changes: 4 additions & 4 deletions src/hooks/useEditor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Monaco } from "@monaco-editor/react";
import type { editor } from "monaco-editor";
import { createRef, type MutableRefObject } from "react";
import { createRef, type RefObject } from "react";
import { toast } from "react-toastify";
import { create } from "zustand";
import { wrapGame } from "../util/compiler";
Expand All @@ -12,7 +12,7 @@ type EditorRuntime = {
monaco: Monaco | null;
currentFile: string;
gylphDecorations: editor.IEditorDecorationsCollection | null;
iframe: MutableRefObject<HTMLIFrameElement>;
iframe: HTMLIFrameElement | null;
};

type EditorStore = {
Expand All @@ -21,7 +21,7 @@ type EditorStore = {
monaco: Monaco | null;
currentFile: string;
gylphDecorations: editor.IEditorDecorationsCollection | null;
iframe: MutableRefObject<HTMLIFrameElement>;
iframe: HTMLIFrameElement | null;
};
update: (value?: string) => void;
run: () => void;
Expand All @@ -41,7 +41,7 @@ export const useEditor = create<EditorStore>((set, get) => ({
monaco: null,
currentFile: "main.js",
gylphDecorations: null,
iframe: createRef() as MutableRefObject<HTMLIFrameElement>,
iframe: null,
isDefaultExample: false,
},
setRuntime: (runtime) => {
Expand Down

0 comments on commit c4723eb

Please sign in to comment.