Corners stay editable after drawing: drag a corner (mouse or touch), focus it and nudge with the arrow keys (Shift for larger steps), '+' on an edge inserts a corner in its middle, double click or Delete removes one. Tested in a browser harness with the real field markup. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
116 lines
7.6 KiB
PHP
116 lines
7.6 KiB
PHP
{{--
|
||
Room area editor (FloorPlanForm): click the corners of a room on the plan picture; stored as "x,y x,y …" in
|
||
percent of the picture. The other rooms are shown as blue outlines for orientation.
|
||
--}}
|
||
@php
|
||
$planPhoto = \App\Models\Photo::query()->find($get('../../photo_id'));
|
||
$currentRoomId = $get('id');
|
||
$otherAreas = collect($get('../../rooms') ?? [])
|
||
->reject(fn ($room): bool => ($room['id'] ?? null) === $currentRoomId)
|
||
->map(fn ($room): string => collect(\App\Models\FloorPlan::points($room['points'] ?? null))->map(fn ($point): string => $point[0].','.$point[1])->implode(' '))
|
||
->filter()
|
||
->values();
|
||
@endphp
|
||
<x-dynamic-component :component="$getFieldWrapperView()" :field="$field">
|
||
@if (! $planPhoto)
|
||
<p style="padding:.75rem 1rem;border-radius:.5rem;background:#fef3c7;color:#92400e">Bitte oben zuerst das Grundriss-Bild wählen.</p>
|
||
@else
|
||
<div
|
||
x-data="{
|
||
state: $wire.$entangle('{{ $getStatePath() }}'),
|
||
dragging: null,
|
||
pairs() {
|
||
return String(this.state ?? '').trim().split(/\s+/)
|
||
.filter((pair) => /^\d+(\.\d+)?,\d+(\.\d+)?$/.test(pair))
|
||
.map((pair) => pair.split(',').map(Number))
|
||
},
|
||
write(pairs) { this.state = pairs.map(([x, y]) => x + ',' + y).join(' ') },
|
||
clamp(value) { return Math.min(100, Math.max(0, Math.round(value * 10) / 10)) },
|
||
position(event) {
|
||
const box = this.$refs.plan.getBoundingClientRect()
|
||
return [this.clamp((event.clientX - box.left) / box.width * 100), this.clamp((event.clientY - box.top) / box.height * 100)]
|
||
},
|
||
midpoints() {
|
||
const pairs = this.pairs()
|
||
if (pairs.length < 2) return []
|
||
return pairs.map((a, index) => {
|
||
const b = pairs[(index + 1) % pairs.length]
|
||
return { index, x: this.clamp((a[0] + b[0]) / 2), y: this.clamp((a[1] + b[1]) / 2) }
|
||
}).slice(0, pairs.length === 2 ? 1 : pairs.length)
|
||
},
|
||
add(event) {
|
||
this.write([...this.pairs(), this.position(event)])
|
||
},
|
||
startDrag(index, event) {
|
||
this.dragging = index
|
||
event.target.setPointerCapture(event.pointerId)
|
||
},
|
||
drag(event) {
|
||
if (this.dragging === null) return
|
||
const pairs = this.pairs()
|
||
pairs[this.dragging] = this.position(event)
|
||
this.write(pairs)
|
||
},
|
||
endDrag() { this.dragging = null },
|
||
insert(index) {
|
||
const pairs = this.pairs()
|
||
const middle = this.midpoints().find((point) => point.index === index)
|
||
pairs.splice(index + 1, 0, [middle.x, middle.y])
|
||
this.write(pairs)
|
||
},
|
||
remove(index) {
|
||
const pairs = this.pairs()
|
||
pairs.splice(index, 1)
|
||
this.write(pairs)
|
||
},
|
||
nudge(index, dx, dy, event) {
|
||
const step = event.shiftKey ? 1 : 0.2
|
||
const pairs = this.pairs()
|
||
pairs[index] = [this.clamp(pairs[index][0] + dx * step), this.clamp(pairs[index][1] + dy * step)]
|
||
this.write(pairs)
|
||
},
|
||
undo() { this.write(this.pairs().slice(0, -1)) },
|
||
clear() { this.state = '' },
|
||
}"
|
||
>
|
||
<div x-ref="plan" x-on:click="add($event)" x-on:pointermove="drag($event)" x-on:pointerup="endDrag()" x-on:pointercancel="endDrag()"
|
||
style="position:relative;cursor:crosshair;user-select:none;touch-action:none;border:1px solid #e4e4e7;border-radius:.5rem;overflow:hidden">
|
||
<img src="{{ $planPhoto->url('lg') }}" alt="" draggable="false" style="display:block;width:100%;height:auto">
|
||
<svg viewBox="0 0 100 100" preserveAspectRatio="none" style="position:absolute;inset:0;width:100%;height:100%;pointer-events:none" aria-hidden="true">
|
||
@foreach ($otherAreas as $area)
|
||
<polygon points="{{ $area }}" fill="rgba(37,99,235,.10)" stroke="#2563eb" stroke-width="1.5" vector-effect="non-scaling-stroke" />
|
||
@endforeach
|
||
<polygon x-bind:points="pairs().map((pair) => pair.join(',')).join(' ')" fill="rgba(217,119,6,.28)" stroke="#d97706" stroke-width="2.5" vector-effect="non-scaling-stroke" />
|
||
</svg>
|
||
<template x-for="point in midpoints()" x-bind:key="'m' + point.index">
|
||
<button type="button" x-on:click.stop="insert(point.index)" x-bind:aria-label="'Punkt zwischen Ecke ' + (point.index + 1) + ' und der nächsten einfügen'" title="Punkt einfügen"
|
||
x-bind:style="`position:absolute;left:${point.x}%;top:${point.y}%;width:18px;height:18px;margin:-9px 0 0 -9px;padding:0;border-radius:999px;border:1.5px solid #d97706;background:rgba(255,255,255,.85);color:#b45309;font:600 13px/1 sans-serif;cursor:copy`">+</button>
|
||
</template>
|
||
<template x-for="(pair, index) in pairs()" x-bind:key="'p' + index">
|
||
<button type="button"
|
||
x-on:pointerdown.stop="startDrag(index, $event)"
|
||
x-on:click.stop
|
||
x-on:dblclick.stop="remove(index)"
|
||
x-on:keydown.arrow-left.prevent="nudge(index, -1, 0, $event)"
|
||
x-on:keydown.arrow-right.prevent="nudge(index, 1, 0, $event)"
|
||
x-on:keydown.arrow-up.prevent="nudge(index, 0, -1, $event)"
|
||
x-on:keydown.arrow-down.prevent="nudge(index, 0, 1, $event)"
|
||
x-on:keydown.delete.prevent="remove(index)"
|
||
x-bind:aria-label="'Ecke ' + (index + 1) + ' – ziehen oder mit den Pfeiltasten verschieben, Entf löscht'"
|
||
x-bind:title="'Ecke ' + (index + 1)"
|
||
x-bind:style="`position:absolute;left:${pair[0]}%;top:${pair[1]}%;width:16px;height:16px;margin:-8px 0 0 -8px;padding:0;background:${dragging === index ? '#d97706' : '#fff'};border:2px solid #d97706;border-radius:3px;cursor:${dragging === index ? 'grabbing' : 'grab'};touch-action:none`"></button>
|
||
</template>
|
||
</div>
|
||
<div style="display:flex;flex-wrap:wrap;align-items:center;gap:.5rem;margin-top:.5rem">
|
||
<span style="font-size:.875rem;color:#52525b" x-text="pairs().length + (pairs().length === 1 ? ' Eckpunkt' : ' Eckpunkte')"></span>
|
||
<x-filament::button size="sm" color="gray" x-on:click="undo()" x-bind:disabled="pairs().length === 0">Letzten Punkt entfernen</x-filament::button>
|
||
<x-filament::button size="sm" color="danger" outlined x-on:click="clear()" x-bind:disabled="pairs().length === 0">Fläche leeren</x-filament::button>
|
||
</div>
|
||
<p style="margin-top:.25rem;font-size:.875rem;color:#71717a">
|
||
Neue Ecke: auf das Bild klicken. Ecke verschieben: Punkt mit der Maus ziehen oder anklicken und mit den Pfeiltasten bewegen (mit Umschalt in größeren Schritten).
|
||
Ecke einfügen: „+“ auf einer Kante. Ecke löschen: Doppelklick auf den Punkt oder Entf. Blau umrandet: die anderen Räume.
|
||
</p>
|
||
</div>
|
||
@endif
|
||
</x-dynamic-component>
|