Skip to content

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

4.7 KB
Raw Blame
import {getHostCapabilities} from "./hostCapabilities";

const CONTAINER_BACKEND_SET = new Set(["docker", "ios", "android", "harmony"]);

export const isKernelInContainer = (): boolean => {
    return CONTAINER_BACKEND_SET.has(window.siyuan.config.system.container);
};

export const isMobile = () => {
    return document.getElementById("sidebar") ? true : false;
};

// "windows" | "linux" | "darwin" | "docker" | "android" | "ios" | "harmony"
export const getBackend = () => {
    if (isKernelInContainer()) {
        return window.siyuan.config.system.container;
    } else {
        return window.siyuan.config.system.os;
    }
};

// "desktop" | "desktop-window" | "mobile" | "browser-desktop" | "browser-mobile"
export const getFrontend = () => {
    /// #if MOBILE
    if (window.navigator.userAgent.startsWith("SiYuan/")) {
        return "mobile";
    } else {
        return "browser-mobile";
    }
    /// #else
    /// #if !BROWSER
    // 设置窗口首屏的浏览器标识可能尚未更新,使用桌面主窗口的集市包兼容性。
    if (document.body.classList.contains("body--settings")) {
        return "desktop";
    }
    /// #endif
    if (window.navigator.userAgent.startsWith("SiYuan/")) {
        if (isWindow()) {
            return "desktop-window";
        }
        return "desktop";
    } else {
        return "browser-desktop";
    }
    /// #endif
};

export const isWindow = () => {
    return document.getElementById("toolbar") ? false : true;
};

export const isTouchDevice = () => {
    return ("ontouchstart" in window) && navigator.maxTouchPoints > 1;
};

export const isArrayEqual = (arr1: string[], arr2: string[]) => {
    return arr1.length === arr2.length && arr1.every((item) => arr2.includes(item));
};

export const getRandom = (min: number, max: number) => {
    return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
};

export const getSearch = (key: string, link = window.location.search) => {
    const params = link.substring(link.indexOf("?"));
    const hashIndex = params.indexOf("#");
    // REF https://developer.mozilla.org/zh-CN/docs/Web/API/URLSearchParams
    const urlSearchParams = new URLSearchParams(params.substring(0, hashIndex >= 0 ? hashIndex : undefined));
    return urlSearchParams.get(key);
};

/**
 * 判断是否是移动端或浏览器环境
 */
export const isBrowser = () => {
    /// #if BROWSER
    return true;
    /// #else
    return false;
    /// #endif
};

export const isDynamicRef = (text: string) => {
    return /^\(\(\d{14}-\w{7} '.*'\)\)$/.test(text);
};

export const isValidCustomAttrName = (name: string) => {
    return /^[a-z][\-0-9a-z]*$/.test(name);
};

// REF https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/eval
export const looseJsonParse = (text: string) => {
    if (getHostCapabilities().remoteKernel) {
        return JSON.parse(text);
    }
    return Function(`"use strict";return (${text})`)();
};

export const objEquals = (a: any, b: any): boolean => {
    if (a === b) return true;
    if (typeof a === "number" && isNaN(a) && typeof b === "number" && isNaN(b)) return true;
    if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
    if (!a || !b || (typeof a !== "object" && typeof b !== "object")) return a === b;
    if (a.prototype !== b.prototype) return false;
    const keys = Object.keys(a);
    if (keys.length !== Object.keys(b).length) return false;
    return keys.every(k => objEquals(a[k], b[k]));
};

export const duplicateNameAddOne = (name: string) => {
    if (!name) {
        return "";
    }

    const nameMatch = name.match(/^(.*) \((\d+)\)$/);
    if (nameMatch) {
        name = `${nameMatch[1]} (${parseInt(nameMatch[2]) + 1})`;
    } else {
        name = `${name} (1)`;
    }
    return name;
};

/// #if !BROWSER
// 红绿灯为原生控件不随缩放变化,缩小时按 zoom 补偿 --b3-toolbar-left-mac 避免与工具栏内容重叠
export const setToolbarLeftMac = (zoom: number) => {
    // 窗口控件属于本机客户端,连接远程内核时仍按本机平台计算占位。
    if (!window.siyuan.config || !navigator.platform.toUpperCase().includes("MAC")) {
        return;
    }
    // 全屏下红绿灯隐藏,清除内联补偿让 body--fullscreen 的 5px 生效
    if (zoom >= .9 || document.body.classList.contains("body--fullscreen")) {
        document.body.style.removeProperty("--b3-toolbar-left-mac");
        return;
    }
    // 从 :root 读取主题基础值(默认 74px,兼容第三方主题),除以 zoom 让缩放后恢复到基础原生像素
    const base = parseInt(getComputedStyle(document.documentElement).getPropertyValue("--b3-toolbar-left-mac")) || 74;
    document.body.style.setProperty("--b3-toolbar-left-mac", (base / zoom * .9) + "px");
};
/// #endif