Skip to content
Open
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
10 changes: 5 additions & 5 deletions src/panels/lovelace/editor/delete-badge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/panels/lovelace/editor/delete-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
74 changes: 72 additions & 2 deletions src/panels/lovelace/hui-root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -130,12 +136,22 @@ class HUIRoot extends LitElement {

@state() private _curView?: number | "hass-unused-entities";

private _configChangedByUndo = false;

private _viewCache?: Record<string, HUIView>;

private _viewScrollPositions: Record<string, number> = {};

private _restoreScroll = false;

private _undoRedoController = new UndoRedoController<LovelaceRawConfig>(
this,
{
apply: (config) => this._applyUndoRedo(config),
currentConfig: () => this.lovelace!.rawConfig,
}
);

private _debouncedConfigChanged: () => void;

private _conversation = memoizeOne((_components) =>
Expand All @@ -157,7 +173,29 @@ class HUIRoot extends LitElement {
const result: TemplateResult[] = [];
if (this._editMode) {
result.push(
html`<ha-button
html`<ha-icon-button
slot="toolbar-icon"
.path=${mdiUndo}
@click=${this._undo}
.disabled=${!this._undoRedoController.canUndo}
id="button-undo"
>
</ha-icon-button>
<ha-tooltip placement="bottom" for="button-undo">
${this.hass.localize("ui.common.undo")}
</ha-tooltip>
<ha-icon-button
slot="toolbar-icon"
.path=${mdiRedo}
@click=${this._redo}
.disabled=${!this._undoRedoController.canRedo}
id="button-redo"
>
</ha-icon-button>
<ha-tooltip placement="bottom" for="button-redo">
${this.hass.localize("ui.common.redo")}
</ha-tooltip>
<ha-button
appearance="filled"
size="small"
class="exit-edit-mode"
Expand Down Expand Up @@ -640,6 +678,24 @@ class HUIRoot extends LitElement {
window.history.scrollRestoration = "auto";
}

protected willUpdate(changedProperties: PropertyValues): void {
if (changedProperties.has("lovelace")) {
const oldLovelace = changedProperties.get("lovelace") as
| Lovelace
| undefined;

if (
oldLovelace &&
this.lovelace!.rawConfig !== oldLovelace!.rawConfig &&
!this._configChangedByUndo
) {
this._undoRedoController.commit(oldLovelace.rawConfig);
} else {
this._configChangedByUndo = false;
}
}
}

protected updated(changedProperties: PropertyValues): void {
super.updated(changedProperties);

Expand Down Expand Up @@ -1024,6 +1080,7 @@ class HUIRoot extends LitElement {

private _editModeDisable(): void {
this.lovelace!.setEditMode(false);
this._undoRedoController.reset();
}

private async _editDashboard() {
Expand Down Expand Up @@ -1202,6 +1259,19 @@ class HUIRoot extends LitElement {
showShortcutsDialog(this);
}

private _applyUndoRedo(config: LovelaceRawConfig) {
this._configChangedByUndo = true;
this.lovelace!.saveConfig(config);
}

private _undo() {
this._undoRedoController.undo();
}

private _redo() {
this._undoRedoController.redo();
}

static get styles(): CSSResultGroup {
return [
haStyle,
Expand Down
Loading