Jelajahi Sumber

:art: fix https://github.com/Vanessa219/vditor/issues/1672

Vanessa 1 tahun lalu
induk
melakukan
ad7522ae42
3 mengubah file dengan 8 tambahan dan 2 penghapusan
  1. 1 0
      CHANGELOG.md
  2. 3 2
      src/ts/markdown/chartRender.ts
  3. 4 0
      src/ts/util/function.ts

+ 1 - 0
CHANGELOG.md

@@ -19,6 +19,7 @@
 
 
 ### v3.10.6 / 2024-09
 ### v3.10.6 / 2024-09
 
 
+* [支持 echarts 中执行函数](https://github.com/Vanessa219/vditor/issues/1672) `改进功能`
 * [井号后输入空格就渲染标题](https://github.com/Vanessa219/vditor/issues/729) `改进功能`
 * [井号后输入空格就渲染标题](https://github.com/Vanessa219/vditor/issues/729) `改进功能`
 * [ir 模式下标题删除报错](https://github.com/Vanessa219/vditor/issues/1666) `修复缺陷`
 * [ir 模式下标题删除报错](https://github.com/Vanessa219/vditor/issues/1666) `修复缺陷`
 * [升级 echarts 至 5.5.1](https://github.com/Vanessa219/vditor/issues/1664) `开发重构`
 * [升级 echarts 至 5.5.1](https://github.com/Vanessa219/vditor/issues/1664) `开发重构`

+ 3 - 2
src/ts/markdown/chartRender.ts

@@ -1,6 +1,7 @@
 import {Constants} from "../constants";
 import {Constants} from "../constants";
 import {addScript} from "../util/addScript";
 import {addScript} from "../util/addScript";
 import {chartRenderAdapter} from "./adapterRender";
 import {chartRenderAdapter} from "./adapterRender";
+import {looseJsonParse} from "../util/function";
 
 
 declare const echarts: {
 declare const echarts: {
     init(element: HTMLElement, theme?: string): IEChart;
     init(element: HTMLElement, theme?: string): IEChart;
@@ -10,7 +11,7 @@ export const chartRender = (element: (HTMLElement | Document) = document, cdn =
     const echartsElements = chartRenderAdapter.getElements(element);
     const echartsElements = chartRenderAdapter.getElements(element);
     if (echartsElements.length > 0) {
     if (echartsElements.length > 0) {
         addScript(`${cdn}/dist/js/echarts/echarts.min.js?v=5.5.1`, "vditorEchartsScript").then(() => {
         addScript(`${cdn}/dist/js/echarts/echarts.min.js?v=5.5.1`, "vditorEchartsScript").then(() => {
-            echartsElements.forEach((e: HTMLDivElement) => {
+            echartsElements.forEach(async (e: HTMLDivElement) => {
                 if (e.parentElement.classList.contains("vditor-wysiwyg__pre") ||
                 if (e.parentElement.classList.contains("vditor-wysiwyg__pre") ||
                     e.parentElement.classList.contains("vditor-ir__marker--pre")) {
                     e.parentElement.classList.contains("vditor-ir__marker--pre")) {
                     return;
                     return;
@@ -24,7 +25,7 @@ export const chartRender = (element: (HTMLElement | Document) = document, cdn =
                     if (e.getAttribute("data-processed") === "true") {
                     if (e.getAttribute("data-processed") === "true") {
                         return;
                         return;
                     }
                     }
-                    const option = JSON.parse(text);
+                    const option = await looseJsonParse(text);
                     echarts.init(e, theme === "dark" ? "dark" : undefined).setOption(option);
                     echarts.init(e, theme === "dark" ? "dark" : undefined).setOption(option);
                     e.setAttribute("data-processed", "true");
                     e.setAttribute("data-processed", "true");
                 } catch (error) {
                 } catch (error) {

+ 4 - 0
src/ts/util/function.ts

@@ -9,3 +9,7 @@ export const getSearch = (key: string, link = window.location.search) => {
     const urlSearchParams = new URLSearchParams(params.substring(0, hashIndex >= 0 ? hashIndex : undefined));
     const urlSearchParams = new URLSearchParams(params.substring(0, hashIndex >= 0 ? hashIndex : undefined));
     return urlSearchParams.get(key);
     return urlSearchParams.get(key);
 };
 };
+
+export const looseJsonParse = (text: string) => {
+    return Function(`"use strict";return (${text})`)();
+};