Skip to content

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

:bug: Preserve selected block reference anchor text https://github.com/siyuan-note/siyuan/issues/18663

Daniel0f9a3c2212f5
diff --git a/app/src/protyle/hint/blockHintRange.test.ts b/app/src/protyle/hint/blockHintRange.test.ts
index 87ff035b92..a1cb5d62eb 100644
--- a/app/src/protyle/hint/blockHintRange.test.ts
+++ b/app/src/protyle/hint/blockHintRange.test.ts
@@ -1,6 +1,6 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
-import {getBlockHintTriggerOffset} from "./blockHintRange";
+import {getBlockHintTriggerOffset, getBlockRefStaticText} from "./blockHintRange";
describe("getBlockHintTriggerOffset", () => {
it("uses the latest overlapping trigger inside existing closing markers", () => {
@@ -51,3 +51,15 @@ describe("getBlockHintTriggerOffset", () => {
assert.equal(text.substring(triggerOffset + 2), "foo]]");
});
});
+
+describe("getBlockRefStaticText", () => {
+ it("preserves the complete toolbar selection", () => {
+ assert.equal(getBlockRefStaticText("旧的开始", "((", false), "旧的开始");
+ assert.equal(getBlockRefStaticText("((literal", "((", false), "((literal");
+ });
+
+ it("removes the trigger from an inline block hint", () => {
+ assert.equal(getBlockRefStaticText("[[旧的开始", "[[", true), "旧的开始");
+ assert.equal(getBlockRefStaticText("((query", "((", true), "query");
+ });
+});
diff --git a/app/src/protyle/hint/blockHintRange.ts b/app/src/protyle/hint/blockHintRange.ts
index 2dd6bd10c5..0422ecfbdf 100644
--- a/app/src/protyle/hint/blockHintRange.ts
+++ b/app/src/protyle/hint/blockHintRange.ts
@@ -7,3 +7,7 @@ export const getBlockHintTriggerOffset = (textBeforeCaret: string, textAfterCare
const tripleOffset = textBeforeCaret.lastIndexOf(splitChar + splitChar.substring(0, 1));
return tripleOffset > -1 ? Math.min(latestOffset, tripleOffset) : latestOffset;
};
+
+export const getBlockRefStaticText = (selectedText: string, splitChar: string, includesTrigger: boolean) => {
+ return includesTrigger ? selectedText.substring(splitChar.length) : selectedText;
+};
diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.ts
index 650b51acac..266fd84a1e 100644
--- a/app/src/protyle/hint/index.ts
+++ b/app/src/protyle/hint/index.ts
@@ -59,7 +59,7 @@ import {updateAttrViewCellAnimation} from "../render/av/action";
import {setFold} from "../util/blockFold";
import {getIconValueKind} from "../../emoji/iconValue";
import {getCreateTargetContext, isSameCreateTargetContext} from "./createTargetContext";
-import {getBlockHintTriggerOffset} from "./blockHintRange";
+import {getBlockHintTriggerOffset, getBlockRefStaticText} from "./blockHintRange";
const genEmojiInsertHTML = (value: string) => {
const kind = getIconValueKind(value);
@@ -721,7 +721,7 @@ ${genHintItemHTML(item)}
tempElement = tempElement.firstElementChild as HTMLDivElement;
if (refIsS) {
const selectedText = range.toString();
- const staticText = selectedText.substring(this.splitChar.length);
+ const staticText = getBlockRefStaticText(selectedText, this.splitChar, this.lastIndex > -1);
if (staticText) {
tempElement.setAttribute("data-subtype", "s");
tempElement.innerText = staticText;