Skip to content

Commit a5a33d4

Browse files
committed
feat(app): add ECharts rendering support
- Add ECharts directive to client-side code - Include ECharts library in various configuration files - Implement ECharts rendering plugin for both client and server
1 parent 12160b0 commit a5a33d4

File tree

9 files changed

+205
-5
lines changed

9 files changed

+205
-5
lines changed

apps/app/components/static/content/Main.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const VNode = () =>
5050
v-fold
5151
v-desc
5252
v-htmlblock
53+
v-echarts
5354
class="protyle-wysiwyg protyle-wysiwyg--attr"
5455
spellcheck="false"
5556
contenteditable="false"

apps/app/nuxt.cloudflare.config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,25 @@ export default defineNuxtConfig({
6262
},
6363
{
6464
children: "eruda.init();console.log('eruda inited');",
65-
},
65+
} as any,
6666
{
6767
defer: true,
6868
src: appBase + "libs/katex/0.16.10/katex.min.js",
6969
},
70+
{
71+
defer: true,
72+
src: appBase + "resources/stage/protyle/js/echarts/echarts.min.js",
73+
},
7074
]
7175
: [
7276
{
7377
defer: true,
7478
src: appBase + "libs/katex/0.16.10/katex.min.js",
7579
},
80+
{
81+
defer: true,
82+
src: appBase + "resources/stage/protyle/js/echarts/echarts.min.js",
83+
},
7684
],
7785
},
7886
},

apps/app/nuxt.config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,25 @@ export default defineNuxtConfig({
6262
},
6363
{
6464
children: "eruda.init();console.log('eruda inited');",
65-
},
65+
} as any,
6666
{
6767
defer: true,
6868
src: appBase + "libs/katex/0.16.10/katex.min.js",
6969
},
70+
{
71+
defer: true,
72+
src: appBase + "resources/stage/protyle/js/echarts/echarts.min.js",
73+
},
7074
]
7175
: [
7276
{
7377
defer: true,
7478
src: appBase + "libs/katex/0.16.10/katex.min.js",
7579
},
80+
{
81+
defer: true,
82+
src: appBase + "resources/stage/protyle/js/echarts/echarts.min.js",
83+
},
7684
],
7785
},
7886
},

apps/app/nuxt.node.config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,25 @@ export default defineNuxtConfig({
6262
},
6363
{
6464
children: "eruda.init();console.log('eruda inited');",
65-
},
65+
} as any,
6666
{
6767
defer: true,
6868
src: appBase + "libs/katex/0.16.10/katex.min.js",
6969
},
70+
{
71+
defer: true,
72+
src: appBase + "resources/stage/protyle/js/echarts/echarts.min.js",
73+
},
7074
]
7175
: [
7276
{
7377
defer: true,
7478
src: appBase + "libs/katex/0.16.10/katex.min.js",
7579
},
80+
{
81+
defer: true,
82+
src: appBase + "resources/stage/protyle/js/echarts/echarts.min.js",
83+
},
7684
],
7785
},
7886
},

apps/app/nuxt.siyuan.config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,25 @@ export default defineNuxtConfig({
9393
},
9494
{
9595
children: "eruda.init();console.log('eruda inited');",
96-
},
96+
} as any,
9797
{
9898
defer: true,
9999
src: appBase + "libs/katex/0.16.10/katex.min.js",
100100
},
101+
{
102+
defer: true,
103+
src: appBase + "resources/stage/protyle/js/echarts/echarts.min.js",
104+
},
101105
]
102106
: [
103107
{
104108
defer: true,
105109
src: appBase + "libs/katex/0.16.10/katex.min.js",
106110
},
111+
{
112+
defer: true,
113+
src: appBase + "resources/stage/protyle/js/echarts/echarts.min.js",
114+
},
107115
],
108116
},
109117
},

apps/app/nuxt.vercel.config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,25 @@ export default defineNuxtConfig({
6262
},
6363
{
6464
children: "eruda.init();console.log('eruda inited');",
65-
},
65+
} as any,
6666
{
6767
defer: true,
6868
src: appBase + "libs/katex/0.16.10/katex.min.js",
6969
},
70+
{
71+
defer: true,
72+
src: appBase + "resources/stage/protyle/js/echarts/echarts.min.js",
73+
},
7074
]
7175
: [
7276
{
7377
defer: true,
7478
src: appBase + "libs/katex/0.16.10/katex.min.js",
7579
},
80+
{
81+
defer: true,
82+
src: appBase + "resources/stage/protyle/js/echarts/echarts.min.js",
83+
},
7684
],
7785
},
7886
},
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
})
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
/**
11+
* ECharts 渲染插件 - 服务端
12+
*
13+
* @author terwer
14+
* @version 1.0.0
15+
* @since 0.0.1
16+
*/
17+
export default defineNuxtPlugin(({ vueApp }) => {
18+
vueApp.directive("echarts", {})
19+
})

apps/app/public/resources/stage/protyle/js/echarts/echarts.min.js

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)