From 87ed15734020663b47cd296030e54c2d809c6090 Mon Sep 17 00:00:00 2001 From: MrKBear Date: Sun, 24 Apr 2022 13:24:55 +0800 Subject: [PATCH] Add block shutdown hendel --- source/Component/HeaderBar/HeaderBar.tsx | 24 ++++++++++++++++++++++++ source/Context/Status.tsx | 2 ++ 2 files changed, 26 insertions(+) diff --git a/source/Component/HeaderBar/HeaderBar.tsx b/source/Component/HeaderBar/HeaderBar.tsx index 8009871..b69b679 100644 --- a/source/Component/HeaderBar/HeaderBar.tsx +++ b/source/Component/HeaderBar/HeaderBar.tsx @@ -126,6 +126,30 @@ class HeaderWindowsAction extends Component { @useStatusWithEvent("fileSave", "fileChange", "fileLoad") class HeaderBar extends Component { + private showCloseMessage = (e: BeforeUnloadEvent) => { + if (!this.props.status?.archive.isSaved) { + const message = I18N(this.props, "ZH_CH"); + (e || window.event).returnValue = message; // 兼容 Gecko + IE + return message; // 兼容 Gecko + Webkit, Safari, Chrome + } + } + + public componentDidMount() { + + if (this.props.setting?.platform === Platform.web) { + // 阻止页面关闭 + window.addEventListener("beforeunload", this.showCloseMessage); + } + } + + public componentWillUnmount() { + + if (this.props.setting?.platform === Platform.web) { + // 阻止页面关闭 + window.removeEventListener("beforeunload", this.showCloseMessage); + } + } + public render(): ReactNode { const { status, setting } = this.props; diff --git a/source/Context/Status.tsx b/source/Context/Status.tsx index ac88446..0d702ca 100644 --- a/source/Context/Status.tsx +++ b/source/Context/Status.tsx @@ -172,6 +172,8 @@ class Status extends Emitter { this.emit("fileChange"); } } + + // 设置文件修改状态 this.on("objectChange", handelFileChange); this.on("behaviorChange", handelFileChange); this.on("labelChange", handelFileChange);