keep persistent EditorSession on demo page

This commit is contained in:
Ilion Beyst 2022-02-07 20:56:08 +01:00
parent d45699189e
commit 4feebe5043
6 changed files with 49 additions and 24 deletions

View file

@ -29,6 +29,7 @@
"svelte-preprocess": "^4.9.4", "svelte-preprocess": "^4.9.4",
"tslib": "^2.3.1", "tslib": "^2.3.1",
"typescript": "^4.4.3", "typescript": "^4.4.3",
"vite-plugin-iso-import": "^0.1.3",
"vite-plugin-wasm-pack": "^0.1.9" "vite-plugin-wasm-pack": "^0.1.9"
}, },
"dependencies": { "dependencies": {

View file

@ -1 +1,5 @@
/// <reference types="@sveltejs/kit" /> /// <reference types="@sveltejs/kit" />
// fallback
declare module "*?client";
declare module "*?server";

View file

@ -0,0 +1,27 @@
<script lang="ts">
import type { Ace } from "ace-builds";
import ace from "ace-builds/src-noconflict/ace?client";
import * as aceGithubTheme from "ace-builds/src-noconflict/theme-github?client";
import { onMount } from "svelte";
export let editSession: Ace.EditSession;
let editorDiv: HTMLDivElement | undefined;
let editor: Ace.Editor | undefined;
onMount(async () => {
let renderer = new ace.VirtualRenderer(editorDiv);
editor = new ace.Editor(renderer, editSession);
editor.setTheme(aceGithubTheme);
});
</script>
<div bind:this={editorDiv} class="editor" />
<style>
.editor {
width: 100%;
height: 100%;
}
</style>

View file

@ -4,38 +4,35 @@
import { onMount } from "svelte"; import { onMount } from "svelte";
import "./style.css"; import "./style.css";
let editor; import ace from "ace-builds/src-noconflict/ace?client";
import Editor from "$lib/components/Editor.svelte";
import type { Ace } from "ace-builds";
let matches = []; let matches = [];
let selectedMatchId: string | undefined = undefined; let selectedMatchId: string | undefined = undefined;
let selectedMatchLog: string | undefined = undefined; let selectedMatchLog: string | undefined = undefined;
let editSession: Ace.EditSession;
onMount(async () => { onMount(async () => {
await load_editor(); await init_editor();
}); });
async function load_editor() { async function init_editor() {
const ace = await import("ace-builds"); const AcePythonMode = await import("ace-builds/src-noconflict/mode-python");
const python_mode = await import("ace-builds/src-noconflict/mode-python"); editSession = new ace.EditSession("");
const gh_theme = await import("ace-builds/src-noconflict/theme-github"); editSession.setMode(new AcePythonMode.Mode());
editor = ace.edit("editor");
editor.getSession().setMode(new python_mode.Mode());
editor.setTheme(gh_theme);
} }
async function submitCode() { async function submitCode() {
if (editor === undefined) {
return;
}
let response = await fetch("/api/submit_bot", { let response = await fetch("/api/submit_bot", {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify({
code: editor.getValue(), code: editSession.getDocument().getValue(),
}), }),
}); });
@ -46,7 +43,7 @@
let responseData = await response.json(); let responseData = await response.json();
let matchId = responseData["match_id"]; let matchId = responseData["match_id"];
// goto(`/submission_matches/${matchId}`);
matches.push({ matchId: matchId }); matches.push({ matchId: matchId });
matches = matches; matches = matches;
} }
@ -72,7 +69,6 @@
function selectEditor() { function selectEditor() {
selectedMatchId = undefined; selectedMatchId = undefined;
selectedMatchLog = undefined; selectedMatchLog = undefined;
load_editor();
} }
</script> </script>
@ -99,7 +95,7 @@
{#if selectedMatchLog !== undefined} {#if selectedMatchLog !== undefined}
<Visualizer matchLog={selectedMatchLog} /> <Visualizer matchLog={selectedMatchLog} />
{:else} {:else}
<div id="editor" /> <Editor {editSession} />
{/if} {/if}
</div> </div>
<div class="sidebar-right"> <div class="sidebar-right">
@ -144,11 +140,6 @@
overflow: hidden; overflow: hidden;
} }
#editor {
width: 100%;
height: 100%;
}
.editor-container { .editor-container {
height: 100%; height: 100%;
} }

View file

@ -1,3 +1,3 @@
body { body {
margin: 0; margin: 0;
} }

View file

@ -2,6 +2,7 @@ import adapter from "@sveltejs/adapter-auto";
import preprocess from "svelte-preprocess"; import preprocess from "svelte-preprocess";
import { viteCommonjs } from "@originjs/vite-plugin-commonjs"; import { viteCommonjs } from "@originjs/vite-plugin-commonjs";
import wasmPack from "vite-plugin-wasm-pack"; import wasmPack from "vite-plugin-wasm-pack";
import { isoImport } from "vite-plugin-iso-import";
/** @type {import('@sveltejs/kit').Config} */ /** @type {import('@sveltejs/kit').Config} */
const config = { const config = {
@ -17,6 +18,7 @@ const config = {
ssr: false, ssr: false,
vite: { vite: {
plugins: [ plugins: [
isoImport(),
wasmPack([], ["planetwars-rs"]), wasmPack([], ["planetwars-rs"]),
viteCommonjs({ viteCommonjs({
transformMixedEsModules: true, transformMixedEsModules: true,