Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/managers/notification-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ class NotificationManager extends LitElement {

static get styles(): CSSResult {
return css`
:host {
ha-toast {
display: flex;
align-items: center;
justify-content: space-between;
}
mwc-button {
color: var(--primary-color);
Expand Down
34 changes: 34 additions & 0 deletions src/panels/lovelace/editor/config-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,40 @@ export const deleteCard = (
};
};

export const insertCard = (
config: LovelaceConfig,
path: [number, number],
cardConfig: LovelaceCardConfig
) => {
const [viewIndex, cardIndex] = path;
const views: LovelaceViewConfig[] = [];

config.views.forEach((viewConf, index) => {
if (index !== viewIndex) {
views.push(config.views[index]);
return;
}

const cards = viewConf.cards
? [
...viewConf.cards.slice(0, cardIndex),
cardConfig,
...viewConf.cards.slice(cardIndex),
]
: [cardConfig];

views.push({
...viewConf,
cards,
});
});

return {
...config,
views,
};
};

export const swapCard = (
config: LovelaceConfig,
path1: [number, number],
Expand Down
10 changes: 7 additions & 3 deletions src/panels/lovelace/editor/delete-card.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Lovelace } from "../types";
import { deleteCard } from "./config-util";
import { deleteCard, insertCard } from "./config-util";
import { showAlertDialog } from "../../../dialogs/generic/show-dialog-box";
import { HomeAssistant } from "../../../types";
import { showDeleteCardDialog } from "./card-editor/show-delete-card-dialog";
Expand All @@ -16,8 +16,12 @@ export async function confDeleteCard(
cardConfig,
deleteCard: async () => {
try {
await lovelace.saveConfig(deleteCard(lovelace.config, path));
showDeleteSuccessToast(element, hass!);
const newLovelace = deleteCard(lovelace.config, path);
await lovelace.saveConfig(newLovelace);
const action = async () => {
await lovelace.saveConfig(insertCard(newLovelace, path, cardConfig));
};
showDeleteSuccessToast(element, hass!, action);
} catch (err) {
showAlertDialog(element, {
text: `Deleting failed: ${err.message}`,
Expand Down
1 change: 1 addition & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@
"cancel": "Cancel",
"delete": "Delete",
"close": "Close",
"undo": "Undo",
"save": "Save",
"yes": "Yes",
"no": "No",
Expand Down
18 changes: 15 additions & 3 deletions src/util/toast-deleted-success.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { showToast } from "./toast";
import { HomeAssistant } from "../types";
import { ShowToastParams } from "../managers/notification-manager";

export const showDeleteSuccessToast = (el: HTMLElement, hass: HomeAssistant) =>
showToast(el, {
export const showDeleteSuccessToast = (
el: HTMLElement,
hass: HomeAssistant,
action?: () => void
) => {
const toastParams: ShowToastParams = {
message: hass!.localize("ui.common.successfully_deleted"),
});
};

if (action) {
toastParams.action = { action, text: hass!.localize("ui.common.undo") };
}

showToast(el, toastParams);
};