190 lines
6.4 KiB
Vue
190 lines
6.4 KiB
Vue
<script setup lang="ts">
|
|
import type { ComponentPublicInstance } from 'vue'
|
|
|
|
export interface DockItem {
|
|
key: string
|
|
title: string
|
|
alarm?: boolean
|
|
/** false hides the box for now (no incoming attacks); it keeps its place. */
|
|
show?: boolean
|
|
}
|
|
|
|
const props = defineProps<{ scope: string; cards: DockItem[] }>()
|
|
const { layout, ordered, toggle, dock, float } = useCardLayout(props.scope)
|
|
|
|
// Dragging only on wide screens; phones keep the boxes in their sheet, still foldable.
|
|
const wide = ref(false)
|
|
const viewport = ref({ w: 1600, h: 900 })
|
|
function measure() {
|
|
viewport.value = { w: window.innerWidth, h: window.innerHeight }
|
|
}
|
|
let mq: MediaQueryList | undefined
|
|
const onMq = (e: MediaQueryListEvent) => (wide.value = e.matches)
|
|
onMounted(() => {
|
|
mq = window.matchMedia('(min-width: 1024px)')
|
|
wide.value = mq.matches
|
|
mq.addEventListener('change', onMq)
|
|
measure()
|
|
window.addEventListener('resize', measure)
|
|
})
|
|
onBeforeUnmount(() => {
|
|
mq?.removeEventListener('change', onMq)
|
|
window.removeEventListener('resize', measure)
|
|
window.removeEventListener('pointermove', onMove)
|
|
})
|
|
|
|
const defaults = computed(() => props.cards.map((c) => c.key))
|
|
const byKey = computed(() => new Map(props.cards.map((c) => [c.key, c])))
|
|
const shown = (k: string) => byKey.value.get(k)?.show !== false
|
|
const floats = (k: string) => wide.value && !!layout.value.floating[k]
|
|
|
|
interface Drag {
|
|
key: string
|
|
x: number
|
|
y: number
|
|
w: number
|
|
/** Over the dock: dropped before this box (null: at the end). */
|
|
over: boolean
|
|
before: string | null
|
|
}
|
|
const drag = ref<Drag | null>(null)
|
|
let pending: { key: string; startX: number; startY: number; dx: number; dy: number; w: number } | null = null
|
|
|
|
const docked = computed(() => ordered(defaults.value).filter((k) => shown(k) && !floats(k) && drag.value?.key !== k))
|
|
const floating = computed(() => ordered(defaults.value).filter((k) => shown(k) && floats(k) && drag.value?.key !== k))
|
|
|
|
const column = ref<HTMLElement | null>(null)
|
|
const els = new Map<string, HTMLElement>()
|
|
function setEl(key: string, c: Element | ComponentPublicInstance | null) {
|
|
const el = c && '$el' in c ? (c.$el as HTMLElement) : (c as HTMLElement | null)
|
|
if (el) els.set(key, el)
|
|
else els.delete(key)
|
|
}
|
|
|
|
/** Header pressed: a click folds the box, a drag moves it. */
|
|
function grab(key: string, e: PointerEvent) {
|
|
if (e.button !== 0) return
|
|
const card = (e.currentTarget as HTMLElement).closest('section')
|
|
if (!card) return
|
|
const r = card.getBoundingClientRect()
|
|
pending = { key, startX: e.clientX, startY: e.clientY, dx: e.clientX - r.left, dy: e.clientY - r.top, w: r.width }
|
|
window.addEventListener('pointermove', onMove)
|
|
window.addEventListener('pointerup', onUp, { once: true })
|
|
}
|
|
|
|
function onMove(e: PointerEvent) {
|
|
const p = pending
|
|
if (!p || !wide.value) return
|
|
if (!drag.value && Math.hypot(e.clientX - p.startX, e.clientY - p.startY) < 5) return
|
|
e.preventDefault()
|
|
const box = column.value?.getBoundingClientRect()
|
|
const over = !!box && e.clientX >= box.left - 24 && e.clientX <= box.right + 24 && e.clientY >= box.top - 24 && e.clientY <= box.bottom + 48
|
|
drag.value = { key: p.key, x: e.clientX - p.dx, y: e.clientY - p.dy, w: p.w, over, before: over ? beforeKey(e.clientY) : null }
|
|
}
|
|
|
|
/** The docked box the pointer is above the middle of. */
|
|
function beforeKey(y: number): string | null {
|
|
for (const k of docked.value) {
|
|
const r = els.get(k)?.getBoundingClientRect()
|
|
if (r && y < r.top + r.height / 2) return k
|
|
}
|
|
return null
|
|
}
|
|
|
|
const clamp = (x: number, y: number) => ({
|
|
x: Math.max(0, Math.min(viewport.value.w - 120, x)),
|
|
y: Math.max(0, Math.min(viewport.value.h - 40, y)),
|
|
})
|
|
|
|
function onUp() {
|
|
window.removeEventListener('pointermove', onMove)
|
|
const p = pending
|
|
const d = drag.value
|
|
pending = null
|
|
drag.value = null
|
|
if (!p) return
|
|
if (!d) return toggle(p.key)
|
|
if (d.over) dock(p.key, d.before, defaults.value)
|
|
else {
|
|
const at = clamp(d.x, d.y)
|
|
float(p.key, at.x, at.y)
|
|
}
|
|
}
|
|
|
|
const placed = (k: string) => {
|
|
const at = clamp(layout.value.floating[k]!.x, layout.value.floating[k]!.y)
|
|
return { left: `${at.x}px`, top: `${at.y}px` }
|
|
}
|
|
const title = (k: string) => byKey.value.get(k)?.title ?? k
|
|
const alarm = (k: string) => !!byKey.value.get(k)?.alarm
|
|
</script>
|
|
|
|
<!--
|
|
A column of boxes the player arranges: each folds by its header, drags to another place
|
|
in the column, or out to float anywhere over the stage and back. The layout is kept per
|
|
browser (useCardLayout); content comes in a slot named after each box's key.
|
|
-->
|
|
<template>
|
|
<div ref="column" class="space-y-2" :class="drag ? 'min-h-24' : ''">
|
|
<template v-for="k in docked" :key="k">
|
|
<div v-if="drag?.over && drag.before === k" class="dock-gap" />
|
|
<UiDockCard
|
|
:ref="(c) => setEl(k, c as ComponentPublicInstance | null)"
|
|
:title="title(k)"
|
|
:alarm="alarm(k)"
|
|
:collapsed="!!layout.collapsed[k]"
|
|
:draggable="wide"
|
|
@grab="grab(k, $event)"
|
|
@toggle="toggle(k)"
|
|
>
|
|
<template #extra><slot :name="`${k}-extra`" /></template>
|
|
<slot :name="k" />
|
|
</UiDockCard>
|
|
</template>
|
|
<div v-if="drag?.over && drag.before === null" class="dock-gap" />
|
|
</div>
|
|
|
|
<Teleport to="body">
|
|
<div v-if="floating.length || drag" class="pointer-events-none fixed inset-0 z-30 text-sm">
|
|
<UiDockCard
|
|
v-for="k in floating"
|
|
:key="k"
|
|
class="float pointer-events-auto absolute w-[16.5rem]"
|
|
:style="placed(k)"
|
|
:title="title(k)"
|
|
:alarm="alarm(k)"
|
|
:collapsed="!!layout.collapsed[k]"
|
|
floating
|
|
draggable
|
|
@grab="grab(k, $event)"
|
|
@toggle="toggle(k)"
|
|
@dock="dock(k, null, defaults)"
|
|
>
|
|
<template #extra><slot :name="`${k}-extra`" /></template>
|
|
<slot :name="k" />
|
|
</UiDockCard>
|
|
<UiDockCard
|
|
v-if="drag"
|
|
class="float absolute cursor-grabbing"
|
|
:style="{ left: `${drag.x}px`, top: `${drag.y}px`, width: `${drag.w}px` }"
|
|
:title="title(drag.key)"
|
|
:alarm="alarm(drag.key)"
|
|
:collapsed="!!layout.collapsed[drag.key]"
|
|
draggable
|
|
>
|
|
<template #extra><slot :name="`${drag.key}-extra`" /></template>
|
|
<slot :name="drag.key" />
|
|
</UiDockCard>
|
|
</div>
|
|
</Teleport>
|
|
</template>
|
|
|
|
<style scoped>
|
|
@reference "~/assets/css/main.css";
|
|
|
|
/* Where the box lands: a dashed slot in the column. */
|
|
.dock-gap {
|
|
@apply h-10 border-2 border-dashed border-wood-700 bg-parchment-200;
|
|
}
|
|
</style>
|