🎨 Auto-close browser page when publish service is closed https://github.com/siyuan-note/siyuan/issues/16587#issuecomment-3698421929 (#16804)

This commit is contained in:
Jeffrey Chen 2026-01-10 19:47:30 +08:00 committed by GitHub
parent 1aaabefe05
commit 840fd99bbb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 97 additions and 3 deletions

View file

@ -27,6 +27,7 @@ import {initMessage} from "./dialog/message";
import {getAllTabs} from "./layout/getAll";
import {getLocalStorage} from "./protyle/util/compatibility";
import {getSearch} from "./util/functions";
import {checkPublishServiceClosed} from "./util/processMessage";
import {hideAllElements} from "./protyle/ui/hideElements";
import {loadPlugins, reloadPlugin} from "./plugin/loader";
import "./assets/scss/base.scss";
@ -46,6 +47,9 @@ export class App {
public appId: string;
constructor() {
if (checkPublishServiceClosed()) {
return;
}
registerServiceWorker(`${Constants.SERVICE_WORKER_PATH}?v=${Constants.SIYUAN_VERSION}`);
addBaseURL();

View file

@ -18,6 +18,7 @@ import {hideKeyboardToolbar, showKeyboardToolbar} from "./util/keyboardToolbar";
import {getLocalStorage, writeText} from "../protyle/util/compatibility";
import {getCurrentEditor, openMobileFileById} from "./editor";
import {getSearch} from "../util/functions";
import {checkPublishServiceClosed} from "../util/processMessage";
import {initRightMenu} from "./menu";
import {openChangelog} from "../boot/openChangelog";
import {registerServiceWorker} from "../util/serviceWorker";
@ -37,6 +38,9 @@ class App {
public appId: string;
constructor() {
if (checkPublishServiceClosed()) {
return;
}
registerServiceWorker(`${Constants.SERVICE_WORKER_PATH}?v=${Constants.SIYUAN_VERSION}`);
addBaseURL();
this.appId = Constants.SIYUAN_APPID;

View file

@ -5,6 +5,7 @@ import {hideMessage, showMessage} from "../dialog/message";
import {setStorageVal} from "../protyle/util/compatibility";
import {Constants} from "../constants";
import {fetchPost} from "./fetch";
import {isBrowser} from "./functions";
export const processMessage = (response: IWebSocketData) => {
if ("msg" === response.cmd) {
@ -61,6 +62,10 @@ export const processMessage = (response: IWebSocketData) => {
}
return false;
}
if ("closepublishpage" === response.cmd) {
handlePublishServiceClosed(response.msg);
return false;
}
// 小于 0 为提示:-2 提示;-1 报错,大于 0 的错误需处理,等于 0 的为正常操作
if (response.code < 0) {
@ -70,3 +75,22 @@ export const processMessage = (response: IWebSocketData) => {
return response;
};
export const handlePublishServiceClosed = (msg: string) => {
if (isBrowser()) {
sessionStorage.setItem("siyuanPublishServiceClosed", msg || "");
window.location.reload();
}
};
export const checkPublishServiceClosed = (): boolean => {
if (isBrowser()) {
const publishServiceClosedMsg = sessionStorage.getItem("siyuanPublishServiceClosed");
if (publishServiceClosedMsg) {
sessionStorage.removeItem("siyuanPublishServiceClosed");
document.body.innerHTML = `<div style="display:flex;align-items:center;justify-content:center;height:100vh">${publishServiceClosedMsg}</div>`;
return true;
}
}
return false;
};