Skip to content

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

:art: Improve responsive dock state transitions https://github.com/siyuan-note/siyuan/issues/12440

Danielee31dbe94d69
diff --git a/app/src/assets/scss/business/_layout.scss b/app/src/assets/scss/business/_layout.scss
index 2897543a78..e2393e54fd 100644
--- a/app/src/assets/scss/business/_layout.scss
+++ b/app/src/assets/scss/business/_layout.scss
@@ -26,6 +26,10 @@
overflow: hidden;
}
+ &--no-transition {
+ transition: none !important;
+ }
+
&__dockl + .layout__resize {
margin: 0 0 0 var(--b3-layout-space-margin);
width: var(--b3-layout-space);
diff --git a/app/src/layout/dock/index.ts b/app/src/layout/dock/index.ts
index 66eafbfdce..35a5b0264f 100644
--- a/app/src/layout/dock/index.ts
+++ b/app/src/layout/dock/index.ts
@@ -341,14 +341,14 @@ export class Dock {
this.responsiveManualOverride = false;
}
- public setResponsiveFloating(value: boolean) {
+ public setResponsiveFloating(value: boolean, preferredSize: number) {
const responsiveFloating = value && this.position !== "Bottom" && this.pin;
if (this.responsiveFloating === responsiveFloating) {
return false;
}
- this.restoreResponsivePreferredSize();
+ this.restoreResponsivePreferredSize(preferredSize);
this.responsiveFloating = responsiveFloating;
- this.applyFloatingState(false);
+ this.applyFloatingState(false, preferredSize);
return true;
}
@@ -478,14 +478,14 @@ export class Dock {
this.applyFloatingState(true);
}
- private applyFloatingState(isSaveLayout: boolean) {
+ private applyFloatingState(isSaveLayout: boolean, preferredSize?: number) {
this.clearDockHoverTimeout();
const hasActive = this.hasActive();
- this.resetDockPosition(hasActive);
+ this.resetDockPosition(hasActive, preferredSize);
this.layout.element.style.opacity = "";
if (this.isFloating()) {
this.resizeElement.classList.add("fn__none");
- this.hideDock(true);
+ this.hideDock(true, preferredSize);
} else {
this.layout.element.style.transform = "";
this.layout.element.style.zIndex = "";
@@ -498,11 +498,12 @@ export class Dock {
setTabPosition(true);
}
- private restoreResponsivePreferredSize() {
+ private restoreResponsivePreferredSize(preferredSize?: number) {
if (this.position === "Bottom") {
return;
}
- const responsiveWidth = parseInt(this.layout.element.getAttribute(Constants.ATTRIBUTE_DOCK_WIDTH));
+ const responsiveWidth = typeof preferredSize === "number" && preferredSize > 0 ? preferredSize :
+ parseInt(this.layout.element.getAttribute(Constants.ATTRIBUTE_DOCK_WIDTH));
this.layout.element.style.maxWidth = "";
this.layout.element.removeAttribute(Constants.ATTRIBUTE_DOCK_WIDTH);
if (responsiveWidth > 0) {
@@ -510,8 +511,8 @@ export class Dock {
}
}
- private resetDockPosition(show: boolean) {
- const size = this.getCurrentSize();
+ private resetDockPosition(show: boolean, preferredSize?: number) {
+ const size = typeof preferredSize === "number" && preferredSize > 0 ? preferredSize : this.getCurrentSize();
if (this.position === "Left") {
this.layout.element.setAttribute("style", `${show ? "margin-right: var(--b3-layout-space);" : ""}width:${size}px;opacity:${show ? 1 : 0};min-height:8px;`);
} else if (this.position === "Right") {
@@ -596,7 +597,7 @@ export class Dock {
}
}
- public hideDock(reset = false) {
+ public hideDock(reset = false, preferredSize?: number) {
this.clearDockHoverTimeout();
if (!reset && (this.layout.element.style.opacity === "0" || !this.isFloating())) {
return;
@@ -621,12 +622,13 @@ export class Dock {
) {
return;
}
+ const size = typeof preferredSize === "number" && preferredSize > 0 ? preferredSize : this.getCurrentSize();
if (this.position === "Left") {
- this.layout.element.style.transform = `translateX(-${this.getCurrentSize() + 8}px)`;
+ this.layout.element.style.transform = `translateX(-${size + 8}px)`;
} else if (this.position === "Right") {
- this.layout.element.style.transform = `translateX(${this.getCurrentSize() + 8}px)`;
+ this.layout.element.style.transform = `translateX(${size + 8}px)`;
} else if (this.position === "Bottom") {
- this.layout.element.style.transform = `translateY(${this.getCurrentSize() + 8}px)`;
+ this.layout.element.style.transform = `translateY(${size + 8}px)`;
}
if (reset) {
return;
diff --git a/app/src/layout/dock/responsive.ts b/app/src/layout/dock/responsive.ts
index ba5838a8a0..9170bef0e1 100644
--- a/app/src/layout/dock/responsive.ts
+++ b/app/src/layout/dock/responsive.ts
@@ -4,6 +4,7 @@ import {
type ICenterMinimumLayoutNode,
resolveDockResponsiveLayout,
} from "./responsiveLayout";
+import {runWithoutDockTransitions} from "./responsiveTransition";
const EDITOR_MINIMUM_WIDTH = 480;
const RESPONSIVE_HYSTERESIS = 32;
@@ -43,27 +44,35 @@ const getLayoutGap = () => {
return Number.isFinite(value) ? Math.max(0, value) : 0;
};
-const clearResponsiveWidth = (dock: typeof window.siyuan.layout.leftDock, preferredSize: number) => {
- const element = dock.layout.element;
- const hasResponsiveWidth = Boolean(element.style.maxWidth ||
- element.hasAttribute(Constants.ATTRIBUTE_DOCK_WIDTH));
- element.style.maxWidth = "";
- element.removeAttribute(Constants.ATTRIBUTE_DOCK_WIDTH);
- if (hasResponsiveWidth && preferredSize > 0) {
- element.style.width = preferredSize + "px";
- }
-};
-
-const applyResponsiveWidth = (
+const updateResponsiveWidth = (
dock: typeof window.siyuan.layout.leftDock,
preferredSize: number,
fixedSize: number,
) => {
- if (dock.isResponsiveFloating() || fixedSize <= 0 || fixedSize >= preferredSize) {
+ const element = dock.layout.element;
+ const hasResponsiveWidth = Boolean(element.style.maxWidth ||
+ element.hasAttribute(Constants.ATTRIBUTE_DOCK_WIDTH));
+ const shouldConstrain = !dock.isResponsiveFloating() && fixedSize > 0 && fixedSize < preferredSize;
+ if (!shouldConstrain) {
+ if (!hasResponsiveWidth) {
+ return;
+ }
+ element.style.maxWidth = "";
+ element.removeAttribute(Constants.ATTRIBUTE_DOCK_WIDTH);
+ const preferredWidth = preferredSize + "px";
+ if (preferredSize > 0 && element.style.width !== preferredWidth) {
+ element.style.width = preferredWidth;
+ }
return;
}
- dock.layout.element.setAttribute(Constants.ATTRIBUTE_DOCK_WIDTH, preferredSize.toString());
- dock.layout.element.style.maxWidth = fixedSize + "px";
+ const preferredWidth = preferredSize.toString();
+ const maximumWidth = fixedSize + "px";
+ if (element.getAttribute(Constants.ATTRIBUTE_DOCK_WIDTH) !== preferredWidth) {
+ element.setAttribute(Constants.ATTRIBUTE_DOCK_WIDTH, preferredWidth);
+ }
+ if (element.style.maxWidth !== maximumWidth) {
+ element.style.maxWidth = maximumWidth;
+ }
};
export const reconcileResponsiveDockLayout = () => {
@@ -127,28 +136,39 @@ export const reconcileResponsiveDockLayout = () => {
},
});
- clearResponsiveWidth(leftDock, leftPreferredSize);
- clearResponsiveWidth(rightDock, rightPreferredSize);
-
- // 拉宽时先恢复左侧,再恢复右侧。
- if (!result.left.autoFloating && leftDock.isResponsiveFloating()) {
- leftDock.setResponsiveFloating(false);
- }
- if (!result.right.autoFloating && rightDock.isResponsiveFloating()) {
- rightDock.setResponsiveFloating(false);
- }
- // 缩窄时先浮动右侧,再浮动左侧。
- if (result.right.autoFloating && !rightDock.isResponsiveFloating()) {
- rightDock.setResponsiveFloating(true);
- }
- if (result.left.autoFloating && !leftDock.isResponsiveFloating()) {
- leftDock.setResponsiveFloating(true);
- }
-
- applyResponsiveWidth(leftDock, leftPreferredSize, result.left.fixedSize);
- applyResponsiveWidth(rightDock, rightPreferredSize, result.right.fixedSize);
- if (centerLayout.element.clientWidth !== centerSizeBefore && hasCenterMaximumWidth(centerLayout)) {
- leftDock.adjustResponsiveCenterLayout();
+ const floatingStateChanged = result.left.autoFloating !== leftDock.isResponsiveFloating() ||
+ result.right.autoFloating !== rightDock.isResponsiveFloating();
+ const applyResult = () => {
+ // 拉宽时先恢复左侧,再恢复右侧。
+ if (!result.left.autoFloating && leftDock.isResponsiveFloating()) {
+ leftDock.setResponsiveFloating(false, leftPreferredSize);
+ }
+ if (!result.right.autoFloating && rightDock.isResponsiveFloating()) {
+ rightDock.setResponsiveFloating(false, rightPreferredSize);
+ }
+ // 缩窄时先浮动右侧,再浮动左侧。
+ if (result.right.autoFloating && !rightDock.isResponsiveFloating()) {
+ rightDock.setResponsiveFloating(true, rightPreferredSize);
+ }
+ if (result.left.autoFloating && !leftDock.isResponsiveFloating()) {
+ leftDock.setResponsiveFloating(true, leftPreferredSize);
+ }
+
+ updateResponsiveWidth(leftDock, leftPreferredSize, result.left.fixedSize);
+ updateResponsiveWidth(rightDock, rightPreferredSize, result.right.fixedSize);
+ if (centerLayout.element.clientWidth !== centerSizeBefore && hasCenterMaximumWidth(centerLayout)) {
+ leftDock.adjustResponsiveCenterLayout();
+ }
+ };
+ if (floatingStateChanged) {
+ runWithoutDockTransitions([
+ leftDock.layout.element,
+ rightDock.layout.element,
+ ], applyResult, () => {
+ void outerLayout.element.offsetWidth;
+ });
+ } else {
+ applyResult();
}
} finally {
applying = false;
diff --git a/app/src/layout/dock/responsiveTransition.test.ts b/app/src/layout/dock/responsiveTransition.test.ts
new file mode 100644
index 0000000000..50bd6c3cc6
--- /dev/null
+++ b/app/src/layout/dock/responsiveTransition.test.ts
@@ -0,0 +1,64 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {
+ DOCK_TRANSITION_DISABLED_CLASS,
+ runWithoutDockTransitions,
+} from "./responsiveTransition";
+
+const createElement = (...initialClasses: string[]) => {
+ const classes = new Set(initialClasses);
+ return {
+ element: {
+ classList: {
+ add: (...tokens: string[]) => tokens.forEach((token) => classes.add(token)),
+ contains: (token: string) => classes.has(token),
+ remove: (...tokens: string[]) => tokens.forEach((token) => classes.delete(token)),
+ },
+ } as unknown as HTMLElement,
+ hasClass: (className: string) => classes.has(className),
+ };
+};
+
+describe("responsive dock transition", () => {
+ it("keeps both docks transition-free until the final layout is flushed", () => {
+ const left = createElement();
+ const right = createElement();
+ const events: string[] = [];
+
+ runWithoutDockTransitions([left.element, right.element], () => {
+ assert.equal(left.hasClass(DOCK_TRANSITION_DISABLED_CLASS), true);
+ assert.equal(right.hasClass(DOCK_TRANSITION_DISABLED_CLASS), true);
+ events.push("apply");
+ }, () => {
+ assert.equal(left.hasClass(DOCK_TRANSITION_DISABLED_CLASS), true);
+ assert.equal(right.hasClass(DOCK_TRANSITION_DISABLED_CLASS), true);
+ events.push("flush");
+ });
+
+ assert.deepEqual(events, ["apply", "flush"]);
+ assert.equal(left.hasClass(DOCK_TRANSITION_DISABLED_CLASS), false);
+ assert.equal(right.hasClass(DOCK_TRANSITION_DISABLED_CLASS), false);
+ });
+
+ it("preserves a transition suppression class owned by another operation", () => {
+ const element = createElement(DOCK_TRANSITION_DISABLED_CLASS);
+
+ runWithoutDockTransitions([element.element], () => undefined, () => undefined);
+
+ assert.equal(element.hasClass(DOCK_TRANSITION_DISABLED_CLASS), true);
+ });
+
+ it("restores transition classes when applying the layout fails", () => {
+ const element = createElement();
+ let flushed = false;
+
+ assert.throws(() => runWithoutDockTransitions([element.element], () => {
+ throw new Error("layout failed");
+ }, () => {
+ flushed = true;
+ assert.equal(element.hasClass(DOCK_TRANSITION_DISABLED_CLASS), true);
+ }), /layout failed/);
+ assert.equal(flushed, true);
+ assert.equal(element.hasClass(DOCK_TRANSITION_DISABLED_CLASS), false);
+ });
+});
diff --git a/app/src/layout/dock/responsiveTransition.ts b/app/src/layout/dock/responsiveTransition.ts
new file mode 100644
index 0000000000..86a6d85447
--- /dev/null
+++ b/app/src/layout/dock/responsiveTransition.ts
@@ -0,0 +1,20 @@
+export const DOCK_TRANSITION_DISABLED_CLASS = "layout--no-transition";
+
+export const runWithoutDockTransitions = (
+ elements: HTMLElement[],
+ apply: () => void,
+ flush: () => void,
+) => {
+ const addedElements = Array.from(new Set(elements)).filter((element) =>
+ !element.classList.contains(DOCK_TRANSITION_DISABLED_CLASS));
+ addedElements.forEach((element) => element.classList.add(DOCK_TRANSITION_DISABLED_CLASS));
+ try {
+ apply();
+ } finally {
+ try {
+ flush();
+ } finally {
+ addedElements.forEach((element) => element.classList.remove(DOCK_TRANSITION_DISABLED_CLASS));
+ }
+ }
+};