summaryrefslogtreecommitdiff
path: root/web/src/lib/ui/menus/SaveModalMenu.svelte
blob: bdd1a58617d75023fd7f5310320095aee15be181 (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
<!--
	The save-state modal (the reference `SaveModalMenu`, Phase 6): Load /
	Save / Delete for one named save plus a rename field.

	Reference button behaviour, ported:
	- **Load** — `loadSave` (the server runs `LoadSaveCommand`, an undoable
	  action).
	- **Save** — when the name was changed, the old slot is deleted first
	  (`deleteSave`) before `save` stores the state under the new name (the
	  reference does `deleteSave(old) + saveState(new)`; same end state, one
	  fewer round-trip when the name is unchanged).
	- **Delete** — `deleteSave`.
-->
<script lang="ts">
	import type { GameSession } from '$lib/state/session';
	import { t } from '$lib/i18n';

	let {
		session,
		name,
		saveOnly = false,
		open = $bindable(true)
	}: {
		session: GameSession;
		/** The original (list) name of the save. */
		name: string;
		/** True from "Add new Save": only Save (no Load/Delete yet). */
		saveOnly?: boolean;
		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.loadSave(name);
		finish();
	}

	function doSave() {
		const trimmed = newName.trim();
		if (!trimmed) return;
		if (trimmed !== name) session.deleteSave(name);
		session.save(trimmed);
		finish();
	}

	function doDelete() {
		session.deleteSave(name);
		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 !saveOnly}
					<button onclick={doLoad}>{t('loadButton')}</button>
				{/if}
				<button class="modal__save" disabled={!newName.trim()} onclick={doSave}>
					{t('saveButton')}
				</button>
				{#if !saveOnly}
					<button class="modal__delete" onclick={doDelete}>{t('deleteButton')}</button>
				{/if}
			</div>
			<div class="modal__name">
				<label for="savename">{t('setSaveName')}</label>
				<input id="savename" 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>