|
| 1 | +/* |
| 2 | + * GNU GENERAL PUBLIC LICENSE |
| 3 | + * Version 3, 29 June 2007 |
| 4 | + * |
| 5 | + * Copyright (C) 2024 Terwer, Inc. <https://terwer.space/> |
| 6 | + * Everyone is permitted to copy and distribute verbatim copies |
| 7 | + * of this license document, but changing it is not allowed. |
| 8 | + */ |
| 9 | + |
| 10 | +import { JsonUtil } from "zhi-common" |
| 11 | + |
| 12 | +/** |
| 13 | + * ECharts 渲染插件 - 客户端 |
| 14 | + * |
| 15 | + * @author terwer |
| 16 | + * @version 1.0.0 |
| 17 | + * @since 0.0.1 |
| 18 | + */ |
| 19 | +export default defineNuxtPlugin(({ vueApp }) => { |
| 20 | + const logger = createAppLogger("echarts-client-plugin") |
| 21 | + |
| 22 | + // 解析配置内容 |
| 23 | + const parseOption = (content: string) => { |
| 24 | + if (!content) { |
| 25 | + return null |
| 26 | + } |
| 27 | + |
| 28 | + // 清理内容 |
| 29 | + const cleanContent = content.trim() |
| 30 | + .replace(/\r\n/g, "\n") |
| 31 | + .replace(/\n\s+/g, "\n") |
| 32 | + |
| 33 | + try { |
| 34 | + // 尝试作为 JSON 解析 |
| 35 | + const jsonOption = JsonUtil.safeParse(cleanContent, null) |
| 36 | + if (jsonOption) { |
| 37 | + return jsonOption |
| 38 | + } |
| 39 | + |
| 40 | + // 尝试作为 JavaScript 对象解析 |
| 41 | + try { |
| 42 | + // 检查是否是立即执行函数 |
| 43 | + const isIIFE = /^\s*\(\s*\(\s*\)\s*=>\s*\{/.test(cleanContent) |
| 44 | + if (isIIFE) { |
| 45 | + // eslint-disable-next-line no-new-func |
| 46 | + const fn = new Function(`return ${cleanContent}`)() |
| 47 | + return fn() |
| 48 | + } |
| 49 | + // 如果是普通 JavaScript 对象 |
| 50 | + // eslint-disable-next-line no-new-func |
| 51 | + const jsOption = new Function(`return ${cleanContent}`)() |
| 52 | + return jsOption |
| 53 | + } catch (e) { |
| 54 | + logger.error("Failed to parse JavaScript option", e) |
| 55 | + return null |
| 56 | + } |
| 57 | + } catch (e) { |
| 58 | + logger.error("Failed to parse option", e) |
| 59 | + return null |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + vueApp.directive("echarts", (el: HTMLElement) => { |
| 64 | + logger.info("Start handling ECharts blocks on client") |
| 65 | + // 查找所有 ECharts 代码块 |
| 66 | + const echartsBlocks = el.querySelectorAll("[data-type=\"NodeCodeBlock\"][data-subtype=\"echarts\"]") |
| 67 | + echartsBlocks.forEach((block: any) => { |
| 68 | + const content = block.getAttribute("data-content") |
| 69 | + if (content) { |
| 70 | + try { |
| 71 | + const option = parseOption(content) |
| 72 | + if (!option) { |
| 73 | + logger.error("Failed to parse ECharts option") |
| 74 | + return |
| 75 | + } |
| 76 | + // 创建新的图表容器 |
| 77 | + const chartContainer = document.createElement("div") |
| 78 | + chartContainer.style.width = "100%" |
| 79 | + chartContainer.style.height = "400px" |
| 80 | + // 替换原有的代码块 |
| 81 | + block.parentNode.replaceChild(chartContainer, block) |
| 82 | + // 初始化图表 |
| 83 | + const chart = (window as any).echarts.init(chartContainer) |
| 84 | + chart.setOption(option) |
| 85 | + // 监听窗口大小变化 |
| 86 | + window.addEventListener("resize", () => { |
| 87 | + chart.resize() |
| 88 | + }) |
| 89 | + } catch (e) { |
| 90 | + logger.error("Failed to render ECharts", e) |
| 91 | + } |
| 92 | + } |
| 93 | + }) |
| 94 | + }) |
| 95 | +}) |
0 commit comments