Skip to content

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

4.8 KB
Raw Blame
import {Constants} from "../constants";
/// #if !MOBILE
import {getAllModels} from "../layout/getAll";
/// #endif
import {getAssetExtension} from "../util/pathName";
import dayjs from "dayjs";
import {escapeAttr, escapeHtml} from "../util/escape";
import {getHTMLAssetIFrameSrc, isHTMLFilePath} from "./html";
import {isBrowserRenderableImagePath} from "../util/imageURL";
import {getAssetsPreviewPath} from "./previewPath";

export const renderAssetsPreview = (pathString: string, dataPath?: string) => {
    if (!pathString) {
        return "";
    }
    const type = getAssetExtension(pathString).toLowerCase();
    const previewPath = escapeAttr(getAssetsPreviewPath(pathString, dataPath));
    if (Constants.SIYUAN_ASSETS_IMAGE.includes(type)) {
        return `<img style="max-height: 100%" src="${previewPath}">`;
    } else if (Constants.SIYUAN_ASSETS_AUDIO.includes(type)) {
        return `<audio style="max-width: 100%" controls="controls" src="${previewPath}"></audio>`;
    } else if (Constants.SIYUAN_ASSETS_VIDEO.includes(type)) {
        return `<video style="max-width: 100%" controls="controls" src="${previewPath}"></video>`;
    } else {
        return escapeHtml(pathString);
    }
};

export const pdfResize = () => {
    /// #if !MOBILE
    getAllModels().asset.forEach(item => {
        const pdfInstance = item.pdfObject;
        if (!pdfInstance) {
            return;
        }
        const {pdfDocument, pdfViewer} = pdfInstance;
        if (!pdfDocument) {
            return;
        }
        // https://github.com/siyuan-note/siyuan/issues/8097
        const pdfViewerElement = item.element.querySelector("#viewerContainer");
        if (pdfViewerElement.clientHeight === 0) {
            return;
        }
        if (pdfViewerElement) {
            // https://github.com/siyuan-note/siyuan/issues/6890
            const scrollTop = pdfViewerElement.getAttribute("data-scrolltop");
            if (scrollTop) {
                pdfViewerElement.scrollTo(0, parseInt(scrollTop));
                pdfViewerElement.removeAttribute("data-scrolltop");
            }
        }
        const currentScaleValue = pdfViewer.currentScaleValue;
        if (
            currentScaleValue === "auto" ||
            currentScaleValue === "page-fit" ||
            currentScaleValue === "page-width"
        ) {
            // Note: the scale is constant for 'page-actual'.
            pdfViewer.currentScaleValue = currentScaleValue;
        }
        pdfViewer.update();
    });
    /// #endif
};

export const genAssetHTML = (type: string, pathString: string, imgName: string, linkName: string, htmlAsIframe = false) => {
    let html = "";
    if (htmlAsIframe && isHTMLFilePath(linkName)) {
        const iframeSrc = escapeAttr(getHTMLAssetIFrameSrc(pathString));
        html = `<div data-node-id="${Lute.NewNodeID()}" data-type="NodeIFrame" class="iframe" updated="${dayjs().format("YYYYMMDDHHmmss")}"><div class="iframe-content">${Constants.ZWSP}<iframe sandbox="allow-scripts" src="${iframeSrc}" border="0" frameborder="no" framespacing="0" allowfullscreen="true"></iframe><span class="protyle-action__drag" contenteditable="false"></span></div><div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div></div>`;
    } else if (Constants.SIYUAN_ASSETS_AUDIO.includes(type)) {
        html = `<div data-node-id="${Lute.NewNodeID()}" data-type="NodeAudio" class="iframe" updated="${dayjs().format("YYYYMMDDHHmmss")}"><div class="iframe-content"><audio controls="controls" src="${pathString}"></audio>${Constants.ZWSP}</div><div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div></div>`;
    } else if (Constants.SIYUAN_ASSETS_IMAGE.includes(type) && isBrowserRenderableImagePath(pathString)) {
        let netHTML = "";
        if (!pathString.startsWith("assets/")) {
            netHTML = '<span class="img__net"><svg><use xlink:href="#iconGlobe"></use></svg></span>';
        }
        html = `<span contenteditable="false" data-type="img" class="img"><span> </span><span><span class="protyle-action protyle-icons"><span class="protyle-icon protyle-icon--only"><svg><use xlink:href="#iconMore"></use></svg></span></span><img src="${pathString}" data-src="${pathString}" alt="${imgName}" /><span class="protyle-action__drag"></span>${netHTML}<span class="protyle-action__title"></span></span><span> </span></span>`;
    } else if (Constants.SIYUAN_ASSETS_VIDEO.includes(type)) {
        html = `<div data-node-id="${Lute.NewNodeID()}" data-type="NodeVideo" class="iframe" updated="${dayjs().format("YYYYMMDDHHmmss")}"><div class="iframe-content">${Constants.ZWSP}<video controls="controls" src="${pathString}"></video><span class="protyle-action__drag" contenteditable="false"></span></div><div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div></div>`;
    } else {
        html = `<span data-type="a" data-href="${pathString}">${linkName}</span>`;
    }
    return html;
};