From 2f513a90d3f6dd727035b9c40a0e0c8699c3faa1 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Wed, 30 Oct 2024 12:16:47 +0100 Subject: [PATCH] Use imported container for individual card move --- .../lovelace/components/hui-card-options.ts | 90 +++++++------------ src/translations/en.json | 10 +-- 2 files changed, 38 insertions(+), 62 deletions(-) diff --git a/src/panels/lovelace/components/hui-card-options.ts b/src/panels/lovelace/components/hui-card-options.ts index 0a9a39be8e38..1b48b3259e84 100644 --- a/src/panels/lovelace/components/hui-card-options.ts +++ b/src/panels/lovelace/components/hui-card-options.ts @@ -28,22 +28,17 @@ import "../../../components/ha-icon-button"; import "../../../components/ha-list-item"; import { LovelaceCardConfig } from "../../../data/lovelace/config/card"; import { saveConfig } from "../../../data/lovelace/config/types"; -import { - isStrategyView, - type LovelaceViewConfig, -} from "../../../data/lovelace/config/view"; +import { isStrategyView } from "../../../data/lovelace/config/view"; import { showAlertDialog, showPromptDialog, } from "../../../dialogs/generic/show-dialog-box"; import { haStyle } from "../../../resources/styles"; import { HomeAssistant } from "../../../types"; -import { showSaveSuccessToast } from "../../../util/toast-saved-success"; import { computeCardSize } from "../common/compute-card-size"; import { showEditCardDialog } from "../editor/card-editor/show-edit-card-dialog"; import { addCard, - addSection, deleteCard, moveCardToContainer, moveCardToIndex, @@ -57,8 +52,6 @@ import { } from "../editor/lovelace-path"; import { showSelectViewDialog } from "../editor/select-view/show-select-view-dialog"; import { Lovelace, LovelaceCard } from "../types"; -import { SECTIONS_VIEW_LAYOUT } from "../views/const"; -import type { LovelaceSectionConfig } from "../../../data/lovelace/config/section"; @customElement("hui-card-options") export class HuiCardOptions extends LitElement { @@ -359,9 +352,13 @@ export class HuiCardOptions extends LitElement { allowDashboardChange: true, header: this.hass!.localize("ui.panel.lovelace.editor.move_card.header"), viewSelectedCallback: async (urlPath, selectedDashConfig, viewIndex) => { - const fromView = selectedDashConfig.views[this.path![0]]; - let toView = selectedDashConfig.views[viewIndex]; - let newConfig = selectedDashConfig; + if (!this.lovelace) return; + const toView = selectedDashConfig.views[viewIndex]; + const newConfig = selectedDashConfig; + + const undoAction = async () => { + this.lovelace!.saveConfig(selectedDashConfig); + }; if (isStrategyView(toView)) { showAlertDialog(this, { @@ -376,53 +373,22 @@ export class HuiCardOptions extends LitElement { return; } - const isSectionsView = toView.type === SECTIONS_VIEW_LAYOUT; - - let toPath: LovelaceContainerPath = [viewIndex]; - - // If the view is a section view and has no "imported cards" section, adds a default section. - if (isSectionsView) { - const importedCardHeading = fromView.title - ? this.hass!.localize( - "ui.panel.lovelace.editor.section.imported_card_section_title_view", - { view_title: fromView.title } - ) - : this.hass!.localize( - "ui.panel.lovelace.editor.section.imported_card_section_title_default" - ); - - let sectionIndex = toView.sections - ? toView.sections.findIndex( - (s) => - "cards" in s && - s.cards?.some( - (c) => - c.type === "heading" && c.heading === importedCardHeading - ) - ) - : -1; - if (sectionIndex === -1) { - const newSection: LovelaceSectionConfig = { - type: "grid", - cards: [ - { - type: "heading", - heading: importedCardHeading, - }, - ], - }; - newConfig = addSection(selectedDashConfig, viewIndex, newSection); - toView = newConfig.views[viewIndex] as LovelaceViewConfig; - sectionIndex = toView.sections!.length - 1; - } - toPath = [viewIndex, sectionIndex]; - } + const toPath: LovelaceContainerPath = [viewIndex]; if (urlPath === this.lovelace!.urlPath) { this.lovelace!.saveConfig( moveCardToContainer(newConfig, this.path!, toPath) ); - showSaveSuccessToast(this, this.hass!); + this.lovelace.showToast({ + message: this.hass!.localize( + "ui.panel.lovelace.editor.move_card.success" + ), + duration: 4000, + action: { + action: undoAction, + text: this.hass!.localize("ui.common.undo"), + }, + }); return; } try { @@ -436,10 +402,22 @@ export class HuiCardOptions extends LitElement { this.lovelace!.saveConfig( deleteCard(this.lovelace!.config, this.path!) ); - showSaveSuccessToast(this, this.hass!); + + this.lovelace.showToast({ + message: this.hass!.localize( + "ui.panel.lovelace.editor.move_card.success" + ), + duration: 4000, + action: { + action: undoAction, + text: this.hass!.localize("ui.common.undo"), + }, + }); } catch (err: any) { - showAlertDialog(this, { - text: `Moving failed: ${err.message}`, + this.lovelace.showToast({ + message: this.hass!.localize( + "ui.panel.lovelace.editor.move_card.error" + ), }); } }, diff --git a/src/translations/en.json b/src/translations/en.json index defd220801a8..79c47232d74f 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -5708,7 +5708,9 @@ "move_card": { "header": "Choose a view to move the card to", "strategy_error_title": "Impossible to move the card", - "strategy_error_text_strategy": "Moving a card to an auto generated view is not supported." + "strategy_error_text_strategy": "Moving a card to an auto generated view is not supported.", + "success": "Card moved successfully", + "error": "Error while moving card" }, "change_position": { "title": "Change card position", @@ -5726,11 +5728,7 @@ "add_badge": "Add badge", "add_card": "[%key:ui::panel::lovelace::editor::edit_card::add%]", "create_section": "Create section", - "default_section_title": "New section", - "imported_card_section_title_view": "Imported cards from ''{view_title}'' view", - "imported_card_section_title_default": "Imported cards from another view", - "imported_cards_title": "Imported cards", - "imported_cards_description": "These cards are imported from another view. They will only be displayed in edit mode. Move them into sections to display them in your view." + "default_section_title": "New section" }, "delete_section": { "title": "Delete section",