Skip to content

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

1.9 KB
Raw Blame
export const shouldFocusAfterZoom = (options: {
    focusId?: string,
    id: string,
    rootID: string,
    isPushBack: boolean,
}) => Boolean(options.focusId || (options.isPushBack && options.id !== options.rootID));

export const hasFocusOffsets = (scrollAttr?: IScrollAttr) => Boolean(scrollAttr?.focusId) &&
    typeof scrollAttr.focusStart === "number" && typeof scrollAttr.focusEnd === "number";

// 恢复光标时优先使用阅读位置,其次使用文档默认页签,过期光标不能重新激活隐藏页。
export const getSavedTabFocusTarget = (element: Element, getActiveID?: (tabs: Element) => string): Element => {
    let target = element;
    for (let item = element?.closest(".tab-item"); item; item = item.parentElement?.closest(".tab-item")) {
        const tabs = item.parentElement;
        if (!tabs?.classList.contains("tabs")) {
            continue;
        }
        const items = Array.from(tabs.children).filter(child => child.classList.contains("tab-item"));
        const readingID = getActiveID?.(tabs);
        const active = items.find(child => child.getAttribute("data-node-id") === readingID) ||
            items.find(child => child.getAttribute("data-node-id") === tabs.getAttribute("tabs-active-id")) || items[0];
        if (active !== item) {
            target = tabs;
        }
    }
    return target;
};

export const getZoomFocusScrollAttr = (rootId: string, focusId?: string,
                                       focusPosition?: { start: number, end: number }): IScrollAttr | undefined =>
    focusId ? {
        rootId,
        focusId,
        focusStart: focusPosition?.start,
        focusEnd: focusPosition?.end,
    } : undefined;

export const getPendingBlockFocusMode = (value: string | null): "default" | "zoom" | undefined => {
    if (value === "zoom") {
        return "zoom";
    }
    if (value === "true") {
        return "default";
    }
};