Skip to content

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

3.0 KB
Raw Blame
import {getTabTask} from "../render/tabsRender";

// 独立页签项离开原容器前,将继承的任务状态写入副本。
export const preserveTabTask = (source: Element, copy: Element): boolean => {
    if (source.getAttribute("data-type") !== "NodeTabItem" || copy.hasAttribute("tabs-task")) {
        return false;
    }
    const marker = getTabTask(source);
    if (marker === null) {
        return false;
    }
    copy.setAttribute("tabs-task", marker);
    return true;
};

export const preserveCopiedTabTask = (source: Element, html: string): string => {
    if (source.getAttribute("data-type") !== "NodeTabItem" || source.hasAttribute("tabs-task") ||
        getTabTask(source) === null) {
        return html;
    }
    const template = document.createElement("template");
    template.innerHTML = html;
    const copy = template.content.firstElementChild;
    if (copy?.getAttribute("data-type") === "NodeTabItem") {
        preserveTabTask(source, copy);
    }
    return template.innerHTML;
};

// 同时映射页签选择和正文、标题中的内部链接,外部引用继续指向原目标。
export const remapTabsDOMIDs = (root: Element, ids: Map<string, string>) => {
    const blocks = [root, ...Array.from(root.querySelectorAll("[tabs-active-id]"))];
    blocks.forEach(group => {
        const active = group.getAttribute("tabs-active-id");
        if (ids.has(active)) {
            group.setAttribute("tabs-active-id", ids.get(active));
        }
    });
    root.querySelectorAll<HTMLElement>('[data-type~="block-ref"], [data-type~="a"]').forEach(link => {
        if (!link.closest(".tab-item")) {
            return;
        }
        if (ids.has(link.dataset.id)) {
            link.dataset.id = ids.get(link.dataset.id);
        }
        const href = link.dataset.href;
        if (href?.startsWith("siyuan://blocks/")) {
            const id = href.substring("siyuan://blocks/".length);
            if (ids.has(id)) {
                link.dataset.href = "siyuan://blocks/" + ids.get(id);
            }
        }
    });
};

// 独立页签项粘贴到普通正文时补齐容器,保持原页签和正文完整。
export const wrapPastedTabItems = (root: Element, lute: Lute) => {
    root.querySelectorAll<HTMLElement>('[data-type="NodeTabItem"]').forEach(item => {
        if (item.parentElement?.getAttribute("data-type") === "NodeTabs") {
            return;
        }
        const template = document.createElement("template");
        template.innerHTML = lute.Md2BlockDOM("::: tabs\n@tab\n\n:::\n");
        const tabs = template.content.firstElementChild;
        tabs.querySelector(":scope > .tab-item").remove();
        item.before(tabs);
        let next: Element = item;
        while (next?.getAttribute("data-type") === "NodeTabItem") {
            const sibling = next.nextElementSibling;
            tabs.insertBefore(next, tabs.querySelector(":scope > .protyle-attr"));
            next = sibling;
        }
        tabs.setAttribute("tabs-active-id", item.dataset.nodeId);
    });
};