mirror of
https://github.com/leaningtech/javafiddle.git
synced 2025-09-04 19:32:26 +00:00
implement examples
This commit is contained in:
12
.editorconfig
Normal file
12
.editorconfig
Normal 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
|
@@ -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}
|
||||
|
51
src/lib/repl/sidebar/Examples.svelte
Normal file
51
src/lib/repl/sidebar/Examples.svelte
Normal 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}
|
@@ -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>
|
||||
|
20
static/examples/hello-world-swing/Main.java
Normal file
20
static/examples/hello-world-swing/Main.java
Normal 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);
|
||||
}
|
||||
}
|
5
static/examples/hello-world/Main.java
Normal file
5
static/examples/hello-world/Main.java
Normal file
@@ -0,0 +1,5 @@
|
||||
class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello, World!");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user