Warning when leave without save

This commit is contained in:
Paulo Menezes 2020-01-02 20:54:10 -03:00
parent 9e6db9a31c
commit 54d466de60

View file

@ -359,6 +359,8 @@ function save() {
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(items)); localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(items));
} }
window.removeEventListener("beforeunload", onUnload);
} }
function restore() { function restore() {
@ -373,6 +375,18 @@ function restore() {
} }
} }
function onUnload(event: BeforeUnloadEvent) {
event.preventDefault();
const confirmationMessage = "excalibur";
event.returnValue = confirmationMessage;
return confirmationMessage;
}
function addOnBeforeUnload() {
window.addEventListener("beforeunload", onUnload);
}
type AppState = { type AppState = {
draggingElement: ExcaliburElement | null; draggingElement: ExcaliburElement | null;
elementType: string; elementType: string;
@ -388,6 +402,7 @@ class App extends React.Component<{}, AppState> {
public componentWillUnmount() { public componentWillUnmount() {
document.removeEventListener("keydown", this.onKeyDown, false); document.removeEventListener("keydown", this.onKeyDown, false);
window.removeEventListener("beforeunload", onUnload);
} }
public state: AppState = { public state: AppState = {
@ -408,6 +423,7 @@ class App extends React.Component<{}, AppState> {
drawScene(); drawScene();
event.preventDefault(); event.preventDefault();
} else if (event.key === "Backspace") { } else if (event.key === "Backspace") {
addOnBeforeUnload();
deleteSelectedElements(); deleteSelectedElements();
drawScene(); drawScene();
event.preventDefault(); event.preventDefault();
@ -417,6 +433,8 @@ class App extends React.Component<{}, AppState> {
event.key === "ArrowUp" || event.key === "ArrowUp" ||
event.key === "ArrowDown" event.key === "ArrowDown"
) { ) {
addOnBeforeUnload();
const step = event.shiftKey ? 5 : 1; const step = event.shiftKey ? 5 : 1;
elements.forEach(element => { elements.forEach(element => {
if (element.isSelected) { if (element.isSelected) {
@ -547,6 +565,7 @@ class App extends React.Component<{}, AppState> {
parsedElement.y += 10; parsedElement.y += 10;
generateDraw(parsedElement); generateDraw(parsedElement);
elements.push(parsedElement); elements.push(parsedElement);
addOnBeforeUnload();
}); });
drawScene(); drawScene();
} }
@ -598,6 +617,8 @@ class App extends React.Component<{}, AppState> {
if (isDraggingElements) { if (isDraggingElements) {
document.documentElement.style.cursor = "move"; document.documentElement.style.cursor = "move";
} }
} else {
addOnBeforeUnload();
} }
if (isTextElement(element)) { if (isTextElement(element)) {
@ -658,6 +679,7 @@ class App extends React.Component<{}, AppState> {
lastX = x; lastX = x;
lastY = y; lastY = y;
drawScene(); drawScene();
addOnBeforeUnload();
return; return;
} }
} }