-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent leaving the editor if there are unsaved changes (#23170)
* Prevent leaving the editor if there are unsaved changes * Process code review * use first composePath target * fix function calls * Use query instead * Remove id on sidebar * suggestions --------- Co-authored-by: Bram Kragten <[email protected]>
- Loading branch information
Showing
5 changed files
with
94 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { LitElement, PropertyValues } from "lit"; | ||
import { isNavigationClick } from "../common/dom/is-navigation-click"; | ||
import type { Constructor } from "../types"; | ||
|
||
export const PreventUnsavedMixin = <T extends Constructor<LitElement>>( | ||
superClass: T | ||
) => | ||
class extends superClass { | ||
private _handleClick = async (e: MouseEvent) => { | ||
// get the right target, otherwise the composedPath would return <home-assistant> in the new event | ||
const target = e.composedPath()[0]; | ||
if (!isNavigationClick(e)) { | ||
return; | ||
} | ||
|
||
const result = await this.promptDiscardChanges(); | ||
if (result) { | ||
this._removeListeners(); | ||
if (target) { | ||
const newEvent = new MouseEvent(e.type, e); | ||
target.dispatchEvent(newEvent); | ||
} | ||
} | ||
}; | ||
|
||
private _handleUnload = (e: BeforeUnloadEvent) => e.preventDefault(); | ||
|
||
private _removeListeners() { | ||
window.removeEventListener("click", this._handleClick, true); | ||
window.removeEventListener("beforeunload", this._handleUnload); | ||
} | ||
|
||
public willUpdate(changedProperties: PropertyValues): void { | ||
super.willUpdate(changedProperties); | ||
|
||
if (this.isDirty) { | ||
window.addEventListener("click", this._handleClick, true); | ||
window.addEventListener("beforeunload", this._handleUnload); | ||
} else { | ||
this._removeListeners(); | ||
} | ||
} | ||
|
||
public disconnectedCallback(): void { | ||
super.disconnectedCallback(); | ||
|
||
this._removeListeners(); | ||
} | ||
|
||
protected get isDirty(): boolean { | ||
return false; | ||
} | ||
|
||
protected async promptDiscardChanges(): Promise<boolean> { | ||
return true; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters