Fix issue #10: Add map support
This commit is contained in:
184
frontend/app/components/windows/CreateMapWindow.vue
Normal file
184
frontend/app/components/windows/CreateMapWindow.vue
Normal file
@@ -0,0 +1,184 @@
|
||||
<script setup>
|
||||
import { onBeforeUnmount, onMounted, ref } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { SetupHandle, SetSize, ResetPosition, ClearWindow, Top } from '@/services/Windows';
|
||||
import WindowHandle from './partials/WindowHandle.vue';
|
||||
import Server, { getBaseUrl } from '~/services/Server';
|
||||
import { emitter } from '~/services/Emitter';
|
||||
|
||||
const { t } = useI18n();
|
||||
const props = defineProps(['data']);
|
||||
const data = props.data;
|
||||
const id = data.id;
|
||||
const handle = ref(null);
|
||||
const wrapper = ref(null);
|
||||
const saving = ref(false);
|
||||
const error = ref('');
|
||||
const imageInput = ref(null);
|
||||
// Passing an entry edits an existing map; otherwise this window creates one.
|
||||
const editing = data.entry || null;
|
||||
|
||||
function initialForm() {
|
||||
return {
|
||||
name: editing?.name ?? '',
|
||||
gridSize: Number(editing?.gridSize ?? 50),
|
||||
offsetX: Number(editing?.offsetX ?? 0),
|
||||
offsetY: Number(editing?.offsetY ?? 0),
|
||||
scale: Number(editing?.scale ?? 5)
|
||||
};
|
||||
}
|
||||
|
||||
const form = ref(initialForm());
|
||||
const selectedImage = ref(null);
|
||||
let previewObjectUrl = null;
|
||||
|
||||
function serverImageUrl(image) {
|
||||
if (!image) return '';
|
||||
return /^(https?:|data:|\/\/)/i.test(image) ? image : `${getBaseUrl()}/public/${image}`;
|
||||
}
|
||||
|
||||
const previewUrl = ref(serverImageUrl(editing?.image));
|
||||
|
||||
onMounted(() => {
|
||||
Top(wrapper);
|
||||
SetupHandle(id, handle);
|
||||
SetSize(id, { width: 500, height: 600 });
|
||||
ResetPosition(id, 'center');
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
ClearWindow({ type: 'create_map' });
|
||||
if (previewObjectUrl) URL.revokeObjectURL(previewObjectUrl);
|
||||
});
|
||||
|
||||
function pickImage() {
|
||||
imageInput.value?.click();
|
||||
}
|
||||
|
||||
function onImageSelected(event) {
|
||||
const file = event.target.files?.[0];
|
||||
event.target.value = '';
|
||||
if (!file) return;
|
||||
if (previewObjectUrl) URL.revokeObjectURL(previewObjectUrl);
|
||||
previewObjectUrl = URL.createObjectURL(file);
|
||||
selectedImage.value = file;
|
||||
previewUrl.value = previewObjectUrl;
|
||||
}
|
||||
|
||||
const numberFields = ['gridSize', 'offsetX', 'offsetY', 'scale'];
|
||||
|
||||
async function saveMap() {
|
||||
if (!form.value.name.trim() || saving.value) return;
|
||||
saving.value = true;
|
||||
error.value = '';
|
||||
try {
|
||||
const body = new FormData();
|
||||
body.append('name', form.value.name.trim());
|
||||
if (editing) body.append('id', editing._id);
|
||||
else body.append('campaign', data.campaign);
|
||||
for (const field of numberFields) {
|
||||
if (Number.isFinite(form.value[field])) body.append(field, String(form.value[field]));
|
||||
}
|
||||
if (selectedImage.value) body.append('image', selectedImage.value);
|
||||
|
||||
const response = await Server().post(editing ? '/map/update' : '/map/create', body, {
|
||||
headers: { "Content-Type": "multipart/form-data" }
|
||||
});
|
||||
if (response.data.status !== 'ok') {
|
||||
error.value = t('campaigns.map.save-error');
|
||||
return;
|
||||
}
|
||||
emitter.emit(editing ? 'campaign-entry-updated' : 'campaign-entry-created', { entityType: 'map', entry: response.data.map });
|
||||
ClearWindow({ id });
|
||||
} catch (requestError) {
|
||||
error.value = t('campaigns.map.save-error');
|
||||
} finally {
|
||||
saving.value = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="window-wrapper" :id="'window-wrapper-' + id" ref="wrapper">
|
||||
<WindowHandle :window="id" ref="handle" />
|
||||
<div class="body">
|
||||
<h3>{{ $t(editing ? 'campaigns.map.edit' : 'campaigns.map.new') }}</h3>
|
||||
<div v-if="error" class="form-error">{{ error }}</div>
|
||||
<form @submit.prevent="saveMap">
|
||||
<label class="form-field">
|
||||
<span>{{ $t('campaigns.fields.name') }}</span>
|
||||
<input v-model="form.name" required autofocus autocomplete="off">
|
||||
</label>
|
||||
|
||||
<div class="form-field">
|
||||
<span>{{ $t('campaigns.map.photo') }}</span>
|
||||
<div class="image-picker" :class="{ empty: !previewUrl }">
|
||||
<img v-if="previewUrl" :src="previewUrl" :alt="$t('campaigns.map.photo')">
|
||||
<button class="pick-image sound-click" type="button" :disabled="saving" @click="pickImage">
|
||||
{{ $t(previewUrl ? 'campaigns.map.change-photo' : 'campaigns.map.upload-photo') }}
|
||||
</button>
|
||||
</div>
|
||||
<small class="field-help">{{ $t('campaigns.map.photo-hint') }}</small>
|
||||
<input ref="imageInput" class="image-input" type="file" accept="image/*" @change="onImageSelected">
|
||||
</div>
|
||||
|
||||
<div class="form-grid">
|
||||
<label class="form-field">
|
||||
<span>{{ $t('campaigns.map.grid-size') }}</span>
|
||||
<input v-model.number="form.gridSize" type="number" min="1" step="1">
|
||||
</label>
|
||||
<label class="form-field">
|
||||
<span>{{ $t('campaigns.map.scale') }}</span>
|
||||
<input v-model.number="form.scale" type="number" min="0.01" step="0.5">
|
||||
</label>
|
||||
<label class="form-field">
|
||||
<span>{{ $t('campaigns.map.offset-x') }}</span>
|
||||
<input v-model.number="form.offsetX" type="number" step="1">
|
||||
</label>
|
||||
<label class="form-field">
|
||||
<span>{{ $t('campaigns.map.offset-y') }}</span>
|
||||
<input v-model.number="form.offsetY" type="number" step="1">
|
||||
</label>
|
||||
</div>
|
||||
<small class="field-help">{{ $t('campaigns.map.scale-help') }}</small>
|
||||
|
||||
<div class="form-actions">
|
||||
<button class="btn-primary sound-click" type="submit" :disabled="saving">
|
||||
{{ saving ? $t('campaigns.actions.saving') : $t('campaigns.actions.save') }}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.window-wrapper { display: flex; align-items: center; flex-direction: column; }
|
||||
.body { width: 100%; padding: 10px 18px 16px; overflow-y: auto; }
|
||||
h3 { margin: 0 0 12px; font-family: MrEavesRemake; }
|
||||
form { display: flex; flex-direction: column; gap: 10px; }
|
||||
.form-field { min-width: 0; display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
|
||||
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
|
||||
input {
|
||||
box-sizing: border-box; width: 100%; padding: 7px 8px; border: 1px solid var(--color-border);
|
||||
border-radius: 6px; background: var(--color-background-soft); color: var(--color-text); font: inherit;
|
||||
}
|
||||
input:focus { outline: none; border-color: var(--color-link); }
|
||||
.image-picker {
|
||||
min-height: 96px; display: flex; align-items: center; justify-content: center; overflow: hidden;
|
||||
border: 1px solid var(--color-border); border-radius: 8px; background: var(--color-background-soft);
|
||||
}
|
||||
.image-picker.empty { border-style: dashed; }
|
||||
.image-picker img { max-width: 100%; max-height: 180px; object-fit: contain; }
|
||||
.pick-image {
|
||||
border: 1px solid var(--color-border); border-radius: 6px; padding: 5px 10px; margin: 6px;
|
||||
background: var(--color-background-light); color: var(--color-text); font-size: 12px; cursor: pointer;
|
||||
}
|
||||
.pick-image:hover { background: var(--color-button-hover); }
|
||||
.pick-image:disabled { opacity: .55; cursor: wait; }
|
||||
.image-input { display: none; }
|
||||
.field-help { font-size: 11px; opacity: .72; }
|
||||
.form-error { padding: 7px 10px; border-radius: 6px; color: var(--color-red); background: var(--color-background-soft); font-size: 12px; }
|
||||
.form-actions { display: flex; justify-content: center; margin-top: 2px; }
|
||||
.form-actions button { width: 100%; max-width: 220px; }
|
||||
</style>
|
||||
224
frontend/app/components/windows/MapViewerWindow.vue
Normal file
224
frontend/app/components/windows/MapViewerWindow.vue
Normal file
@@ -0,0 +1,224 @@
|
||||
<script setup>
|
||||
import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
|
||||
import { SetupHandle, SetSize, ResetPosition, ClearWindow, Top, CreateWindow } from '@/services/Windows';
|
||||
import WindowHandle from './partials/WindowHandle.vue';
|
||||
import { getBaseUrl } from '~/services/Server';
|
||||
import { emitter } from '~/services/Emitter';
|
||||
|
||||
const props = defineProps(['data']);
|
||||
const id = props.data.id;
|
||||
const campaignId = props.data.campaign;
|
||||
const handle = ref(null);
|
||||
const wrapper = ref(null);
|
||||
|
||||
const map = ref(props.data.map ? { ...props.data.map } : null);
|
||||
const viewport = ref(null);
|
||||
|
||||
const zoom = ref(1);
|
||||
const pan = ref({ x: 0, y: 0 });
|
||||
const imageSize = ref({ width: 0, height: 0 });
|
||||
const showGrid = ref(true);
|
||||
const dragging = ref(false);
|
||||
|
||||
const minZoom = 0.05;
|
||||
const maxZoom = 8;
|
||||
|
||||
const imageUrl = computed(() => {
|
||||
const image = map.value?.image;
|
||||
if (!image) return '';
|
||||
return /^(https?:|data:|\/\/)/i.test(image) ? image : `${getBaseUrl()}/public/${image}`;
|
||||
});
|
||||
|
||||
const stageStyle = computed(() => ({
|
||||
transform: `translate3d(${pan.value.x}px, ${pan.value.y}px, 0) scale(${zoom.value})`
|
||||
}));
|
||||
|
||||
// The grid is drawn in screen space over the image so its lines stay one pixel wide at any zoom.
|
||||
const gridStyle = computed(() => {
|
||||
if (!showGrid.value || !imageSize.value.width) return { display: 'none' };
|
||||
const cell = `${Math.max(1, Number(map.value?.gridSize) || 50) * zoom.value}px`;
|
||||
return {
|
||||
width: `${imageSize.value.width * zoom.value}px`,
|
||||
height: `${imageSize.value.height * zoom.value}px`,
|
||||
transform: `translate3d(${pan.value.x + (Number(map.value?.offsetX) || 0) * zoom.value}px, ${pan.value.y + (Number(map.value?.offsetY) || 0) * zoom.value}px, 0)`,
|
||||
backgroundImage: `linear-gradient(to right, var(--color-link) 1px, transparent 1px), linear-gradient(to bottom, var(--color-link) 1px, transparent 1px)`,
|
||||
backgroundSize: `${cell} ${cell}`
|
||||
};
|
||||
});
|
||||
|
||||
const zoomPercent = computed(() => `${Math.round(zoom.value * 100)}%`);
|
||||
|
||||
function clampZoom(value) {
|
||||
if (!Number.isFinite(value)) return minZoom;
|
||||
return Math.min(maxZoom, Math.max(minZoom, value));
|
||||
}
|
||||
|
||||
function fitToWindow() {
|
||||
const bounds = viewport.value?.getBoundingClientRect();
|
||||
if (!bounds || !imageSize.value.width) return;
|
||||
zoom.value = clampZoom(Math.min(bounds.width / imageSize.value.width, bounds.height / imageSize.value.height));
|
||||
centerOn();
|
||||
}
|
||||
|
||||
function centerOn() {
|
||||
const bounds = viewport.value?.getBoundingClientRect();
|
||||
if (!bounds || !imageSize.value.width) return;
|
||||
pan.value = {
|
||||
x: (bounds.width - imageSize.value.width * zoom.value) / 2,
|
||||
y: (bounds.height - imageSize.value.height * zoom.value) / 2
|
||||
};
|
||||
}
|
||||
|
||||
function onImageLoaded(event) {
|
||||
imageSize.value = { width: event.target.naturalWidth, height: event.target.naturalHeight };
|
||||
fitToWindow();
|
||||
}
|
||||
|
||||
// Zoom keeping the map point under `point` where it is.
|
||||
function zoomAt(factor, point) {
|
||||
const next = clampZoom(zoom.value * factor);
|
||||
if (next === zoom.value) return;
|
||||
const ratio = next / zoom.value;
|
||||
pan.value = {
|
||||
x: point.x - (point.x - pan.value.x) * ratio,
|
||||
y: point.y - (point.y - pan.value.y) * ratio
|
||||
};
|
||||
zoom.value = next;
|
||||
}
|
||||
|
||||
function onWheel(event) {
|
||||
const bounds = viewport.value?.getBoundingClientRect();
|
||||
if (!bounds) return;
|
||||
zoomAt(event.deltaY < 0 ? 1.15 : 1 / 1.15, { x: event.clientX - bounds.left, y: event.clientY - bounds.top });
|
||||
}
|
||||
|
||||
function zoomFromToolbar(incoming) {
|
||||
const bounds = viewport.value?.getBoundingClientRect();
|
||||
if (!bounds) return;
|
||||
zoomAt(incoming ? 1.25 : 1 / 1.25, { x: bounds.width / 2, y: bounds.height / 2 });
|
||||
}
|
||||
|
||||
let pointerId = null;
|
||||
let lastPointer = { x: 0, y: 0 };
|
||||
|
||||
function onPointerDown(event) {
|
||||
if (event.button !== 0 || !viewport.value) return;
|
||||
dragging.value = true;
|
||||
pointerId = event.pointerId;
|
||||
lastPointer = { x: event.clientX, y: event.clientY };
|
||||
viewport.value.setPointerCapture?.(event.pointerId);
|
||||
}
|
||||
|
||||
function onPointerMove(event) {
|
||||
if (!dragging.value || event.pointerId !== pointerId) return;
|
||||
pan.value = { x: pan.value.x + (event.clientX - lastPointer.x), y: pan.value.y + (event.clientY - lastPointer.y) };
|
||||
lastPointer = { x: event.clientX, y: event.clientY };
|
||||
}
|
||||
|
||||
function onPointerUp(event) {
|
||||
if (event.pointerId !== pointerId) return;
|
||||
dragging.value = false;
|
||||
pointerId = null;
|
||||
viewport.value?.releasePointerCapture?.(event.pointerId);
|
||||
}
|
||||
|
||||
function editMap() {
|
||||
if (!map.value) return;
|
||||
CreateWindow('create_map', { campaign: campaignId, entry: map.value });
|
||||
}
|
||||
|
||||
function handleMapUpdated({ entityType, entry }) {
|
||||
if (entityType !== 'map' || entry?._id !== map.value?._id) return;
|
||||
const imageChanged = entry.image !== map.value.image;
|
||||
map.value = { ...entry };
|
||||
// A new image is fitted again by onImageLoaded; grid-only changes just move the overlay.
|
||||
if (imageChanged) imageSize.value = { width: 0, height: 0 };
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
Top(wrapper);
|
||||
SetupHandle(id, handle);
|
||||
SetSize(id, {
|
||||
width: Math.max(480, Math.min(1100, window.innerWidth - 24)),
|
||||
height: Math.max(360, Math.min(760, window.innerHeight - 32))
|
||||
});
|
||||
ResetPosition(id, 'center');
|
||||
emitter.on('campaign-entry-updated', handleMapUpdated);
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
emitter.off('campaign-entry-updated', handleMapUpdated);
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="window-wrapper viewer-window" :id="'window-wrapper-' + id" ref="wrapper">
|
||||
<WindowHandle :window="id" ref="handle" />
|
||||
<div class="viewer-bar">
|
||||
<div class="viewer-identity">
|
||||
<strong>{{ map?.name }}</strong>
|
||||
<span class="viewer-meta">{{ $t('campaigns.map.summary', { size: map?.gridSize, scale: map?.scale }) }}</span>
|
||||
</div>
|
||||
<div class="viewer-tools">
|
||||
<button class="viewer-tool" :class="{ selected: showGrid }" type="button" :title="$t('campaigns.map.toggle-grid')"
|
||||
:aria-label="$t('campaigns.map.toggle-grid')" :aria-pressed="showGrid.toString()" @click="showGrid = !showGrid">
|
||||
<img class="tool-icon" src="/icons/iconoir/regular/view-grid.svg" alt="" aria-hidden="true">
|
||||
</button>
|
||||
<button class="viewer-tool" type="button" :title="$t('campaigns.map.zoom-out')" :aria-label="$t('campaigns.map.zoom-out')" @click="zoomFromToolbar(false)">
|
||||
<img class="tool-icon" src="/icons/iconoir/regular/minus.svg" alt="" aria-hidden="true">
|
||||
</button>
|
||||
<span class="zoom-readout">{{ zoomPercent }}</span>
|
||||
<button class="viewer-tool" type="button" :title="$t('campaigns.map.zoom-in')" :aria-label="$t('campaigns.map.zoom-in')" @click="zoomFromToolbar(true)">
|
||||
<img class="tool-icon" src="/icons/iconoir/regular/plus.svg" alt="" aria-hidden="true">
|
||||
</button>
|
||||
<button class="viewer-tool" type="button" :title="$t('campaigns.map.fit')" :aria-label="$t('campaigns.map.fit')" @click="fitToWindow">
|
||||
<img class="tool-icon" src="/icons/iconoir/regular/expand.svg" alt="" aria-hidden="true">
|
||||
</button>
|
||||
<button class="viewer-tool" type="button" :title="$t('campaigns.map.edit')" :aria-label="$t('campaigns.map.edit')" @click="editMap">
|
||||
<img class="tool-icon" src="/icons/iconoir/regular/edit.svg" alt="" aria-hidden="true">
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
ref="viewport"
|
||||
class="map-viewport"
|
||||
:class="{ dragging }"
|
||||
@wheel.prevent="onWheel"
|
||||
@pointerdown="onPointerDown"
|
||||
@pointermove="onPointerMove"
|
||||
@pointerup="onPointerUp"
|
||||
@pointercancel="onPointerUp"
|
||||
>
|
||||
<template v-if="imageUrl">
|
||||
<img class="map-image" :src="imageUrl" :style="stageStyle" :alt="map?.name" draggable="false" @load="onImageLoaded">
|
||||
<div class="map-grid" :style="gridStyle" aria-hidden="true"></div>
|
||||
</template>
|
||||
<div v-else class="map-empty">
|
||||
{{ $t('campaigns.map.no-image') }}
|
||||
<button class="map-edit" type="button" @click="editMap">{{ $t('campaigns.map.upload-photo') }}</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.viewer-window { min-width: 320px; min-height: 0; }
|
||||
.viewer-bar { width: 100%; box-sizing: border-box; display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 10px; border-bottom: 1px solid var(--color-border); background: var(--color-background-light); }
|
||||
.viewer-identity { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
|
||||
.viewer-identity strong { font-family: MrEavesRemake; overflow-wrap: anywhere; }
|
||||
.viewer-meta { font-size: 11px; opacity: .72; }
|
||||
.viewer-tools { display: flex; align-items: center; gap: 4px; }
|
||||
.viewer-tool { width: 30px; height: 30px; margin: 0; padding: 0; border: 1px solid var(--color-border); border-radius: 7px; background: var(--color-background-soft); display: flex; align-items: center; justify-content: center; cursor: pointer; }
|
||||
.viewer-tool:hover { background: var(--color-button-hover); }
|
||||
.viewer-tool.selected { border-color: var(--color-link); background: var(--color-button-hover); }
|
||||
.tool-icon { width: 16px; height: 16px; filter: invert(var(--color-icon-invert)); }
|
||||
.zoom-readout { min-width: 46px; text-align: center; font-size: 12px; }
|
||||
.map-viewport { position: relative; width: 100%; flex: 1; min-height: 0; overflow: hidden; background-color: var(--color-background); cursor: grab; touch-action: none; }
|
||||
.map-viewport.dragging { cursor: grabbing; }
|
||||
.map-image { position: absolute; top: 0; left: 0; max-width: none; transform-origin: top left; user-select: none; }
|
||||
.map-grid { position: absolute; top: 0; left: 0; opacity: .55; pointer-events: none; }
|
||||
.map-empty { width: 100%; height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; font-size: 13px; opacity: .8; }
|
||||
.map-edit { border: 1px solid var(--color-border); border-radius: 7px; padding: 6px 11px; background: var(--color-background-soft); color: var(--color-text); font-size: 12px; cursor: pointer; }
|
||||
.map-edit:hover { background: var(--color-button-hover); }
|
||||
</style>
|
||||
Reference in New Issue
Block a user