implement examples

This commit is contained in:
Alex Bates
2023-09-27 15:12:46 +01:00
parent 635c2a54ef
commit f7794fe996
6 changed files with 92 additions and 1 deletions

12
.editorconfig Normal file
View File

@@ -0,0 +1,12 @@
root = true
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = true
[*.java]
indent_style = space
indent_size = 4

View File

@@ -22,7 +22,7 @@
</div>
<div class="w-80 grow overflow-hidden">
<div class="h-1/2 overflow-y-auto">
<div class="h-1/2 overflow-y-auto flex flex-col">
<SidebarOptions forceClose={!$isSidebarOpen} on:selectOption={() => $isSidebarOpen = true} />
</div>
{#if $isSidebarOpen}

View File

@@ -0,0 +1,51 @@
<script lang="ts">
import { goto } from "$app/navigation";
import Loading from "$lib/Loading.svelte";
import { compress, type Fiddle } from "$lib/compress-fiddle";
import { onMount } from "svelte";
async function fetchExampleFile(path: string) {
const res = await fetch(`/examples/${path}`);
return res.text();
}
let examples: Fiddle[] = [];
onMount(async () => {
examples = [
{
title: "Hello world",
files: [
{
path: "Main.java",
content: await fetchExampleFile("hello-world/Main.java"),
},
],
},
{
title: "Hello world with Swing",
files: [
{
path: "Main.java",
content: await fetchExampleFile("hello-world-swing/Main.java"),
},
],
},
];
});
</script>
{#each examples as fiddle}
<button
class="w-full text-left flex items-center px-4 py-2 hover:bg-stone-200 dark:hover:bg-stone-900"
on:click={async () => {
const id = compress(fiddle);
await goto(`/${id}`);
}}
>
<div class="grow">{fiddle.title}</div>
</button>
{:else}
<div class="flex justify-center grow">
<Loading />
</div>
{/each}

View File

@@ -3,6 +3,7 @@
import { createEventDispatcher } from "svelte";
import Favourites from "./Favourites.svelte";
import { browser } from "$app/environment";
import Examples from "./Examples.svelte";
const dispatch = createEventDispatcher<{ selectOption: number }>();
@@ -41,6 +42,8 @@
</div>
{#if selectedOptionIndex === 0}
<Favourites />
{:else if selectedOptionIndex === 1}
<Examples />
{/if}
{:else}
<ul>

View File

@@ -0,0 +1,20 @@
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
class Main implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Main());
}
public void run() {
JFrame f = new JFrame("Swing example");
JLabel l = new JLabel("Hello, world!");
l.setFont(new Font("Serif", Font.PLAIN, 42));
f.add(l);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}

View File

@@ -0,0 +1,5 @@
class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}