Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Remove invalid editors from floating windows https://github.com/siyuan-note/siyuan/issues/18649

Danielcc30c6a9fb19
diff --git a/app/src/block/Panel.ts b/app/src/block/Panel.ts
index 3a51dbd39f..34e48fa2eb 100644
--- a/app/src/block/Panel.ts
+++ b/app/src/block/Panel.ts
@@ -19,6 +19,12 @@ import {checkFold} from "../util/noRelyPCFunction";
import {updateHotkeyAfterTip} from "../protyle/util/compatibility";
import {getTopBarHeight} from "../layout/getTopBarHeight";
import {activateAVLocateWithRetry} from "../protyle/render/av/locate";
+import {
+ IBlockPanelItemInfo,
+ IBlockPanelRemovalOptions,
+ matchesBlockPanelRemoval,
+ planBlockPanelRemoval
+} from "./panelRemoval";
const BLOCK_PANEL_EDITOR_MIN_HEIGHT = 155;
@@ -36,6 +42,10 @@ export class BlockPanel {
private observerLoad: IntersectionObserver;
private originalRefBlockIDs: IObject;
private editorResizeCleanup?: () => void;
+ private refDefElements = new Map<IRefDefs, HTMLElement>();
+ private refDefEditors = new Map<IRefDefs, Protyle>();
+ private refDefInfos = new Map<IRefDefs, IBlockPanelItemInfo>();
+ private destroying = false;
// x,y 和 targetElement 二选一必传
constructor(options: {
@@ -186,6 +196,97 @@ export class BlockPanel {
return this.editors.find(item => item.protyle.element === editorElement);
}
+ private updateEditorIndexes() {
+ this.refDefs.forEach((refDef, index) => {
+ this.refDefElements.get(refDef)?.setAttribute("data-index", index.toString());
+ });
+ }
+
+ private removeRefDef(refDef: IRefDefs) {
+ if (!this.element || !this.refDefs.includes(refDef)) {
+ return;
+ }
+ this.editorResizeCleanup?.();
+ const editor = this.refDefEditors.get(refDef);
+ if (editor) {
+ this.refDefEditors.delete(refDef);
+ const editorIndex = this.editors.indexOf(editor);
+ if (editorIndex > -1) {
+ this.editors.splice(editorIndex, 1);
+ }
+ hideElements(["util"], editor.protyle);
+ editor.destroy();
+ }
+ const editorElement = this.refDefElements.get(refDef);
+ if (editorElement) {
+ this.observerLoad?.unobserve(editorElement);
+ }
+ if (editorElement?.nextElementSibling?.classList.contains("block__edit-resize")) {
+ editorElement.nextElementSibling.remove();
+ }
+ editorElement?.remove();
+ this.refDefElements.delete(refDef);
+ this.refDefInfos.delete(refDef);
+ this.refDefs.splice(this.refDefs.indexOf(refDef), 1);
+ if (this.refDefs.length === 0) {
+ this.destroy();
+ return;
+ }
+ this.updateEditorIndexes();
+ if (this.refDefs.length === 1) {
+ this.element.querySelectorAll(".block__edit-resize").forEach(item => item.remove());
+ const remainingEditor = this.refDefEditors.get(this.refDefs[0]);
+ if (remainingEditor) {
+ this.setAutoEditorHeight(remainingEditor);
+ this.resizeEditor(remainingEditor);
+ }
+ }
+ }
+
+ private resolveRemoval(refDef: IRefDefs, options: IBlockPanelRemovalOptions) {
+ if (!this.element || !this.refDefs.includes(refDef)) {
+ return;
+ }
+ fetchPost("/api/block/getBlockInfo", {id: refDef.refID}, (response) => {
+ if (!this.element || !this.refDefs.includes(refDef)) {
+ return;
+ }
+ if (response.code === 3) {
+ return;
+ }
+ if (response.code !== 0) {
+ this.removeRefDef(refDef);
+ return;
+ }
+ const info = {
+ notebookId: response.data.box,
+ rootID: response.data.rootID,
+ };
+ this.refDefInfos.set(refDef, info);
+ if (matchesBlockPanelRemoval(info, options)) {
+ this.removeRefDef(refDef);
+ }
+ });
+ }
+
+ public removeEditors(options: IBlockPanelRemovalOptions) {
+ if (!this.element) {
+ return;
+ }
+ const removalPlan = planBlockPanelRemoval(this.refDefs, (refDef) => {
+ const editor = this.refDefEditors.get(refDef);
+ const cachedInfo = this.refDefInfos.get(refDef);
+ return editor ? {
+ notebookId: editor.protyle.notebookId || cachedInfo?.notebookId,
+ rootID: editor.protyle.block.rootID || cachedInfo?.rootID,
+ } : cachedInfo;
+ }, options);
+ removalPlan.removeItems.forEach(refDef => this.removeRefDef(refDef));
+ if (this.element) {
+ removalPlan.unresolvedItems.forEach(refDef => this.resolveRemoval(refDef, options));
+ }
+ }
+
private bindEditorResize() {
const contentElement = this.element.querySelector(".block__content") as HTMLElement;
contentElement.querySelectorAll(".block__edit-resize").forEach((resizeElement: HTMLElement) => {
@@ -284,17 +385,31 @@ export class BlockPanel {
}
private initProtyle(editorElement: HTMLElement, afterCB?: () => void) {
- const index = parseInt(editorElement.getAttribute("data-index"));
- fetchPost("/api/block/getBlockInfo", {id: this.refDefs[index].refID}, (response) => {
+ if (!editorElement.isConnected) {
+ return;
+ }
+ const refDef = this.refDefs[parseInt(editorElement.getAttribute("data-index"))];
+ if (!refDef) {
+ return;
+ }
+ fetchPost("/api/block/getBlockInfo", {id: refDef.refID}, (response) => {
+ if (!this.element || !this.refDefs.includes(refDef) || !editorElement.isConnected) {
+ return;
+ }
if (response.code === 3) {
showMessage(response.msg);
return;
}
- if (!this.targetElement && typeof this.x === "undefined" && typeof this.y === "undefined") {
+ if (response.code !== 0) {
+ this.removeRefDef(refDef);
return;
}
+ this.refDefInfos.set(refDef, {
+ notebookId: response.data.box,
+ rootID: response.data.rootID,
+ });
const action: TProtyleAction[] = [];
- if (response.data.rootID !== this.refDefs[index].refID) {
+ if (response.data.rootID !== refDef.refID) {
action.push(Constants.CB_GET_ALL);
} else {
action.push(Constants.CB_GET_CONTEXT);
@@ -304,12 +419,12 @@ export class BlockPanel {
if (this.isBacklink) {
action.push(Constants.CB_GET_BACKLINK);
}
- const isDocument = response.data.rootID === this.refDefs[index].refID;
+ const isDocument = response.data.rootID === refDef.refID;
let isInitialRender = true;
const editor = new Protyle(this.app, editorElement, {
databaseAttr: true,
- blockId: this.refDefs[index].refID,
- defIds: this.refDefs[index].defIDs || [],
+ blockId: refDef.refID,
+ defIds: refDef.defIDs || [],
originalRefBlockIDs: this.isBacklink ? this.originalRefBlockIDs : undefined,
action,
render: {
@@ -321,7 +436,9 @@ export class BlockPanel {
},
typewriterMode: false,
after: (editor) => {
- const refDef = this.refDefs[index];
+ if (!this.element || !this.refDefs.includes(refDef) || !editor.protyle.element.isConnected) {
+ return;
+ }
if (refDef.avItemID) {
activateAVLocateWithRetry(editor.protyle, refDef.refID, {
itemID: refDef.avItemID,
@@ -332,7 +449,7 @@ export class BlockPanel {
persistView: false,
});
}
- if (response.data.rootID !== this.refDefs[index].refID) {
+ if (response.data.rootID !== refDef.refID) {
editor.protyle.breadcrumb.element.parentElement.lastElementChild.classList.remove("fn__none");
}
if (afterCB) {
@@ -359,11 +476,16 @@ export class BlockPanel {
editor.protyle.scroll.element.parentElement.setAttribute("style", `--b3-dynamicscroll-width:${Math.min(editor.protyle.contentElement.clientHeight - 49, 200)}px;`);
}
});
+ this.refDefEditors.set(refDef, editor);
this.editors.push(editor);
});
}
public destroy() {
+ if (!this.element || this.destroying) {
+ return;
+ }
+ this.destroying = true;
this.editorResizeCleanup?.();
this.observerResize?.disconnect();
this.observerLoad?.disconnect();
@@ -381,6 +503,9 @@ export class BlockPanel {
});
this.editors = [];
}
+ this.refDefElements.clear();
+ this.refDefEditors.clear();
+ this.refDefInfos.clear();
const level = parseInt(this.element.dataset.level);
this.element.remove();
this.element = undefined;
@@ -456,6 +581,7 @@ export class BlockPanel {
});
const topBarHeight = getTopBarHeight();
this.element.querySelectorAll(".block__edit").forEach((item: HTMLElement, index) => {
+ this.refDefElements.set(this.refDefs[index], item);
if (index < 5) {
this.initProtyle(item, index === 0 ? () => {
if (!document.contains(this.element)) {
@@ -519,3 +645,14 @@ export class BlockPanel {
});
}
}
+
+export const removeBlockPanelEditors = (options: {notebookId?: string, rootIDs?: string[]}) => {
+ const removalOptions = {
+ notebookId: options.notebookId,
+ rootIDs: new Set(options.rootIDs || []),
+ };
+ if (!removalOptions.notebookId && removalOptions.rootIDs.size === 0) {
+ return;
+ }
+ [...window.siyuan.blockPanels].forEach(item => item.removeEditors(removalOptions));
+};
diff --git a/app/src/block/panelRemoval.test.ts b/app/src/block/panelRemoval.test.ts
new file mode 100644
index 0000000000..81f29f0418
--- /dev/null
+++ b/app/src/block/panelRemoval.test.ts
@@ -0,0 +1,63 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {matchesBlockPanelRemoval, planBlockPanelRemoval} from "./panelRemoval";
+
+describe("matchesBlockPanelRemoval", () => {
+ it("matches editors from a closed notebook", () => {
+ assert.equal(matchesBlockPanelRemoval({
+ notebookId: "20260811000000-notebook",
+ rootID: "20260811000000-document",
+ }, {
+ notebookId: "20260811000000-notebook",
+ rootIDs: new Set(),
+ }), true);
+ });
+
+ it("matches editors from removed root documents", () => {
+ assert.equal(matchesBlockPanelRemoval({
+ notebookId: "20260811000000-notebook",
+ rootID: "20260811000000-document",
+ }, {
+ rootIDs: new Set(["20260811000000-document"]),
+ }), true);
+ });
+
+ it("keeps editors outside the removal scope", () => {
+ assert.equal(matchesBlockPanelRemoval({
+ notebookId: "20260811000000-notebook-a",
+ rootID: "20260811000000-document-a",
+ }, {
+ notebookId: "20260811000000-notebook-b",
+ rootIDs: new Set(["20260811000000-document-b"]),
+ }), false);
+ assert.equal(matchesBlockPanelRemoval(undefined, {
+ rootIDs: new Set(["20260811000000-document-b"]),
+ }), false);
+ });
+});
+
+describe("planBlockPanelRemoval", () => {
+ it("separates matching, retained, and unresolved editors", () => {
+ const items = ["removed", "retained", "unresolved"];
+ const infos = new Map([
+ ["removed", {notebookId: "20260811000000-notebook-a"}],
+ ["retained", {notebookId: "20260811000000-notebook-b"}],
+ ]);
+ assert.deepEqual(planBlockPanelRemoval(items, item => infos.get(item), {
+ notebookId: "20260811000000-notebook-a",
+ rootIDs: new Set(),
+ }), {
+ removeItems: ["removed"],
+ unresolvedItems: ["unresolved"],
+ });
+ });
+
+ it("returns an empty plan when the panel has no editors", () => {
+ assert.deepEqual(planBlockPanelRemoval([], () => undefined, {
+ rootIDs: new Set(["20260811000000-document"]),
+ }), {
+ removeItems: [],
+ unresolvedItems: [],
+ });
+ });
+});
diff --git a/app/src/block/panelRemoval.ts b/app/src/block/panelRemoval.ts
new file mode 100644
index 0000000000..8651a65cc6
--- /dev/null
+++ b/app/src/block/panelRemoval.ts
@@ -0,0 +1,34 @@
+export interface IBlockPanelItemInfo {
+ notebookId?: string;
+ rootID?: string;
+}
+
+export interface IBlockPanelRemovalOptions {
+ notebookId?: string;
+ rootIDs: ReadonlySet<string>;
+}
+
+export const matchesBlockPanelRemoval = (info: IBlockPanelItemInfo | undefined,
+ options: IBlockPanelRemovalOptions) => {
+ if (!info) {
+ return false;
+ }
+ return (!!options.notebookId && info.notebookId === options.notebookId) ||
+ (!!info.rootID && options.rootIDs.has(info.rootID));
+};
+
+export const planBlockPanelRemoval = <T>(items: readonly T[],
+ getInfo: (item: T) => IBlockPanelItemInfo | undefined,
+ options: IBlockPanelRemovalOptions) => {
+ const removeItems: T[] = [];
+ const unresolvedItems: T[] = [];
+ items.forEach((item) => {
+ const info = getInfo(item);
+ if (matchesBlockPanelRemoval(info, options)) {
+ removeItems.push(item);
+ } else if (!info) {
+ unresolvedItems.push(item);
+ }
+ });
+ return {removeItems, unresolvedItems};
+};
diff --git a/app/src/index.ts b/app/src/index.ts
index 2f37c79dd0..792c54bb1e 100644
--- a/app/src/index.ts
+++ b/app/src/index.ts
@@ -53,6 +53,7 @@ import {reloadSync} from "./util/reloadSync";
import {setTitle} from "./util/processTitle";
import {ensureUILayout} from "./util/ensureUILayout";
import {applyEntryVisibility} from "./config/entryVisibility/runtime";
+import {removeBlockPanelEditors} from "./block/Panel";
export class App {
public plugins: import("./plugin").Plugin[] = [];
@@ -171,6 +172,7 @@ export class App {
break;
case "closeBox":
case "removeBox":
+ removeBlockPanelEditors({notebookId: data.data.box});
getAllTabs().forEach((tab) => {
if (tab.headElement) {
const initTab = tab.headElement.getAttribute("data-initdata");
@@ -184,6 +186,7 @@ export class App {
});
break;
case "removeDoc":
+ removeBlockPanelEditors({rootIDs: data.data.ids});
getAllTabs().forEach((tab) => {
if (tab.headElement) {
const initTab = tab.headElement.getAttribute("data-initdata");
diff --git a/app/src/util/reloadSync.ts b/app/src/util/reloadSync.ts
index b3b84f7438..36337acf49 100644
--- a/app/src/util/reloadSync.ts
+++ b/app/src/util/reloadSync.ts
@@ -9,6 +9,9 @@ import {getAllModels} from "../layout/getAll";
import {setStorageVal} from "../protyle/util/compatibility";
import type {Tab} from "../layout/Tab";
import {setTitle} from "./processTitle";
+/// #if !MOBILE
+import {removeBlockPanelEditors} from "../block/Panel";
+/// #endif
export const reloadSync = (
app: App,
@@ -60,6 +63,7 @@ export const reloadSync = (
window.siyuan.mobile.docks.file?.init(false);
});
/// #else
+ removeBlockPanelEditors({rootIDs: data.removeRootIDs});
const allModels = getAllModels();
const updateTitle = (rootID: string, tab: Tab, protyle?: IProtyle) => {
const docInfoParam: IObject = {
diff --git a/app/src/window/index.ts b/app/src/window/index.ts
index 701cd201bd..f73ee0eda5 100644
--- a/app/src/window/index.ts
+++ b/app/src/window/index.ts
@@ -32,6 +32,7 @@ import {reloadSync} from "../util/reloadSync";
import {setTitle} from "../util/processTitle";
import {ensureUILayout} from "../util/ensureUILayout";
import {applyEntryVisibility} from "../config/entryVisibility/runtime";
+import {removeBlockPanelEditors} from "../block/Panel";
class App {
public plugins: import("../plugin").Plugin[] = [];
@@ -128,6 +129,7 @@ class App {
break;
case "closeBox":
case "removeBox":
+ removeBlockPanelEditors({notebookId: data.data.box});
getAllTabs().forEach((tab) => {
if (tab.headElement) {
const initTab = tab.headElement.getAttribute("data-initdata");
@@ -141,6 +143,7 @@ class App {
});
break;
case "removeDoc":
+ removeBlockPanelEditors({rootIDs: data.data.ids});
getAllTabs().forEach((tab) => {
if (tab.headElement) {
const initTab = tab.headElement.getAttribute("data-initdata");