fix: separate hierarchy management from selected building editing

This commit is contained in:
jhartworks committed 2026-11-19 09:46:31 +01:00
1 parent bffc4d0acc
commit 382d6e04f2
18 files changed
+2103 -1648

No files matched your search

+127 -26
View File
@@ -1,4 +1,6 @@
import { floorSelection, normalizeTopology, topologyFloors } from "../topology.ts";
import { editorFloors, editorScopeChoices, editorScopeFromKey, editorScopeKey, moveSelectedFloor, reconcileEditorContext, scopeForFloor, selectEditorScope, type EditorScope } from "./editor/editor-context.ts";
import "./editor/hierarchy-manager.ts";
import { floorSelection, floorTopologyPath, normalizeTopology, topologyFloors } from "../topology.ts";
// 2D editor: floors, rooms (rectangles and free shapes), snapping, undo, background template.
import { html, LitElement, nothing, svg, type PropertyValues, type TemplateResult } from "lit";
@@ -205,6 +207,8 @@ export class NfEditor extends LitElement {
_tour: { state: true },
_sideOpen: { state: true },
_floorId: { state: true },
_editorScope: { state: true },
_floorMove: { state: true },
_energyNote: { state: true },
_ctx: { state: true },
_fixedHint: { state: true },
@@ -261,6 +265,8 @@ export class NfEditor extends LitElement {
private tourChecked = false;
private declare _sideOpen: boolean;
private declare _floorId: string | null;
private declare _editorScope: EditorScope | null;
private declare _floorMove: {floorId:string;targetId:string} | null;
/** What the import from the energy dashboard did (shown under its button). */
private declare _energyNote: string | null;
/** Open context menu (right-click, long press) at a plan point, for one item. */
@@ -327,6 +333,7 @@ export class NfEditor extends LitElement {
this.narrow = false;
this.roomLabels = true;
this._floorId = null;
this._editorScope=null;this._floorMove=null;
this._arcEdit = null;
this._routeDraft = null;
this.roof.sectionId = null;
@@ -634,7 +641,7 @@ export class NfEditor extends LitElement {
const next = structuredClone(this._doc);
if (!moveFurnitureToFloor(next, from, id, floorId)) return;
this.setDoc(next);
this._floorId = floorId;
this.adoptFloor(floorId);
this.selectItem("furniture", id);
}
@@ -680,7 +687,7 @@ export class NfEditor extends LitElement {
return new RoofController({
host: this.host,
floorId: () => editor._floorId,
setFloorId: (id) => (editor._floorId = id),
setFloorId: (id) => editor.adoptFloor(id),
confirm: (text) => confirm(text),
refresh: () => editor.requestUpdate(),
});
@@ -704,7 +711,7 @@ export class NfEditor extends LitElement {
/** What the roof and solar forms need. */
private roofDeps(): RoofPanelDeps {
return { host: this.host, roof: this.roof, floorId: this._floorId, setFloorId: (id) => (this._floorId = id), toggleLockPlan: () => this.toggleLockPlan() };
return { host: this.host, roof: this.roof, floorId: this._floorId, setFloorId: (id) => this.adoptFloor(id), toggleLockPlan: () => this.toggleLockPlan() };
}
private makeBackup(): BackupService {
@@ -719,7 +726,7 @@ export class NfEditor extends LitElement {
t: (key, vars) => editor.t(key, vars),
adopt: (building) => {
editor.setDoc(building);
editor._floorId = building.floors[0]?.id ?? null;
editor._editorScope=null;editor.adoptFloor(building.floors[0]?.id??null);
editor.selectItem("room", null);
editor.fit();
},
@@ -783,9 +790,10 @@ export class NfEditor extends LitElement {
}
this._doc = this.building;
this._doc3d = this.building;
if (!this._doc.floors.some((f) => f.id === this._floorId)) this._floorId = this._doc.floors[0]?.id ?? null;
this.syncEditorContext();
if (!this.floor?.rooms.some((r) => r.id === this.sel.room)) this.sel.room = null;
}
if(changed.has("_doc"))this.syncEditorContext();
}
protected firstUpdated(): void {
@@ -1090,11 +1098,11 @@ export class NfEditor extends LitElement {
}}
@device-move=${this.onDeviceMoved3d}
@floor-tap=${(e: CustomEvent<{ floorId: string | null }>) => {
if (e.detail.floorId) this._floorId = e.detail.floorId;
if (e.detail.floorId) this.adoptFloor(e.detail.floorId);
this._sideOpen = false;
}}
@room-tap=${(e: CustomEvent<{ floorId: string; roomId: string | null }>) => {
if (e.detail.floorId) this._floorId = e.detail.floorId;
if (e.detail.floorId) this.adoptFloor(e.detail.floorId);
if (e.detail.roomId) this.selectFrom3d("room", e.detail.roomId);
else this._sideOpen = false;
}}
@@ -1124,6 +1132,93 @@ export class NfEditor extends LitElement {
// ------------------------------------------------------------------ document helpers
private clearPlanSelection(): void {
this.cancelPick();
this.selectItem("room",null);
this._note=null;this._draft=[];this._cursor=null;this._ctx=null;this._wallView=null;this.drag=null;this._fixedHint=false;
this.side.floorCopy=null;this.side.floorMenu=false;
this.side.shiftX=0;this.side.shiftZ=0;
}
private syncEditorContext(): void {
const next=reconcileEditorContext(this._doc,{scope:this._editorScope,floorId:this._floorId});
if(next.floorId===this._floorId&&editorScopeKey(next.scope)===editorScopeKey(this._editorScope))return;
this._editorScope=next.scope;this._floorId=next.floorId;this.clearPlanSelection();
}
private adoptFloor(id:string|null):void {
const f=id?this._doc.floors.find((f)=>f.id===id):undefined;
if(id&&!f)return;
this._floorId=f?.id??null;
if(f)this._editorScope=this._doc.topology?scopeForFloor(f):null;
this.clearPlanSelection();
}
private selectBuilding(scope:EditorScope):void {
const valid=editorScopeFromKey(this._doc,editorScopeKey(scope));if(!valid)return;
const next=selectEditorScope(this._doc,valid);
this._editorScope=next.scope;this._floorId=next.floorId;
this.clearPlanSelection();this.fit();
}
private openHierarchy():void {
const dialog=this.renderRoot.querySelector<HTMLDialogElement>(".nf-hierarchy-dialog");
if(dialog&&!dialog.open)dialog.showModal();
}
private async openFloorMove():Promise<void> {
if(!this.isAdmin||!this.floor||this.floor.outdoor_location_id||!this._doc.topology)return;
this._floorMove={floorId:this.floor.id,targetId:""};await this.updateComplete;
const dialog=this.renderRoot.querySelector<HTMLDialogElement>(".nf-floor-move-dialog");
if(dialog&&!dialog.open)dialog.showModal();
}
private applyFloorMove():void {
if(!this.isAdmin||!this._floorMove)return;
const {floorId,targetId}=this._floorMove,next=structuredClone(this._doc);
if(!moveSelectedFloor(next,this.floor?.id??null,floorId,targetId))return;
this.setDoc(next);this.adoptFloor(floorId);this.fit();
this.renderRoot.querySelector<HTMLDialogElement>(".nf-floor-move-dialog")?.close();
}
private renderEditorNavigation():TemplateResult|typeof nothing {
if(!this._doc.topology)return isSymcon(this.hass)?html`<div class="nf-editor-context"><button class="nf-btn nf-hierarchy-open" @click=${()=>this.openHierarchy()}>Hierarchie</button></div>`:nothing;
const scope=this._editorScope,choices=editorScopeChoices(this._doc),floors=editorFloors(this._doc,scope);
const label=choices.find(([key])=>key===editorScopeKey(scope))?.[1]??"Gebäude auswählen";
return html`<div class="nf-editor-context">
<div class="nf-editor-building-row"><label>Gebäude<select class="nf-editor-building-select" aria-label="Gebäude" title=${label} @change=${(e:Event)=>{const s=editorScopeFromKey(this._doc,(e.target as HTMLSelectElement).value);if(s)this.selectBuilding(s);}}>
${!scope?html`<option value="">Gebäude auswählen</option>`:nothing}
${choices.map(([key,name])=>html`<option value=${key} .selected=${key===editorScopeKey(scope)}>${name}</option>`)}
</select></label><button class="nf-btn nf-hierarchy-open" @click=${()=>this.openHierarchy()}>Hierarchie</button></div>
<nav class="nf-editor-floor-nav" aria-label="Etagen des gewählten Gebäudes">
<span class="nf-muted">${scope?.kind==="outside"?"Außenplan":"Etagen"}</span>
${[...floors].reverse().map((f)=>html`<button class="nf-chip" data-editor-floor=${f.id} aria-pressed=${f.id===this._floorId} @click=${()=>{this.adoptFloor(f.id);this.fit();}}>${f.name}</button>`)}
${scope?.kind==="building"?html`<button class="nf-btn nf-editor-add-floor" ?disabled=${!this.isAdmin} @click=${()=>this.addFloor(null)}>+ Etage</button>`:nothing}
${this.floor&&!this.floor.outdoor_location_id?html`<button class="nf-link nf-editor-move-floor" ?disabled=${!this.isAdmin} @click=${()=>void this.openFloorMove()}>Ausgewählte Etage verschieben…</button>`:nothing}
</nav>
</div>`;
}
private renderHierarchyDialogs():TemplateResult {
const move=this._floorMove,source=move?this._doc.floors.find((f)=>f.id===move.floorId):undefined;
const targets=editorScopeChoices(this._doc).filter(([key])=>key.startsWith("building:")&&key!==`building:${source?.building_id}`);
return html`<dialog class="nf-hierarchy-dialog" aria-label="Hierarchie verwalten" @keydown=${(e:KeyboardEvent)=>e.stopPropagation()}>
<header><h2>Hierarchie verwalten</h2><button class="nf-btn" aria-label="Schließen" @click=${()=>this.renderRoot.querySelector<HTMLDialogElement>(".nf-hierarchy-dialog")?.close()}>×</button></header>
<nf-hierarchy-manager .doc=${this._doc} .admin=${this.isAdmin} .suggestedName=${this.hass?.symconTitle?.trim()||"Gebäude 1"}
@hierarchy-change=${(e:CustomEvent<{building:Building}>)=>{e.stopPropagation();if(this.isAdmin)this.setDoc(e.detail.building);}}
@hierarchy-open=${(e:CustomEvent<EditorScope>)=>{e.stopPropagation();this.selectBuilding(e.detail);this.renderRoot.querySelector<HTMLDialogElement>(".nf-hierarchy-dialog")?.close();}}></nf-hierarchy-manager>
</dialog>
<dialog class="nf-floor-move-dialog" aria-label="Ausgewählte Etage verschieben" @close=${()=>this._floorMove=null} @keydown=${(e:KeyboardEvent)=>e.stopPropagation()}>
<header><h2>Ausgewählte Etage verschieben</h2><button class="nf-btn" aria-label="Schließen" @click=${()=>this.renderRoot.querySelector<HTMLDialogElement>(".nf-floor-move-dialog")?.close()}>×</button></header>
${move&&source?html`<p class="nf-floor-move-source">${floorTopologyPath(this._doc,source)}</p>
<label class="nf-field">Zielgebäude<select class="nf-floor-move-target" @change=${(e:Event)=>this._floorMove={...move,targetId:(e.target as HTMLSelectElement).value}}>
<option value="" .selected=${!move.targetId}>Zielgebäude auswählen</option>
${targets.map(([key,name])=>html`<option value=${key.slice(9)} .selected=${move.targetId===key.slice(9)}>${name}</option>`)}
</select></label>
<button class="nf-btn nf-primary nf-floor-move-apply" ?disabled=${!this.isAdmin||!move.targetId||source.id!==this.floor?.id||!targets.some(([key])=>key===`building:${move.targetId}`)} @click=${()=>this.applyFloorMove()}>Etage „${source.name}“ verschieben</button>`:nothing}
</dialog>`;
}
private get floor(): Floor | undefined {
return this._doc?.floors.find((f) => f.id === this._floorId);
}
@@ -1141,6 +1236,7 @@ export class NfEditor extends LitElement {
if (base) this.history.record(base);
if(next.topology)normalizeTopology(next);
this._doc = next;
this.syncEditorContext();
this.syncHistoryButtons();
this.dispatchEvent(new CustomEvent("building-changed", { detail: { building: next }, bubbles: true, composed: true }));
}
@@ -1171,7 +1267,7 @@ export class NfEditor extends LitElement {
private restore(doc: Building): void {
this._doc = doc;
if (!doc.floors.some((f) => f.id === this._floorId)) this._floorId = doc.floors[0]?.id ?? null;
this.syncEditorContext();
if (!this.floor?.rooms.some((r) => r.id === this.sel.room)) this.sel.room = null;
this.sel.corner = null;
this.syncHistoryButtons();
@@ -2228,8 +2324,8 @@ export class NfEditor extends LitElement {
const next = structuredClone(this._doc);
next.floors = next.floors.filter((f) => f.id !== floor.id);
this.setDoc(next);
this._floorId = next.floors[0]?.id ?? null;
this.sel.room = null;
this.syncEditorContext();
this.clearPlanSelection();
}
// ------------------------------------------------------------------ roof sections
@@ -2324,12 +2420,14 @@ export class NfEditor extends LitElement {
// ------------------------------------------------------------------ floors and rooms
private addFloor(from: HassFloor | null = null, parent?: string): void {
const floor = newFloorFor(this._doc, from, { first: this.t("default_floor"), next: (n) => this.t("new_floor", { n }) }, parent ?? this.floor?.building_id);
if(!this.isAdmin)return;
const target=parent??(this._editorScope?.kind==="building"?this._editorScope.id:undefined);
if(this._doc.topology&&(!target||!this._doc.topology.buildings.some((g)=>g.id===target)))return;
const floor = newFloorFor(this._doc, from, { first: this.t("default_floor"), next: (n) => this.t("new_floor", { n }) }, target);
const next = structuredClone(this._doc);
next.floors = floorsWith(next.floors, floor);
this.setDoc(next);
this._floorId = floor.id;
this.sel.room = null;
this.adoptFloor(floor.id);
this.side.floorMenu = false;
this.fit();
}
@@ -2351,8 +2449,7 @@ export class NfEditor extends LitElement {
const next = structuredClone(this._doc);
next.floors = floorsWith(next.floors, copy);
this.setDoc(next);
this._floorId = copy.id;
this.sel.room = null;
this.adoptFloor(copy.id);
this.side.floorCopy = null;
this.fit();
}
@@ -2360,7 +2457,11 @@ export class NfEditor extends LitElement {
private moveFloor(direction: -1 | 1): void {
const id = this._floorId;
if (!id) return;
const order = floorsSwapped(this._doc.floors, id, direction);
const siblings = editorFloors(this._doc, this._editorScope);
const reordered = floorsSwapped(siblings, id, direction);
const ids = new Set(siblings.map((f) => f.id));
let at = 0;
const order = this._doc.floors.map((f) => ids.has(f.id) ? reordered[at++] : f);
if (order.every((f, i) => f.id === this._doc.floors[i].id)) return;
const next = structuredClone(this._doc);
next.floors = order.map((f) => next.floors.find((x) => x.id === f.id)!);
@@ -2370,15 +2471,11 @@ export class NfEditor extends LitElement {
private floorActions(): FloorActions {
return {
openFloor: (id) => {
this._floorId = id;
this.sel.room = null;
this.sel.corner = null;
this._draft = [];
this.adoptFloor(id);
this.fit();
},
print: () => this.printPlan(),
addFloor: (from) => this.addFloor(from),
addFloorIn: (id) => this.addFloor(null,id),
moveFloor: (direction) => this.moveFloor(direction),
deleteFloor: () => this.deleteFloor(),
duplicateFloor: (options) => this.duplicateFloor(options),
@@ -2393,8 +2490,8 @@ export class NfEditor extends LitElement {
private roomActions(room: Room): RoomActions {
return {
select: (id) => this.selectItem("room", id),
patch: (patch) => this.updateRoom(patch),
setArea: (id) => this.setArea(id),
patch: (patch) => { if(this.room?.id===room.id)this.updateRoom(patch); },
setArea: (id) => { if(this.room?.id===room.id)this.setArea(id); },
setRect: (field, value) => this.setRect(field, value),
setCorner: (index, axis, value) => this.setPoint(index, axis, value),
removeCorner: (index) => this.deleteVertex(index),
@@ -2725,7 +2822,7 @@ export class NfEditor extends LitElement {
}
private openEnergyDevice(floor: Floor, item: Furniture): void {
this._floorId = floor.id;
this.adoptFloor(floor.id);
this.roof.solarId = null;
this.selectItem("furniture", item.id);
this.showPoint(item.x, item.z);
@@ -3162,12 +3259,15 @@ export class NfEditor extends LitElement {
const floor = this.floor;
const walls = floor ? generateWalls(floor.rooms, { exterior: this._doc.settings.wall_exterior, interior: this._doc.settings.wall_interior }, floor.walls ?? []) : null;
return html`
${this.renderHierarchyDialogs()}
${renderPreview(this.host, this.lib.preview)}
${this._wallView
? html`<nf-wall-view .host=${this.host} .doc=${this._doc} .roomId=${this._wallView.roomId} .edge=${this._wallView.edge} @close=${() => (this._wallView = null)}></nf-wall-view>`
: nothing}
<div class="nf-editor ${this.narrow ? "nf-narrow" : ""}">
<div class="nf-main">
<div class="nf-editor-head">
${this.renderEditorNavigation()}
<div class="nf-toolbar">
${this.narrow
? html`<select class="nf-tool-select" aria-label=${this.t("tool_select")} ?disabled=${!floor} @change=${(e: Event) => {
@@ -3210,6 +3310,7 @@ export class NfEditor extends LitElement {
</div>
${walls?.warnings.length ? html`<span class="nf-warn">${this.t("overlap_warning")}</span>` : nothing}
</div>
</div>
<div class="nf-stage-pair ${this._split ? "nf-split" : ""}" style=${this._split && !this.narrow ? `--nf-split:${Math.round(this._splitRatio * 100)}%` : ""}>
<div class="nf-canvas-wrap">
${this.houseTool ? html`<div class="nf-tool-note">${this.t(this._tool === "energy" ? "energy_only_note" : "roof_only_note")}</div>` : nothing}
@@ -3228,7 +3329,7 @@ export class NfEditor extends LitElement {
${this.planLayers(floor, walls?.walls ?? null)}
</svg>
${this.renderContext()}
${this._pick || this._fixedHint ? html`<p class="nf-hint ${this._fixedHint ? "nf-hint-fixed" : ""}">${this._pick ? this.t("robot_cal_doing") : this.t("fixed_drag_hint")}</p>` : html`<div class="nf-hint">${hint(!floor ? this.t("hint_empty") : this.t(`hint_${this._tool}` as I18nKey))}</div>`}
${this._pick || this._fixedHint ? html`<p class="nf-hint ${this._fixedHint ? "nf-hint-fixed" : ""}">${this._pick ? this.t("robot_cal_doing") : this.t("fixed_drag_hint")}</p>` : html`<div class="nf-hint">${hint(!floor ? this._doc.topology ? "Wähle oben ein Gebäude und lege dort mit + Etage ein Stockwerk an. Ort, Niederlassung und Gebäude verwaltest du über Hierarchie." : this.t("hint_empty") : this.t(`hint_${this._tool}` as I18nKey))}</div>`}
<button class="nf-keys-open" title=${this.t("keys_title")} aria-label=${this.t("keys_title")} @click=${() => (this._keys = true)}>?</button>
${this.isAdmin
? renderTour((k, vars) => this.t(k, vars), this._tour, (step) => {