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 duplicate block references from cross-block selections https://github.com/siyuan-note/siyuan/issues/18494
Daniel30a808b9c8be
diff --git a/app/src/protyle/toolbar/BlockRef.ts b/app/src/protyle/toolbar/BlockRef.tsindex 338983c150..d59343b175 100644--- a/app/src/protyle/toolbar/BlockRef.ts+++ b/app/src/protyle/toolbar/BlockRef.ts@@ -1,6 +1,7 @@import {ToolbarItem} from "./ToolbarItem";import {hintRef} from "../hint/extend";import {fixTableRange} from "../util/selection";+import {isSameBlockRange} from "../../util/newFileSelection";export class BlockRef extends ToolbarItem {public element: HTMLElement;@@ -9,10 +10,13 @@ export class BlockRef extends ToolbarItem {super(protyle, menuItem);// 不能用 getEventName,否则会导致光标位置变动到点击的文档中this.element.addEventListener("click", (event: MouseEvent & { changedTouches: MouseEvent[] }) => {- if (protyle.toolbar.range.toString() === "") {+ if (protyle.toolbar.range.toString() === "" || this.element.hasAttribute("disabled")) {return;}fixTableRange(protyle.toolbar.range);+ if (!isSameBlockRange(protyle.toolbar.range)) {+ return;+ }hintRef(protyle.toolbar.range.toString(), protyle, "search");protyle.toolbar.element.classList.add("fn__none");event.stopPropagation();diff --git a/app/src/util/newFile.ts b/app/src/util/newFile.tsindex e0ff037aac..47666079e4 100644--- a/app/src/util/newFile.ts+++ b/app/src/util/newFile.ts@@ -19,6 +19,7 @@ import {createNewFileSelectionContext,isNewFileSelectionValid,isRangeInEditor,+ isSameBlockRange,isSameRange,NewFileSelectionContext} from "./newFileSelection";@@ -152,6 +153,9 @@ export const newFileBySelect = (protyle: IProtyle, newFileName: string, context:export const newFileBySelectRange = (protyle: IProtyle, range: Range, target: "subDoc" | "configured",refSubtype: "d" | "s" = "d", name?: string) => {+ if (!isSameBlockRange(range)) {+ return;+ }const nodeElement = hasClosestBlock(range.startContainer);if (!nodeElement) {return;diff --git a/app/src/util/newFileSelection.test.ts b/app/src/util/newFileSelection.test.tsindex 846eff0142..1686444901 100644--- a/app/src/util/newFileSelection.test.ts+++ b/app/src/util/newFileSelection.test.ts@@ -4,6 +4,7 @@ import {createNewFileSelectionContext,isNewFileSelectionValid,isRangeInEditor,+ isSameBlockRange,isSameRange,NewFileSelectionContext} from "./newFileSelection";@@ -150,4 +151,12 @@ describe("new document selection context", () => {assert.equal(isSameRange(range, movedRange), false);assert.equal(isRangeInEditor(asElement(editor), movedRange), true);});++ it("distinguishes single-block and cross-block selections", () => {+ const {range, startText} = createFixture();++ assert.equal(isSameBlockRange(createRange(startText, startText)), true);+ assert.equal(isSameBlockRange(range), false);+ assert.equal(isSameBlockRange(createRange(new TestText(), startText)), false);+ });});diff --git a/app/src/util/newFileSelection.ts b/app/src/util/newFileSelection.tsindex 02d5fddd8e..029f2603ef 100644--- a/app/src/util/newFileSelection.ts+++ b/app/src/util/newFileSelection.ts@@ -33,6 +33,11 @@ export const isSameRange = (range: Range, targetRange: Range) => range.startContrange.startOffset === targetRange.startOffset && range.endContainer === targetRange.endContainer &&range.endOffset === targetRange.endOffset;+export const isSameBlockRange = (range: Range) => {+ const startBlockElement = hasClosestBlock(range.startContainer);+ return !!startBlockElement && startBlockElement === hasClosestBlock(range.endContainer);+};+export const isRangeInEditor = (editorElement: Element, range: Range) => range.startContainer.isConnected &&range.endContainer.isConnected && editorElement.contains(range.startContainer) && editorElement.contains(range.endContainer);