Skip to content

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

:bug: Prevent blocks from being moved into their descendants https://github.com/siyuan-note/siyuan/issues/18555

Danielb4b13a4ac1c6
diff --git a/app/src/protyle/util/dragDocument.test.ts b/app/src/protyle/util/dragDocument.test.ts
index 4819cb8247..9fed4c3773 100644
--- a/app/src/protyle/util/dragDocument.test.ts
+++ b/app/src/protyle/util/dragDocument.test.ts
@@ -1,12 +1,18 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {
+ isDragTargetInSource,
isSameDragEditor,
isSameSiblingMove,
replaceDragUndoOperation,
uniqueDragIds
} from "./dragDocument";
+const createBlockElement = (id: string, parentElement: Element = null) => ({
+ getAttribute: (name: string) => name === "data-node-id" ? id : null,
+ parentElement
+}) as unknown as Element;
+
describe("isSameDragEditor", () => {
it("does not treat a nested Protyle as the target editor", () => {
const sourceElement = {} as Element;
@@ -31,6 +37,32 @@ describe("uniqueDragIds", () => {
});
});
+describe("isDragTargetInSource", () => {
+ it("recognizes a target rendered under the same block ID in another editor", () => {
+ const source = createBlockElement("source");
+ const renderedSource = createBlockElement("source");
+ const child = createBlockElement("child", renderedSource);
+ const target = createBlockElement("target", child);
+
+ assert.equal(isDragTargetInSource([source], target), true);
+ });
+
+ it("allows a target outside all source blocks", () => {
+ const source = createBlockElement("source");
+ const target = createBlockElement("target", createBlockElement("parent"));
+
+ assert.equal(isDragTargetInSource([source], target), false);
+ });
+
+ it("checks every source in a multi-block drag", () => {
+ const firstSource = createBlockElement("first");
+ const secondSource = createBlockElement("second");
+ const target = createBlockElement("target", createBlockElement("second"));
+
+ assert.equal(isDragTargetInSource([firstSource, secondSource], target), true);
+ });
+});
+
describe("isSameSiblingMove", () => {
it("treats dropping a selection onto one of its items as a no-op", () => {
assert.equal(isSameSiblingMove(["a", "b", "c"], ["a", "b", "c"], "c", true), true);
diff --git a/app/src/protyle/util/dragDocument.ts b/app/src/protyle/util/dragDocument.ts
index d52c15ebc7..c8b01f91bb 100644
--- a/app/src/protyle/util/dragDocument.ts
+++ b/app/src/protyle/util/dragDocument.ts
@@ -2,6 +2,16 @@ export const isSameDragEditor = (targetEditor: Pick<Element, "contains">, source
return targetEditor.contains(sourceElement);
};
+export const isDragTargetInSource = (sourceElements: Element[], targetElement: Element) => {
+ const sourceIDs = new Set(sourceElements.map(item => item.getAttribute("data-node-id")).filter(Boolean));
+ for (let target = targetElement; target; target = target.parentElement) {
+ if (sourceIDs.has(target.getAttribute("data-node-id"))) {
+ return true;
+ }
+ }
+ return false;
+};
+
export const uniqueDragIds = (ids: string[]) => {
return Array.from(new Set(ids.filter(Boolean)));
};
diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts
index fbf996c99e..f4ce438270 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 {
+ isDragTargetInSource,
isSameDragEditor,
isSameSiblingMove,
replaceDragUndoOperation,
@@ -593,6 +594,9 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElement: Element, isBottom: boolean,
direct: "col" | "row", isCopy: boolean) => {
+ if (!isCopy && isDragTargetInSource(sourceElements, targetElement)) {
+ return;
+ }
// 底部反链 Protyle 嵌套在正文 Protyle 中,仅 wysiwyg 包含关系能表示同一编辑器。
const isSameEditor = isSameDragEditor(protyle.wysiwyg.element, sourceElements[0]);
// 移动前记录源块所在的超级块,移动后刷新其手柄(移出后需重建)https://github.com/siyuan-note/siyuan/issues/9521
@@ -761,6 +765,9 @@ const dragSb = async (protyle: IProtyle, sourceElements: Element[], targetElemen
};
const dragSame = async (protyle: IProtyle, sourceElements: Element[], targetElement: Element, isBottom: boolean, isCopy: boolean) => {
+ if (!isCopy && isDragTargetInSource(sourceElements, targetElement)) {
+ return;
+ }
const siblingElements = Array.from(targetElement.parentElement.children)
.filter(item => item.hasAttribute("data-node-id"));
if (!isCopy && isSameSiblingMove(siblingElements, sourceElements, targetElement, isBottom)) {
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 54faa10a7d..69f837c58b 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -121,7 +121,7 @@ func flushTx(tx *Transaction) {
if txErr := performTx(tx); nil != txErr {
switch txErr.code {
case TxErrCodeSkipTx:
- // 操作已跳过,提示消息已在具体函数中 PushMsg,不弹状态异常
+ // 操作已跳过,不弹状态异常
return
case TxErrCodeBlockNotFound, TxErrCodePushMsg:
pushMsg := txErr.msg
@@ -187,7 +187,7 @@ const (
TxErrCodeWriteTree = 2
TxErrHandleAttributeView = 3
TxErrCodePushMsg = 4
- TxErrCodeSkipTx = 5 // 操作被跳过(如跨加密边界移动),已 PushMsg 提示,不弹状态异常
+ TxErrCodeSkipTx = 5 // 操作被跳过,不弹状态异常
)
type TxErr struct {
@@ -605,7 +605,7 @@ func (tx *Transaction) doMove(operation *Operation) (ret *TxErr) {
targetParentID := operation.ParentID
if "" != targetPreviousID {
if id == targetPreviousID {
- return
+ return &TxErr{code: TxErrCodeSkipTx}
}
var targetTree *parse.Tree
@@ -644,11 +644,11 @@ func (tx *Transaction) doMove(operation *Operation) (ret *TxErr) {
}
if isMovingFoldHeadingIntoSelf(targetNode, headingChildren) {
- return
+ return &TxErr{code: TxErrCodeSkipTx}
}
if isMovingParentIntoChild(srcNode, targetNode) {
- return
+ return &TxErr{code: TxErrCodeSkipTx}
}
if 0 < len(headingChildren) {
@@ -684,7 +684,7 @@ func (tx *Transaction) doMove(operation *Operation) (ret *TxErr) {
}
if id == targetParentID {
- return
+ return &TxErr{code: TxErrCodeSkipTx}
}
targetTree, err := tx.loadTree(targetParentID)
@@ -709,11 +709,11 @@ func (tx *Transaction) doMove(operation *Operation) (ret *TxErr) {
}
if isMovingFoldHeadingIntoSelf(targetNode, headingChildren) {
- return
+ return &TxErr{code: TxErrCodeSkipTx}
}
if isMovingParentIntoChild(srcNode, targetNode) {
- return
+ return &TxErr{code: TxErrCodeSkipTx}
}
tx.markListItemFoldCandidate(srcParent, srcTree)
diff --git a/kernel/model/transaction_fold_test.go b/kernel/model/transaction_fold_test.go
index b59467b2e5..5361ff4110 100644
--- a/kernel/model/transaction_fold_test.go
+++ b/kernel/model/transaction_fold_test.go
@@ -41,6 +41,24 @@ func TestMovingFoldHeadingIntoDescendantContainer(t *testing.T) {
}
}
+func TestMovingParentIntoChild(t *testing.T) {
+ source := &ast.Node{Type: ast.NodeListItem, ID: "source"}
+ child := &ast.Node{Type: ast.NodeList, ID: "child"}
+ grandchild := &ast.Node{Type: ast.NodeListItem, ID: "grandchild"}
+ source.AppendChild(child)
+ child.AppendChild(grandchild)
+
+ if !isMovingParentIntoChild(source, grandchild) {
+ t.Fatal("moving a parent block into a descendant should be rejected")
+ }
+ if isMovingParentIntoChild(source, source) {
+ t.Fatal("moving a block relative to itself is handled separately")
+ }
+ if isMovingParentIntoChild(source, &ast.Node{Type: ast.NodeParagraph, ID: "unrelated"}) {
+ t.Fatal("moving a block next to an unrelated block should be allowed")
+ }
+}
+
func TestInsertOuterSuperBlockBeforeTargetWrapper(t *testing.T) {
const (
sourceID = "20260726000000-source0"