Skip to content

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

2.3 KB
Raw Blame
import {hasClosestByAttribute, hasClosestByClassName} from "../../protyle/util/hasClosest";
import {notifyMobileKeyboardChange} from "./mobileKeyboardChange";

export let keyboardLockUntil = 0;

export const armKeyboardLock = () => {
    // 某些机型(比如鸿蒙 Pura X)在弹起键盘后会立即触发 activeBlur 导致键盘被关闭;移动端浏览器(比如三星键盘)在编辑器获得焦点后也会触发 resize,
    // 进而立即关闭键盘。因此主动唤起键盘或点击可编辑区域时,锁定接下来的一段时间,禁止 activeBlur 关闭键盘
    keyboardLockUntil = Date.now() + 500;
};

export const callMobileAppShowKeyboard = () => {
    armKeyboardLock();
    notifyMobileKeyboardChange(true);

    if (window.JSAndroid && window.JSAndroid.showKeyboard) {
        window.JSAndroid.showKeyboard();
    } else if (window.JSHarmony && window.JSHarmony.showKeyboard) {
        window.JSHarmony.showKeyboard();
    }
};


export const canInput = (element: Element) => {
    if (!element || element.nodeType !== 1) {
        return false;
    }
    if ((
        element.tagName === "TEXTAREA" ||
        (element.tagName === "INPUT" && ["email", "number", "password", "search", "tel", "text", "url", "", null].includes(element.getAttribute("type")))
    )) {
        return !element.hasAttribute("readonly") && !element.hasAttribute("disabled") && element;
    }
    const wysiwygElement = hasClosestByClassName(element, "protyle-wysiwyg", true);
    if (wysiwygElement && wysiwygElement.getAttribute("data-readonly") === "false") {
        // 按最近的编辑边界判断,避免点击不可编辑的渲染区域时唤起键盘并恢复旧光标位置。
        const editableElement = hasClosestByAttribute(element, "contenteditable", null, true);
        return editableElement && ["true", "plaintext-only"].includes(editableElement.getAttribute("contenteditable")) && editableElement;
    }
    return false;
};

export const setWebViewFocusable = () => {
    if ((window.JSAndroid || window.JSHarmony) && document.activeElement.tagName === "IFRAME") {
        if (window.JSAndroid?.setWebViewFocusable) {
            window.JSAndroid.setWebViewFocusable(true);
        } else if (window.JSHarmony?.setWebViewFocusable) {
            window.JSHarmony.setWebViewFocusable(true);
        }
    }
};