An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Improve database attribute, backlink, and mention collapse animations https://github.com/siyuan-note/siyuan/issues/18456
Daniel279548502fdd
diff --git a/app/src/layout/dock/Backlink.ts b/app/src/layout/dock/Backlink.tsindex e5c37864c8..8f4ee9917e 100644--- a/app/src/layout/dock/Backlink.ts+++ b/app/src/layout/dock/Backlink.ts@@ -23,6 +23,12 @@ import {shouldRenderBacklinkResponse,shouldSaveBacklinkStatus} from "./backlinkRefresh";+import {+ cancelHeightAnimation,+ collapseHeight,+ expandHeight,+ isHeightAnimating+} from "../../util/heightAnimation";interface IBacklinkItemRecord {revision: string,@@ -500,12 +506,23 @@ export class Backlink extends Model {}private setBottomLayout(element: HTMLElement, listElement: HTMLElement) {+ if (isHeightAnimating(listElement)) {+ return;+ }const folded = !listElement.classList.contains("fn__none");if (folded) {this.cancelContextRequests(listElement, listElement === this.mTree.element);this.hideEditorGutters(listElement);+ listElement.dataset.heightFolding = "true";+ collapseHeight(listElement, () => {+ delete listElement.dataset.heightFolding;+ listElement.classList.add("fn__none");+ });+ } else {+ delete listElement.dataset.heightFolding;+ listElement.classList.remove("fn__none");+ expandHeight(listElement);}- listElement.classList.toggle("fn__none", folded);if (folded) {listElement.querySelector(".b3-list-item--focus")?.classList.remove("b3-list-item--focus");}@@ -654,6 +671,9 @@ export class Backlink extends Model {private expandDocumentItems(tree: Tree, isMention: boolean) {const listElement = tree.element;+ if (this.type === "bottom" && isHeightAnimating(listElement)) {+ return;+ }const folded = this.type === "bottom" ?listElement.classList.contains("fn__none") :(isMention ? listElement.style.height === "0px" : listElement.classList.contains("fn__none"));@@ -971,6 +991,10 @@ export class Backlink extends Model {}private resetRenderedData(resetLists: boolean) {+ cancelHeightAnimation(this.tree.element);+ cancelHeightAnimation(this.mTree.element);+ delete this.tree.element.dataset.heightFolding;+ delete this.mTree.element.dataset.heightFolding;this.cancelContextRequests(this.tree.element, false);this.cancelContextRequests(this.mTree.element, true);this.editors.forEach(item => item.destroy());@@ -1200,9 +1224,12 @@ export class Backlink extends Model {backlinkOpenIds: [],backlinkMOpenIds: [],backlinkMStatus: 3, // 0 全展开,1 展开一半箭头向下,2 展开一半箭头向上,3 全收起- backlinkFolded: this.tree.element.classList.contains("fn__none"),+ backlinkFolded: this.tree.element.classList.contains("fn__none") ||+ this.tree.element.dataset.heightFolding === "true",backmentionFolded: this.type === "bottom" ?- this.mTree.element.classList.contains("fn__none") : this.mTree.element.style.height === "0px"+ (this.mTree.element.classList.contains("fn__none") ||+ this.mTree.element.dataset.heightFolding === "true") :+ this.mTree.element.style.height === "0px"};this.tree.element.querySelectorAll(".b3-list-item__arrow--open").forEach(item => {this.status[this.blockId].backlinkOpenIds.push(item.parentElement.parentElement.getAttribute("data-node-id"));@@ -1370,6 +1397,8 @@ export class Backlink extends Model {}private restoreBottomLayout(element: HTMLElement, listElement: HTMLElement, folded: boolean) {+ cancelHeightAnimation(listElement);+ delete listElement.dataset.heightFolding;listElement.classList.toggle("fn__none", folded);element.setAttribute("aria-label", folded ? window.siyuan.languages.expand : window.siyuan.languages.collapse);element.querySelector("use").setAttribute("xlink:href", folded ? "#iconRight" : "#iconDown");@@ -1447,6 +1476,10 @@ export class Backlink extends Model {public destroy() {this.destroyed = true;+ cancelHeightAnimation(this.tree.element);+ cancelHeightAnimation(this.mTree.element);+ delete this.tree.element.dataset.heightFolding;+ delete this.mTree.element.dataset.heightFolding;if (this.ownerFocusoutListener) {this.ownerProtyle.element.removeEventListener("focusout", this.ownerFocusoutListener);}diff --git a/app/src/protyle/render/av/attributePanel.ts b/app/src/protyle/render/av/attributePanel.tsindex 22c1d05c1d..28ee65b1cb 100644--- a/app/src/protyle/render/av/attributePanel.ts+++ b/app/src/protyle/render/av/attributePanel.ts@@ -1,4 +1,10 @@import {renderAVAttribute} from "./blockAttr";+import {+ cancelHeightAnimation,+ collapseHeight,+ expandHeight,+ isHeightAnimating+} from "../../../util/heightAnimation";const updateEmptyState = (element: HTMLElement, hideEmpty: boolean) => {element.classList.toggle("protyle-db-attr--hide-empty", hideEmpty);@@ -91,6 +97,7 @@ export class AVAttributePanel {const bodyElement = document.createElement("div");bodyElement.className = "custom-attr protyle-db-attr__body";if (!keepCurrentBody) {+ this.updateCollapsedState();currentBodyElement.replaceWith(bodyElement);this.bodyElement = bodyElement;}@@ -100,6 +107,7 @@ export class AVAttributePanel {}this.renderingTargetID = "";if (keepCurrentBody) {+ this.updateCollapsedState();currentBodyElement.replaceWith(renderedElement);this.bodyElement = renderedElement;}@@ -174,18 +182,16 @@ export class AVAttributePanel {}}- public expand(avID?: string) {+ public expand(avID?: string, animate = false) {if (avID) {this.activeAvID = avID;}- this.collapsed = false;- this.updateCollapsedState();+ this.setCollapsed(false, animate);this.updateTabs();}public toggle() {- this.collapsed = !this.collapsed;- this.updateCollapsedState();+ this.setCollapsed(!this.collapsed, true);}public displayEmptyFields() {@@ -197,12 +203,43 @@ export class AVAttributePanel {}private updateCollapsedState() {+ cancelHeightAnimation(this.bodyElement);this.element.classList.toggle("protyle-db-attr--collapsed", this.collapsed);+ this.updateCollapsedControls();+ this.updateEmptyState();+ }++ private updateCollapsedControls() {const toggleElement = this.element.querySelector('[data-type="toggle"]');toggleElement?.setAttribute("aria-expanded", (!this.collapsed).toString());const useElement = toggleElement?.querySelector("use");useElement?.setAttribute("xlink:href", this.collapsed ? "#iconRight" : "#iconDown");+ }++ private setCollapsed(collapsed: boolean, animate: boolean) {+ if (animate && isHeightAnimating(this.bodyElement)) {+ return;+ }+ if (animate && collapsed === this.collapsed) {+ return;+ }+ this.collapsed = collapsed;+ if (!animate) {+ this.updateCollapsedState();+ return;+ }+ this.updateCollapsedControls();this.updateEmptyState();+ if (collapsed) {+ collapseHeight(this.bodyElement, () => {+ if (this.collapsed) {+ this.element.classList.add("protyle-db-attr--collapsed");+ }+ });+ } else {+ this.element.classList.remove("protyle-db-attr--collapsed");+ expandHeight(this.bodyElement);+ }}private updateTabs() {@@ -250,6 +287,7 @@ export class AVAttributePanel {private hideByDisplayConfig() {this.showEmptyFields = false;+ cancelHeightAnimation(this.bodyElement);if (this.renderingTargetID) {this.renderToken++;this.renderingTargetID = "";diff --git a/app/src/protyle/wysiwyg/commonClick.ts b/app/src/protyle/wysiwyg/commonClick.tsindex dc7fc9293a..f2d3cbbbd5 100644--- a/app/src/protyle/wysiwyg/commonClick.ts+++ b/app/src/protyle/wysiwyg/commonClick.ts@@ -61,7 +61,7 @@ export const commonClick = (event: MouseEvent & {}} else if (data) {if (avID) {- databaseAttributePanel.expand(avID);+ databaseAttributePanel.expand(avID, true);} else {databaseAttributePanel.toggle();}@@ -70,7 +70,7 @@ export const commonClick = (event: MouseEvent & {const blockID = blockElement ? blockElement.getAttribute("data-node-id") : "";if (blockID && protyle.block.showAll && blockID === protyle.block.id) {if (avID) {- databaseAttributePanel.expand(avID);+ databaseAttributePanel.expand(avID, true);} else {databaseAttributePanel.toggle();}@@ -78,7 +78,7 @@ export const commonClick = (event: MouseEvent & {zoomOut({protyle,id: blockID,- callback: avID ? () => databaseAttributePanel.expand(avID) : undefined,+ callback: avID ? () => databaseAttributePanel.expand(avID, true) : undefined,});}}diff --git a/app/src/util/heightAnimation.test.ts b/app/src/util/heightAnimation.test.tsnew file mode 100644index 0000000000..893fb7dec7--- /dev/null+++ b/app/src/util/heightAnimation.test.ts@@ -0,0 +1,181 @@+import {after, before, beforeEach, describe, it} from "node:test";+import * as assert from "node:assert/strict";+import {+ cancelHeightAnimation,+ collapseHeight,+ expandHeight,+ isHeightAnimating+} from "./heightAnimation";++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 createElement = () => {+ const animation = createAnimation();+ let frames!: Keyframe[];+ let options!: KeyframeAnimationOptions;+ const style = {+ minHeight: "",+ overflow: "",+ removeProperty(property: string) {+ if (property === "min-height") {+ this.minHeight = "";+ }+ if (property === "overflow") {+ this.overflow = "";+ }+ return "";+ },+ } as unknown as CSSStyleDeclaration;+ const element = {+ scrollHeight: 96,+ style,+ animate(animationFrames: Keyframe[], animationOptions: KeyframeAnimationOptions) {+ frames = animationFrames;+ options = animationOptions;+ return animation.animation;+ },+ } as unknown as HTMLElement;+ return {+ animation,+ element,+ getFrames: () => frames,+ getOptions: () => options,+ };+};++describe("heightAnimation", () => {+ it("expands to the content height and restores overflow", async () => {+ const target = createElement();+ let finished = false;+ assert.equal(expandHeight(target.element, () => {+ finished = true;+ }), true);++ assert.deepEqual(target.getFrames(), [{height: "0"}, {height: "96px"}]);+ assert.equal(target.getOptions().duration, 200);+ assert.equal(target.element.style.minHeight, "0");+ assert.equal(target.element.style.overflow, "hidden");+ assert.equal(isHeightAnimating(target.element), true);++ target.animation.finish();+ await target.animation.animation.finished;+ assert.equal(target.animation.isCanceled(), true);+ assert.equal(target.element.style.minHeight, "");+ assert.equal(target.element.style.overflow, "");+ assert.equal(isHeightAnimating(target.element), false);+ assert.equal(finished, true);+ });++ it("collapses from the content height", async () => {+ const target = createElement();+ collapseHeight(target.element);++ assert.deepEqual(target.getFrames(), [{height: "96px"}, {height: "0"}]);+ target.animation.finish();+ await target.animation.animation.finished;+ });++ it("uses no duration when reduced motion is requested", async () => {+ reduceMotion = true;+ const target = createElement();+ expandHeight(target.element);+ assert.equal(target.getOptions().duration, 0);+ target.animation.finish();+ await target.animation.animation.finished;+ });++ it("restores existing layout styles", async () => {+ const target = createElement();+ target.element.style.minHeight = "24px";+ target.element.style.overflow = "visible";+ collapseHeight(target.element);++ target.animation.finish();+ await target.animation.animation.finished;+ assert.equal(target.element.style.minHeight, "24px");+ assert.equal(target.element.style.overflow, "visible");+ });++ it("ignores repeated animations for the same element", async () => {+ const target = createElement();+ assert.equal(collapseHeight(target.element), true);+ assert.equal(expandHeight(target.element), false);+ target.animation.finish();+ await target.animation.animation.finished;+ });++ it("cancels without running the completion callback", async () => {+ const target = createElement();+ let finished = false;+ collapseHeight(target.element, () => {+ finished = true;+ });++ assert.equal(cancelHeightAnimation(target.element), true);+ await assert.rejects(target.animation.animation.finished);+ assert.equal(target.animation.isCanceled(), true);+ assert.equal(target.element.style.minHeight, "");+ assert.equal(target.element.style.overflow, "");+ assert.equal(isHeightAnimating(target.element), false);+ assert.equal(finished, false);+ });++ it("finishes the target state when an animation fails", async () => {+ const target = createElement();+ let finished = false;+ collapseHeight(target.element, () => {+ finished = true;+ });++ target.animation.fail(new Error("Animation failed"));+ await assert.rejects(target.animation.animation.finished);+ assert.equal(target.element.style.overflow, "");+ assert.equal(isHeightAnimating(target.element), false);+ assert.equal(finished, true);+ });+});diff --git a/app/src/util/heightAnimation.ts b/app/src/util/heightAnimation.tsnew file mode 100644index 0000000000..0e1d5d8336--- /dev/null+++ b/app/src/util/heightAnimation.ts@@ -0,0 +1,83 @@+const HEIGHT_ANIMATION_DURATION = 200;+const HEIGHT_ANIMATION_EASING = "cubic-bezier(0, 0, .2, 1)";+const heightAnimations = new WeakMap<HTMLElement, {+ animation: Animation,+ minHeight: string,+ overflow: string,+}>();++const getAnimationOptions = (): KeyframeAnimationOptions => ({+ duration: window.matchMedia("(prefers-reduced-motion: reduce)").matches ? 0 : HEIGHT_ANIMATION_DURATION,+ easing: HEIGHT_ANIMATION_EASING,+ fill: "both",+});++const restoreStyles = (element: HTMLElement, minHeight: string, overflow: string) => {+ if (minHeight) {+ element.style.minHeight = minHeight;+ } else {+ element.style.removeProperty("min-height");+ }+ if (overflow) {+ element.style.overflow = overflow;+ } else {+ element.style.removeProperty("overflow");+ }+};++const animateHeight = (element: HTMLElement, keyframes: Keyframe[], onFinish?: () => void) => {+ if (heightAnimations.has(element)) {+ return false;+ }+ const minHeight = element.style.minHeight;+ const overflow = element.style.overflow;+ element.style.minHeight = "0";+ element.style.overflow = "hidden";+ const animation = element.animate(keyframes, getAnimationOptions());+ const animationState = {animation, minHeight, overflow};+ heightAnimations.set(element, animationState);+ animation.finished.then(() => {+ if (heightAnimations.get(element) !== animationState) {+ return;+ }+ heightAnimations.delete(element);+ animation.cancel();+ restoreStyles(element, minHeight, overflow);+ onFinish?.();+ }, () => {+ if (heightAnimations.get(element) !== animationState) {+ return;+ }+ heightAnimations.delete(element);+ restoreStyles(element, minHeight, overflow);+ onFinish?.();+ });+ return true;+};++export const isHeightAnimating = (element: HTMLElement) => heightAnimations.has(element);++export const cancelHeightAnimation = (element: HTMLElement) => {+ const animationState = heightAnimations.get(element);+ if (!animationState) {+ return false;+ }+ heightAnimations.delete(element);+ animationState.animation.cancel();+ restoreStyles(element, animationState.minHeight, animationState.overflow);+ return true;+};++export const expandHeight = (element: HTMLElement, onFinish?: () => void) => {+ return animateHeight(element, [+ {height: "0"},+ {height: `${element.scrollHeight}px`},+ ], onFinish);+};++export const collapseHeight = (element: HTMLElement, onFinish?: () => void) => {+ return animateHeight(element, [+ {height: `${element.scrollHeight}px`},+ {height: "0"},+ ], onFinish);+};