From 36540aa8fb0a9a385f8859be478105c7970ba9d8 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Wed, 21 Aug 2024 16:14:10 +0200 Subject: [PATCH] Create card editor --- .../editor/card-editor/hui-card-editor.ts | 131 ++++++++++++++++++ .../card-editor/hui-dialog-edit-card.ts | 18 ++- 2 files changed, 139 insertions(+), 10 deletions(-) create mode 100644 src/panels/lovelace/editor/card-editor/hui-card-editor.ts diff --git a/src/panels/lovelace/editor/card-editor/hui-card-editor.ts b/src/panels/lovelace/editor/card-editor/hui-card-editor.ts new file mode 100644 index 0000000000..4dac2544dc --- /dev/null +++ b/src/panels/lovelace/editor/card-editor/hui-card-editor.ts @@ -0,0 +1,131 @@ +import { css, CSSResultGroup, html, LitElement, nothing } from "lit"; +import { customElement, property, query, state } from "lit/decorators"; +import memoizeOne from "memoize-one"; +import { LovelaceConfig } from "../../../../data/lovelace/config/types"; +import { HomeAssistant } from "../../../../types"; +import "./hui-card-element-editor"; +import "./hui-card-layout-editor"; +import "./hui-card-visibility-editor"; +import { LovelaceCardConfig } from "../../../../data/lovelace/config/card"; +import type { HuiCardElementEditor } from "./hui-card-element-editor"; +import { fireEvent } from "../../../../common/dom/fire_event"; + +const TABS = ["config", "visibility", "layout"] as const; + +@customElement("hui-card-editor") +class HuiCardEditor extends LitElement { + @property({ attribute: false }) public hass!: HomeAssistant; + + @property({ attribute: false }) public lovelace!: LovelaceConfig; + + @property({ attribute: false }) public config!: LovelaceCardConfig; + + @property({ type: Boolean, attribute: "show-visibility-tab" }) + public showVisibilityTab = false; + + @property({ type: Boolean, attribute: "show-layout-tab" }) + public showLayoutTab = false; + + @query("hui-card-element-editor") + public elementEditor?: HuiCardElementEditor; + + @state() private _selectedTab: (typeof TABS)[number] = TABS[0]; + + private _tabs = memoizeOne((showLayoutTab: boolean, cardType: string) => + TABS.filter((tab) => { + if (tab === "visibility") return cardType !== "conditional"; + if (tab === "layout") return showLayoutTab; + return true; + }) + ); + + private renderContent() { + if (this._selectedTab === "config") { + return html` + + `; + } + if (this._selectedTab === "visibility") { + return html` + + `; + } + if (this._selectedTab === "layout") { + return html` + + + `; + } + return nothing; + } + + private _configChanged(ev: CustomEvent): void { + ev.stopPropagation(); + fireEvent(this, "config-changed", { config: ev.detail.value }); + } + + protected render() { + const cardType = this.config.type; + const tabs = this._tabs(this.showLayoutTab, cardType); + + if (tabs.length <= 1) { + return this.renderContent(); + } + return html` + + ${tabs.map( + (tab) => html` + + + ` + )} + + ${this.renderContent()} + `; + } + + private _handleTabChanged(ev: CustomEvent): void { + const cardType = this.config.type; + const tabs = this._tabs(this.showLayoutTab, cardType); + const newTab = tabs[ev.detail.index]; + if (newTab === this._selectedTab) { + return; + } + this._selectedTab = newTab; + } + + static get styles(): CSSResultGroup { + return css` + mwc-tab-bar { + text-transform: uppercase; + margin-bottom: 16px; + border-bottom: 1px solid var(--divider-color); + } + `; + } +} + +declare global { + interface HTMLElementTagNameMap { + "hui-card-editor": HuiCardEditor; + } +} diff --git a/src/panels/lovelace/editor/card-editor/hui-dialog-edit-card.ts b/src/panels/lovelace/editor/card-editor/hui-dialog-edit-card.ts index 96779a34cc..c228e0208d 100644 --- a/src/panels/lovelace/editor/card-editor/hui-dialog-edit-card.ts +++ b/src/panels/lovelace/editor/card-editor/hui-dialog-edit-card.ts @@ -37,7 +37,7 @@ import { getCardDocumentationURL } from "../get-dashboard-documentation-url"; import type { ConfigChangedEvent } from "../hui-element-editor"; import { findLovelaceContainer } from "../lovelace-path"; import type { GUIModeChangedEvent } from "../types"; -import "./hui-card-element-editor"; +import "./hui-card-editor"; import type { HuiCardElementEditor } from "./hui-card-element-editor"; import type { EditCardDialogParams } from "./show-edit-card-dialog"; @@ -75,7 +75,7 @@ export class HuiDialogEditCard @state() private _guiModeAvailable? = true; - @query("hui-card-element-editor") + @query("hui-card-editor") private _cardEditorEl?: HuiCardElementEditor; @state() private _GUImode = true; @@ -235,19 +235,17 @@ export class HuiDialogEditCard
- + > +
${this._isInSection @@ -348,7 +346,7 @@ export class HuiDialogEditCard private _opened() { window.addEventListener("dialog-closed", this._enableEscapeKeyClose); window.addEventListener("hass-more-info", this._disableEscapeKeyClose); - this._cardEditorEl?.focusYamlEditor(); + // this._cardEditorEl?.focusYamlEditor(); } private get _isInSection() {