From dcabb0eabe31e9e3ffea13c9571a4aa59e2dcf27 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Mon, 12 Feb 2024 17:36:48 +0100 Subject: [PATCH] Improve lovelace card events --- .../lovelace/components/hui-card-options.ts | 4 ++-- .../editor/card-editor/hui-dialog-create-card.ts | 4 ++-- .../editor/card-editor/hui-dialog-edit-card.ts | 16 +++++++--------- .../card-editor/show-create-card-dialog.ts | 2 +- .../editor/card-editor/show-edit-card-dialog.ts | 5 +++-- src/panels/lovelace/views/hui-view.ts | 10 ++++++++-- 6 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/panels/lovelace/components/hui-card-options.ts b/src/panels/lovelace/components/hui-card-options.ts index 7f59aaf3e921..8d960e15f808 100644 --- a/src/panels/lovelace/components/hui-card-options.ts +++ b/src/panels/lovelace/components/hui-card-options.ts @@ -275,9 +275,9 @@ export class HuiCardOptions extends LitElement { const cardConfig = this._currentView.cards![path[1]]; showEditCardDialog(this, { lovelaceConfig: this.lovelace!.config, - cardConfig, saveConfig: this.lovelace!.saveConfig, - path: [path[0]], + path: [path[0], null], + newCardConfig: cardConfig, }); } diff --git a/src/panels/lovelace/editor/card-editor/hui-dialog-create-card.ts b/src/panels/lovelace/editor/card-editor/hui-dialog-create-card.ts index 4ce93735d430..3c7798411f00 100644 --- a/src/panels/lovelace/editor/card-editor/hui-dialog-create-card.ts +++ b/src/panels/lovelace/editor/card-editor/hui-dialog-create-card.ts @@ -214,8 +214,8 @@ export class HuiCreateDialogCard showEditCardDialog(this, { lovelaceConfig: this._params!.lovelaceConfig, saveConfig: this._params!.saveConfig, - path: this._params!.path, - cardConfig: config, + path: [this._params!.path[0], null], + newCardConfig: config, }); this.closeDialog(); 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 98427c14f45e..44e47035677d 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 @@ -87,12 +87,13 @@ export class HuiDialogEditCard const [view, card] = params.path; this._viewConfig = params.lovelaceConfig.views[view]; this._cardConfig = - card !== undefined ? this._viewConfig.cards![card] : params.cardConfig; + params.newCardConfig ?? + (card !== null ? this._viewConfig.cards![card] : undefined); this.large = false; if (this._cardConfig && !Object.isFrozen(this._cardConfig)) { this._cardConfig = deepFreeze(this._cardConfig); } - if (params.cardConfig) { + if (params.newCardConfig) { this._dirty = true; } } @@ -368,16 +369,13 @@ export class HuiDialogEditCard return; } this._saving = true; + const [view, card] = this._params!.path; await this._params!.saveConfig( - this._params!.path.length === 1 - ? addCard( - this._params!.lovelaceConfig, - this._params!.path as [number], - this._cardConfig! - ) + card === null + ? addCard(this._params!.lovelaceConfig, [view], this._cardConfig!) : replaceCard( this._params!.lovelaceConfig, - this._params!.path as [number, number], + [view, card], this._cardConfig! ) ); diff --git a/src/panels/lovelace/editor/card-editor/show-create-card-dialog.ts b/src/panels/lovelace/editor/card-editor/show-create-card-dialog.ts index dedd6d1b6a25..230047d39911 100644 --- a/src/panels/lovelace/editor/card-editor/show-create-card-dialog.ts +++ b/src/panels/lovelace/editor/card-editor/show-create-card-dialog.ts @@ -4,7 +4,7 @@ import type { LovelaceConfig } from "../../../../data/lovelace/config/types"; export interface CreateCardDialogParams { lovelaceConfig: LovelaceConfig; saveConfig: (config: LovelaceConfig) => void; - path: [number] | [number, number]; + path: [number]; entities?: string[]; // We can pass entity id's that will be added to the config when a card is picked } diff --git a/src/panels/lovelace/editor/card-editor/show-edit-card-dialog.ts b/src/panels/lovelace/editor/card-editor/show-edit-card-dialog.ts index f6b55e3b0e70..46070fd7ffad 100644 --- a/src/panels/lovelace/editor/card-editor/show-edit-card-dialog.ts +++ b/src/panels/lovelace/editor/card-editor/show-edit-card-dialog.ts @@ -5,8 +5,9 @@ import type { LovelaceConfig } from "../../../../data/lovelace/config/types"; export interface EditCardDialogParams { lovelaceConfig: LovelaceConfig; saveConfig: (config: LovelaceConfig) => void; - path: [number] | [number, number]; - cardConfig?: LovelaceCardConfig; + path: [number, number | null]; + // If specified, the card will be replaced with the new card. + newCardConfig?: LovelaceCardConfig; } export const importEditCardDialog = () => import("./hui-dialog-edit-card"); diff --git a/src/panels/lovelace/views/hui-view.ts b/src/panels/lovelace/views/hui-view.ts index 1d78c5e7a827..4338bc9eff52 100644 --- a/src/panels/lovelace/views/hui-view.ts +++ b/src/panels/lovelace/views/hui-view.ts @@ -31,13 +31,19 @@ import { LovelaceViewConfig, isStrategyView, } from "../../../data/lovelace/config/view"; +import { HASSDomEvent } from "../../../common/dom/fire_event"; declare global { // for fire event interface HASSDomEvents { "ll-create-card": undefined; - "ll-edit-card": { path: [number] | [number, number] }; - "ll-delete-card": { path: [number] | [number, number]; confirm: boolean }; + "ll-edit-card": { path: [number, number] }; + "ll-delete-card": { path: [number, number]; confirm: boolean }; + } + interface HTMLElementEventMap { + "ll-create-card": HASSDomEvent; + "ll-edit-card": HASSDomEvent; + "ll-delete-card": HASSDomEvent; } }