ECharts 图表块支持 loose-json (#6917)

* 🎨 ECharts 图表块支持 loose-json

* 📝 更新用户手册 ECharts 相关内容

* 📝 更新用户手册 ECharts 相关内容
This commit is contained in:
颖逸 2022-12-25 10:27:02 +08:00 committed by GitHub
parent a96d6a019f
commit 27c9ac2b25
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 799 additions and 28 deletions

View file

@ -1,6 +1,7 @@
import {addScript} from "../util/addScript";
import {Constants} from "../../constants";
import {hasClosestByClassName} from "../util/hasClosest";
import {looseJsonParse} from "../../util/functions";
export const chartRender = (element: Element, cdn = Constants.PROTYLE_CDN) => {
let echartsElements: Element[] = [];
@ -38,7 +39,7 @@ export const chartRender = (element: Element, cdn = Constants.PROTYLE_CDN) => {
const renderElement = e.firstElementChild.nextElementSibling as HTMLElement;
try {
renderElement.style.height = e.style.height;
const option = JSON.parse(Lute.UnEscapeHTMLStr(e.getAttribute("data-content")));
const option = looseJsonParse(Lute.UnEscapeHTMLStr(e.getAttribute("data-content")));
echarts.init(renderElement, window.siyuan.config.appearance.mode === 1 ? "dark" : undefined, {width}).setOption(option);
e.setAttribute("data-render", "true");
renderElement.classList.remove("ft__error");

View file

@ -45,3 +45,8 @@ export const isDynamicRef = (text: string) => {
export const isFileAnnotation = (text: string) => {
return /^<<assets\/.+\/\d{14}-\w{7} ".+">>$/.test(text);
};
// REF https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/eval
export const looseJsonParse = (text: string) => {
return Function(`"use strict";return (${text})`)();
}