From d148565623d9f01651df0d3551184ccf2bf07843 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Wed, 16 Oct 2024 16:18:22 +0200 Subject: [PATCH 1/5] Allow to move card from other view to section view --- .../lovelace/components/hui-card-options.ts | 32 +++++++++++++++---- src/translations/en.json | 4 +-- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/panels/lovelace/components/hui-card-options.ts b/src/panels/lovelace/components/hui-card-options.ts index c5c5d8cc677d..8939dce3592d 100644 --- a/src/panels/lovelace/components/hui-card-options.ts +++ b/src/panels/lovelace/components/hui-card-options.ts @@ -28,7 +28,10 @@ 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 } from "../../../data/lovelace/config/view"; +import { + isStrategyView, + LovelaceViewConfig, +} from "../../../data/lovelace/config/view"; import { showAlertDialog, showPromptDialog, @@ -40,12 +43,14 @@ import { computeCardSize } from "../common/compute-card-size"; import { showEditCardDialog } from "../editor/card-editor/show-edit-card-dialog"; import { addCard, + addSection, deleteCard, moveCardToContainer, moveCardToIndex, } from "../editor/config-util"; import { LovelaceCardPath, + LovelaceContainerPath, findLovelaceItems, getLovelaceContainerPath, parseLovelaceCardPath, @@ -353,34 +358,49 @@ export class HuiCardOptions extends LitElement { allowDashboardChange: true, header: this.hass!.localize("ui.panel.lovelace.editor.move_card.header"), viewSelectedCallback: async (urlPath, selectedDashConfig, viewIndex) => { - const view = selectedDashConfig.views[viewIndex]; + let view = selectedDashConfig.views[viewIndex]; + let newConfig = selectedDashConfig; - if (!isStrategyView(view) && view.type === SECTIONS_VIEW_LAYOUT) { + if (isStrategyView(view)) { showAlertDialog(this, { title: this.hass!.localize( "ui.panel.lovelace.editor.move_card.error_title" ), text: this.hass!.localize( - "ui.panel.lovelace.editor.move_card.error_text_section" + "ui.panel.lovelace.editor.move_card.error_text_strategy" ), warning: true, }); 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 toPath: LovelaceContainerPath = isSectionsView + ? [viewIndex, view.sections!.length - 1] + : [viewIndex]; + if (urlPath === this.lovelace!.urlPath) { this.lovelace!.saveConfig( - moveCardToContainer(this.lovelace!.config, this.path!, [viewIndex]) + moveCardToContainer(newConfig, this.path!, toPath) ); showSaveSuccessToast(this, this.hass!); return; } try { const { cardIndex } = parseLovelaceCardPath(this.path!); + const card = this._cards[cardIndex]; await saveConfig( this.hass!, urlPath, - addCard(selectedDashConfig, [viewIndex], this._cards[cardIndex]) + addCard(newConfig, toPath, card) ); this.lovelace!.saveConfig( deleteCard(this.lovelace!.config, this.path!) diff --git a/src/translations/en.json b/src/translations/en.json index 1f1964330484..914a8bd0de19 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -5691,8 +5691,8 @@ }, "move_card": { "header": "Choose a view to move the card to", - "error_title": "Impossible to move the card", - "error_text_section": "Moving a card to a section view is not supported yet. Use copy/cut/paste instead." + "strategy_error_title": "Impossible to move the card", + "strategy_error_text_strategy": "Moving a card to a strategy view is not supported." }, "change_position": { "title": "Change card position", From 169a5f1f868885bbb8c01e49ddd10ced3286291a Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Tue, 22 Oct 2024 10:30:58 +0200 Subject: [PATCH 2/5] Update src/panels/lovelace/components/hui-card-options.ts Co-authored-by: Wendelin <12148533+wendevlin@users.noreply.github.com> --- src/panels/lovelace/components/hui-card-options.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/panels/lovelace/components/hui-card-options.ts b/src/panels/lovelace/components/hui-card-options.ts index 8939dce3592d..cac66c340445 100644 --- a/src/panels/lovelace/components/hui-card-options.ts +++ b/src/panels/lovelace/components/hui-card-options.ts @@ -30,7 +30,7 @@ import { LovelaceCardConfig } from "../../../data/lovelace/config/card"; import { saveConfig } from "../../../data/lovelace/config/types"; import { isStrategyView, - LovelaceViewConfig, + type LovelaceViewConfig, } from "../../../data/lovelace/config/view"; import { showAlertDialog, From 52fccba40632e3d28778c9c4ea99f3807168b7f5 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Tue, 22 Oct 2024 17:43:22 +0200 Subject: [PATCH 3/5] 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", From b55e10672c4da3b57bb6da2c90c22552a0e314fd Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Mon, 28 Oct 2024 14:22:43 +0100 Subject: [PATCH 4/5] Feedbacks --- .../lovelace/components/hui-card-options.ts | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/panels/lovelace/components/hui-card-options.ts b/src/panels/lovelace/components/hui-card-options.ts index 8b8667f7bf29..0a9a39be8e38 100644 --- a/src/panels/lovelace/components/hui-card-options.ts +++ b/src/panels/lovelace/components/hui-card-options.ts @@ -50,7 +50,7 @@ import { } from "../editor/config-util"; import { LovelaceCardPath, - LovelaceContainerPath, + type LovelaceContainerPath, findLovelaceItems, getLovelaceContainerPath, parseLovelaceCardPath, @@ -380,7 +380,7 @@ export class HuiCardOptions extends LitElement { let toPath: LovelaceContainerPath = [viewIndex]; - // If the view is a section view and has no i"mported cards" section, adds a default section. + // 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( @@ -391,15 +391,16 @@ export class HuiCardOptions extends LitElement { "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; + 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", From 04d2395c28e2873e12a5d1bac6f0d5eebcbc6524 Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Mon, 28 Oct 2024 15:30:20 +0100 Subject: [PATCH 5/5] Update src/translations/en.json Co-authored-by: Wendelin <12148533+wendevlin@users.noreply.github.com> --- src/translations/en.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/translations/en.json b/src/translations/en.json index 451bd71b22a2..206ea1fba91e 100644 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -5692,7 +5692,7 @@ "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 a strategy view is not supported." + "strategy_error_text_strategy": "Moving a card to an auto generated view is not supported." }, "change_position": { "title": "Change card position",