Skip to content

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

:bug: Prevent floating docks from flashing during startup https://github.com/siyuan-note/siyuan/issues/20637

Danielab2dbc11aec4
diff --git a/app/src/layout/dock/index.test.ts b/app/src/layout/dock/index.test.ts
index 0095aaca34..0c228c9ed7 100644
--- a/app/src/layout/dock/index.test.ts
+++ b/app/src/layout/dock/index.test.ts
@@ -8,7 +8,7 @@ import {resolveDockPanelVisibility} from "./panelVisibility";
const loadDockPrototype = (frames: Array<() => void>, document: object, adjustLayout: () => void,
resizeTabs: () => void, isTouchHoverInput = () => false,
- timers: Array<() => void> = []) => {
+ timers: Array<() => void> = [], siyuan: object = {}) => {
const exports: {Dock?: {prototype: object}} = {};
const source = ts.transpileModule(readFileSync(join(__dirname, "index.ts"), "utf8"), {
compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},
@@ -16,7 +16,8 @@ const loadDockPrototype = (frames: Array<() => void>, document: object, adjustLa
runInNewContext(source, {
exports,
document,
- window: {setTimeout: (callback: () => void) => timers.push(callback), clearTimeout() {}},
+ window: {siyuan, setTimeout: (callback: () => void) => timers.push(callback), clearTimeout() {}},
+ setTimeout: (callback: () => void) => timers.push(callback),
requestAnimationFrame: (callback: () => void) => frames.push(callback),
require: (name: string) => {
if (name === "../util") {
@@ -37,12 +38,86 @@ const loadDockPrototype = (frames: Array<() => void>, document: object, adjustLa
if (name.endsWith("/hoverInput")) {
return {isTouchHoverInput};
}
+ if (name.endsWith("/panelTransition")) {
+ return {bindPanelTransitionGuard() {}};
+ }
return {};
},
});
return exports.Dock.prototype;
};
+describe("dock initialization", () => {
+ for (const position of ["Left", "Right", "Bottom"] as const) {
+ for (const pin of [false, true]) {
+ it(`${position} ${pin ? "fixed" : "floating"} panel stays behind loading until positioned`, () => {
+ const timers: Array<() => void> = [];
+ const classes = new Set<string>();
+ const classList = {
+ add: (...names: string[]) => names.forEach(name => classes.add(name)),
+ contains: (name: string) => classes.has(name),
+ remove: (...names: string[]) => names.forEach(name => classes.delete(name)),
+ };
+ const style = {opacity: "", transform: "", zIndex: ""};
+ const element = {
+ style, classList, clientWidth: 260, clientHeight: 240,
+ nextElementSibling: {classList}, previousElementSibling: {classList},
+ insertAdjacentHTML() {}, addEventListener() {}, contains: () => false,
+ querySelector: (selector: string) => selector === ".layout__dockresize" ? {addEventListener() {}} : null,
+ setAttribute: (_name: string, value: string) => {
+ style.opacity = /opacity:([^;]+)/.exec(value)?.[1] || "";
+ style.transform = "";
+ style.zIndex = "";
+ },
+ };
+ const active = {classList, getAttribute: () => "outline"};
+ const items = {clientWidth: 40, parentElement: {classList, addEventListener() {}},
+ querySelector: () => active};
+ const layout = {element, children: [] as unknown[]};
+ const siyuan = {zIndex: 10, config: {uiLayout: {hideDock: false}},
+ layout: {layout: {children: [{children: [layout, {}, layout]}, layout]}}};
+ const document = {activeElement: null as HTMLElement | null,
+ querySelector: (): null => null,
+ querySelectorAll: () => [items, items],
+ getElementById: (id: string) => id === "status" ? {offsetHeight: 24} : {lastElementChild: items},
+ };
+ const prototype = loadDockPrototype([], document, () => {}, () => {}, () => false, timers, siyuan);
+ let restored = 0;
+ Object.assign(prototype, {toggleModel() {
+ restored++;
+ this.showDock();
+ }});
+ const Dock = prototype.constructor as typeof import("./index").Dock;
+ const dock = new Dock({app: {} as import("../../index").App,
+ position, data: {pin, data: []}});
+ assert.equal(restored, 2);
+ assert.equal(style.opacity, pin ? "" : "0");
+ assert.equal(style.zIndex, "");
+ assert.equal(siyuan.zIndex, 10);
+ assert.equal(timers.length, pin ? 0 : 1);
+ timers.forEach(callback => callback());
+ if (!pin) {
+ assert.ok(classes.has("layout--float"));
+ assert.equal(style.opacity, "0");
+ assert.equal(style.transform, position === "Left" ? "translateX(-268px)" :
+ position === "Right" ? "translateX(268px)" : "translateY(248px)");
+ }
+ dock.showDock();
+ assert.equal(style.opacity, pin ? "" : "1");
+ assert.equal(siyuan.zIndex, pin ? 10 : 11);
+ if (!pin) {
+ assert.equal(style.transform, position === "Left" ? "translateX(40px)" :
+ position === "Right" ? "translateX(-40px)" : "translateY(-24px)");
+ dock.hideDock();
+ assert.equal(style.opacity, "0");
+ assert.equal(style.transform, position === "Left" ? "translateX(-268px)" :
+ position === "Right" ? "translateX(268px)" : "translateY(248px)");
+ }
+ });
+ }
+ }
+});
+
it("touch compatibility hover skips dock timers while mouse hover remains available", () => {
let touch = true;
const timers: Array<() => void> = [];
diff --git a/app/src/layout/dock/index.ts b/app/src/layout/dock/index.ts
index 1cdfa87ed9..7451c9698f 100644
--- a/app/src/layout/dock/index.ts
+++ b/app/src/layout/dock/index.ts
@@ -56,6 +56,7 @@ export class Dock {
private collapsedPanelSize = "";
private responsiveFloating = false;
private responsiveManualOverride = false;
+ private initializing = true;
constructor(options: {
app: App,
@@ -91,6 +92,10 @@ export class Dock {
this.app = options.app;
this.position = options.position;
this.pin = options.data.pin;
+ // 浮动面板完成定位前保持隐藏,避免恢复内容时覆盖启动遮罩。
+ if (!this.pin) {
+ this.layout.element.style.opacity = "0";
+ }
bindPanelTransitionGuard(this.layout.element, () => this.isFloating());
this.data = {};
if (options.data.data[0]) {
@@ -327,7 +332,10 @@ export class Dock {
this.hideDock(true);
this.layout.element.classList.add("layout--float");
this.resizeElement.classList.add("fn__none");
+ this.initializing = false;
}); // 需等待所有 Dock 初始化完成后才有稳定布局,才可进行定位
+ } else {
+ this.initializing = false;
}
}
@@ -579,7 +587,7 @@ export class Dock {
public showDock(reset = false) {
this.clearDockHoverTimeout();
- if (!this.panelVisible || (!reset && (!this.isFloating() || this.layout.element.style.opacity === "1")) ||
+ if (this.initializing || !this.panelVisible || (!reset && (!this.isFloating() || this.layout.element.style.opacity === "1")) ||
(!this.elements[0].querySelector(".dock__item--active") && !this.elements[1].querySelector(".dock__item--active"))
) {
return;