Skip to content

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

:bug: Serialize document tree animations https://github.com/siyuan-note/siyuan/issues/18700

Danielefa877b75a2c
diff --git a/app/src/layout/dock/fileTreeAnimation.test.ts b/app/src/layout/dock/fileTreeAnimation.test.ts
index 2752cbae82..0f6e937fae 100644
--- a/app/src/layout/dock/fileTreeAnimation.test.ts
+++ b/app/src/layout/dock/fileTreeAnimation.test.ts
@@ -63,12 +63,9 @@ const createTreeElements = (expanded = true) => {
let arrowOpen = expanded;
const style = {
overflow: "",
- position: "",
removeProperty(property: string) {
if (property === "overflow") {
this.overflow = "";
- } else if (property === "position") {
- this.position = "";
}
return "";
},
@@ -125,13 +122,11 @@ describe("fileTreeAnimation", () => {
assert.deepEqual(tree.getFrames(), [{height: "0"}, {height: "96px"}]);
assert.equal(tree.getOptions().duration, 200);
assert.equal(tree.leafElement.style.overflow, "clip");
- assert.equal(tree.leafElement.style.position, "relative");
tree.animation.finish();
await tree.animation.animation.finished;
assert.equal(tree.animation.isCanceled(), true);
assert.equal(tree.leafElement.style.overflow, "");
- assert.equal(tree.leafElement.style.position, "");
assert.equal(finished, true);
});
@@ -145,7 +140,6 @@ describe("fileTreeAnimation", () => {
tree.animation.fail(new Error("Animation failed"));
await assert.rejects(tree.animation.animation.finished);
assert.equal(tree.leafElement.style.overflow, "");
- assert.equal(tree.leafElement.style.position, "");
assert.equal(finished, false);
});
@@ -168,7 +162,6 @@ describe("fileTreeAnimation", () => {
assert.equal(isFileTreeCollapsing(tree.liElement), true);
assert.deepEqual(tree.getFrames(), [{height: "96px"}, {height: "0"}]);
assert.equal(tree.leafElement.style.overflow, "clip");
- assert.equal(tree.leafElement.style.position, "relative");
tree.animation.finish();
await tree.animation.animation.finished;
@@ -219,6 +212,21 @@ describe("fileTreeAnimation", () => {
assert.equal(collapseCount, 1);
});
+ it("ignores a collapse request until expansion finishes", async () => {
+ const tree = createTreeElements();
+ let collapseCount = 0;
+ let expandCount = 0;
+ expandFileTree(tree.leafElement);
+ toggleFileTree(tree.liElement, () => collapseCount++, () => expandCount++);
+
+ assert.equal(tree.isArrowClosed(), false);
+ assert.equal(isFileTreeCollapsing(tree.liElement), false);
+ assert.equal(collapseCount, 0);
+ assert.equal(expandCount, 0);
+ tree.animation.finish();
+ await tree.animation.animation.finished;
+ });
+
it("routes a collapsed item through expansion", () => {
const tree = createTreeElements(false);
let collapseCount = 0;
diff --git a/app/src/layout/dock/fileTreeAnimation.ts b/app/src/layout/dock/fileTreeAnimation.ts
index 3ea8405232..46ea0c06bd 100644
--- a/app/src/layout/dock/fileTreeAnimation.ts
+++ b/app/src/layout/dock/fileTreeAnimation.ts
@@ -1,6 +1,9 @@
const FILE_TREE_ANIMATION_DURATION = 200;
const FILE_TREE_ANIMATION_EASING = "cubic-bezier(0, 0, .2, 1)";
-const collapsingElements = new WeakMap<HTMLElement, Animation>();
+const animatingElements = new WeakMap<HTMLElement, {
+ animation: Animation,
+ type: "expand" | "collapse",
+}>();
const getAnimationOptions = (): KeyframeAnimationOptions => ({
duration: window.matchMedia("(prefers-reduced-motion: reduce)").matches ? 0 : FILE_TREE_ANIMATION_DURATION,
@@ -10,21 +13,27 @@ const getAnimationOptions = (): KeyframeAnimationOptions => ({
const setAnimationStyles = (element: HTMLElement) => {
element.style.overflow = "clip";
- element.style.position = "relative";
};
const clearAnimationStyles = (element: HTMLElement) => {
element.style.removeProperty("overflow");
- element.style.removeProperty("position");
};
export const expandFileTree = (element: HTMLElement, onFinish?: () => void) => {
+ if (animatingElements.has(element)) {
+ return;
+ }
setAnimationStyles(element);
const animation = element.animate([
{height: "0"},
{height: `${element.scrollHeight}px`},
], getAnimationOptions());
+ animatingElements.set(element, {animation, type: "expand"});
animation.finished.then(() => {
+ if (animatingElements.get(element)?.animation !== animation) {
+ return;
+ }
+ animatingElements.delete(element);
animation.cancel();
if (!element.isConnected) {
return;
@@ -32,9 +41,14 @@ export const expandFileTree = (element: HTMLElement, onFinish?: () => void) => {
clearAnimationStyles(element);
onFinish?.();
}, () => {
- if (element.isConnected) {
- clearAnimationStyles(element);
+ if (animatingElements.get(element)?.animation !== animation) {
+ return;
+ }
+ animatingElements.delete(element);
+ if (!element.isConnected) {
+ return;
}
+ clearAnimationStyles(element);
});
};
@@ -45,43 +59,44 @@ const getLeafElement = (liElement: Element) => {
export const isFileTreeCollapsing = (liElement: Element) => {
const leafElement = getLeafElement(liElement);
- return leafElement ? collapsingElements.has(leafElement) : false;
+ return leafElement ? animatingElements.get(leafElement)?.type === "collapse" : false;
};
export const cancelFileTreeCollapse = (liElement: Element) => {
const leafElement = getLeafElement(liElement);
- const animation = leafElement && collapsingElements.get(leafElement);
- if (!animation) {
+ const animationState = leafElement && animatingElements.get(leafElement);
+ if (animationState?.type !== "collapse") {
return false;
}
- collapsingElements.delete(leafElement);
- animation.cancel();
+ animatingElements.delete(leafElement);
+ animationState.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) {
+ liElement.querySelector(".b3-list-item__arrow")?.classList.remove("b3-list-item__arrow--open");
onFinish();
return;
}
- if (collapsingElements.has(leafElement)) {
+ if (animatingElements.has(leafElement)) {
return;
}
+ liElement.querySelector(".b3-list-item__arrow")?.classList.remove("b3-list-item__arrow--open");
setAnimationStyles(leafElement);
const animation = leafElement.animate([
{height: `${leafElement.scrollHeight}px`},
{height: "0"},
], getAnimationOptions());
- collapsingElements.set(leafElement, animation);
+ animatingElements.set(leafElement, {animation, type: "collapse"});
animation.finished.then(() => {
- if (collapsingElements.get(leafElement) !== animation) {
+ if (animatingElements.get(leafElement)?.animation !== animation) {
return;
}
- collapsingElements.delete(leafElement);
+ animatingElements.delete(leafElement);
if (!leafElement.isConnected) {
animation.cancel();
return;
@@ -90,10 +105,10 @@ export const collapseFileTree = (liElement: Element, onFinish: () => void) => {
animation.cancel();
onFinish();
}, () => {
- if (collapsingElements.get(leafElement) !== animation) {
+ if (animatingElements.get(leafElement)?.animation !== animation) {
return;
}
- collapsingElements.delete(leafElement);
+ animatingElements.delete(leafElement);
if (!leafElement.isConnected) {
return;
}
@@ -103,6 +118,10 @@ export const collapseFileTree = (liElement: Element, onFinish: () => void) => {
};
export const toggleFileTree = (liElement: Element, onCollapse: () => void, onExpand: () => void) => {
+ const leafElement = getLeafElement(liElement);
+ if (leafElement && animatingElements.has(leafElement)) {
+ return;
+ }
if (liElement.querySelector(".b3-list-item__arrow--open")) {
collapseFileTree(liElement, onCollapse);
} else if (!isFileTreeCollapsing(liElement)) {