An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Prevent reference drops on database titles https://github.com/siyuan-note/siyuan/issues/18565
Daniel8ed6678b3406
diff --git a/app/src/protyle/util/dragDocument.test.ts b/app/src/protyle/util/dragDocument.test.tsindex 9fed4c3773..cfcae9d7c8 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 {+ isAttributeViewTitleTarget,isDragTargetInSource,isSameDragEditor,isSameSiblingMove,@@ -8,6 +9,19 @@ import {uniqueDragIds} from "./dragDocument";+const createClassElement = (classNames: string[], parentElement: Element = null) => ({+ nodeType: 1,+ classList: {+ contains: (className: string) => classNames.includes(className),+ },+ parentElement,+}) as unknown as Element;++const createTextNode = (parentElement: Element) => ({+ nodeType: 3,+ parentElement,+}) as unknown as Node;+const createBlockElement = (id: string, parentElement: Element = null) => ({getAttribute: (name: string) => name === "data-node-id" ? id : null,parentElement@@ -31,6 +45,25 @@ describe("isSameDragEditor", () => {});});+describe("isAttributeViewTitleTarget", () => {+ it("recognizes the database title and its descendants", () => {+ const title = createClassElement(["av__title"]);+ const titleChild = createClassElement([], title);+ const titleText = createTextNode(title);++ assert.equal(isAttributeViewTitleTarget(title), true);+ assert.equal(isAttributeViewTitleTarget(titleChild), true);+ assert.equal(isAttributeViewTitleTarget(titleText), true);+ });++ it("allows other database areas", () => {+ const database = createClassElement(["av"]);+ const row = createClassElement(["av__row"], database);++ assert.equal(isAttributeViewTitleTarget(row), false);+ });+});+describe("uniqueDragIds", () => {it("removes empty and duplicate block IDs while preserving their order", () => {assert.deepEqual(uniqueDragIds(["a", "", "b", "a", "b", "c"]), ["a", "b", "c"]);diff --git a/app/src/protyle/util/dragDocument.ts b/app/src/protyle/util/dragDocument.tsindex c8b01f91bb..b2456bce37 100644--- a/app/src/protyle/util/dragDocument.ts+++ b/app/src/protyle/util/dragDocument.ts@@ -2,6 +2,19 @@ export const isSameDragEditor = (targetEditor: Pick<Element, "contains">, sourcereturn targetEditor.contains(sourceElement);};+export const isAttributeViewTitleTarget = (targetNode: Node | null) => {+ if (!targetNode) {+ return false;+ }+ const targetElement = targetNode.nodeType === 3 ? targetNode.parentElement : targetNode as Element;+ for (let element = targetElement; element; element = element.parentElement) {+ if (element.classList.contains("av__title")) {+ return true;+ }+ }+ return false;+};+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) {diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.tsindex 7848b1c02b..6906d965d9 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 {+ isAttributeViewTitleTarget,isDragTargetInSource,isSameDragEditor,isSameSiblingMove,@@ -1119,12 +1120,16 @@ 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;+ }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 || hasClosestByAttribute(range.startContainer, "data-type", "NodeBlockQueryEmbed")) {+ if (!range || isAttributeViewTitleTarget(range.startContainer) ||+ hasClosestByAttribute(range.startContainer, "data-type", "NodeBlockQueryEmbed")) {return false;}focusByRange(range);@@ -1136,8 +1141,17 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {item.removeAttribute("select-start");item.removeAttribute("select-end");});- if (event.y <= protyle.wysiwyg.element.lastElementChild.getBoundingClientRect().bottom) {- const range = getRangeByPoint(event.clientX, event.clientY);+ 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))) {+ event.dataTransfer.dropEffect = "none";+ hideDragTip();+ hideCaretLine();+ event.preventDefault();+ return;+ }+ if (isWithinEditor) {let caretLineShown = false;if (range && !hasClosestByAttribute(range.startContainer, "data-type", "NodeBlockQueryEmbed")) {const rect = range.getBoundingClientRect();@@ -1343,11 +1357,27 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {const gutterTypes = gutterType.replace(Constants.SIYUAN_DROP_GUTTER, "").split(Constants.ZWSP);const selectedIds = uniqueDragIds(gutterTypes[2].split(","));let insertPosition: "before" | "after";+ if ((event.altKey || (event.shiftKey && protyle.lite)) &&+ isAttributeViewTitleTarget(event.target as Node)) {+ event.preventDefault();+ event.stopPropagation();+ hideCaretLine();+ cleanupDragIndicators(editorElement);+ return;+ }if (event.altKey || event.shiftKey) {if (event.y > protyle.wysiwyg.element.lastElementChild.getBoundingClientRect().bottom) {insertEmptyBlock(protyle, "afterend", protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id"));} else {const range = getRangeByPoint(event.clientX, event.clientY);+ if ((event.altKey || (event.shiftKey && protyle.lite)) &&+ isAttributeViewTitleTarget(range.startContainer)) {+ event.preventDefault();+ event.stopPropagation();+ hideCaretLine();+ cleanupDragIndicators(editorElement);+ return;+ }if (hasClosestByAttribute(range.startContainer, "data-type", "NodeBlockQueryEmbed")) {return;} else {@@ -1835,15 +1865,12 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {!targetElement.classList.contains("av__row") && !targetElement.classList.contains("av__gallery-item") &&!targetElement.classList.contains("av__gallery-add")))) {- if (event.y > protyle.wysiwyg.element.lastElementChild.getBoundingClientRect().bottom) {- insertEmptyBlock(protyle, "afterend", protyle.wysiwyg.element.lastElementChild.getAttribute("data-node-id"));- } else {- const range = getRangeByPoint(event.clientX, event.clientY);- if (hasClosestByAttribute(range.startContainer, "data-type", "NodeBlockQueryEmbed")) {- return;- } else {- focusByRange(range);- }+ if (!focusBlockRefDrop(event)) {+ event.preventDefault();+ event.stopPropagation();+ hideCaretLine();+ cleanupDragIndicators(editorElement);+ return;}await insertBlockRefs(ids);} else if (targetElement && !protyle.options.backlinkData && targetElement.className.indexOf("dragover__") > -1) {