Compare commits

...

1 Commits

Author SHA1 Message Date
Petar Petrov
cdc2085f5b Fix location handling for undo/redo 2025-11-25 16:32:31 +02:00
2 changed files with 8 additions and 6 deletions

View File

@@ -12,7 +12,7 @@ const UNDO_REDO_STACK_LIMIT = 75;
*/ */
export interface UndoRedoControllerConfig<ConfigType> { export interface UndoRedoControllerConfig<ConfigType> {
stackLimit?: number; stackLimit?: number;
currentConfig: () => ConfigType; currentConfig: (itemBeingApplied?: ConfigType) => ConfigType;
apply: (config: ConfigType) => void; apply: (config: ConfigType) => void;
} }
@@ -34,7 +34,9 @@ export class UndoRedoController<ConfigType> implements ReactiveController {
throw new Error("No apply function provided"); throw new Error("No apply function provided");
}; };
private readonly _currentConfig: () => ConfigType = () => { private readonly _currentConfig: (
itemBeingApplied?: ConfigType
) => ConfigType = () => {
throw new Error("No currentConfig function provided"); throw new Error("No currentConfig function provided");
}; };
@@ -105,8 +107,8 @@ export class UndoRedoController<ConfigType> implements ReactiveController {
if (this._undoStack.length === 0) { if (this._undoStack.length === 0) {
return; return;
} }
this._redoStack.push({ ...this._currentConfig() });
const config = this._undoStack.pop()!; const config = this._undoStack.pop()!;
this._redoStack.push({ ...this._currentConfig(config) });
this._apply(config); this._apply(config);
this._host.requestUpdate(); this._host.requestUpdate();
} }
@@ -119,8 +121,8 @@ export class UndoRedoController<ConfigType> implements ReactiveController {
if (this._redoStack.length === 0) { if (this._redoStack.length === 0) {
return; return;
} }
this._undoStack.push({ ...this._currentConfig() });
const config = this._redoStack.pop()!; const config = this._redoStack.pop()!;
this._undoStack.push({ ...this._currentConfig(config) });
this._apply(config); this._apply(config);
this._host.requestUpdate(); this._host.requestUpdate();
} }

View File

@@ -151,8 +151,8 @@ class HUIRoot extends LitElement {
private _undoRedoController = new UndoRedoController<UndoStackItem>(this, { private _undoRedoController = new UndoRedoController<UndoStackItem>(this, {
apply: (config) => this._applyUndoRedo(config), apply: (config) => this._applyUndoRedo(config),
currentConfig: () => ({ currentConfig: (itemBeingApplied) => ({
location: this.route!.path, location: itemBeingApplied?.location ?? this.route!.path,
config: this.lovelace!.rawConfig, config: this.lovelace!.rawConfig,
}), }),
}); });