feat: confirm before closing the browser tab (#723)

when the editor is open and file is changed
This commit is contained in:
Stefan Dej authored and GitHub committed 2022-03-20 01:45:06 +01:00
1 parent 8a00b373d5
commit 1a68218267
3 files changed
+36 -8

No files matched your search

+27 -7
View File
@@ -17,7 +17,7 @@
(filepath ? filepath.slice(1) + '/' : '') +
filename +
' ' +
(isWriteable ? changed : '(' + $t('Editor.FileReadOnly') + ')')
(isWriteable ? changedOutput : '(' + $t('Editor.FileReadOnly') + ')')
">
<template #buttons>
<v-btn
@@ -129,9 +129,9 @@
</template>
<script lang="ts">
import { Component, Mixins } from 'vue-property-decorator'
import { Component, Mixins, Watch } from 'vue-property-decorator'
import BaseMixin from '@/components/mixins/base'
import { formatFilesize } from '@/plugins/helpers'
import { formatFilesize, windowBeforeUnloadFunction } from '@/plugins/helpers'
import Panel from '@/components/ui/Panel.vue'
import CodemirrorAsync from '@/components/inputs/CodemirrorAsync'
import {
@@ -171,7 +171,11 @@ export default class TheEditor extends Mixins(BaseMixin) {
}
get changed() {
return this.$store.state.editor.changed ? '*' : ''
return this.$store.state.editor.changed ?? false
}
get changedOutput() {
return this.changed ? '*' : ''
}
get show() {
@@ -243,16 +247,24 @@ export default class TheEditor extends Mixins(BaseMixin) {
return null
}
get confirmUnsavedChanges() {
return this.$store.state.gui.editor.confirmUnsavedChanges ?? false
}
get escToClose() {
return this.$store.state.gui.editor.escToClose ?? false
}
cancelDownload() {
this.$store.dispatch('editor/cancelLoad')
}
escClose() {
if (this.$store.state.gui.editor.escToClose) this.close()
if (this.escToClose) this.close()
}
close() {
if (this.$store.state.gui.editor.confirmUnsavedChanges) this.promptUnsavedChanges()
if (this.confirmUnsavedChanges) this.promptUnsavedChanges()
else this.$store.dispatch('editor/close')
}
@@ -262,7 +274,7 @@ export default class TheEditor extends Mixins(BaseMixin) {
}
promptUnsavedChanges() {
if (!this.$store.state.editor.changed || !this.isWriteable) this.$store.dispatch('editor/close')
if (!this.changed || !this.isWriteable) this.$store.dispatch('editor/close')
else this.dialogConfirmChange = true
}
@@ -274,5 +286,13 @@ export default class TheEditor extends Mixins(BaseMixin) {
restartServiceName: restartServiceName,
})
}
@Watch('changed')
changedChanged(newVal: boolean) {
if (this.confirmUnsavedChanges) {
if (newVal) window.addEventListener('beforeunload', windowBeforeUnloadFunction)
else window.removeEventListener('beforeunload', windowBeforeUnloadFunction)
}
}
}
</script>
+5
View File
@@ -263,3 +263,8 @@ export function getMacroParams(macro: { gcode: string }): PrinterStateMacroParam
return ret
}
export function windowBeforeUnloadFunction(e: BeforeUnloadEvent) {
e.preventDefault()
e.returnValue = ''
}
+4 -1
View File
@@ -5,13 +5,14 @@ import axios from 'axios'
import { sha256 } from 'js-sha256'
import Vue from 'vue'
import i18n from '@/plugins/i18n'
import { windowBeforeUnloadFunction } from '@/plugins/helpers'
export const actions: ActionTree<EditorState, RootState> = {
reset({ commit }) {
commit('reset')
},
openFile({ state, dispatch, commit, rootGetters }, payload) {
openFile({ state, dispatch, commit, rootGetters, rootState }, payload) {
const fullFilepath = payload.root + payload.path + '/' + payload.filename
const url = rootGetters['socket/getUrl'] + '/server/files/' + encodeURI(fullFilepath) + `?${Date.now()}`
@@ -173,6 +174,8 @@ export const actions: ActionTree<EditorState, RootState> = {
close({ commit }) {
commit('reset')
window.removeEventListener('beforeunload', windowBeforeUnloadFunction)
},
updateSourcecode({ commit }, payload) {