diff --git a/src/panels/lovelace/editor/delete-badge.ts b/src/panels/lovelace/editor/delete-badge.ts index 8ee3c1eba779..a425c4385c14 100644 --- a/src/panels/lovelace/editor/delete-badge.ts +++ b/src/panels/lovelace/editor/delete-badge.ts @@ -2,6 +2,7 @@ import type { HomeAssistant } from "../../../types"; import type { Lovelace } from "../types"; import { deleteBadge } from "./config-util"; import type { LovelaceCardPath } from "./lovelace-path"; +import { fireEvent } from "../../../common/dom/fire_event"; export interface DeleteBadgeParams { path: LovelaceCardPath; @@ -23,14 +24,13 @@ export async function performDeleteBadge( return; } - const action = async () => { - lovelace.saveConfig(oldConfig); - }; - lovelace.showToast({ message: hass.localize("ui.common.successfully_deleted"), duration: 8000, - action: { action, text: hass.localize("ui.common.undo") }, + action: { + action: () => fireEvent(window, "undo-change"), + text: hass.localize("ui.common.undo"), + }, }); } catch (err: any) { // eslint-disable-next-line no-console diff --git a/src/panels/lovelace/editor/delete-card.ts b/src/panels/lovelace/editor/delete-card.ts index 71c37b85e3e1..daf0536b56db 100644 --- a/src/panels/lovelace/editor/delete-card.ts +++ b/src/panels/lovelace/editor/delete-card.ts @@ -2,6 +2,7 @@ import type { HomeAssistant } from "../../../types"; import type { Lovelace } from "../types"; import { deleteCard } from "./config-util"; import type { LovelaceCardPath } from "./lovelace-path"; +import { fireEvent } from "../../../common/dom/fire_event"; export interface DeleteCardParams { path: LovelaceCardPath; @@ -23,14 +24,13 @@ export async function performDeleteCard( return; } - const action = async () => { - lovelace.saveConfig(oldConfig); - }; - lovelace.showToast({ message: hass.localize("ui.common.successfully_deleted"), duration: 8000, - action: { action, text: hass.localize("ui.common.undo") }, + action: { + action: () => fireEvent(window, "undo-change"), + text: hass.localize("ui.common.undo"), + }, }); } catch (err: any) { // eslint-disable-next-line no-console diff --git a/src/panels/lovelace/hui-root.ts b/src/panels/lovelace/hui-root.ts index 6e7bfacb8826..b4d89948fd6c 100644 --- a/src/panels/lovelace/hui-root.ts +++ b/src/panels/lovelace/hui-root.ts @@ -11,10 +11,12 @@ import { mdiMagnify, mdiPencil, mdiPlus, + mdiRedo, mdiRefresh, mdiRobot, mdiShape, mdiSofa, + mdiUndo, mdiViewDashboard, } from "@mdi/js"; import type { CSSResultGroup, PropertyValues, TemplateResult } from "lit"; @@ -50,7 +52,10 @@ import "../../components/ha-tab-group-tab"; import "../../components/ha-tooltip"; import { createAreaRegistryEntry } from "../../data/area_registry"; import type { LovelacePanelConfig } from "../../data/lovelace"; -import type { LovelaceConfig } from "../../data/lovelace/config/types"; +import type { + LovelaceConfig, + LovelaceRawConfig, +} from "../../data/lovelace/config/types"; import { isStrategyDashboard } from "../../data/lovelace/config/types"; import type { LovelaceViewConfig } from "../../data/lovelace/config/view"; import { @@ -92,6 +97,7 @@ import "./views/hui-view"; import type { HUIView } from "./views/hui-view"; import "./views/hui-view-background"; import "./views/hui-view-container"; +import { UndoRedoController } from "../../common/controllers/undo-redo-controller"; interface ActionItem { icon: string; @@ -130,12 +136,22 @@ class HUIRoot extends LitElement { @state() private _curView?: number | "hass-unused-entities"; + private _configChangedByUndo = false; + private _viewCache?: Record; private _viewScrollPositions: Record = {}; private _restoreScroll = false; + private _undoRedoController = new UndoRedoController( + this, + { + apply: (config) => this._applyUndoRedo(config), + currentConfig: () => this.lovelace!.rawConfig, + } + ); + private _debouncedConfigChanged: () => void; private _conversation = memoizeOne((_components) => @@ -157,7 +173,29 @@ class HUIRoot extends LitElement { const result: TemplateResult[] = []; if (this._editMode) { result.push( - html` + + + ${this.hass.localize("ui.common.undo")} + + + + + ${this.hass.localize("ui.common.redo")} + +