@@ -11,10 +11,12 @@ import {
1111 mdiMagnify ,
1212 mdiPencil ,
1313 mdiPlus ,
14+ mdiRedo ,
1415 mdiRefresh ,
1516 mdiRobot ,
1617 mdiShape ,
1718 mdiSofa ,
19+ mdiUndo ,
1820 mdiViewDashboard ,
1921} from "@mdi/js" ;
2022import type { CSSResultGroup , PropertyValues , TemplateResult } from "lit" ;
@@ -50,7 +52,10 @@ import "../../components/ha-tab-group-tab";
5052import "../../components/ha-tooltip" ;
5153import { createAreaRegistryEntry } from "../../data/area_registry" ;
5254import type { LovelacePanelConfig } from "../../data/lovelace" ;
53- import type { LovelaceConfig } from "../../data/lovelace/config/types" ;
55+ import type {
56+ LovelaceConfig ,
57+ LovelaceRawConfig ,
58+ } from "../../data/lovelace/config/types" ;
5459import { isStrategyDashboard } from "../../data/lovelace/config/types" ;
5560import type { LovelaceViewConfig } from "../../data/lovelace/config/view" ;
5661import {
@@ -92,6 +97,7 @@ import "./views/hui-view";
9297import type { HUIView } from "./views/hui-view" ;
9398import "./views/hui-view-background" ;
9499import "./views/hui-view-container" ;
100+ import { UndoRedoMixin } from "../../mixins/undo-redo-mixin" ;
95101
96102interface ActionItem {
97103 icon : string ;
@@ -114,7 +120,9 @@ interface SubActionItem {
114120}
115121
116122@customElement ( "hui-root" )
117- class HUIRoot extends LitElement {
123+ class HUIRoot extends UndoRedoMixin < typeof LitElement , LovelaceRawConfig > (
124+ LitElement
125+ ) {
118126 @property ( { attribute : false } ) public panel ?: PanelInfo < LovelacePanelConfig > ;
119127
120128 @property ( { attribute : false } ) public hass ! : HomeAssistant ;
@@ -130,6 +138,8 @@ class HUIRoot extends LitElement {
130138
131139 @state ( ) private _curView ?: number | "hass-unused-entities" ;
132140
141+ private _configChangedByUndo = false ;
142+
133143 private _viewCache ?: Record < string , HUIView > ;
134144
135145 private _viewScrollPositions : Record < string , number > = { } ;
@@ -157,7 +167,29 @@ class HUIRoot extends LitElement {
157167 const result : TemplateResult [ ] = [ ] ;
158168 if ( this . _editMode ) {
159169 result . push (
160- html `<ha- butto n
170+ html `<ha- icon- butto n
171+ slot= "toolbar-icon"
172+ .path = ${ mdiUndo }
173+ @click = ${ this . undo }
174+ .disabled = ${ ! this . canUndo }
175+ id= "button-undo"
176+ >
177+ </ ha- icon- butto n>
178+ <ha- to oltip placement= "bottom" for = "button-undo" >
179+ ${ this . hass . localize ( "ui.common.undo" ) }
180+ </ ha- to oltip>
181+ <ha- icon- butto n
182+ slot= "toolbar-icon"
183+ .path = ${ mdiRedo }
184+ @click = ${ this . redo }
185+ .disabled = ${ ! this . canRedo }
186+ id= "button-redo"
187+ >
188+ </ ha- icon- butto n>
189+ <ha- to oltip placement= "bottom" for = "button-redo" >
190+ ${ this . hass . localize ( "ui.common.redo" ) }
191+ </ ha- to oltip>
192+ <ha- butto n
161193 appearance= "filled"
162194 size = "small"
163195 class = "exit-edit-mode"
@@ -640,6 +672,24 @@ class HUIRoot extends LitElement {
640672 window . history . scrollRestoration = "auto" ;
641673 }
642674
675+ protected willUpdate ( changedProperties : PropertyValues ) : void {
676+ if ( changedProperties . has ( "lovelace" ) ) {
677+ const oldLovelace = changedProperties . get ( "lovelace" ) as
678+ | Lovelace
679+ | undefined ;
680+
681+ if (
682+ oldLovelace &&
683+ this . lovelace ! . rawConfig !== oldLovelace ! . rawConfig &&
684+ ! this . _configChangedByUndo
685+ ) {
686+ this . pushToUndo ( oldLovelace . rawConfig ) ;
687+ } else {
688+ this . _configChangedByUndo = false ;
689+ }
690+ }
691+ }
692+
643693 protected updated ( changedProperties : PropertyValues ) : void {
644694 super . updated ( changedProperties ) ;
645695
@@ -1024,6 +1074,7 @@ class HUIRoot extends LitElement {
10241074
10251075 private _editModeDisable ( ) : void {
10261076 this . lovelace ! . setEditMode ( false ) ;
1077+ this . clearUndoRedo ( ) ;
10271078 }
10281079
10291080 private async _editDashboard ( ) {
@@ -1202,6 +1253,15 @@ class HUIRoot extends LitElement {
12021253 showShortcutsDialog ( this ) ;
12031254 }
12041255
1256+ protected get currentConfig ( ) {
1257+ return this . lovelace ?. rawConfig ;
1258+ }
1259+
1260+ protected applyUndoRedo ( config : LovelaceRawConfig ) {
1261+ this . _configChangedByUndo = true ;
1262+ this . lovelace ! . saveConfig ( config ) ;
1263+ }
1264+
12051265 static get styles ( ) : CSSResultGroup {
12061266 return [
12071267 haStyle ,
0 commit comments