Skip to content

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

:bug: Correct reference drop detection on database titles https://github.com/siyuan-note/siyuan/issues/18565

Daniel684bc4741e06
diff --git a/app/src/protyle/util/dragDocument.test.ts b/app/src/protyle/util/dragDocument.test.ts
index cfcae9d7c8..bb43adf03b 100644
--- a/app/src/protyle/util/dragDocument.test.ts
+++ b/app/src/protyle/util/dragDocument.test.ts
@@ -15,6 +15,7 @@ const createClassElement = (classNames: string[], parentElement: Element = null)
contains: (className: string) => classNames.includes(className),
},
parentElement,
+ querySelector: (): Element | null => null,
}) as unknown as Element;
const createTextNode = (parentElement: Element) => ({
@@ -62,6 +63,25 @@ describe("isAttributeViewTitleTarget", () => {
assert.equal(isAttributeViewTitleTarget(row), false);
});
+
+ it("recognizes coordinates over the database title when the browser targets its container", () => {
+ const title = createClassElement(["av__title"]);
+ const database = createClassElement(["av"]);
+ const container = createClassElement([], database);
+ (database as HTMLElement).querySelector = () => title;
+ (title as HTMLElement).getBoundingClientRect = () => ({
+ bottom: 40,
+ height: 20,
+ left: 10,
+ right: 110,
+ top: 20,
+ width: 100,
+ }) as DOMRect;
+
+ assert.equal(isAttributeViewTitleTarget(container, {x: 50, y: 30}), true);
+ assert.equal(isAttributeViewTitleTarget(container, {x: 4, y: 30}), true);
+ assert.equal(isAttributeViewTitleTarget(container, {x: 50, y: 60}), false);
+ });
});
describe("uniqueDragIds", () => {
diff --git a/app/src/protyle/util/dragDocument.ts b/app/src/protyle/util/dragDocument.ts
index b2456bce37..35029e2ce9 100644
--- a/app/src/protyle/util/dragDocument.ts
+++ b/app/src/protyle/util/dragDocument.ts
@@ -2,17 +2,31 @@ export const isSameDragEditor = (targetEditor: Pick<Element, "contains">, source
return targetEditor.contains(sourceElement);
};
-export const isAttributeViewTitleTarget = (targetNode: Node | null) => {
+export const isAttributeViewTitleTarget = (targetNode: Node | null, point?: {x: number, y: number}) => {
if (!targetNode) {
return false;
}
const targetElement = targetNode.nodeType === 3 ? targetNode.parentElement : targetNode as Element;
+ let attributeViewElement: Element | null = null;
for (let element = targetElement; element; element = element.parentElement) {
if (element.classList.contains("av__title")) {
return true;
}
+ if (!attributeViewElement && element.classList.contains("av")) {
+ attributeViewElement = element;
+ }
}
- return false;
+ if (!attributeViewElement || !point) {
+ return false;
+ }
+ const titleElement = attributeViewElement.querySelector(".av__title") as HTMLElement;
+ if (!titleElement || titleElement.classList.contains("fn__none")) {
+ return false;
+ }
+ const rect = titleElement.getBoundingClientRect();
+ const hitSlop = 12;
+ return rect.width > 0 && rect.height > 0 && point.x >= rect.left - hitSlop && point.x <= rect.right + hitSlop &&
+ point.y >= rect.top - hitSlop && point.y <= rect.bottom + hitSlop;
};
export const isDragTargetInSource = (sourceElements: Element[], targetElement: Element) => {
diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts
index 6906d965d9..5d352c44e0 100644
--- a/app/src/protyle/util/editorCommonEvent.ts
+++ b/app/src/protyle/util/editorCommonEvent.ts
@@ -1119,16 +1119,23 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
}
insertHTML(protyle.lute.Md2BlockDOM(markdown), protyle);
};
- const focusBlockRefDrop = (event: DragEvent) => {
- if (isAttributeViewTitleTarget(event.target as Node)) {
- return false;
+ const isAttributeViewTitleDrop = (event: DragEvent, range?: Range) => {
+ const point = {x: event.clientX, y: event.clientY};
+ if (isAttributeViewTitleTarget(event.target as Node, point) ||
+ isAttributeViewTitleTarget(range?.startContainer || null, point)) {
+ return true;
}
+ const probeOffset = 12;
+ return [[0, 0], [probeOffset, 0], [-probeOffset, 0], [0, probeOffset], [0, -probeOffset]].some((offset) =>
+ isAttributeViewTitleTarget(document.elementFromPoint(point.x + offset[0], point.y + offset[1]), point));
+ };
+ const focusBlockRefDrop = (event: DragEvent) => {
if (event.y > protyle.wysiwyg.element.lastElementChild.getBoundingClientRect().bottom) {
insertEmptyBlock(protyle, "afterend", protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id"));
return true;
}
const range = getRangeByPoint(event.clientX, event.clientY);
- if (!range || isAttributeViewTitleTarget(range.startContainer) ||
+ if (!range || isAttributeViewTitleDrop(event, range) ||
hasClosestByAttribute(range.startContainer, "data-type", "NodeBlockQueryEmbed")) {
return false;
}
@@ -1143,8 +1150,7 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
});
const isWithinEditor = event.y <= protyle.wysiwyg.element.lastElementChild.getBoundingClientRect().bottom;
const range = isWithinEditor ? getRangeByPoint(event.clientX, event.clientY) : undefined;
- if (isAttributeViewTitleTarget(event.target as Node) ||
- (range && isAttributeViewTitleTarget(range.startContainer))) {
+ if (isAttributeViewTitleDrop(event, range)) {
event.dataTransfer.dropEffect = "none";
hideDragTip();
hideCaretLine();
@@ -1358,7 +1364,7 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
const selectedIds = uniqueDragIds(gutterTypes[2].split(","));
let insertPosition: "before" | "after";
if ((event.altKey || (event.shiftKey && protyle.lite)) &&
- isAttributeViewTitleTarget(event.target as Node)) {
+ isAttributeViewTitleDrop(event)) {
event.preventDefault();
event.stopPropagation();
hideCaretLine();
@@ -1371,7 +1377,7 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
} else {
const range = getRangeByPoint(event.clientX, event.clientY);
if ((event.altKey || (event.shiftKey && protyle.lite)) &&
- isAttributeViewTitleTarget(range.startContainer)) {
+ isAttributeViewTitleDrop(event, range)) {
event.preventDefault();
event.stopPropagation();
hideCaretLine();
@@ -1861,6 +1867,17 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
} else if (event.dataTransfer.getData(Constants.SIYUAN_DROP_FILE)?.split("-").length > 1) {
// 文件树拖拽
const ids = event.dataTransfer.getData(Constants.SIYUAN_DROP_FILE).split(",");
+ const isAvItemTarget = hasClosestByClassName(event.target, "av__row") ||
+ hasClosestByClassName(event.target, "av__row--util") ||
+ hasClosestByClassName(event.target, "av__gallery-item") ||
+ hasClosestByClassName(event.target, "av__gallery-add");
+ if (!event.altKey && hasClosestByClassName(event.target, "av") && !isAvItemTarget) {
+ event.preventDefault();
+ event.stopPropagation();
+ hideCaretLine();
+ cleanupDragIndicators(editorElement);
+ return;
+ }
if (!event.altKey && (!targetElement || (
!targetElement.classList.contains("av__row") && !targetElement.classList.contains("av__gallery-item") &&
!targetElement.classList.contains("av__gallery-add")
@@ -2252,6 +2269,14 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
hasClosestByClassName(event.target, "av__gallery-add");
if (event.dataTransfer.types.includes(Constants.SIYUAN_DROP_FILE)) {
// 文档面板拖拽文档到编辑器
+ if (hasClosestByClassName(event.target, "av") && !isAvTarget) {
+ event.preventDefault();
+ event.dataTransfer.dropEffect = "none";
+ hideDragTip();
+ hideCaretLine();
+ cleanupDragIndicators(editorElement);
+ return;
+ }
showDragTip(window.siyuan.dragTitle || "",
isAvTarget ? window.siyuan.languages.addToDatabase :
(event.altKey ? window.siyuan.languages.dragTip2Heading : window.siyuan.languages.dragTipRef),