Skip to content

Commit 03a58cb

Browse files
committed
bad dropdowns
1 parent 46e7d26 commit 03a58cb

5 files changed

Lines changed: 77 additions & 38 deletions

File tree

src/serializer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Blockly from 'blockly';
2-
import * as JSZip from 'jszip';
2+
import JSZip from 'jszip';
33

44
function sanitizeFilename(name: string): string {
55
return name

src/ui/Editor.svelte

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
}
1313
1414
export interface EditorState {
15+
workspace: Blockly.WorkspaceSvg
16+
canvas: HTMLCanvasElement
1517
editorModalKind?: "variable" | "projectSettings" | "editorSettings" | null,
1618
save: {
1719
unsavedChanges: boolean,
@@ -48,22 +50,23 @@
4850
})
4951
5052
let editorState: EditorState = $state({
53+
workspace: null,
54+
canvas: null,
5155
editorModalKind: null,
5256
save: null
5357
})
5458
5559
registerContinuousToolbox();
5660
5761
let blocklyDiv: HTMLDivElement | null = null;
58-
let workspace: Blockly.WorkspaceSvg | null = null;
5962
6063
onMount(() => {
6164
if (!blocklyDiv) return;
6265
6366
const toolboxElement = document.createElement("xml");
6467
toolboxElement.innerHTML = toolbox;
6568
66-
workspace = Blockly.inject(blocklyDiv, {
69+
editorState.workspace = Blockly.inject(blocklyDiv, {
6770
toolbox: toolboxElement,
6871
collapse: true,
6972
comments: true,
@@ -282,29 +285,30 @@
282285
</block>
283286
`
284287
285-
Blockly.Xml.domToWorkspace(defaultWorkspaceElement, workspace)
288+
Blockly.Xml.domToWorkspace(defaultWorkspaceElement, editorState.workspace)
286289
287-
workspace.addChangeListener(Blockly.Events.disableOrphans);
290+
editorState.workspace.addChangeListener(Blockly.Events.disableOrphans);
288291
289292
const listener = (e: Blockly.Events.Abstract) => {
290293
if (
291294
e.isUiEvent ||
292295
e.type === Blockly.Events.FINISHED_LOADING ||
293-
workspace?.isDragging()
296+
editorState.workspace?.isDragging()
294297
)
295298
return;
296-
Compiler.compile(workspace!);
299+
Compiler.compile(editorState.workspace!);
297300
if (editorState.save) {
298301
editorState.save.unsavedChanges = true
299302
}
300303
};
301304
302-
workspace.addChangeListener(listener);
305+
editorState.workspace.addChangeListener(listener);
303306
304307
onDestroy(() => {
305-
workspace?.removeChangeListener(listener);
306-
workspace?.dispose();
307-
workspace = null;
308+
editorState.workspace?.removeChangeListener(listener);
309+
editorState.workspace?.removeChangeListener(Blockly.Events.disableOrphans);
310+
editorState.workspace?.dispose();
311+
editorState.workspace = null;
308312
});
309313
});
310314
</script>
@@ -317,16 +321,33 @@
317321
<div id="pageContainer">
318322
<div bind:this={blocklyDiv} id="blocklyDiv"></div>
319323
<RaymarcherPreview
320-
size={projectSettings.size}
321-
previewSize={savedEditorState.preview.size}
324+
{editorState}
325+
{projectSettings}
326+
{savedEditorState}
322327
/>
323328
</div>
324329
{#if editorState.editorModalKind === "variable"}
325330
<EditorModalVariable
326-
title="Create a Variable"
327-
acceptText="Create"
328-
cancelText="Cancel"
331+
title="Create a Variable"
332+
acceptText="Create"
333+
cancelText="Cancel"
329334
></EditorModalVariable>
335+
{:else if editorState.editorModalKind === "editorSettings"}
336+
<EditorModal
337+
title="Editor Settings"
338+
acceptText="Accept"
339+
cancelText="Cancel"
340+
onAccept={() => {}}
341+
onCancel={() => {editorState.editorModalKind = null}}
342+
></EditorModal>
343+
{:else if editorState.editorModalKind === "projectSettings"}
344+
<EditorModal
345+
title="Project Settings"
346+
acceptText="Accept"
347+
cancelText="Cancel"
348+
onAccept={() => {}}
349+
onCancel={() => {editorState.editorModalKind = null}}
350+
></EditorModal>
330351
{/if}
331352
</div>
332353
<AlphaWarning />

src/ui/EditorTopBar/EditorTopBar.svelte

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import EditorTopBarDropdownContentOption from "./EditorTopBarDropdown/EditorTopBarDropdownContentOption/EditorTopBarDropdownContentOption.svelte";
55
import EditorTopBarDropdownContentSeparator from "./EditorTopBarDropdownContentSeparator/EditorTopBarDropdownContentSeparator.svelte";
66
7+
import * as Serializer from "../../serializer"
8+
79
import type { ProjectSettings } from "../Editor.svelte"
810
import type { EditorState } from "../Editor.svelte"
911
@@ -25,16 +27,25 @@
2527
<EditorTopBarDropdownContentOption onclick={() => {}}>Load form Computer</EditorTopBarDropdownContentOption>
2628
<EditorTopBarDropdownContentSeparator />
2729
<EditorTopBarDropdownContentOption onclick={() => {
28-
30+
2931
}}>Save as...</EditorTopBarDropdownContentOption>
30-
<EditorTopBarDropdownContentOption onclick={() => {
31-
// Serializer.createFile(Workspace, "Untitled Project", Canvas).then(blob => {
32-
// const a = document.createElement('a');
33-
// a.href = URL.createObjectURL(blob);
34-
// a.download = 'Untitled Project.json';
35-
// a.click();
36-
// URL.revokeObjectURL(a.href);
37-
// });
32+
<EditorTopBarDropdownContentOption onclick={async () => {
33+
const file = await Serializer.createFile(
34+
props.editorState.workspace,
35+
props.projectSettings.name,
36+
props.editorState.canvas
37+
);
38+
39+
const url = URL.createObjectURL(file);
40+
const a = document.createElement('a');
41+
42+
a.href = url;
43+
a.download = file.name;
44+
45+
document.body.appendChild(a);
46+
a.click();
47+
document.body.removeChild(a);
48+
URL.revokeObjectURL(url);
3849
}}>Save to seperate file</EditorTopBarDropdownContentOption>
3950
</EditorTopBarDropdown>
4051

src/ui/EditorTopBar/EditorTopBarDropdown/EditorTopBarDropdown.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
</script>
1111

1212
<div class="dropdown">
13-
<button onclick={() => open = !open} onfocusout={() => open = false} class="dropbtn">{props.text} <span class="arrow">▼</span></button>
13+
<button onclick={() => open = !open} class="dropbtn">
14+
{props.text} <span class="arrow">▼</span>
15+
</button>
1416
{#if open}
1517
<div class="dropdown-content">
16-
{@render props.children?.()}
18+
{@render props.children?.()}
1719
</div>
1820
{/if}
1921
</div>

src/ui/RaymarcherPreview/RaymarcherPreview.svelte

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,31 @@
22
import { draggable } from "@neodrag/svelte";
33
import { makeGraphics, startLoop } from "../../compilier"
44
import { onMount } from "svelte";
5+
6+
import type { EditorState } from "../Editor.svelte"
7+
import type { ProjectSettings } from "../Editor.svelte"
8+
import type { SavedEditorState } from "../Editor.svelte"
59
6-
let canvas: HTMLCanvasElement
10+
interface RaymarcherPreviewProps {
11+
editorState: EditorState,
12+
projectSettings: ProjectSettings,
13+
savedEditorState: SavedEditorState
14+
}
715
8-
const props: {
9-
size: [number, number]
10-
previewSize: [number, number]
11-
} = $props()
16+
const props: RaymarcherPreviewProps = $props()
1217
1318
onMount(() => {
14-
makeGraphics(canvas)
19+
makeGraphics(props.editorState.canvas)
1520
startLoop()
1621
})
1722
</script>
1823

19-
<canvas bind:this={canvas} use:draggable={{ bounds: 'parent' }}
20-
width={props.size[0]}
21-
height={props.size[1]}
24+
<canvas bind:this={props.editorState.canvas} use:draggable={{ bounds: 'parent' }}
25+
width={props.projectSettings.size[0]}
26+
height={props.projectSettings.size[1]}
2227
id="raymarcherCanvas"
23-
style:width='{props.previewSize[0]}px'
24-
style:height='{props.previewSize[1]}px'
28+
style:width='{props.savedEditorState.preview.size[0]}px'
29+
style:height='{props.savedEditorState.preview.size[1]}px'
2530
>
2631
</canvas>
2732

0 commit comments

Comments
 (0)