Demo:编辑本地文件时如果未保存关闭页面增加拦截提示
This commit is contained in:
parent
4fb9bd5135
commit
11f2078ee0
@ -208,7 +208,8 @@ export default {
|
|||||||
},
|
},
|
||||||
fileTreeVisible: false,
|
fileTreeVisible: false,
|
||||||
rootDirName: '',
|
rootDirName: '',
|
||||||
fileTreeExpand: true
|
fileTreeExpand: true,
|
||||||
|
waitingWriteToLocalFile: false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
@ -232,11 +233,13 @@ export default {
|
|||||||
this.computeToolbarShowThrottle = throttle(this.computeToolbarShow, 300)
|
this.computeToolbarShowThrottle = throttle(this.computeToolbarShow, 300)
|
||||||
window.addEventListener('resize', this.computeToolbarShowThrottle)
|
window.addEventListener('resize', this.computeToolbarShowThrottle)
|
||||||
this.$bus.$on('lang_change', this.computeToolbarShowThrottle)
|
this.$bus.$on('lang_change', this.computeToolbarShowThrottle)
|
||||||
|
window.addEventListener('beforeunload', this.onUnload)
|
||||||
},
|
},
|
||||||
beforeDestroy() {
|
beforeDestroy() {
|
||||||
this.$bus.$off('write_local_file', this.onWriteLocalFile)
|
this.$bus.$off('write_local_file', this.onWriteLocalFile)
|
||||||
window.removeEventListener('resize', this.computeToolbarShowThrottle)
|
window.removeEventListener('resize', this.computeToolbarShowThrottle)
|
||||||
this.$bus.$off('lang_change', this.computeToolbarShowThrottle)
|
this.$bus.$off('lang_change', this.computeToolbarShowThrottle)
|
||||||
|
window.removeEventListener('beforeunload', this.onUnload)
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
// 计算工具按钮如何显示
|
// 计算工具按钮如何显示
|
||||||
@ -269,11 +272,22 @@ export default {
|
|||||||
// 监听本地文件读写
|
// 监听本地文件读写
|
||||||
onWriteLocalFile(content) {
|
onWriteLocalFile(content) {
|
||||||
clearTimeout(this.timer)
|
clearTimeout(this.timer)
|
||||||
|
if (fileHandle && this.isHandleLocalFile) {
|
||||||
|
this.waitingWriteToLocalFile = true
|
||||||
|
}
|
||||||
this.timer = setTimeout(() => {
|
this.timer = setTimeout(() => {
|
||||||
this.writeLocalFile(content)
|
this.writeLocalFile(content)
|
||||||
}, 1000)
|
}, 1000)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
onUnload(e) {
|
||||||
|
if (this.waitingWriteToLocalFile) {
|
||||||
|
const msg = '存在未保存的数据'
|
||||||
|
e.returnValue = msg
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
// 加载本地文件树
|
// 加载本地文件树
|
||||||
async loadFileTreeNode(node, resolve) {
|
async loadFileTreeNode(node, resolve) {
|
||||||
try {
|
try {
|
||||||
@ -429,6 +443,7 @@ export default {
|
|||||||
// 写入本地文件
|
// 写入本地文件
|
||||||
async writeLocalFile(content) {
|
async writeLocalFile(content) {
|
||||||
if (!fileHandle || !this.isHandleLocalFile) {
|
if (!fileHandle || !this.isHandleLocalFile) {
|
||||||
|
this.waitingWriteToLocalFile = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (!this.isFullDataFile) {
|
if (!this.isFullDataFile) {
|
||||||
@ -438,6 +453,7 @@ export default {
|
|||||||
const writable = await fileHandle.createWritable()
|
const writable = await fileHandle.createWritable()
|
||||||
await writable.write(string)
|
await writable.write(string)
|
||||||
await writable.close()
|
await writable.close()
|
||||||
|
this.waitingWriteToLocalFile = false
|
||||||
},
|
},
|
||||||
|
|
||||||
// 创建本地文件
|
// 创建本地文件
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user