Skip to content

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

:bug: Align embedded block drops with the insertion indicator https://github.com/siyuan-note/siyuan/issues/19005

Daniel4904bd7318d6
diff --git a/app/src/protyle/util/dragDocument.test.ts b/app/src/protyle/util/dragDocument.test.ts
index 5fe27496cd..92bc46e4c9 100644
--- a/app/src/protyle/util/dragDocument.test.ts
+++ b/app/src/protyle/util/dragDocument.test.ts
@@ -3,6 +3,7 @@ import * as assert from "node:assert/strict";
import {
BLOCK_DRAGOVER_SELECTOR,
getAVRowDropTarget,
+ getBlockDragInsertPosition,
getBlockDragoverTarget,
getSameSuperBlockEdgeTarget,
getSuperBlockResizeDropTarget,
@@ -120,6 +121,29 @@ describe("getBlockDragoverTarget", () => {
});
});
+describe("getBlockDragInsertPosition", () => {
+ const createTarget = (className: string) => ({
+ classList: {
+ contains: (value: string) => value === className,
+ },
+ }) as unknown as Element;
+
+ it("maps top and left indicators to insertion before the target", () => {
+ assert.equal(getBlockDragInsertPosition(createTarget("dragover__top")), "before");
+ assert.equal(getBlockDragInsertPosition(createTarget("dragover__left")), "before");
+ });
+
+ it("maps bottom and right indicators to insertion after the target", () => {
+ assert.equal(getBlockDragInsertPosition(createTarget("dragover__bottom")), "after");
+ assert.equal(getBlockDragInsertPosition(createTarget("dragover__right")), "after");
+ });
+
+ it("leaves structural list indicators to their dedicated drop handling", () => {
+ assert.equal(getBlockDragInsertPosition(createTarget("dragover__top--sibling")), undefined);
+ assert.equal(getBlockDragInsertPosition(createTarget("dragover__bottom--child")), undefined);
+ });
+});
+
describe("isAttributeViewTitleTarget", () => {
it("recognizes the database title and its descendants", () => {
const title = createClassElement(["av__title"]);
diff --git a/app/src/protyle/util/dragDocument.ts b/app/src/protyle/util/dragDocument.ts
index 58bdf17cf2..0715c0a9da 100644
--- a/app/src/protyle/util/dragDocument.ts
+++ b/app/src/protyle/util/dragDocument.ts
@@ -21,6 +21,15 @@ export const getBlockDragoverTarget = (scope: HTMLElement, cachedTarget?: Elemen
return scope.querySelector(BLOCK_DRAGOVER_SELECTOR) || (cachedTargetInScope ? cachedTarget : null);
};
+export const getBlockDragInsertPosition = (targetElement?: Element | null): "before" | "after" | undefined => {
+ if (targetElement?.classList.contains("dragover__top") || targetElement?.classList.contains("dragover__left")) {
+ return "before";
+ }
+ if (targetElement?.classList.contains("dragover__bottom") || targetElement?.classList.contains("dragover__right")) {
+ return "after";
+ }
+};
+
export const isAttributeViewTitleTarget = (targetNode: Node | null, point?: {x: number, y: number}) => {
if (!targetNode) {
return false;
diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts
index 471a8f6db4..5c3e0fd8ea 100644
--- a/app/src/protyle/util/editorCommonEvent.ts
+++ b/app/src/protyle/util/editorCommonEvent.ts
@@ -57,6 +57,7 @@ import {setFold} from "./blockFold";
import {isEncryptedBox} from "../../util/pathName";
import {
getAVRowDropTarget,
+ getBlockDragInsertPosition,
getBlockDragoverTarget,
getSameSuperBlockEdgeTarget,
getSuperBlockResizeDropTarget,
@@ -76,6 +77,7 @@ import {getCaretRect} from "./caretRect";
import {isBlockRefDropTargetDisabled} from "./blockRefDrop";
const KANBAN_GROUP_DRAG_TYPE = `${Constants.SIYUAN_DROP_GUTTER}NodeAttributeView${Constants.ZWSP}Group${Constants.ZWSP}`;
+const SHIFT_EMBED_INSERT_TARGET_TYPES = ["NodeParagraph", "NodeHeading", "NodeCodeBlock", "NodeAttributeView"];
const convertListItemSubtype = (listItem: Element, subtype: string) => {
const actionElement = listItem.querySelector(".protyle-action");
@@ -1457,17 +1459,14 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
if (isBlockRefDropTargetDisabled([event.target as Node, range.startContainer])) {
return;
} else {
- // 数据库和代码块的工具区不属于编辑内容,需按拖拽指示线将光标定位到可编辑区域开头或末尾。
- if (event.shiftKey && ["NodeAttributeView", "NodeCodeBlock"].includes(targetElement?.getAttribute("data-type"))) {
+ // 嵌入块为块级插入,需按拖拽指示线统一目标与方向,避免光标命中块上半区时插入到下方。
+ if (event.shiftKey && SHIFT_EMBED_INSERT_TARGET_TYPES.includes(targetElement?.getAttribute("data-type"))) {
const editableElement = getContenteditableElement(targetElement);
- const isBefore = targetElement.classList.contains("dragover__top") ||
- targetElement.classList.contains("dragover__left");
- const isAfter = targetElement.classList.contains("dragover__bottom") ||
- targetElement.classList.contains("dragover__right");
- if (editableElement && (isBefore || isAfter)) {
+ const dragInsertPosition = getBlockDragInsertPosition(targetElement);
+ if (editableElement && dragInsertPosition) {
range.selectNodeContents(editableElement);
- range.collapse(isBefore);
- insertPosition = isBefore ? "before" : "after";
+ range.collapse(dragInsertPosition === "before");
+ insertPosition = dragInsertPosition;
}
}
focusByRange(range);