Demo:编辑本地文件时如果未保存关闭页面增加拦截提示

This commit is contained in:
街角小林
2024-07-29 17:20:21 +08:00
parent 4fb9bd5135
commit 11f2078ee0

View File

@@ -208,7 +208,8 @@ export default {
},
fileTreeVisible: false,
rootDirName: '',
fileTreeExpand: true
fileTreeExpand: true,
waitingWriteToLocalFile: false
}
},
computed: {
@@ -232,11 +233,13 @@ export default {
this.computeToolbarShowThrottle = throttle(this.computeToolbarShow, 300)
window.addEventListener('resize', this.computeToolbarShowThrottle)
this.$bus.$on('lang_change', this.computeToolbarShowThrottle)
window.addEventListener('beforeunload', this.onUnload)
},
beforeDestroy() {
this.$bus.$off('write_local_file', this.onWriteLocalFile)
window.removeEventListener('resize', this.computeToolbarShowThrottle)
this.$bus.$off('lang_change', this.computeToolbarShowThrottle)
window.removeEventListener('beforeunload', this.onUnload)
},
methods: {
// 计算工具按钮如何显示
@@ -269,11 +272,22 @@ export default {
// 监听本地文件读写
onWriteLocalFile(content) {
clearTimeout(this.timer)
if (fileHandle && this.isHandleLocalFile) {
this.waitingWriteToLocalFile = true
}
this.timer = setTimeout(() => {
this.writeLocalFile(content)
}, 1000)
},
onUnload(e) {
if (this.waitingWriteToLocalFile) {
const msg = '存在未保存的数据'
e.returnValue = msg
return msg
}
},
// 加载本地文件树
async loadFileTreeNode(node, resolve) {
try {
@@ -429,6 +443,7 @@ export default {
// 写入本地文件
async writeLocalFile(content) {
if (!fileHandle || !this.isHandleLocalFile) {
this.waitingWriteToLocalFile = false
return
}
if (!this.isFullDataFile) {
@@ -438,6 +453,7 @@ export default {
const writable = await fileHandle.createWritable()
await writable.write(string)
await writable.close()
this.waitingWriteToLocalFile = false
},
// 创建本地文件