Skip to content

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

2.0 KB
Raw Blame
import {ToolbarItem} from "./ToolbarItem";
import {hasClosestBlock, hasClosestByAttribute} from "../util/hasClosest";
import {stripSemanticMarkersFromRangeText} from "../util/inlineElementMarker";
import {Constants} from "../../constants";
import {getFirstSelectedInlineMemoContent, isExactInlineMemoSelection} from "./inlineMemoSelection";
import {getContenteditableElement} from "../wysiwyg/getBlock";
import {getSelectionOffset} from "../util/selectionOffsets";

export class InlineMemo extends ToolbarItem {
    public element: HTMLElement;

    constructor(protyle: IProtyle, menuItem: IMenuItem) {
        super(protyle, menuItem);
        this.element.addEventListener("click", async (event: MouseEvent & { changedTouches: MouseEvent[] }) => {
            protyle.toolbar.element.classList.add("fn__none");
            event.stopPropagation();

            const range = protyle.toolbar.range;
            const nodeElement = hasClosestBlock(range.startContainer);
            if (!nodeElement) {
                return;
            }
            const memoElement = hasClosestByAttribute(range.startContainer, "data-type", "inline-memo");
            const memoContent = getFirstSelectedInlineMemoContent(range);
            const selectedText = stripSemanticMarkersFromRangeText(range).split(Constants.ZWSP).join("");
            const editableElement = memoElement && getContenteditableElement(nodeElement, range.startContainer);
            if (memoElement && editableElement && isExactInlineMemoSelection(range, memoElement, currentRange =>
                getSelectionOffset(editableElement, undefined, currentRange, true))) {
                // https://github.com/siyuan-note/siyuan/issues/6569
                protyle.toolbar.showRender(protyle, memoElement);
                return;
            }

            if (selectedText === "") {
                return;
            }

            protyle.toolbar.setInlineMark(protyle, "inline-memo", "range", {
                type: "inline-memo",
                color: memoContent,
            });
        });
    }
}