Skip to content

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

:art: https://github.com/siyuan-note/siyuan/pull/18288

Vanessa3c3d796b1ff1
diff --git a/app/src/assets/scss/main/_main.scss b/app/src/assets/scss/main/_main.scss
index b149e6cb1b..d8048e08ec 100644
--- a/app/src/assets/scss/main/_main.scss
+++ b/app/src/assets/scss/main/_main.scss
@@ -282,13 +282,6 @@ html[data-frontend="browser-desktop"] {
transition: var(--b3-transition);
}
- &__sliderDown,
- &__sliderUp {
- height: 0;
- overflow: hidden;
- transition: var(--b3-transition);
- }
-
&__item--icon-expand {
> .b3-list-item__toggle {
position: relative;
diff --git a/app/src/assets/scss/main/_mobile.scss b/app/src/assets/scss/main/_mobile.scss
index baf4f54699..62a4a3e419 100644
--- a/app/src/assets/scss/main/_mobile.scss
+++ b/app/src/assets/scss/main/_mobile.scss
@@ -73,13 +73,6 @@
}
}
-.file-tree__sliderDown,
-.file-tree__sliderUp {
- height: 0;
- overflow: hidden;
- transition: var(--b3-transition);
-}
-
.counter {
margin-left: 4px;
padding: 0 8px;
diff --git a/app/src/layout/dock/Files.ts b/app/src/layout/dock/Files.ts
index 0ab57829e4..9f2d62ad74 100644
--- a/app/src/layout/dock/Files.ts
+++ b/app/src/layout/dock/Files.ts
@@ -33,6 +33,12 @@ import {ipcRenderer} from "electron";
import {hideTooltip, showTooltip} from "../../dialog/tooltip";
import {selectOpenTab} from "./util";
import {hideDragTip, showDragTip, transparentImgSrc} from "../../protyle/util/dragTip";
+import {
+ cancelFileTreeCollapse,
+ collapseFileTree,
+ expandFileTree,
+ isFileTreeCollapsing
+} from "./fileTreeAnimation";
export class Files extends Model {
public element: HTMLElement;
@@ -248,8 +254,8 @@ export class Files extends Model {
} else if (isNotCtrl(event) && target.classList.contains("b3-list-item__toggle")) {
const liElement = target.parentElement;
if (liElement.querySelector(".b3-list-item__arrow--open")) {
- this.collapseLeaf(liElement);
- } else if ((liElement.nextElementSibling as HTMLElement)?.dataset.collapsing !== "true") {
+ collapseFileTree(liElement, () => this.getOpenPaths());
+ } else if (!isFileTreeCollapsing(liElement)) {
this.getLeaf(liElement, notebookId);
}
event.preventDefault();
@@ -1338,18 +1344,15 @@ data-type="navigation-root" data-path="/" data-count="${item.subFileCount || 0}"
return;
}
liElement.querySelector(".b3-list-item__arrow").classList.add("b3-list-item__arrow--open");
- liElement.insertAdjacentHTML("afterend", `<ul class="file-tree__sliderDown">${fileHTML}</ul>`);
+ liElement.insertAdjacentHTML("afterend", `<ul>${fileHTML}</ul>`);
nextElement = liElement.nextElementSibling;
- setTimeout(() => {
- nextElement.setAttribute("style", `top: -1px;position: relative;height:${nextElement.childElementCount * (liElement.clientHeight + 1) - 1}px;`);
- setTimeout(() => {
- nextElement.classList.remove("file-tree__sliderDown");
- nextElement.removeAttribute("style");
- if (typeof scrollTop === "number") {
- this.element.scroll({top: scrollTop, behavior: "smooth"});
- }
- }, 200);
- }, 2);
+ nextElement.setAttribute("style", "top: -1px;position: relative;");
+ expandFileTree(nextElement as HTMLElement, () => {
+ nextElement.removeAttribute("style");
+ if (typeof scrollTop === "number") {
+ this.element.scroll({top: scrollTop, behavior: "smooth"});
+ }
+ });
this.refreshPublishAccessSwitch();
}
@@ -1416,40 +1419,15 @@ data-type="navigation-root" data-path="/" data-count="${item.subFileCount || 0}"
}
}
- private collapseLeaf(liElement: Element) {
- const toggleElement = liElement.querySelector(".b3-list-item__arrow");
- const leafElement = liElement.nextElementSibling as HTMLElement;
- toggleElement.classList.remove("b3-list-item__arrow--open");
- if (leafElement?.tagName !== "UL") {
- this.getOpenPaths();
- return;
- }
-
- // 仅当手动点击折叠按钮时才需要动画,其他情况下默认直接移除列表容器。
- leafElement.dataset.collapsing = "true";
- leafElement.style.height = `${leafElement.scrollHeight}px`;
- leafElement.style.overflow = "hidden";
- leafElement.classList.add("file-tree__sliderUp");
- window.setTimeout(() => {
- leafElement.style.height = "0";
- window.setTimeout(() => {
- if (!leafElement.isConnected) {
- return;
- }
- leafElement.remove();
- this.getOpenPaths();
- }, 200);
- }, 2);
- }
-
public getLeaf(liElement: Element, notebookId: string, focusUpdate = false) {
const toggleElement = liElement.querySelector(".b3-list-item__arrow");
- const leafElement = liElement.nextElementSibling as HTMLElement;
- if (leafElement?.dataset.collapsing === "true") {
- leafElement.remove();
+ if (cancelFileTreeCollapse(liElement)) {
this.getOpenPaths();
- return;
+ if (!focusUpdate) {
+ return;
+ }
}
+ const leafElement = liElement.nextElementSibling as HTMLElement;
if (toggleElement.classList.contains("b3-list-item__arrow--open") && !focusUpdate) {
toggleElement.classList.remove("b3-list-item__arrow--open");
if (leafElement?.tagName === "UL") {
diff --git a/app/src/layout/dock/fileTreeAnimation.test.ts b/app/src/layout/dock/fileTreeAnimation.test.ts
new file mode 100644
index 0000000000..7e6ca9ef69
--- /dev/null
+++ b/app/src/layout/dock/fileTreeAnimation.test.ts
@@ -0,0 +1,180 @@
+import {after, before, beforeEach, describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {
+ cancelFileTreeCollapse,
+ collapseFileTree,
+ expandFileTree,
+ isFileTreeCollapsing
+} from "./fileTreeAnimation";
+
+let reduceMotion = false;
+const originalWindow = Object.getOwnPropertyDescriptor(globalThis, "window");
+
+before(() => {
+ Object.defineProperty(globalThis, "window", {
+ configurable: true,
+ value: {
+ matchMedia: () => ({matches: reduceMotion}),
+ },
+ });
+});
+
+beforeEach(() => {
+ reduceMotion = false;
+});
+
+after(() => {
+ if (originalWindow) {
+ Object.defineProperty(globalThis, "window", originalWindow);
+ } else {
+ Reflect.deleteProperty(globalThis, "window");
+ }
+});
+
+const createAnimation = () => {
+ let resolve!: () => void;
+ let reject!: (reason?: unknown) => void;
+ let canceled = false;
+ const finished = new Promise<void>((resolvePromise, rejectPromise) => {
+ resolve = resolvePromise;
+ reject = rejectPromise;
+ });
+ const animation = {
+ finished,
+ cancel() {
+ canceled = true;
+ reject(new Error("Animation canceled"));
+ },
+ } as unknown as Animation;
+ return {
+ animation,
+ finish: resolve,
+ fail: reject,
+ isCanceled: () => canceled,
+ };
+};
+
+const createTreeElements = () => {
+ const animation = createAnimation();
+ let frames!: Keyframe[] | PropertyIndexedKeyframes;
+ let options!: number | KeyframeAnimationOptions;
+ let arrowClosed = false;
+ const style = {
+ overflow: "",
+ removeProperty(property: string) {
+ if (property === "overflow") {
+ this.overflow = "";
+ }
+ return "";
+ },
+ } as unknown as CSSStyleDeclaration;
+ const leafElement = {
+ tagName: "UL",
+ scrollHeight: 96,
+ style,
+ isConnected: true,
+ animate(animationFrames: Keyframe[] | PropertyIndexedKeyframes, animationOptions?: number | KeyframeAnimationOptions) {
+ frames = animationFrames;
+ options = animationOptions;
+ return animation.animation;
+ },
+ remove() {
+ this.isConnected = false;
+ },
+ } as unknown as HTMLElement;
+ const liElement = {
+ nextElementSibling: leafElement,
+ querySelector() {
+ return {
+ classList: {
+ remove() {
+ arrowClosed = true;
+ },
+ },
+ };
+ },
+ } as unknown as Element;
+ return {
+ animation,
+ leafElement,
+ liElement,
+ getFrames: () => frames,
+ getOptions: () => options as KeyframeAnimationOptions,
+ isArrowClosed: () => arrowClosed,
+ };
+};
+
+describe("fileTreeAnimation", () => {
+ it("expands to the actual content height and restores overflow", async () => {
+ const tree = createTreeElements();
+ let finished = false;
+ expandFileTree(tree.leafElement, () => {
+ finished = true;
+ });
+
+ assert.deepEqual(tree.getFrames(), [{height: "0"}, {height: "96px"}]);
+ assert.equal(tree.getOptions().duration, 200);
+ assert.equal(tree.leafElement.style.overflow, "hidden");
+
+ tree.animation.finish();
+ await tree.animation.animation.finished;
+ assert.equal(tree.animation.isCanceled(), true);
+ assert.equal(tree.leafElement.style.overflow, "");
+ assert.equal(finished, true);
+ });
+
+ it("uses no duration when reduced motion is requested", async () => {
+ reduceMotion = true;
+ const tree = createTreeElements();
+ expandFileTree(tree.leafElement);
+ assert.equal(tree.getOptions().duration, 0);
+ tree.animation.finish();
+ await tree.animation.animation.finished;
+ });
+
+ it("collapses once and removes the child list after the animation", async () => {
+ const tree = createTreeElements();
+ let finishCount = 0;
+ collapseFileTree(tree.liElement, () => finishCount++);
+ collapseFileTree(tree.liElement, () => finishCount++);
+
+ assert.equal(tree.isArrowClosed(), true);
+ assert.equal(isFileTreeCollapsing(tree.liElement), true);
+ assert.deepEqual(tree.getFrames(), [{height: "96px"}, {height: "0"}]);
+
+ tree.animation.finish();
+ await tree.animation.animation.finished;
+ assert.equal(tree.leafElement.isConnected, false);
+ assert.equal(isFileTreeCollapsing(tree.liElement), false);
+ assert.equal(finishCount, 1);
+ });
+
+ it("cancels and removes an in-progress collapse", async () => {
+ const tree = createTreeElements();
+ let finished = false;
+ collapseFileTree(tree.liElement, () => {
+ finished = true;
+ });
+
+ assert.equal(cancelFileTreeCollapse(tree.liElement), true);
+ await assert.rejects(tree.animation.animation.finished);
+ assert.equal(tree.animation.isCanceled(), true);
+ assert.equal(tree.leafElement.isConnected, false);
+ assert.equal(isFileTreeCollapsing(tree.liElement), false);
+ assert.equal(finished, false);
+ });
+
+ it("finishes a collapse if the animation fails unexpectedly", async () => {
+ const tree = createTreeElements();
+ let finished = false;
+ collapseFileTree(tree.liElement, () => {
+ finished = true;
+ });
+
+ tree.animation.fail(new Error("Animation failed"));
+ await assert.rejects(tree.animation.animation.finished);
+ assert.equal(tree.leafElement.isConnected, false);
+ assert.equal(isFileTreeCollapsing(tree.liElement), false);
+ assert.equal(finished, true);
+ });
+});
diff --git a/app/src/layout/dock/fileTreeAnimation.ts b/app/src/layout/dock/fileTreeAnimation.ts
new file mode 100644
index 0000000000..5bfbc2e182
--- /dev/null
+++ b/app/src/layout/dock/fileTreeAnimation.ts
@@ -0,0 +1,93 @@
+const FILE_TREE_ANIMATION_DURATION = 200;
+const FILE_TREE_ANIMATION_EASING = "cubic-bezier(0, 0, .2, 1)";
+const collapsingElements = new WeakMap<HTMLElement, Animation>();
+
+const getAnimationOptions = (): KeyframeAnimationOptions => ({
+ duration: window.matchMedia("(prefers-reduced-motion: reduce)").matches ? 0 : FILE_TREE_ANIMATION_DURATION,
+ easing: FILE_TREE_ANIMATION_EASING,
+ fill: "both",
+});
+
+export const expandFileTree = (element: HTMLElement, onFinish?: () => void) => {
+ element.style.overflow = "hidden";
+ const animation = element.animate([
+ {height: "0"},
+ {height: `${element.scrollHeight}px`},
+ ], getAnimationOptions());
+ animation.finished.then(() => {
+ animation.cancel();
+ if (!element.isConnected) {
+ return;
+ }
+ element.style.removeProperty("overflow");
+ onFinish?.();
+ }, () => {
+ if (element.isConnected) {
+ element.style.removeProperty("overflow");
+ }
+ });
+};
+
+const getLeafElement = (liElement: Element) => {
+ const leafElement = liElement.nextElementSibling as HTMLElement;
+ return leafElement?.tagName === "UL" ? leafElement : undefined;
+};
+
+export const isFileTreeCollapsing = (liElement: Element) => {
+ const leafElement = getLeafElement(liElement);
+ return leafElement ? collapsingElements.has(leafElement) : false;
+};
+
+export const cancelFileTreeCollapse = (liElement: Element) => {
+ const leafElement = getLeafElement(liElement);
+ const animation = leafElement && collapsingElements.get(leafElement);
+ if (!animation) {
+ return false;
+ }
+ collapsingElements.delete(leafElement);
+ animation.cancel();
+ leafElement.remove();
+ return true;
+};
+
+export const collapseFileTree = (liElement: Element, onFinish: () => void) => {
+ liElement.querySelector(".b3-list-item__arrow")?.classList.remove("b3-list-item__arrow--open");
+ const leafElement = getLeafElement(liElement);
+ if (!leafElement) {
+ onFinish();
+ return;
+ }
+ if (collapsingElements.has(leafElement)) {
+ return;
+ }
+
+ leafElement.style.overflow = "hidden";
+ const animation = leafElement.animate([
+ {height: `${leafElement.scrollHeight}px`},
+ {height: "0"},
+ ], getAnimationOptions());
+ collapsingElements.set(leafElement, animation);
+ animation.finished.then(() => {
+ if (collapsingElements.get(leafElement) !== animation) {
+ return;
+ }
+ collapsingElements.delete(leafElement);
+ if (!leafElement.isConnected) {
+ animation.cancel();
+ return;
+ }
+ leafElement.remove();
+ animation.cancel();
+ onFinish();
+ }, () => {
+ if (collapsingElements.get(leafElement) !== animation) {
+ return;
+ }
+ collapsingElements.delete(leafElement);
+ if (!leafElement.isConnected) {
+ return;
+ }
+ leafElement.remove();
+ onFinish();
+ });
+};
diff --git a/app/src/mobile/dock/MobileFiles.ts b/app/src/mobile/dock/MobileFiles.ts
index f9e723d77e..bca4293872 100644
--- a/app/src/mobile/dock/MobileFiles.ts
+++ b/app/src/mobile/dock/MobileFiles.ts
@@ -21,6 +21,12 @@ import {refreshFileTree} from "../../dialog/processSystem";
import {setStorageVal} from "../../protyle/util/compatibility";
import {showMessage} from "../../dialog/message";
import {dragOverScroll, stopScrollAnimation} from "../../boot/globalEvent/dragover";
+import {
+ cancelFileTreeCollapse,
+ collapseFileTree,
+ expandFileTree,
+ isFileTreeCollapsing
+} from "../../layout/dock/fileTreeAnimation";
export class MobileFiles extends Model {
public element: HTMLElement;
@@ -152,8 +158,8 @@ export class MobileFiles extends Model {
const notebookId = ulElement.getAttribute("data-url");
const liElement = target.parentElement;
if (liElement.querySelector(".b3-list-item__arrow--open")) {
- this.collapseLeaf(liElement);
- } else if ((liElement.nextElementSibling as HTMLElement)?.dataset.collapsing !== "true") {
+ collapseFileTree(liElement, () => this.getOpenPaths());
+ } else if (!isFileTreeCollapsing(liElement)) {
this.getLeaf(liElement, notebookId);
}
this.setCurrent(target.parentElement);
@@ -965,18 +971,13 @@ export class MobileFiles extends Model {
return;
}
liElement.querySelector(".b3-list-item__arrow").classList.add("b3-list-item__arrow--open");
- liElement.insertAdjacentHTML("afterend", `<ul class="file-tree__sliderDown">${fileHTML}</ul>`);
+ liElement.insertAdjacentHTML("afterend", `<ul>${fileHTML}</ul>`);
nextElement = liElement.nextElementSibling;
- setTimeout(() => {
- nextElement.setAttribute("style", `height:${nextElement.childElementCount * liElement.clientHeight}px;`);
- setTimeout(() => {
- nextElement.classList.remove("file-tree__sliderDown");
- nextElement.removeAttribute("style");
- if (typeof scrollTop === "number") {
- this.element.scroll({top: scrollTop, behavior: "smooth"});
- }
- }, 200);
- }, 2);
+ expandFileTree(nextElement as HTMLElement, () => {
+ if (typeof scrollTop === "number") {
+ this.element.scroll({top: scrollTop, behavior: "smooth"});
+ }
+ });
this.refreshPublishAccessSwitch();
}
@@ -1040,40 +1041,15 @@ export class MobileFiles extends Model {
}
}
- private collapseLeaf(liElement: Element) {
- const toggleElement = liElement.querySelector(".b3-list-item__arrow");
- const leafElement = liElement.nextElementSibling as HTMLElement;
- toggleElement.classList.remove("b3-list-item__arrow--open");
- if (leafElement?.tagName !== "UL") {
- this.getOpenPaths();
- return;
- }
-
- // 仅当手动点击折叠按钮时才需要动画,其他情况下默认直接移除列表容器。
- leafElement.dataset.collapsing = "true";
- leafElement.style.height = `${leafElement.scrollHeight}px`;
- leafElement.style.overflow = "hidden";
- leafElement.classList.add("file-tree__sliderUp");
- window.setTimeout(() => {
- leafElement.style.height = "0";
- window.setTimeout(() => {
- if (!leafElement.isConnected) {
- return;
- }
- leafElement.remove();
- this.getOpenPaths();
- }, 200);
- }, 2);
- }
-
public getLeaf(liElement: Element, notebookId: string, focusUpdate = false) {
const toggleElement = liElement.querySelector(".b3-list-item__arrow");
- const leafElement = liElement.nextElementSibling as HTMLElement;
- if (leafElement?.dataset.collapsing === "true") {
- leafElement.remove();
+ if (cancelFileTreeCollapse(liElement)) {
this.getOpenPaths();
- return;
+ if (!focusUpdate) {
+ return;
+ }
}
+ const leafElement = liElement.nextElementSibling as HTMLElement;
if (toggleElement.classList.contains("b3-list-item__arrow--open") && !focusUpdate) {
toggleElement.classList.remove("b3-list-item__arrow--open");
leafElement?.remove();
diff --git a/app/src/util/pathName.ts b/app/src/util/pathName.ts
index b0d1fd7511..0f51aef5f6 100644
--- a/app/src/util/pathName.ts
+++ b/app/src/util/pathName.ts
@@ -17,6 +17,7 @@ import {matchHotKey} from "../protyle/util/hotKey";
import {Menu} from "../plugin/Menu";
import {hasClosestByClassName} from "../protyle/util/hasClosest";
import {mergePathSegments} from "./mergePathSegments";
+import {expandFileTree} from "../layout/dock/fileTreeAnimation";
export const useShell = (cmd: "showItemInFolder" | "openPath", filePath: string) => {
/// #if !BROWSER
@@ -715,15 +716,8 @@ const getLeaf = (liElement: HTMLElement, flashcard: boolean) => {
return;
}
toggleElement.classList.add("b3-list-item__arrow--open");
- liElement.insertAdjacentHTML("afterend", `<ul class="file-tree__sliderDown">${fileHTML}</ul>`);
- const nextElement = liElement.nextElementSibling;
- setTimeout(() => {
- nextElement.setAttribute("style", `height:${nextElement.childElementCount * liElement.clientHeight}px;`);
- setTimeout(() => {
- nextElement.classList.remove("file-tree__sliderDown");
- nextElement.removeAttribute("style");
- }, 120);
- }, 2);
+ liElement.insertAdjacentHTML("afterend", `<ul>${fileHTML}</ul>`);
+ expandFileTree(liElement.nextElementSibling as HTMLElement);
});
};