From 11f2078ee0e6aa823c0b7c2aa5cc74c77ec3a35a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=97=E8=A7=92=E5=B0=8F=E6=9E=97?= <1013335014@qq.com> Date: Mon, 29 Jul 2024 17:20:21 +0800 Subject: [PATCH] =?UTF-8?q?Demo=EF=BC=9A=E7=BC=96=E8=BE=91=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E6=96=87=E4=BB=B6=E6=97=B6=E5=A6=82=E6=9E=9C=E6=9C=AA?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E5=85=B3=E9=97=AD=E9=A1=B5=E9=9D=A2=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=8B=A6=E6=88=AA=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/pages/Edit/components/Toolbar.vue | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/web/src/pages/Edit/components/Toolbar.vue b/web/src/pages/Edit/components/Toolbar.vue index d9367a6b..5de18d5d 100644 --- a/web/src/pages/Edit/components/Toolbar.vue +++ b/web/src/pages/Edit/components/Toolbar.vue @@ -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 }, // 创建本地文件