Skip to content

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

:bug: Capture backlink filter position before toolbar hides https://github.com/siyuan-note/siyuan/issues/20802

Daniel2e7d7a66e788
diff --git a/app/src/layout/dock/BacklinkContent.ts b/app/src/layout/dock/BacklinkContent.ts
index 32461a2620..a464746ca8 100644
--- a/app/src/layout/dock/BacklinkContent.ts
+++ b/app/src/layout/dock/BacklinkContent.ts
@@ -993,6 +993,8 @@ export class BacklinkContent extends Model {
}
private showSourceFilterMenu(target: HTMLElement) {
+ // 工具栏离开悬停后会隐藏按钮,在打开菜单前保留其位置。
+ const rect = target.getBoundingClientRect();
toggleMenu({
target: target,
build: (_menu, session) => {
@@ -1130,6 +1132,7 @@ export class BacklinkContent extends Model {
keyword: this.inputsElement[0].value,
filter: this.sourceFilter,
target,
+ anchorRect: rect,
protyle: this.ownerProtyle || this.editors[0]?.protyle,
getSelected: () => this.sourceFilter.database,
isCurrent: () => !this.destroyed && refFilterGeneration === this.viewStateGeneration,
@@ -1170,7 +1173,6 @@ export class BacklinkContent extends Model {
applyFoldTypes([]);
}
}).element);
- const rect = target.getBoundingClientRect();
session.show(() => window.siyuan.menus.menu.popup({x: rect.left, y: rect.bottom, h: rect.height}));
},
diff --git a/app/src/layout/dock/backlinkDatabaseFilter.test.ts b/app/src/layout/dock/backlinkDatabaseFilter.test.ts
index d7b33347c5..9c926024cf 100644
--- a/app/src/layout/dock/backlinkDatabaseFilter.test.ts
+++ b/app/src/layout/dock/backlinkDatabaseFilter.test.ts
@@ -31,6 +31,7 @@ const setup = () => {
const options = {
id: "topic", notebook: "", keyword: "", filter: {dailyNote: "all", excludeSelf: false, excludedNotebookIDs: []} as IBacklinkSourceFilter,
target: {getBoundingClientRect: () => targetRect} as HTMLElement,
+ anchorRect: targetRect,
isCurrent: () => current,
getSelected: () => selected,
apply: (value?: IBacklinkSourceFilter["database"]) => { selected = value; },
@@ -74,8 +75,8 @@ test("database candidates arriving after the target changes do not reopen a filt
test("database filtering retains the toolbar position across candidate loading and source refresh", async () => {
const h = setup();
- const pending = h.methods.loadBacklinkDatabaseFilterMenu(h.options);
h.hideTarget();
+ const pending = h.methods.loadBacklinkDatabaseFilterMenu(h.options);
h.respond({code: 0, data: [{id: "tasks", name: "Tasks", keys: []}]});
const menu = await pending;
menu[2].click(null, null);
diff --git a/app/src/layout/dock/backlinkDatabaseFilter.ts b/app/src/layout/dock/backlinkDatabaseFilter.ts
index 625182b559..3e29e2ee79 100644
--- a/app/src/layout/dock/backlinkDatabaseFilter.ts
+++ b/app/src/layout/dock/backlinkDatabaseFilter.ts
@@ -27,12 +27,13 @@ export const loadBacklinkDatabaseFilterMenu = async (options: {
keyword: string;
filter: IBacklinkSourceFilter;
target: HTMLElement;
+ anchorRect: DOMRect;
protyle?: IProtyle;
isCurrent: () => boolean;
getSelected: () => IBacklinkSourceFilter["database"];
apply: (database?: IBacklinkSourceFilter["database"]) => void;
}): Promise<IMenu[]> => {
- const anchorRect = options.target.getBoundingClientRect();
+ const anchorRect = options.anchorRect;
const response = await fetchSyncPost("/api/ref/getBacklinkDatabases", {
id: options.id,
notebook: options.notebook,
diff --git a/app/src/layout/dock/backlinkDatabaseFilterPosition.test.ts b/app/src/layout/dock/backlinkDatabaseFilterPosition.test.ts
index c7c71de404..7f9acef03c 100644
--- a/app/src/layout/dock/backlinkDatabaseFilterPosition.test.ts
+++ b/app/src/layout/dock/backlinkDatabaseFilterPosition.test.ts
@@ -24,12 +24,13 @@ const browserCases = async (sources: string[], css: string) => {
document.body.append(toolbar);
const target = toolbar.firstElementChild as HTMLElement;
const originalRect = target.getBoundingClientRect();
- let selected: {avID: string, filters: IAVFilter[]};
+ type Selection = {avID: string, filters: IAVFilter[]};
let finishFields: () => void;
let context: {menuElement: HTMLElement};
const fieldsReady = new Promise<void>(resolve => { finishFields = resolve; });
window.siyuan = {zIndex: 0, config: {editor: {backlinkContainChildren: true}},
- languages: {reset: "Reset"}, menus: {menu: {remove() {}}}} as unknown as typeof window.siyuan;
+ notebooks: [], languages: {reset: "Reset", backlinkDatabaseFields: "Database fields"},
+ menus: {menu: {remove() {}, append() {}, popup() {}}}} as unknown as typeof window.siyuan;
const position = load(sources[0], {getTopBarHeight: () => 42});
const panel = load(sources[1], new Proxy({
...position, getPageSize: () => ({}), isMobile: () => false,
@@ -47,14 +48,33 @@ const browserCases = async (sources: string[], css: string) => {
...panel, escapeHtml: (value: string) => value,
fetchSyncPost: async () => ({code: 0, data: databases}),
});
- const items: IMenu[] = await databaseFilter.loadBacklinkDatabaseFilterMenu({
- id: "topic", notebook: "", keyword: "", filter: {}, target,
- isCurrent: () => true, getSelected: () => selected,
- apply: (value: typeof selected) => {
- selected = value;
+ const menuItems: IMenu[] = [];
+ const backlinkModule = load(sources[3], new Proxy({
+ Model: class {}, BACKLINK_BLOCK_TYPES: [],
+ normalizeBacklinkFoldTypes: (): string[] => [], getBacklinkSourceFilterParam: (): undefined => undefined,
+ isEncryptedBox: () => false, ...databaseFilter,
+ toggleMenu: (options: {build: (menu: object, session: {show: (render: () => void) => void}) => void}) => {
+ options.build({}, {show: render => render()});
+ },
+ MenuItem: class {
+ element = document.createElement("button");
+ constructor(options: IMenu) { menuItems.push(options); options.bind?.(this.element); }
+ },
+ }, {get: (object, key) => key in object ? Reflect.get(object, key) : () => {}}));
+ const backlink = Object.create(backlinkModule.BacklinkContent.prototype);
+ Object.assign(backlink, {
+ sourceFilter: {dailyNote: "all", excludeSelf: false, excludedRefDefIDs: [], excludedNotebookIDs: []},
+ blockId: "topic", inputsElement: [{value: ""}], editors: [],
+ applySourceFilter: (filter: {database: Selection}) => {
+ backlink.sourceFilter = filter;
target.replaceWith(target.cloneNode(true));
},
});
+ backlink.showSourceFilterMenu(target);
+ // 移入子菜单后,原工具栏按钮不再处于悬停显示状态。
+ target.style.display = "none";
+ check.equal(target.getBoundingClientRect().width, 0);
+ const items: IMenu[] = await menuItems.find(item => item.label === "Database fields").loadSubmenu();
items[2].click(null, null);
check.equal(target.isConnected, false);
finishFields();
@@ -67,6 +87,7 @@ const browserCases = async (sources: string[], css: string) => {
window.dispatchEvent(new Event("resize"));
check.equal(menu.getBoundingClientRect().top, originalRect.bottom, "a detached button cannot reset the anchor to the origin");
toolbar.replaceChildren(target);
+ target.style.display = "";
target.style.left = "760px";
target.style.top = "300px";
window.dispatchEvent(new Event("resize"));
@@ -87,7 +108,7 @@ test("backlink database filters stay anchored during source refresh, field loadi
skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
timeout: 30000,
}, async () => {
- const sources = ["../../util/setPosition.ts", "../../protyle/render/av/openMenuPanel.ts", "backlinkDatabaseFilter.ts"].map(file =>
+ const sources = ["../../util/setPosition.ts", "../../protyle/render/av/openMenuPanel.ts", "backlinkDatabaseFilter.ts", "BacklinkContent.ts"].map(file =>
transpileModule(readFileSync(path.join(__dirname, file), "utf8"), {
compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},
}).outputText);