diff --git a/src/esphome-main.ts b/src/esphome-main.ts index f02411bb..55bbc5a9 100644 --- a/src/esphome-main.ts +++ b/src/esphome-main.ts @@ -12,7 +12,9 @@ class ESPHomeMainView extends LitElement { @property() logoutUrl?: string; - @state() private editing?: string; + private url = new URL(document.baseURI); + @state() private editing?: string = + this.url.searchParams.get("edit") || undefined; protected render() { if (this.editing) { @@ -63,13 +65,25 @@ class ESPHomeMainView extends LitElement { protected firstUpdated(changedProps: PropertyValues): void { super.firstUpdated(changedProps); + document.body.addEventListener("edit-file", (ev) => { + this.url.searchParams.set("edit", ev.detail); + window.history.pushState({}, "", this.url.toString()); this.editing = ev.detail; }); + + window.addEventListener("popstate", () => { + const url = new URL(document.baseURI); + const editing = url.searchParams.get("edit") || undefined; + this.editing = editing; + }); + import("./editor/esphome-editor"); } private _handleEditorClose() { + this.url.searchParams.delete("edit"); + window.history.pushState({}, "", this.url.toString()); this.editing = undefined; } }