schreiben/src/views/Page.svelte

69 lines
1.7 KiB
Svelte
Raw Normal View History

2023-03-05 17:10:29 +00:00
<script lang="ts">
import type { XmlFragment } from "yjs";
import { inject } from "regexparam";
2023-03-05 17:10:29 +00:00
import Editor from "../editor/Editor.svelte";
import { getWorldPage, getWorldY, type WorldY } from "../lib/doc";
import { routes } from "../lib/routes";
import { loadWorlds } from "../lib/worldStorage";
import { lastPageStore } from "../lib/router";
2023-03-05 17:10:29 +00:00
export let worldId: string;
export let pageId: string;
async function loadDoc(
worldId: string,
pageId: string
): Promise<{ worldY: WorldY; doc: XmlFragment }> {
const worlds = await loadWorlds();
const worldIdentifier = worlds.find((w) => w.room === worldId);
if (!worldIdentifier) {
throw new Error("No conozco ese mundo.");
}
const worldY = getWorldY(worldIdentifier);
2023-03-25 23:28:56 +00:00
2023-03-05 17:10:29 +00:00
return { worldY, doc: getWorldPage(worldY.ydoc, pageId) };
}
2023-03-25 23:28:56 +00:00
let state: "loading" | { worldY: WorldY; doc: XmlFragment } | { error: any };
$: {
state = "loading";
loadDoc(worldId, pageId)
.then((doc) => {
state = doc;
})
.catch((error) => (state = { error }));
}
async function saveLastPage() {
await lastPageStore.set({ worldId, pageId });
}
saveLastPage();
2023-03-05 17:10:29 +00:00
</script>
<nav>
<details>
<summary>Opciones</summary>
<ul>
<li><a href={routes.ChooseWorld}>🠔 Elegir otro mundo</a></li>
<li>
<a href={inject(routes.ShareWorld, { worldId })}
>📱 Agregar mundo a otro dispositivo</a
>
</li>
</ul>
</details>
</nav>
2023-03-25 23:28:56 +00:00
{#if state === "loading"}Cargando...{:else if "doc" in state}
<Editor doc={state.doc} worldY={state.worldY} />
{:else if "error" in state}
{state.error}
2023-03-05 17:10:29 +00:00
<a href={routes.ChooseWorld}>Volver al inicio</a>
2023-03-25 23:28:56 +00:00
{/if}
2023-03-05 17:10:29 +00:00
<style>
nav a {
2023-03-05 17:10:29 +00:00
color: inherit;
}
</style>