Skip to content

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

:bug: Keep the floating dock bar attached to its panel https://github.com/siyuan-note/siyuan/issues/20833

Jeffrey Chen57c720cae4ed
diff --git a/app/src/boot/globalEvent/mousemove.ts b/app/src/boot/globalEvent/mousemove.ts
index 6b83b92367..20a6c3b468 100644
--- a/app/src/boot/globalEvent/mousemove.ts
+++ b/app/src/boot/globalEvent/mousemove.ts
@@ -4,6 +4,7 @@ import {hasClosestBlock, hasClosestByClassName, hasClosestByTag} from "../../pro
import {getColIndex} from "../../protyle/util/table";
import {isContainerGutterBridge} from "../../protyle/gutter/container";
import {renderGutterOnHover} from "../../protyle/gutter/hover";
+import {setDockBarPointerOver} from "../../layout/dock/barVisibility";
export const getTableResizeBounds = (tableElement: HTMLTableElement) => {
const captionElement = tableElement.querySelector("caption");
@@ -118,8 +119,10 @@ export const windowMouseMove = (event: MouseEvent) => {
// 隐藏停靠栏会导致点击两侧内容触发浮动面板弹出,因此需减小鼠标范围
(leftBarWidth > 0 || event.clientX < 8);
if (leftTrigger || leftDock.layout.element.contains(target) || leftBar.contains(target)) {
+ setDockBarPointerOver(leftBar, leftBar.contains(target));
leftDock.showDockByHover();
} else {
+ setDockBarPointerOver(leftBar, false);
leftDock.hideDockByHover();
}
@@ -131,8 +134,10 @@ export const windowMouseMove = (event: MouseEvent) => {
event.clientX > window.innerWidth - Math.max(rightBarWidth - 2, 16) &&
(rightBarWidth > 0 || event.clientX > window.innerWidth - 8);
if (rightTrigger || rightDock.layout.element.contains(target) || rightBar.contains(target)) {
+ setDockBarPointerOver(rightBar, rightBar.contains(target));
rightDock.showDockByHover();
} else {
+ setDockBarPointerOver(rightBar, false);
rightDock.hideDockByHover();
}
diff --git a/app/src/layout/dock/barVisibility.ts b/app/src/layout/dock/barVisibility.ts
index c36e54121b..a9d0473fb5 100644
--- a/app/src/layout/dock/barVisibility.ts
+++ b/app/src/layout/dock/barVisibility.ts
@@ -10,9 +10,26 @@ const floatingBarEffects = new WeakMap<HTMLElement, {
backdrop: HTMLElement;
animations: Animation[];
closing: boolean;
+ followsPanel: boolean;
frame: number;
}>();
+// 面板偏移与停靠栏动画共用停靠栏宽度,宽度来源不同会在动画结束时留下缝隙。
+export const getDockBarWidth = (barElement: HTMLElement) => barElement.clientWidth;
+
+const pointerOverDockBars = new WeakSet<HTMLElement>();
+
+// 面板收起时指针仍停在图标栏上,保留临时显示以便继续切换工具。
+export const setDockBarPointerOver = (barElement: HTMLElement, over: boolean) => {
+ if (over) {
+ pointerOverDockBars.add(barElement);
+ } else {
+ pointerOverDockBars.delete(barElement);
+ }
+};
+
+export const isDockBarPointerOver = (barElement: HTMLElement) => pointerOverDockBars.has(barElement);
+
const clearFloatingBarEffects = (element: HTMLElement) => {
const effects = floatingBarEffects.get(element);
effects?.animations.forEach(animation => animation.cancel());
@@ -31,6 +48,7 @@ const animateFloatingBar = (element: HTMLElement, from: number, to: number, clos
effects.animations.forEach(animation => animation.cancel());
cancelAnimationFrame(effects.frame);
effects.closing = closing;
+ effects.followsPanel = Boolean(panel);
element.classList.toggle("dock--closing", closing);
element.inert = closing;
effects.animations = [element, effects.backdrop].flatMap(target => [
@@ -81,7 +99,8 @@ const animateFloatingBar = (element: HTMLElement, from: number, to: number, clos
{opacity: panelStyle.opacity}];
// 无过渡的属性直接使用面板当前值;延迟开始的过渡也要保持起始帧。
return [element, effects.backdrop].map(target => {
- const animation = target.animate(frames, {
+ const animation = target.animate(transform && !closing ?
+ [...frames.slice(0, -1), {...frames[frames.length - 1], transform: "translateX(0px)"}] : frames, {
...(source ? source.effect.getTiming() : {duration: 0}), fill: "both",
});
if (typeof source?.startTime === "number") {
@@ -152,10 +171,14 @@ export const syncDockBarVisibility = () => {
const effects = floatingBarEffects.get(item);
const direction = item.id === "dockLeft" ? -1 : 1;
if (!floating && effects) {
- if (canFloat && dock.isPanelVisible() && !dock.layout.element.classList.contains("fn__none")) {
+ if (canFloat) {
+ // 面板收起时图标栏跟随面板;面板已经消失时单独滑出,避免被瞬间移除。
+ const panelVisible = dock.isPanelVisible() && !dock.layout.element.classList.contains("fn__none");
if (!effects.closing) {
+ const barWidth = Math.max(item.clientWidth, effects.backdrop.clientWidth);
animateFloatingBar(item, getTranslateX(item),
- direction * (dock.layout.element.clientWidth + item.clientWidth + 8), true, dock.layout.element);
+ direction * (dock.layout.element.clientWidth + barWidth + 8), true,
+ panelVisible ? dock.layout.element : undefined);
}
} else {
clearFloatingBarEffects(item);
@@ -185,7 +208,7 @@ export const syncDockBarVisibility = () => {
backdrop.className = item.id === "dockLeft" ? "dock__backdrop" : "dock__backdrop dock__backdrop--right";
backdrop.setAttribute("aria-hidden", "true");
item.before(backdrop);
- floatingBarEffects.set(item, {backdrop, animations: [], closing: false, frame: 0});
+ floatingBarEffects.set(item, {backdrop, animations: [], closing: false, followsPanel: false, frame: 0});
}
const currentEffects = floatingBarEffects.get(item);
const cornerElement = document.body.classList.contains("body--toolbar-hide") ?
@@ -199,10 +222,13 @@ export const syncDockBarVisibility = () => {
currentEffects.backdrop.style.top = backgroundTop + "px";
currentEffects.backdrop.style.bottom = item.style.bottom;
currentEffects.backdrop.style.setProperty("--b3-dock-background-y", -backgroundTop + "px");
- if (floating && (!effects || effects.closing)) {
+ // 面板展开时图标栏要重新贴合面板外沿;面板已就位时保持当前状态,避免重复启动动画。
+ const needsFollow = floating && floatingPanelOpen && !currentEffects?.followsPanel &&
+ Math.abs(getTranslateX(item) - (getTranslateX(dock.layout.element) + direction * item.clientWidth)) >= 1;
+ if (floating && (!effects || effects.closing || needsFollow)) {
// 面板先进入窗口,图标栏贴着面板外沿使用相同的位移曲线滑入。
- const from = effects ? getTranslateX(item) : floatingPanelOpen ?
- getTranslateX(dock.layout.element) + direction * item.clientWidth : direction * item.clientWidth;
+ const from = floatingPanelOpen ? getTranslateX(dock.layout.element) + direction * item.clientWidth :
+ effects ? getTranslateX(item) : direction * item.clientWidth;
animateFloatingBar(item, from, 0, false, floatingPanelOpen ? dock.layout.element : undefined);
}
} else if (wasFloating) {
diff --git a/app/src/layout/dock/index.test.ts b/app/src/layout/dock/index.test.ts
index 295abad06f..8c34328e20 100644
--- a/app/src/layout/dock/index.test.ts
+++ b/app/src/layout/dock/index.test.ts
@@ -42,7 +42,9 @@ const loadDockPrototype = (frames: Array<() => void>, document: object, adjustLa
return {bindPanelTransitionGuard() {}};
}
if (name.endsWith("/barVisibility")) {
- return {syncDockBarVisibility() {}, setDockBarHover() {}};
+ return {syncDockBarVisibility() {}, setDockBarHover() {}, setDockBarPointerOver() {},
+ isDockBarPointerOver: () => false,
+ getDockBarWidth: (element: {clientWidth: number}) => element.clientWidth};
}
return {};
},
@@ -74,7 +76,7 @@ describe("dock initialization", () => {
},
};
const active = {classList, getAttribute: () => "outline"};
- const items = {clientWidth: 40, parentElement: {classList, addEventListener() {}},
+ const items = {clientWidth: 40, parentElement: {classList, clientWidth: 40, addEventListener() {}},
querySelector: () => active};
const layout = {element, children: [] as unknown[]};
const siyuan = {zIndex: 10, config: {uiLayout: {hideDock: false}},
diff --git a/app/src/layout/dock/index.ts b/app/src/layout/dock/index.ts
index 0372ee117b..d2f634df8a 100644
--- a/app/src/layout/dock/index.ts
+++ b/app/src/layout/dock/index.ts
@@ -34,7 +34,8 @@ import {syncDockEntryOrders} from "../../config/entryVisibility/runtime";
import {isWindow} from "../../util/functions";
import {bindPanelTransitionGuard} from "../../util/panelTransition";
import {isTouchHoverInput} from "../../util/hoverInput";
-import {setDockBarHover, syncDockBarVisibility} from "./barVisibility";
+import {getDockBarWidth, isDockBarPointerOver, setDockBarHover, setDockBarPointerOver,
+ syncDockBarVisibility} from "./barVisibility";
const TYPES = ["file", "outline", "inbox", "bookmark", "tag", "graph", "globalGraph", "backlink", "agentChat"];
const DEFAULT_DOCK_SIZE = 232;
@@ -133,6 +134,8 @@ export class Dock {
}
if (this.position !== "Bottom") {
this.elements[0].parentElement.addEventListener("mousedown", (event: MouseEvent) => {
+ // 按压图标栏即视为指针在栏上,收起面板时不隐藏停靠栏。
+ setDockBarPointerOver(this.elements[0].parentElement, true);
const item = hasClosestByClassName(event.target as HTMLElement, "dock__item");
if (!item || !item.getAttribute("data-type")) {
return;
@@ -628,15 +631,27 @@ export class Dock {
return;
}
+ const barElement = this.elements[0].parentElement;
+ // 停靠栏已浮出且未在收起时,面板直接从停靠栏右缘展开,避免从窗口外滑入扫过停靠栏造成回缩。
+ const showFromBarEdge = !reset && this.position !== "Bottom" &&
+ barElement.classList.contains("dock--float") && !barElement.classList.contains("dock--closing") &&
+ Number(getComputedStyle(this.layout.element).opacity) < .01;
if (!reset) {
+ if (showFromBarEdge) {
+ this.layout.element.style.transition = "none";
+ this.layout.element.style.transform = `translateX(${
+ (this.position === "Left" ? 1 : -1) * getDockBarWidth(barElement)}px)`;
+ void this.layout.element.offsetWidth;
+ this.layout.element.style.transition = "";
+ }
this.layout.element.style.opacity = "1";
}
this.layout.element.style.zIndex = (++window.siyuan.zIndex).toString();
syncDockBarVisibility();
if (this.position === "Left") {
- this.layout.element.style.transform = `translateX(${this.elements[0].clientWidth}px)`;
+ this.layout.element.style.transform = `translateX(${getDockBarWidth(this.elements[0].parentElement)}px)`;
} else if (this.position === "Right") {
- this.layout.element.style.transform = `translateX(-${this.elements[0].clientWidth}px)`;
+ this.layout.element.style.transform = `translateX(-${getDockBarWidth(this.elements[0].parentElement)}px)`;
} else if (this.position === "Bottom") {
this.layout.element.style.transform = `translateY(-${document.getElementById("status").offsetHeight}px)`;
this.layout.element.style.left = (window.siyuan.config.uiLayout.hideDock ? 0 : this.elements[0].clientWidth) + "px";
@@ -699,7 +714,9 @@ export class Dock {
this.elements[1].querySelector(".dock__item--activefocus")?.classList.remove("dock__item--activefocus");
this.layout.element.querySelector(".layout__tab--active")?.classList.remove("layout__tab--active");
if (this.position === "Left" || this.position === "Right") {
- setDockBarHover(this.elements[0].parentElement, false);
+ const barElement = this.elements[0].parentElement;
+ // 指针仍停在图标栏上时保留临时显示,否则随面板一起滑出。
+ setDockBarHover(barElement, isDockBarPointerOver(barElement));
} else {
syncDockBarVisibility();
}
diff --git a/app/tests/dockBarVisibility.test.js b/app/tests/dockBarVisibility.test.js
index a77031df10..e8c3f94158 100644
--- a/app/tests/dockBarVisibility.test.js
+++ b/app/tests/dockBarVisibility.test.js
@@ -117,19 +117,29 @@ const browserCases = async (sources, css, template, animationOnly = false) => {
await settle();
dock.hideDock();
await settle();
- const checkAttached = (message) => {
+ const checkAttached = (message, checkOpacity = true) => {
const panelRect = panel.getBoundingClientRect();
const barRect = element.getBoundingClientRect();
const gap = position === "Left" ? panelRect.left - barRect.right : barRect.left - panelRect.right;
check.ok(Math.abs(gap) < 2, `${position} ${message}: gap=${gap}`);
- check.ok(Math.abs(Number(getComputedStyle(panel).opacity) - Number(getComputedStyle(element).opacity)) < .02,
- `${position} ${message}: opacity follows the panel`);
+ if (checkOpacity) {
+ check.ok(Math.abs(Number(getComputedStyle(panel).opacity) - Number(getComputedStyle(element).opacity)) < .02,
+ `${position} ${message}: opacity follows the panel`);
+ }
check.deepEqual(rect(center.element), initialEditor, "custom animations do not resize the editor");
};
const finish = async () => {
await Promise.all([...panel.getAnimations(), ...element.getAnimations()].map(animation => animation.finished));
await nextFrames();
};
+ // 动画结束后必须回到停靠栏原始位置,否则边缘会留出缝隙并在动画释放时跳动。
+ const checkResting = (message) => {
+ const barRect = element.getBoundingClientRect();
+ const edge = position === "Left" ? barRect.left : window.innerWidth - barRect.right;
+ check.ok(Math.abs(edge) < 1, `${position} ${message}: resting edge=${edge}`);
+ check.equal(getComputedStyle(element).transform, "none",
+ `${position} ${message}: no residual translation`);
+ };
for (const transition of ["none", "transform 600ms linear, opacity 50ms linear",
"transform 600ms linear", "opacity 300ms linear", "transform 350ms linear 120ms, opacity 50ms linear 120ms"]) {
panel.style.transition = transition;
@@ -143,6 +153,7 @@ const browserCases = async (sources, css, template, animationOnly = false) => {
checkAttached(`entry after opacity completes with ${transition}`);
}
await finish();
+ checkResting(`entry with ${transition}`);
dock.hideDock();
await nextFrames();
if (transition === "none") {
@@ -173,6 +184,23 @@ const browserCases = async (sources, css, template, animationOnly = false) => {
await nextFrames();
await finish();
check.ok(hidden(element));
+ // 面板在停靠栏已经浮出时展开,停靠栏仍需贴着面板外沿一起移动。
+ bar.setDockBarHover(element, true);
+ await nextFrames();
+ check.ok(!hidden(element) && element.classList.contains("dock--float"),
+ "the edge reveals the bar before the panel opens");
+ await finish();
+ dock.showDock();
+ await nextFrames();
+ // 停靠栏已在位时面板只淡入,停靠栏不参与透明度过渡。
+ checkAttached("panel opens while the bar is already floating", false);
+ await finish();
+ checkResting("panel opens while the bar is already floating");
+ bar.setDockBarHover(element, false);
+ dock.hideDock();
+ await nextFrames();
+ await finish();
+ check.ok(hidden(element));
panel.style.transition = "";
}
return "Floating dock bar cases passed";
@@ -214,11 +242,15 @@ const browserCases = async (sources, css, template, animationOnly = false) => {
check.equal(dock.layout.element.style.opacity, "1", "clicking a revealed entry opens its panel");
check.ok(!hidden(element) && hidden(otherBar), "only the expanded side reveals its bar");
check.equal(getComputedStyle(element).position, "fixed");
+ check.equal(getComputedStyle(element).transform, "none",
+ "clicking a revealed entry does not restart the bar from the window edge");
check.deepEqual(rect(center.element), initialEditor, "revealing a bar and panel does not resize the editor");
const panelRect = dock.layout.element.getBoundingClientRect();
const barRect = element.getBoundingClientRect();
check.equal(position === "Left" ? panelRect.left : panelRect.right,
position === "Left" ? barRect.right : barRect.left, "panel stays beside the bar");
+ check.equal(getComputedStyle(element).transform, "none",
+ "the settled bar keeps no residual translation, so no gap opens at the window edge");
const temporary = {bar: rect(element), panel: rect(dock.layout.element),
barStyle: appearance(element), panelStyle: appearance(dock.layout.element)};
const backdrop = element.previousElementSibling;
@@ -294,8 +326,12 @@ const browserCases = async (sources, css, template, animationOnly = false) => {
check.ok(hidden(entries[0]) && !hidden(element), "profile-hidden entries stay hidden during refresh");
entries[0].classList.remove("fn__none");
- dock.hideDockByHover();
+ // 指针离开图标栏后,位置刷新不应取消待执行的收起。
+ center.element.dispatchEvent(new MouseEvent("mousemove", {bubbles: true,
+ clientX: window.innerWidth / 2, clientY: 200}));
dock.showDock(true);
+ // 悬停收起要等延迟计时和面板过渡都结束,位置刷新不得打断它们。
+ await settle();
await settle();
check.ok(hidden(element), "position refreshes do not cancel a pending hover close");
dock.showDock();
@@ -345,6 +381,10 @@ const browserCases = async (sources, css, template, animationOnly = false) => {
check.deepEqual(rect(center.element), initialEditor, "closing a temporary bar restores the editor space");
dock.showDock();
dock.togglePanel(false);
+ await new Promise(resolve => requestAnimationFrame(resolve));
+ check.ok(!hidden(element) && element.getAnimations().length > 0,
+ "explicitly collapsing a panel slides its temporary bar out instead of removing it at once");
+ await settle();
check.ok(hidden(element), "explicitly collapsing a panel clears its temporary bar");
center.element.dispatchEvent(new MouseEvent("mousemove", {bubbles: true,
clientX: position === "Left" ? 1 : window.innerWidth - 1, clientY: 200}));
@@ -361,13 +401,25 @@ const browserCases = async (sources, css, template, animationOnly = false) => {
dock.showDock();
await settle();
check.ok(!hidden(element));
+ // 指针停在图标栏上时点击已打开的图标收起面板,停靠栏保持可见以便继续切换工具。
+ element.dispatchEvent(new MouseEvent("mousemove", {bubbles: true,
+ clientX: position === "Left" ? 1 : window.innerWidth - 1, clientY: 200}));
+ await settle();
dock.toggleModel(position + "two", false, true, false, false);
await settle();
- check.ok(hidden(element), "closing the final active item hides the temporary bar");
+ check.ok(!hidden(element), "the pointer over the bar keeps the temporary bar visible after the panel closes");
+ center.element.dispatchEvent(new MouseEvent("mousemove", {bubbles: true,
+ clientX: window.innerWidth / 2, clientY: 200}));
+ await new Promise(resolve => setTimeout(resolve, 60));
+ check.ok(!hidden(element) && element.getAnimations().length > 0,
+ "leaving the bar slides it out instead of removing it at once");
+ await settle();
+ check.ok(hidden(element), "leaving the bar hides the temporary bar after the panel closes");
center.element.dispatchEvent(new MouseEvent("mousemove", {bubbles: true,
clientX: position === "Left" ? 1 : window.innerWidth - 1, clientY: 200}));
await settle();
check.ok(!hidden(element), "the edge remains usable after closing the last panel");
+ bar.setDockBarHover(element, false);
dock.hideDock();
}
document.removeEventListener("mousemove", move);