Skip to content

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

5.6 KB
Raw Blame
const FILE_TREE_ANIMATION_DURATION = 200;
const FILE_TREE_ANIMATION_EASING = "cubic-bezier(0, 0, .2, 1)";
const visibilityAnimations = new WeakMap<HTMLElement, Animation>();

// 保留置顶列表节点,通过高度过渡切换可见性,快速反向操作时取消上一次动画。
export const setFileTreeVisibility = (element: HTMLElement, visible: boolean, animate = false) => {
    const previous = visibilityAnimations.get(element);
    const startHeight = previous ? element.getBoundingClientRect().height : visible ? 0 : element.getBoundingClientRect().height;
    visibilityAnimations.delete(element);
    previous?.cancel();
    element.style.removeProperty("overflow");
    if (!animate) {
        element.classList.toggle("fn__none", !visible);
        return;
    }
    element.classList.remove("fn__none");
    const endHeight = visible ? element.getBoundingClientRect().height : 0;
    element.style.overflow = "clip";
    const animation = element.animate([
        {height: `${startHeight}px`, maxHeight: `${startHeight}px`},
        {height: `${endHeight}px`, maxHeight: `${endHeight}px`},
    ], getAnimationOptions());
    visibilityAnimations.set(element, animation);
    const finish = () => {
        if (visibilityAnimations.get(element) !== animation) { return; }
        visibilityAnimations.delete(element);
        element.classList.toggle("fn__none", !visible);
        animation.cancel();
        element.style.removeProperty("overflow");
    };
    animation.finished.then(finish, finish);
};
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,
    easing: FILE_TREE_ANIMATION_EASING,
    fill: "both",
});

const setAnimationStyles = (element: HTMLElement) => {
    element.style.overflow = "clip";
};

const clearAnimationStyles = (element: HTMLElement) => {
    element.style.removeProperty("overflow");
};

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;
        }
        clearAnimationStyles(element);
        onFinish?.();
    }, () => {
        if (animatingElements.get(element)?.animation !== animation) {
            return;
        }
        animatingElements.delete(element);
        if (!element.isConnected) {
            return;
        }
        clearAnimationStyles(element);
    });
};

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 ? animatingElements.get(leafElement)?.type === "collapse" : false;
};

export const cancelFileTreeCollapse = (liElement: Element) => {
    const leafElement = getLeafElement(liElement);
    const animationState = leafElement && animatingElements.get(leafElement);
    if (animationState?.type !== "collapse") {
        return false;
    }
    animatingElements.delete(leafElement);
    animationState.animation.cancel();
    leafElement.remove();
    return true;
};

export const collapseFileTree = (liElement: Element, onFinish: () => void) => {
    const leafElement = getLeafElement(liElement);
    if (!leafElement) {
        liElement.querySelector(".b3-list-item__arrow")?.classList.remove("b3-list-item__arrow--open");
        onFinish();
        return;
    }
    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());
    animatingElements.set(leafElement, {animation, type: "collapse"});
    animation.finished.then(() => {
        if (animatingElements.get(leafElement)?.animation !== animation) {
            return;
        }
        animatingElements.delete(leafElement);
        if (!leafElement.isConnected) {
            animation.cancel();
            return;
        }
        leafElement.remove();
        animation.cancel();
        onFinish();
    }, () => {
        if (animatingElements.get(leafElement)?.animation !== animation) {
            return;
        }
        animatingElements.delete(leafElement);
        if (!leafElement.isConnected) {
            return;
        }
        leafElement.remove();
        onFinish();
    });
};

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)) {
        onExpand();
    }
};