Skip to content

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

:art: Hide empty bottom backlinks without reveal motion https://github.com/siyuan-note/siyuan/issues/18319

Daniel6aad491737c4
diff --git a/app/src/assets/scss/protyle/_protyle.scss b/app/src/assets/scss/protyle/_protyle.scss
index e643d2595b..3affdfbdf9 100644
--- a/app/src/assets/scss/protyle/_protyle.scss
+++ b/app/src/assets/scss/protyle/_protyle.scss
@@ -357,6 +357,13 @@
transition: margin .3s cubic-bezier(.25, .46, .45, .94) 0ms;
}
}
+
+ &--backlink-reveal {
+ > .protyle-wysiwyg,
+ > .sy__backlink--bottom {
+ transition: none;
+ }
+ }
}
.sy__backlink--bottom {
@@ -364,6 +371,15 @@
min-height: 32px;
outline: none;
+ &.sy__backlink--pending {
+ height: 1px;
+ min-height: 1px;
+ opacity: 0;
+ overflow: hidden;
+ padding-bottom: 0 !important;
+ pointer-events: none;
+ }
+
> .block__icons {
border-top: 1px solid var(--b3-border-color);
flex-shrink: 0;
diff --git a/app/src/editor/index.ts b/app/src/editor/index.ts
index 2ea163c20d..2d872d6a80 100644
--- a/app/src/editor/index.ts
+++ b/app/src/editor/index.ts
@@ -19,6 +19,7 @@ export class Editor extends Model {
private backlinkElement?: HTMLElement;
private backlinkIntersectionObserver?: IntersectionObserver;
private backlinkMutationObserver?: MutationObserver;
+ private backlinkEmpty = false;
constructor(options: {
app: App,
@@ -97,7 +98,7 @@ export class Editor extends Model {
}
const backlinkElement = document.createElement("div");
- backlinkElement.className = "fn__none sy__backlink--bottom";
+ backlinkElement.className = "fn__none sy__backlink--bottom sy__backlink--pending";
this.backlinkElement = backlinkElement;
this.editor.protyle.wysiwyg.element.after(backlinkElement);
setPadding(this.editor.protyle);
@@ -115,6 +116,24 @@ export class Editor extends Model {
type: "bottom",
element: backlinkElement,
ownerProtyle: this.editor.protyle,
+ emptyChange: (empty) => {
+ if (this.backlinkElement !== backlinkElement) {
+ return;
+ }
+ const pending = backlinkElement.classList.contains("sy__backlink--pending");
+ if (!pending && this.backlinkEmpty === empty) {
+ return;
+ }
+ const contentElement = this.editor.protyle.contentElement;
+ contentElement.classList.add("protyle-content--backlink-reveal");
+ backlinkElement.classList.remove("sy__backlink--pending");
+ this.backlinkEmpty = empty;
+ this.updateBacklinkVisibility(pending);
+ contentElement.getBoundingClientRect();
+ requestAnimationFrame(() => {
+ contentElement.classList.remove("protyle-content--backlink-reveal");
+ });
+ },
});
} else {
this.backlink.refreshDirty();
@@ -157,14 +176,14 @@ export class Editor extends Model {
return protyle.block.showAll ? protyle.block.id : (protyle.block.parentID || protyle.block.rootID);
}
- private updateBacklinkVisibility() {
+ private updateBacklinkVisibility(forcePadding = false) {
if (!this.backlinkElement) {
return;
}
const lastElement = this.editor.protyle.wysiwyg.element.lastElementChild;
- const hidden = !this.editor.protyle.block.showAll && this.editor.protyle.block.scroll &&
- lastElement?.getAttribute("data-eof") !== "2";
- if (this.backlinkElement.classList.contains("fn__none") !== hidden) {
+ const hidden = this.backlinkEmpty || (!this.editor.protyle.block.showAll && this.editor.protyle.block.scroll &&
+ lastElement?.getAttribute("data-eof") !== "2");
+ if (this.backlinkElement.classList.contains("fn__none") !== hidden || forcePadding) {
this.backlinkElement.classList.toggle("fn__none", hidden);
setPadding(this.editor.protyle);
}
@@ -180,6 +199,7 @@ export class Editor extends Model {
this.backlinkMutationObserver = undefined;
this.backlink = undefined;
this.backlinkElement = undefined;
+ this.backlinkEmpty = false;
if (hasBacklinkElement) {
setPadding(this.editor.protyle);
}
diff --git a/app/src/layout/dock/Backlink.ts b/app/src/layout/dock/Backlink.ts
index 292c43d670..b817c33725 100644
--- a/app/src/layout/dock/Backlink.ts
+++ b/app/src/layout/dock/Backlink.ts
@@ -14,7 +14,11 @@ import {isSupportCSSHL, searchMarkRender} from "../../protyle/render/searchMarkR
import {getDocDisplayName, isEncryptedBox} from "../../util/pathName";
import {getAllModels} from "../getAll";
import {hideElements} from "../../protyle/ui/hideElements";
-import {shouldRenderBacklinkResponse, shouldSaveBacklinkStatus} from "./backlinkRefresh";
+import {
+ shouldHideBottomBacklinks,
+ shouldRenderBacklinkResponse,
+ shouldSaveBacklinkStatus
+} from "./backlinkRefresh";
export class Backlink extends Model {
public element: HTMLElement;
@@ -49,6 +53,8 @@ export class Backlink extends Model {
private showingLoading = false;
private contextRequestVersions = [0, 0];
private ownerFocusoutListener?: (event: FocusEvent) => void;
+ private empty = false;
+ private emptyChange?: (empty: boolean) => void;
constructor(options: {
app: App,
@@ -57,7 +63,8 @@ export class Backlink extends Model {
blockId: string,
rootId?: string,
type: "pin" | "local" | "bottom",
- ownerProtyle?: IProtyle
+ ownerProtyle?: IProtyle,
+ emptyChange?: (empty: boolean) => void,
}) {
super({app: options.app});
@@ -74,6 +81,7 @@ export class Backlink extends Model {
this.rootId = options.rootId;
this.type = options.type;
this.ownerProtyle = options.ownerProtyle;
+ this.emptyChange = options.emptyChange;
this.element = options.element || options.tab.panelElement;
this.element.classList.add("fn__flex-column", "file-tree", "sy__backlink", "dockPanel");
if (this.type === "bottom") {
@@ -868,7 +876,6 @@ export class Backlink extends Model {
mCountElement.classList.remove("fn__none");
mCountElement.textContent = data.mentionsCount.toString();
}
-
if (!this.status[this.blockId]) {
this.status[this.blockId] = {
sort: window.siyuan.config.editor.backlinkSort,
@@ -953,6 +960,11 @@ export class Backlink extends Model {
}
this.tree.element.previousElementSibling.querySelector('[data-type="sort"]').setAttribute("data-sort", this.status[this.blockId].sort.toString());
this.mTree.element.previousElementSibling.querySelector('[data-type="mSort"]').setAttribute("data-sort", this.status[this.blockId].mSort.toString());
+ if (this.type === "bottom") {
+ const empty = shouldHideBottomBacklinks(data.linkRefsCount, data.mentionsCount, data.k, data.mk);
+ this.empty = empty;
+ this.emptyChange?.(empty);
+ }
setTimeout(() => {
this.tree.element.scrollTop = this.status[this.blockId].scrollTop;
@@ -980,12 +992,18 @@ export class Backlink extends Model {
if (ignoreFocus) {
this.ignoreFocusOnNextRefresh = true;
}
+ if (!this.element.isConnected || this.ownerProtyle.element.getClientRects().length === 0) {
+ return;
+ }
+ if (this.empty) {
+ this.searchBacklinks();
+ return;
+ }
if (!this.ignoreFocusOnNextRefresh && this.ownerProtyle.element.contains(document.activeElement)) {
return;
}
if (this.element.classList.contains("fn__none") ||
- !this.element.isConnected || this.element.getClientRects().length === 0 ||
- this.ownerProtyle.element.getClientRects().length === 0) {
+ this.element.getClientRects().length === 0) {
return;
}
const rect = this.element.getBoundingClientRect();
diff --git a/app/src/layout/dock/backlinkRefresh.test.ts b/app/src/layout/dock/backlinkRefresh.test.ts
index 5708748ab6..9b962297e7 100644
--- a/app/src/layout/dock/backlinkRefresh.test.ts
+++ b/app/src/layout/dock/backlinkRefresh.test.ts
@@ -2,6 +2,7 @@ import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {
shouldForceBacklinkRefresh,
+ shouldHideBottomBacklinks,
shouldRenderBacklinkResponse,
shouldSaveBacklinkStatus
} from "./backlinkRefresh";
@@ -32,3 +33,16 @@ describe("shouldSaveBacklinkStatus", () => {
assert.equal(shouldSaveBacklinkStatus(false, true), false);
});
});
+
+describe("shouldHideBottomBacklinks", () => {
+ it("hides the bottom area only when backlinks and mentions are both empty", () => {
+ assert.equal(shouldHideBottomBacklinks(0, 0, "", ""), true);
+ assert.equal(shouldHideBottomBacklinks(1, 0, "", ""), false);
+ assert.equal(shouldHideBottomBacklinks(0, 1, "", ""), false);
+ });
+
+ it("keeps the area visible when an active filter has no results", () => {
+ assert.equal(shouldHideBottomBacklinks(0, 0, "backlink", ""), false);
+ assert.equal(shouldHideBottomBacklinks(0, 0, "", "mention"), false);
+ });
+});
diff --git a/app/src/layout/dock/backlinkRefresh.ts b/app/src/layout/dock/backlinkRefresh.ts
index 305d2a4e53..2ca502cebe 100644
--- a/app/src/layout/dock/backlinkRefresh.ts
+++ b/app/src/layout/dock/backlinkRefresh.ts
@@ -9,3 +9,12 @@ export const shouldRenderBacklinkResponse = (refreshQueued: boolean, searchQueue
export const shouldSaveBacklinkStatus = (init: boolean, showingLoading: boolean) => {
return !init && !showingLoading;
};
+
+export const shouldHideBottomBacklinks = (
+ linkRefsCount: number,
+ mentionsCount: number,
+ backlinkKeyword: string,
+ mentionKeyword: string
+) => {
+ return linkRefsCount === 0 && mentionsCount === 0 && !backlinkKeyword && !mentionKeyword;
+};
diff --git a/app/src/protyle/ui/initUI.ts b/app/src/protyle/ui/initUI.ts
index 3b2ecac0cd..9da86cc3dc 100644
--- a/app/src/protyle/ui/initUI.ts
+++ b/app/src/protyle/ui/initUI.ts
@@ -336,7 +336,9 @@ export const setPadding = (protyle: IProtyle) => {
const paddingLeft = protyle.options.backlinkData ? 24 : padding.left;
const paddingRight = protyle.options.backlinkData ? 16 : padding.right;
const backlinkBottomElement = protyle.contentElement.querySelector(".sy__backlink--bottom") as HTMLElement;
- const backlinkBottomVisible = backlinkBottomElement && !backlinkBottomElement.classList.contains("fn__none");
+ const backlinkBottomVisible = backlinkBottomElement &&
+ !backlinkBottomElement.classList.contains("fn__none") &&
+ !backlinkBottomElement.classList.contains("sy__backlink--pending");
if (protyle.options.backlinkData) {
protyle.wysiwyg.element.style.padding = `4px ${paddingRight}px 4px ${paddingLeft}px`;