153 lines
4.0 KiB
Svelte
153 lines
4.0 KiB
Svelte
<script lang="ts">
|
|
import { en } from '../copy/en.js';
|
|
import type { RoseWiring } from './types.js';
|
|
|
|
/**
|
|
* Keeping her (keep-her-safe.md): what the browser has promised, asking it to keep
|
|
* her, saving her to a file, and bringing her back from one. Decides nothing; the
|
|
* file's shape is checked by storage.
|
|
*/
|
|
let { wiring, currentKey, today, onrestored }: { wiring: RoseWiring; currentKey: string; today: string; onrestored: () => void } = $props();
|
|
|
|
const uid = $props.id();
|
|
let persisted = $state<boolean | null | undefined>(undefined);
|
|
let refused = $state(false);
|
|
let said = $state('');
|
|
|
|
$effect(() => {
|
|
wiring.keeping.persisted().then(
|
|
(p) => (persisted = p),
|
|
(error: unknown) => {
|
|
console.error('keep: the browser could not say whether it keeps the garden', error);
|
|
persisted = null;
|
|
}
|
|
);
|
|
});
|
|
|
|
async function ask(): Promise<void> {
|
|
try {
|
|
const granted = await wiring.keeping.persist();
|
|
persisted = granted;
|
|
refused = granted !== true;
|
|
} catch (error) {
|
|
console.error('keep: asking the browser to keep the garden failed', error);
|
|
refused = true;
|
|
}
|
|
}
|
|
|
|
async function save(): Promise<void> {
|
|
try {
|
|
const file = await wiring.exportRose(currentKey);
|
|
wiring.saveFile(`rose-${today}.json`, JSON.stringify(file, null, 1));
|
|
} catch (error) {
|
|
console.error('keep: the rose could not be saved to a file', error);
|
|
}
|
|
}
|
|
|
|
async function bringBack(event: Event & { currentTarget: HTMLInputElement }): Promise<void> {
|
|
const input = event.currentTarget;
|
|
const file = input.files?.[0];
|
|
if (!file) return;
|
|
try {
|
|
const { added, petals } = await wiring.importRose(JSON.parse(await file.text()), currentKey);
|
|
said = added === 0 && petals === 0 ? en.rose.safe.alreadyHere : en.rose.safe.broughtBack(added);
|
|
onrestored();
|
|
} catch (error) {
|
|
// A wrong file is an ordinary thing to choose; the player is told calmly, the console fully.
|
|
console.warn('keep: the chosen file is not a rose', error);
|
|
said = en.rose.safe.notARose;
|
|
} finally {
|
|
input.value = '';
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<section class="safe" aria-labelledby="{uid}-title">
|
|
<h3 id="{uid}-title">{en.rose.safe.title}</h3>
|
|
{#if persisted !== undefined}
|
|
<p>{refused ? en.rose.safe.refused : persisted === true ? en.rose.safe.kept : persisted === false ? en.rose.safe.mayClear : en.rose.safe.unknown}</p>
|
|
{/if}
|
|
{#if persisted === false && !refused}
|
|
<button type="button" class="quiet" onclick={ask}>{en.rose.safe.ask}</button>
|
|
{/if}
|
|
{#if wiring.installHint}
|
|
<p>{en.rose.safe.install}</p>
|
|
{/if}
|
|
|
|
<button type="button" onclick={save}>{en.rose.safe.save}</button>
|
|
<p class="note">{en.rose.safe.fileNote}</p>
|
|
|
|
<label class="file">
|
|
<span>{en.rose.safe.bringBack}</span>
|
|
<input type="file" accept="application/json,.json" onchange={bringBack} />
|
|
</label>
|
|
<p class="said" role="status">{said}</p>
|
|
</section>
|
|
|
|
<style>
|
|
.safe {
|
|
display: grid;
|
|
gap: var(--space-2);
|
|
justify-items: center;
|
|
max-width: 24rem;
|
|
text-align: center;
|
|
}
|
|
|
|
h3 {
|
|
margin: 0;
|
|
font-family: var(--font-display);
|
|
font-weight: 400;
|
|
font-size: 1.1rem;
|
|
}
|
|
|
|
p {
|
|
margin: 0;
|
|
}
|
|
|
|
.note,
|
|
.said {
|
|
color: var(--ink-soft);
|
|
font-size: 0.9rem;
|
|
}
|
|
|
|
.said:empty {
|
|
display: none;
|
|
}
|
|
|
|
button,
|
|
.file {
|
|
font: inherit;
|
|
padding: var(--space-2) var(--space-3);
|
|
border-radius: 999px;
|
|
border: 1px solid var(--rose);
|
|
background: var(--paper);
|
|
color: var(--ink);
|
|
cursor: pointer;
|
|
}
|
|
|
|
.quiet {
|
|
border: none;
|
|
padding: var(--space-1) 0;
|
|
color: var(--rose);
|
|
text-decoration: underline;
|
|
}
|
|
|
|
.file {
|
|
position: relative;
|
|
}
|
|
|
|
/* The real input stays operable and labelled; only its default look is hidden. */
|
|
.file input {
|
|
position: absolute;
|
|
inset: 0;
|
|
opacity: 0;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.file:focus-within,
|
|
button:focus-visible {
|
|
outline: 2px solid var(--rose);
|
|
outline-offset: 2px;
|
|
}
|
|
</style>
|