From 52fccba40632e3d28778c9c4ea99f3807168b7f5 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Tue, 22 Oct 2024 17:43:22 +0200 Subject: [PATCH] Move to dedicated section --- .../lovelace/components/hui-card-options.ts | 56 ++++++++++++++----- src/translations/en.json | 4 +- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/panels/lovelace/components/hui-card-options.ts b/src/panels/lovelace/components/hui-card-options.ts index cac66c340445..8b8667f7bf29 100644 --- a/src/panels/lovelace/components/hui-card-options.ts +++ b/src/panels/lovelace/components/hui-card-options.ts @@ -58,6 +58,7 @@ import { 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 { @@ -358,10 +359,11 @@ export class HuiCardOptions extends LitElement { allowDashboardChange: true, header: this.hass!.localize("ui.panel.lovelace.editor.move_card.header"), viewSelectedCallback: async (urlPath, selectedDashConfig, viewIndex) => { - let view = selectedDashConfig.views[viewIndex]; + const fromView = selectedDashConfig.views[this.path![0]]; + let toView = selectedDashConfig.views[viewIndex]; let newConfig = selectedDashConfig; - if (isStrategyView(view)) { + if (isStrategyView(toView)) { showAlertDialog(this, { title: this.hass!.localize( "ui.panel.lovelace.editor.move_card.error_title" @@ -374,19 +376,47 @@ export class HuiCardOptions extends LitElement { return; } - const isSectionsView = view.type === SECTIONS_VIEW_LAYOUT; - - // If the view is a section view and has no sections, add a default section. - if (isSectionsView && !view.sections?.length) { - const newSection = { type: "grid", cards: [] }; - newConfig = addSection(selectedDashConfig, viewIndex, newSection); - view = newConfig.views[viewIndex] as LovelaceViewConfig; + const isSectionsView = toView.type === SECTIONS_VIEW_LAYOUT; + + let toPath: LovelaceContainerPath = [viewIndex]; + + // If the view is a section view and has no i"mported 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?.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 = isSectionsView - ? [viewIndex, view.sections!.length - 1] - : [viewIndex]; - if (urlPath === this.lovelace!.urlPath) { this.lovelace!.saveConfig( moveCardToContainer(newConfig, this.path!, toPath) diff --git a/src/translations/en.json b/src/translations/en.json index 914a8bd0de19..451bd71b22a2 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -5710,7 +5710,9 @@ "add_badge": "Add badge", "add_card": "[%key:ui::panel::lovelace::editor::edit_card::add%]", "create_section": "Create section", - "default_section_title": "New 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" }, "delete_section": { "title": "Delete section",