Skip to content

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

:bug: Fix database row drops and notebook binding cleanup https://github.com/siyuan-note/siyuan/issues/18599

Daniel75f0780b8aba
diff --git a/app/src/protyle/util/dragDocument.test.ts b/app/src/protyle/util/dragDocument.test.ts
index 26a2685892..5b892f2da0 100644
--- a/app/src/protyle/util/dragDocument.test.ts
+++ b/app/src/protyle/util/dragDocument.test.ts
@@ -1,6 +1,7 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {
+ getAVRowDropTarget,
getSameSuperBlockEdgeTarget,
getTopListDragTarget,
isAttributeViewTitleTarget,
@@ -117,6 +118,22 @@ describe("uniqueDragIds", () => {
});
});
+describe("getAVRowDropTarget", () => {
+ it("uses the preceding row for the table utility row", () => {
+ const row = createClassElement(["av__row"]);
+ const utilityRow = createClassElement(["av__row--util"]);
+ Object.defineProperty(utilityRow, "previousElementSibling", {value: row});
+
+ assert.equal(getAVRowDropTarget(utilityRow as HTMLElement), row);
+ });
+
+ it("keeps regular rows unchanged", () => {
+ const row = createClassElement(["av__row"]);
+
+ assert.equal(getAVRowDropTarget(row as HTMLElement), row);
+ });
+});
+
describe("isDragTargetInSource", () => {
it("recognizes a target rendered under the same block ID in another editor", () => {
const source = createBlockElement("source");
diff --git a/app/src/protyle/util/dragDocument.ts b/app/src/protyle/util/dragDocument.ts
index 11b38731de..9cbf219cb6 100644
--- a/app/src/protyle/util/dragDocument.ts
+++ b/app/src/protyle/util/dragDocument.ts
@@ -43,6 +43,13 @@ export const uniqueDragIds = (ids: string[]) => {
return Array.from(new Set(ids.filter(Boolean)));
};
+export const getAVRowDropTarget = (targetElement: HTMLElement | false): HTMLElement | false => {
+ if (!targetElement || !targetElement.classList.contains("av__row--util")) {
+ return targetElement;
+ }
+ return targetElement.previousElementSibling as HTMLElement || targetElement;
+};
+
export const isSameSiblingMove = <T>(siblings: T[], sources: T[], target: T, isBottom: boolean) => {
if (sources.length === 0 || sources.includes(target)) {
return sources.includes(target);
diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts
index f3de83f340..0c557c90b5 100644
--- a/app/src/protyle/util/editorCommonEvent.ts
+++ b/app/src/protyle/util/editorCommonEvent.ts
@@ -54,6 +54,7 @@ import {dragoverTab} from "../render/av/view";
import {setFold} from "./blockFold";
import {isEncryptedBox} from "../../util/pathName";
import {
+ getAVRowDropTarget,
getSameSuperBlockEdgeTarget,
getTopListDragTarget,
isAttributeViewTitleTarget,
@@ -2376,6 +2377,7 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
targetElement = hasClosestByClassName(event.target, "av__gallery-item") || hasClosestByClassName(event.target, "av__gallery-add") ||
hasClosestByClassName(event.target, "av__row") || hasClosestByClassName(event.target, "av__row--util") ||
hasClosestBlock(event.target);
+ targetElement = getAVRowDropTarget(targetElement);
const directTargetElement = targetElement;
if (targetElement && ["gallery", "kanban"].includes(targetElement.getAttribute("data-av-type")) && event.target.classList.contains("av__gallery")) {
// 拖拽到属性视图 gallery 内,但没选中 item
@@ -2844,8 +2846,6 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
}
} else if (targetElement.classList.contains("av__row--header")) {
targetElement.classList.add("dragover__bottom");
- } else if (targetElement.classList.contains("av__row--util")) {
- targetElement.previousElementSibling.classList.add("dragover__bottom");
} else {
if (event.clientY > nodeRect.top + nodeRect.height / 2 && disabledPosition !== "bottom") {
targetElement.classList.add("dragover__bottom");
diff --git a/kernel/model/mount.go b/kernel/model/mount.go
index 5d108d7643..0fa62a4730 100644
--- a/kernel/model/mount.go
+++ b/kernel/model/mount.go
@@ -171,6 +171,18 @@ func removeBoxDir(p string) (err error) {
return
}
+func collectBoxDeletedAttributeViewBlocks(boxID string) (ret map[string]map[string]struct{}, err error) {
+ rootIDs := treenode.GetRootBlockIDsByBoxID(boxID)
+ if 1 > len(rootIDs) {
+ return map[string]map[string]struct{}{}, nil
+ }
+ boundAVIDs, err := sql.QueryBoundBlockAVIDsInBox(nil, rootIDs, boxID)
+ if nil != err {
+ return nil, err
+ }
+ return groupDeletedAttributeViewBlocks(boundAVIDs), nil
+}
+
func RemoveBox(boxID string) (err error) {
if !ast.IsNodeIDPattern(boxID) {
return errors.New("invalid notebook ID")
@@ -186,6 +198,12 @@ func RemoveBox(boxID string) (err error) {
}
FlushTxQueue()
+ sql.FlushQueue()
+ // 索引和笔记本目录删除后无法再读取 custom-avs,需提前收集;实际删除成功后再清理绑定行。
+ deletedAttrViewBlockIDs, err := collectBoxDeletedAttributeViewBlocks(boxID)
+ if nil != err {
+ return fmt.Errorf("query database-bound blocks in notebook [%s] failed: %w", boxID, err)
+ }
isUserGuide := IsUserGuide(boxID)
localPath := filepath.Join(util.DataDir, boxID)
if !filelock.IsExist(localPath) {
@@ -250,6 +268,8 @@ func RemoveBox(boxID string) (err error) {
if err = removeBoxDir(localPath); err != nil {
return
}
+ // 目录删除成功后再清理,避免删除失败时提前移除数据库条目。
+ flushDeletedAttributeViewBlocks(deletedAttrViewBlockIDs)
// 加密笔记本删除时清理其独立加密 db 文件(含 WAL/SHM),避免残留
if isEncrypted {
sql.RemoveEncryptedDBFile(boxID)
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 3de011ffb2..d711cbd8e4 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -1319,6 +1319,29 @@ func collectDeletedAttributeViewBlocks(node *ast.Node, delChildrenWhenDelParent
})
}
+func groupDeletedAttributeViewBlocks(boundAVIDs map[string][]string) (ret map[string]map[string]struct{}) {
+ ret = map[string]map[string]struct{}{}
+ for blockID, avIDs := range boundAVIDs {
+ blockID = strings.TrimSpace(blockID)
+ if "" == blockID {
+ continue
+ }
+ for _, avID := range avIDs {
+ avID = strings.TrimSpace(avID)
+ if "" == avID {
+ continue
+ }
+ blockIDs := ret[avID]
+ if nil == blockIDs {
+ blockIDs = map[string]struct{}{}
+ ret[avID] = blockIDs
+ }
+ blockIDs[blockID] = struct{}{}
+ }
+ }
+ return
+}
+
func (tx *Transaction) flushDeletedAttributeViewBlocks() {
flushDeletedAttributeViewBlocks(tx.deletedAttrViewBlockIDs)
tx.deletedAttrViewBlockIDs = map[string]map[string]struct{}{}
diff --git a/kernel/model/transaction_attribute_view_test.go b/kernel/model/transaction_attribute_view_test.go
index 938be64ab5..3c4304d628 100644
--- a/kernel/model/transaction_attribute_view_test.go
+++ b/kernel/model/transaction_attribute_view_test.go
@@ -91,3 +91,24 @@ func TestCollectDeletedAttributeViewBlocks(t *testing.T) {
t.Fatal("child block should not be collected when delChildrenWhenDelParent is false")
}
}
+
+func TestGroupDeletedAttributeViewBlocks(t *testing.T) {
+ deletedAttrViewBlockIDs := groupDeletedAttributeViewBlocks(map[string][]string{
+ "20260807000000-block1": {"20260807000000-av1", "20260807000000-av2", "20260807000000-av1"},
+ "20260807000000-block2": {"20260807000000-av1", ""},
+ "": {"20260807000000-av3"},
+ })
+
+ if 2 != len(deletedAttrViewBlockIDs) {
+ t.Fatalf("expected 2 attribute views, got %#v", deletedAttrViewBlockIDs)
+ }
+ if blockIDs := deletedAttrViewBlockIDs["20260807000000-av1"]; 2 != len(blockIDs) {
+ t.Fatalf("expected 2 blocks for av1, got %#v", blockIDs)
+ }
+ if blockIDs := deletedAttrViewBlockIDs["20260807000000-av2"]; 1 != len(blockIDs) {
+ t.Fatalf("expected 1 block for av2, got %#v", blockIDs)
+ }
+ if _, exists := deletedAttrViewBlockIDs["20260807000000-av3"]; exists {
+ t.Fatal("an empty block ID should be ignored")
+ }
+}