UNPKG

@matechat/core

Version:

前端智能化场景解决方案UI库,轻松构建你的AI应用。

49 lines (48 loc) 1.45 kB
class n { constructor(i = {}) { this.config = i, this.mermaidInstance = null, this.isLoading = !1, this.lastValidResult = ""; } async loadMermaid() { if (this.mermaidInstance) return this.mermaidInstance; if (this.isLoading) return new Promise((i) => { const t = () => { this.mermaidInstance ? i(this.mermaidInstance) : setTimeout(t, 50); }; t(); }); this.isLoading = !0; try { const { default: i } = await import("mermaid"); return i.initialize({ theme: this.config.theme || "default", startOnLoad: !1, suppressErrorRendering: !0, ...this.config }), this.mermaidInstance = i, i; } catch (i) { throw console.error("Failed to load mermaid:", i), new Error("Failed to load mermaid library"); } finally { this.isLoading = !1; } } async renderMermaid(i, t = "light") { try { const e = await this.loadMermaid(); this.config.theme !== t && (this.config.theme = t, e.initialize({ startOnLoad: !1, suppressErrorRendering: !0, theme: t === "dark" ? "dark" : "default", ...this.config })); const a = `mc_mermaid_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`, { svg: r } = await e.render(a, i); return this.lastValidResult = r, r; } catch { return this.lastValidResult; } } } export { n as MermaidService };