Skip to content

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

:bug: Preserve text after block reference hints https://github.com/siyuan-note/siyuan/issues/18626

Daniele795937d6837
diff --git a/app/src/protyle/hint/blockHintRange.test.ts b/app/src/protyle/hint/blockHintRange.test.ts
index 77976dc8a4..87ff035b92 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 {getBlockHintCloseLength, getBlockHintTriggerOffset} from "./blockHintRange";
+import {getBlockHintTriggerOffset} from "./blockHintRange";
describe("getBlockHintTriggerOffset", () => {
it("uses the latest overlapping trigger inside existing closing markers", () => {
@@ -13,11 +13,8 @@ describe("getBlockHintTriggerOffset", () => {
const caretOffset = 3;
const triggerOffset = getBlockHintTriggerOffset(text.substring(0, caretOffset),
text.substring(caretOffset), "[[", "]]");
- const closeLength = getBlockHintCloseLength(text.substring(0, triggerOffset),
- text.substring(caretOffset), "[[", "]]");
-
assert.equal(text.substring(0, triggerOffset) + "reference" +
- text.substring(caretOffset + closeLength), "[reference]]");
+ text.substring(caretOffset), "[reference]]");
});
it("keeps the first trigger when the query starts with an opening marker", () => {
@@ -36,26 +33,21 @@ describe("getBlockHintTriggerOffset", () => {
assert.equal(text.substring(triggerOffset + 2), "query");
});
-});
-describe("getBlockHintCloseLength", () => {
- it("preserves closing markers paired before the actual trigger", () => {
- assert.equal(getBlockHintCloseLength("[", "]]", "[[", "]]"), 0);
- assert.equal(getBlockHintCloseLength("【", "】】", "【【", "】】"), 0);
- });
+ it("preserves text after the caret when replacing a block hint", () => {
+ const text = "[[query]]tail";
+ const caretOffset = 7;
+ const triggerOffset = getBlockHintTriggerOffset(text.substring(0, caretOffset),
+ text.substring(caretOffset), "[[", "]]");
- it("removes an automatically completed closing marker", () => {
- assert.equal(getBlockHintCloseLength("text", "]]tail", "[[", "]]"), 2);
- assert.equal(getBlockHintCloseLength("text", "))tail", "((", "))"), 2);
+ assert.equal(text.substring(0, triggerOffset) + "reference" + text.substring(caretOffset),
+ "reference]]tail");
});
- it("does not remove unrelated closing markers", () => {
- assert.equal(getBlockHintCloseLength("text", "tail]]", "[[", "]]"), 0);
- assert.equal(getBlockHintCloseLength("text))", "tail", "((", "))"), 0);
- });
+ it("includes manually entered closing markers in the query", () => {
+ const text = "[[foo]]";
+ const triggerOffset = getBlockHintTriggerOffset(text, "", "[[", "]]");
- it("accounts for balanced pairs before the trigger", () => {
- assert.equal(getBlockHintCloseLength("[done]", "]]", "[[", "]]"), 2);
- assert.equal(getBlockHintCloseLength("(done)", "))", "((", "))"), 2);
+ assert.equal(text.substring(triggerOffset + 2), "foo]]");
});
});
diff --git a/app/src/protyle/hint/blockHintRange.ts b/app/src/protyle/hint/blockHintRange.ts
index 2af2ef3e12..2dd6bd10c5 100644
--- a/app/src/protyle/hint/blockHintRange.ts
+++ b/app/src/protyle/hint/blockHintRange.ts
@@ -7,24 +7,3 @@ 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 getBlockHintCloseLength = (textBeforeTrigger: string, textAfterCaret: string,
- splitChar: string, endSplit: string) => {
- const openChar = splitChar.substring(0, 1);
- const closeChar = endSplit.substring(0, 1);
- if (!openChar || !closeChar || splitChar !== openChar.repeat(splitChar.length) ||
- endSplit !== closeChar.repeat(endSplit.length) || !textAfterCaret.startsWith(endSplit)) {
- return 0;
- }
-
- let unmatchedOpenCount = 0;
- for (const char of textBeforeTrigger) {
- if (char === openChar) {
- unmatchedOpenCount++;
- } else if (char === closeChar && unmatchedOpenCount > 0) {
- unmatchedOpenCount--;
- }
- }
-
- return unmatchedOpenCount === 0 ? endSplit.length : 0;
-};
diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.ts
index ea4964b06c..1c2ea29d85 100644
--- a/app/src/protyle/hint/index.ts
+++ b/app/src/protyle/hint/index.ts
@@ -58,7 +58,7 @@ import {updateAttrViewCellAnimation} from "../render/av/action";
import {setFold} from "../util/blockFold";
import {getIconValueKind} from "../../emoji/iconValue";
import {getCreateTargetContext, isSameCreateTargetContext} from "./createTargetContext";
-import {getBlockHintCloseLength, getBlockHintTriggerOffset} from "./blockHintRange";
+import {getBlockHintTriggerOffset} from "./blockHintRange";
const genEmojiInsertHTML = (value: string) => {
const kind = getIconValueKind(value);
@@ -92,23 +92,6 @@ const getWholeTextOffset = (textNode: Text, offset: number) => {
return offset;
};
-const setRangeEndByWholeTextOffset = (range: Range, textNode: Text, offset: number) => {
- while (textNode.previousSibling?.nodeType === 3) {
- textNode = textNode.previousSibling as Text;
- }
- while (textNode) {
- if (offset <= textNode.textContent.length) {
- range.setEnd(textNode, offset);
- return;
- }
- offset -= textNode.textContent.length;
- if (textNode.nextSibling?.nodeType !== 3) {
- return;
- }
- textNode = textNode.nextSibling as Text;
- }
-};
-
export class Hint {
public timeId: number;
public element: HTMLDivElement;
@@ -686,23 +669,6 @@ ${genHintItemHTML(item)}
}
const html = nodeElement.outerHTML;
// 自顶向下法新建文档后光标定位问题 https://github.com/siyuan-note/siyuan/issues/299
- // QQ 拼音输入法自动补全需移除补全内容 https://github.com/siyuan-note/siyuan/issues/320
- // 前后有标记符的情况 https://github.com/siyuan-note/siyuan/issues/2511
- const endSplit = Constants.BLOCK_HINT_CLOSE_KEYS[this.splitChar];
- // 仅移除当前提示所属的闭合符号,保留触发范围外的原有符号
- let removeCloseLength = 0;
- if (Constants.BLOCK_HINT_KEYS.includes(this.splitChar) && endSplit && this.lastIndex > -1 &&
- range.startContainer.nodeType === 3) {
- const textNode = range.startContainer as Text;
- const caretOffset = getWholeTextOffset(textNode, range.startOffset);
- const triggerOffset = getWholeTextOffset(textNode, this.lastIndex);
- removeCloseLength = getBlockHintCloseLength(textNode.wholeText.substring(0, triggerOffset),
- textNode.wholeText.substring(caretOffset), this.splitChar, endSplit);
- if (removeCloseLength > 0) {
- setRangeEndByWholeTextOffset(range, textNode, caretOffset + removeCloseLength);
- }
- }
-
if (this.lastIndex > -1) {
range.setStart(range.startContainer, this.lastIndex);
focusByRange(range);
@@ -747,8 +713,7 @@ ${genHintItemHTML(item)}
tempElement = tempElement.firstElementChild as HTMLDivElement;
if (refIsS) {
const selectedText = range.toString();
- const staticText = selectedText.substring(this.splitChar.length,
- Math.max(this.splitChar.length, selectedText.length - removeCloseLength));
+ const staticText = selectedText.substring(this.splitChar.length);
if (staticText) {
tempElement.setAttribute("data-subtype", "s");
tempElement.innerText = staticText;