Skip to content

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

:bug: Correct database binding candidate positioning and dismissal https://github.com/siyuan-note/siyuan/issues/19935

Daniel6f3228182d9d
diff --git a/app/src/protyle/hint/bindingLoading.test.ts b/app/src/protyle/hint/bindingLoading.test.ts
new file mode 100644
index 0000000000..0574c89710
--- /dev/null
+++ b/app/src/protyle/hint/bindingLoading.test.ts
@@ -0,0 +1,53 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {join} from "node:path";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {createSourceFile, isClassDeclaration, ScriptTarget, transpileModule} from "typescript";
+
+test("panel binding loads at its field and dismisses only on outside pointer events", () => {
+ const source = createSourceFile("index.ts", readFileSync(join(__dirname, "index.ts"), "utf8"), ScriptTarget.Latest);
+ const declaration = source.statements.find(node => isClassDeclaration(node) && node.name.text === "Hint");
+ assert.ok(declaration && isClassDeclaration(declaration));
+ const methods = declaration.members.filter(member =>
+ ["genLoading", "cancelLoadingAnimation", "destroy"].includes(member.name?.getText(source)))
+ .map(member => member.getText(source)).join("\n");
+ const document = new EventTarget();
+ const classes = new Set(["fn__none"]);
+ const input = {};
+ const positions: number[][] = [];
+ const mobilePositions: number[][] = [];
+ const element = {
+ classList: {contains: (name: string) => classes.has(name),
+ add: (name: string) => classes.add(name), remove: (name: string) => classes.delete(name)},
+ contains: (target: unknown) => target === input,
+ querySelector: () => ({}),
+ };
+ const hint = runInNewContext(transpileModule(`class Hint {
+ destroyEmojiPanel() {}
+ setMobilePosition(...position) { mobilePositions.push(position); }
+ ${methods}
+ }
+ new Hint();`, {compilerOptions: {target: ScriptTarget.ES2020}}).outputText, {
+ document, AbortController, mobilePositions,
+ getAVBindingCell: () => ({dataset: {rowId: "item"},
+ getBoundingClientRect: () => ({left: 100, top: 200, bottom: 234, height: 34})}),
+ getSelectionPosition: () => { throw new Error("Detached editor has no selection"); },
+ setPosition: (_element: unknown, ...position: number[]) => positions.push(position),
+ });
+ hint.element = element;
+ const protyle = {toolbar: {range: {}}, wysiwyg: {element: {}}};
+ hint.genLoading(protyle, 0, "av");
+ assert.deepEqual(positions, [[100, 234, 34]]);
+ assert.deepEqual(JSON.parse(JSON.stringify(mobilePositions)), [[200, 234]]);
+ const inside = new Event("pointerdown");
+ Object.defineProperty(inside, "target", {value: input});
+ document.dispatchEvent(inside);
+ assert.equal(classes.has("fn__none"), false);
+ document.dispatchEvent(new Event("pointerdown"));
+ assert.equal(classes.has("fn__none"), true);
+ hint.genLoading(protyle, 0, "av");
+ hint.destroy();
+ document.dispatchEvent(new Event("pointerdown"));
+ assert.equal(classes.has("fn__none"), false);
+});
diff --git a/app/src/protyle/hint/extend.ts b/app/src/protyle/hint/extend.ts
index 03bde9ba26..ce2befe13e 100644
--- a/app/src/protyle/hint/extend.ts
+++ b/app/src/protyle/hint/extend.ts
@@ -576,7 +576,7 @@ export const hintRef = (key: string, protyle: IProtyle, source: THintSource): IH
const nodeElement = hasClosestBlock((source === "av" ? protyle.toolbar.range :
getEditorRange(protyle.wysiwyg.element)).startContainer);
const createTarget = protyle.hint.prepareCreateTarget(protyle, "ref");
- protyle.hint.genLoading(protyle);
+ protyle.hint.genLoading(protyle, 0, source);
let refParam: import("../../types/api").SearchRefBlockRequestInput;
if (protyle.lite) {
refParam = {k: key, id: "", rootID: "", beforeLen: 48, isDatabase: false, isSquareBrackets: true};
diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.ts
index 7778523db3..324c3ad62d 100644
--- a/app/src/protyle/hint/index.ts
+++ b/app/src/protyle/hint/index.ts
@@ -135,6 +135,7 @@ export class Hint {
private emojiPanel?: EmojiPanelController;
private emojiBrowseMode = false;
private loadingAnimation?: Animation;
+ private bindingDismissController?: AbortController;
constructor(protyle: IProtyle) {
this.element = document.createElement("div");
@@ -190,6 +191,7 @@ export class Hint {
}
public destroy() {
+ this.bindingDismissController?.abort();
this.cancelLoadingAnimation();
this.destroyEmojiPanel();
}
@@ -458,7 +460,21 @@ export class Hint {
this.loadingAnimation = undefined;
}
- public genLoading(protyle: IProtyle, delay = 0) {
+ public genLoading(protyle: IProtyle, delay = 0, source = this.source) {
+ this.source = source;
+ this.bindingDismissController?.abort();
+ if (source === "av" && getAVBindingCell(protyle.toolbar.range)?.dataset.rowId) {
+ // 独立条目面板没有编辑器的空白点击处理,使用捕获事件兼容鼠标和触摸。
+ const controller = new AbortController();
+ this.bindingDismissController = controller;
+ document.addEventListener("pointerdown", event => {
+ if (this.element.contains(event.target as Node)) {
+ return;
+ }
+ this.element.classList.add("fn__none");
+ controller.abort();
+ }, {capture: true, signal: controller.signal});
+ }
const delayPanel = this.loadingAnimation?.playState === "running" &&
(this.loadingAnimation.effect as KeyframeEffect)?.target === this.element;
this.cancelLoadingAnimation();
@@ -631,6 +647,7 @@ export class Hint {
this.fill(decodeURIComponent(this.element.querySelector(".b3-list-item--focus").getAttribute("data-value")), protyle, false, isNotCtrl(event));
event.preventDefault();
} else if (event.key === "Escape") {
+ this.bindingDismissController?.abort();
this.element.classList.add("fn__none");
focusByRange(protyle.toolbar.range);
}
@@ -774,6 +791,7 @@ ${genHintItemHTML(item)}
}
public fill(value: string, protyle: IProtyle, updateRange = true, refIsS = false) {
+ this.bindingDismissController?.abort();
hideElements(["hint", "toolbar"], protyle);
if (updateRange && this.source !== "av") {
protyle.toolbar.range = getEditorRange(protyle.wysiwyg.element);