Skip to content

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

:bug: Detect exact inline memo selections by range https://github.com/siyuan-note/siyuan/issues/19091

Daniel055306c69104
diff --git a/app/src/protyle/toolbar/InlineMemo.ts b/app/src/protyle/toolbar/InlineMemo.ts
index 8bced8a0a4..e8a838f299 100644
--- a/app/src/protyle/toolbar/InlineMemo.ts
+++ b/app/src/protyle/toolbar/InlineMemo.ts
@@ -1,8 +1,10 @@
import {ToolbarItem} from "./ToolbarItem";
import {hasClosestBlock, hasClosestByAttribute} from "../util/hasClosest";
-import {getSemanticInlineVisibleText, stripSemanticMarkersFromRangeText} from "../util/inlineElementMarker";
+import {stripSemanticMarkersFromRangeText} from "../util/inlineElementMarker";
import {Constants} from "../../constants";
-import {getFirstSelectedInlineMemoContent} from "./inlineMemoSelection";
+import {getFirstSelectedInlineMemoContent, isExactInlineMemoSelection} from "./inlineMemoSelection";
+import {getContenteditableElement} from "../wysiwyg/getBlock";
+import {getSelectionOffset} from "../util/selection";
export class InlineMemo extends ToolbarItem {
public element: HTMLElement;
@@ -21,7 +23,9 @@ export class InlineMemo extends ToolbarItem {
const memoElement = hasClosestByAttribute(range.startContainer, "data-type", "inline-memo");
const memoContent = getFirstSelectedInlineMemoContent(range);
const selectedText = stripSemanticMarkersFromRangeText(range).split(Constants.ZWSP).join("");
- if (memoElement && getSemanticInlineVisibleText(memoElement) === selectedText) {
+ const editableElement = memoElement && getContenteditableElement(nodeElement, range.startContainer);
+ if (memoElement && editableElement && isExactInlineMemoSelection(range, memoElement, currentRange =>
+ getSelectionOffset(editableElement, undefined, currentRange, true))) {
// https://github.com/siyuan-note/siyuan/issues/6569
protyle.toolbar.showRender(protyle, memoElement);
return;
diff --git a/app/src/protyle/toolbar/inlineMemoSelection.test.ts b/app/src/protyle/toolbar/inlineMemoSelection.test.ts
index 0d96defe13..9bd8bc4717 100644
--- a/app/src/protyle/toolbar/inlineMemoSelection.test.ts
+++ b/app/src/protyle/toolbar/inlineMemoSelection.test.ts
@@ -1,6 +1,10 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
-import {getFirstSelectedInlineMemoContent, setInlineMemoContentIfMissing} from "./inlineMemoSelection";
+import {
+ getFirstSelectedInlineMemoContent,
+ isExactInlineMemoSelection,
+ setInlineMemoContentIfMissing
+} from "./inlineMemoSelection";
const element = (type: string | null, content?: string, parentElement: HTMLElement | null = null) => {
const attributes = new Map<string, string>();
@@ -84,3 +88,25 @@ describe("setInlineMemoContentIfMissing", () => {
assert.equal(memoElement.hasAttribute("data-inline-memo-content"), false);
});
});
+
+describe("isExactInlineMemoSelection", () => {
+ it("recognizes matching selection boundaries", () => {
+ const selectedRange = {} as Range;
+ const memoRange = {selectNodeContents: (): void => undefined} as unknown as Range;
+ const memoElement = element("inline-memo", "111");
+ Object.defineProperty(memoElement, "ownerDocument", {value: {createRange: () => memoRange}});
+
+ assert.equal(isExactInlineMemoSelection(selectedRange, memoElement, currentRange =>
+ currentRange === selectedRange ? {start: 3, end: 6} : {start: 3, end: 6}), true);
+ });
+
+ it("does not compare equal text selected from different boundaries", () => {
+ const selectedRange = {} as Range;
+ const memoRange = {selectNodeContents: (): void => undefined} as unknown as Range;
+ const memoElement = element("inline-memo", "111");
+ Object.defineProperty(memoElement, "ownerDocument", {value: {createRange: () => memoRange}});
+
+ assert.equal(isExactInlineMemoSelection(selectedRange, memoElement, currentRange =>
+ currentRange === selectedRange ? {start: 4, end: 7} : {start: 3, end: 6}), false);
+ });
+});
diff --git a/app/src/protyle/toolbar/inlineMemoSelection.ts b/app/src/protyle/toolbar/inlineMemoSelection.ts
index 6a6fbe095e..0bd59768fe 100644
--- a/app/src/protyle/toolbar/inlineMemoSelection.ts
+++ b/app/src/protyle/toolbar/inlineMemoSelection.ts
@@ -1,5 +1,10 @@
import {hasClosestByAttribute} from "../util/hasClosest";
+interface IRangePosition {
+ start: number;
+ end: number;
+}
+
export const getFirstSelectedInlineMemoContent = (range: Range) => {
const startMemoElement = hasClosestByAttribute(range.startContainer, "data-type", "inline-memo");
const memoElement = startMemoElement ||
@@ -15,3 +20,12 @@ export const setInlineMemoContentIfMissing = (element: HTMLElement, content?: st
element.setAttribute("data-inline-memo-content", content);
}
};
+
+export const isExactInlineMemoSelection = (range: Range, memoElement: HTMLElement,
+ getRangePosition: (range: Range) => IRangePosition) => {
+ const memoRange = memoElement.ownerDocument.createRange();
+ memoRange.selectNodeContents(memoElement);
+ const selectedPosition = getRangePosition(range);
+ const memoPosition = getRangePosition(memoRange);
+ return selectedPosition.start === memoPosition.start && selectedPosition.end === memoPosition.end;
+};