Fix issue #15: Dragonroll Frontend: Map Viewer adjustmentes
This commit is contained in:
233
frontend/app/components/viewer/map/MapViewerPane.vue
Normal file
233
frontend/app/components/viewer/map/MapViewerPane.vue
Normal file
@@ -0,0 +1,233 @@
|
||||
<script setup>
|
||||
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue';
|
||||
import { CreateWindow } from '@/services/Windows';
|
||||
import { getBaseUrl } from '~/services/Server';
|
||||
import { emitter } from '~/services/Emitter';
|
||||
import { useCampaignService } from '~/services/Campaign.js';
|
||||
|
||||
// The map viewer without window chrome: it fills whatever flex column holds it, so the same
|
||||
// component works inside MapViewerWindow.vue and anchored on the campaign content area.
|
||||
const props = defineProps({
|
||||
map: { type: Object, default: null },
|
||||
// Anchored maps can be detached into a floating window; the window itself does not offer it.
|
||||
anchored: { type: Boolean, default: false }
|
||||
});
|
||||
const emit = defineEmits(['detach']);
|
||||
|
||||
const { Campaign } = useCampaignService();
|
||||
const campaignId = computed(() => Campaign.value?._id ?? Campaign.value?.id ?? null);
|
||||
|
||||
const map = ref(props.map ? { ...props.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 || !campaignId.value) return;
|
||||
CreateWindow('create_map', { campaign: campaignId.value, 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 };
|
||||
}
|
||||
|
||||
// A different map was anchored while this pane was already mounted.
|
||||
watch(() => props.map, (incoming, previous) => {
|
||||
if (incoming === previous) return;
|
||||
map.value = incoming ? { ...incoming } : null;
|
||||
imageSize.value = { width: 0, height: 0 };
|
||||
});
|
||||
|
||||
onMounted(() => {
|
||||
emitter.on('campaign-entry-updated', handleMapUpdated);
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
emitter.off('campaign-entry-updated', handleMapUpdated);
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="map-pane">
|
||||
<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>
|
||||
<button v-if="anchored" class="viewer-tool" type="button" :title="$t('campaigns.map.open-window')" :aria-label="$t('campaigns.map.open-window')"
|
||||
@click="emit('detach')">
|
||||
<img class="tool-icon" src="/icons/iconoir/regular/open-in-window.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>
|
||||
.map-pane { width: 100%; flex: 1; min-width: 0; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
|
||||
.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