Skip to content

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

2.1 KB
Raw Blame
import {getAtomicVerticalNavigationOwner} from "../wysiwyg/verticalNavigationState";

// 动态回收按响应到达时的选区判断;连续区间在遇到用户位置时停止裁剪。
export const containsCurrentSelection = (element: Element) => {
    const selection = element.ownerDocument.getSelection();
    for (let index = 0; selection && index < selection.rangeCount; index++) {
        const range = selection.getRangeAt(index);
        const atomicOwner = getAtomicVerticalNavigationOwner(range);
        if (range.intersectsNode(element) || atomicOwner && element.contains(atomicOwner)) {
            return true;
        }
    }
    const activeElement = element.ownerDocument.activeElement;
    return activeElement && element.contains(activeElement);
};

export const updateDocumentBottomEof = (wysiwygElement: HTMLElement, preserveCurrent = false) => {
    if (preserveCurrent && wysiwygElement.hasAttribute("data-bottom-eof")) {
        return;
    }
    wysiwygElement.toggleAttribute(
        "data-bottom-eof",
        wysiwygElement.lastElementChild?.getAttribute("data-eof") === "2"
    );
};

export const isDocumentBoundaryLoaded = (wysiwygElement: HTMLElement, position: "before" | "after") => {
    if (position === "after") {
        return wysiwygElement.hasAttribute("data-bottom-eof");
    }
    const firstElement = wysiwygElement.firstElementChild;
    return firstElement?.getAttribute("data-eof") === "1" ||
        firstElement?.getAttribute("data-node-index") === "0";
};

export const markDocumentBoundaryLoaded = (wysiwygElement: HTMLElement, position: "before" | "after") => {
    const boundaryElement = position === "before" ?
        wysiwygElement.firstElementChild : wysiwygElement.lastElementChild;
    boundaryElement?.setAttribute("data-eof", position === "before" ? "1" : "2");
    if (position === "after") {
        updateDocumentBottomEof(wysiwygElement);
    }
};

export const hasUnloadedDocumentBlocks = (wysiwygElement: HTMLElement, dynamicLoad: boolean) => {
    if (!dynamicLoad) {
        return false;
    }
    return !isDocumentBoundaryLoaded(wysiwygElement, "before") ||
        !isDocumentBoundaryLoaded(wysiwygElement, "after");
};