summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/menus/SaveCharacterModalMenu.svelte
blob: 4fc82108f9d88cc2e4b1edb1426610e7ffd99c2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<!--
	The character-save modal (the reference `SaveCharacterModalMenu`,
	Phase 6). Button visibility follows the reference:
	- **Load** — from an existing entry: `loadCharacterSave(saveId)`.
	- **Save** — only from the "Add new Save" path (a live character was
	  passed in): when the name changed, the old slot is deleted first
	  (`deleteCharacterSave`) before `saveCharacter` stores the character
	  (the reference does `deleteCharacterSave + saveCharacterState`).
	- **Delete** — from an existing entry: `deleteCharacterSave(saveId)`.
-->
<script lang="ts">
	import type { GameSession } from '$lib/state/session';
	import { t } from '$lib/i18n';

	let {
		session,
		name,
		saveId,
		characterId = null,
		open = $bindable(true)
	}: {
		session: GameSession;
		/** The original (list) name of the save. */
		name: string;
		/** The full save id `"$name\n$characterId"`. */
		saveId: string;
		/** The live character to save (the "Add new Save" path); null from a list entry. */
		characterId?: string | null;
		open?: boolean;
	} = $props();

	// The rename field is seeded from the original name once on mount.
	// svelte-ignore state_referenced_locally
	let newName = $state(name);

	function finish() {
		open = false;
	}

	function doLoad() {
		session.loadCharacterSave(saveId);
		finish();
	}

	function doSave() {
		const trimmed = newName.trim();
		if (!trimmed || characterId == null) return;
		if (trimmed !== name) session.deleteCharacterSave(saveId);
		session.saveCharacter(trimmed, characterId);
		finish();
	}

	function doDelete() {
		session.deleteCharacterSave(saveId);
		finish();
	}
</script>

<svelte:window
	onkeydown={(e) => {
		// Only acts while the modal is open.
		if (open && e.key === 'Escape') finish();
	}}
/>
{#if open}
	<div class="modal" role="dialog" aria-modal="true" aria-label={t('setSaveName')}>
		<div class="modal__scrim" aria-hidden="true" onclick={finish}></div>
		<div class="modal__card">
			<div class="modal__buttons">
				{#if characterId == null}
					<button onclick={doLoad}>{t('loadButton')}</button>
				{/if}
				{#if characterId != null}
					<button class="modal__save" disabled={!newName.trim()} onclick={doSave}>
						{t('saveButton')}
					</button>
				{/if}
				{#if characterId == null}
					<button class="modal__delete" onclick={doDelete}>{t('deleteButton')}</button>
				{/if}
			</div>
			<div class="modal__name">
				<label for="charsavename">{t('setSaveName')}</label>
				<input id="charsavename" type="text" bind:value={newName} />
			</div>
		</div>
	</div>
{/if}

<style>
	.modal {
		position: fixed;
		inset: 0;
		z-index: 200;
		display: grid;
		place-items: center;
	}
	.modal__scrim {
		position: absolute;
		inset: 0;
		background: #000000aa;
	}
	.modal__card {
		position: relative;
		width: min(320px, calc(100vw - 24px));
		padding: 14px;
		background: var(--dialog-bg);
		color: var(--fg);
		border-radius: 8px;
		box-shadow: 0 4px 24px #00000080;
	}
	.modal__buttons {
		display: flex;
		justify-content: center;
		gap: 10px;
	}
	.modal__buttons button {
		flex: 1;
		padding: 8px 4px;
		font-size: 14px;
		border: 2px solid #4a7dff;
		border-radius: 4px;
		background: none;
		color: inherit;
		cursor: pointer;
	}
	.modal__buttons button:hover:not(:disabled) {
		background: rgba(74, 125, 255, 0.2);
	}
	.modal__buttons button:disabled {
		opacity: 0.4;
		cursor: default;
	}
	.modal__name {
		display: flex;
		flex-direction: column;
		align-items: center;
		gap: 6px;
		margin-top: 20px;
	}
	.modal__name input {
		width: 200px;
		padding: 6px 8px;
		font-size: 14px;
		border: 1px solid currentColor;
		border-radius: 4px;
		background: transparent;
		color: inherit;
	}
</style>