Skip to content

Commit

Permalink
feat(editor): support setting theme for charts via the URL parameter:…
Browse files Browse the repository at this point in the history
… `theme=themeName`
  • Loading branch information
plainheart committed Feb 5, 2024
1 parent 0068a81 commit c9bd8b9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/common/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export const store = {
version: '',
locale: '',

// 'light' and 'dark' are default built-in themes
theme: URL_PARAMS.theme === 'light' ? null : URL_PARAMS.theme,
darkMode: URL_PARAMS.theme === 'dark',
enableDecal: 'decal' in URL_PARAMS,
renderer: (() => {
Expand Down
2 changes: 1 addition & 1 deletion src/editor/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ export default {
ts: store.typeCheck,
esm: this.fullCodeConfig.esm,
// legacy: true,
theme: store.darkMode ? 'dark' : '',
theme: store.darkMode ? 'dark' : store.theme,
renderer: store.renderer,
useDirtyRect: store.useDirtyRect,
ROOT_PATH: store.cdnRoot,
Expand Down
12 changes: 11 additions & 1 deletion src/editor/Preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -191,28 +191,38 @@ function getScripts(nightly) {
const code = store.runCode;
return [
// echarts
echartsDir +
getScriptURL(SCRIPT_URLS.echartsJS) +
(store.isPR ? '?_=' + (store.prLatestCommit || Date.now()) : ''),
// echarts-gl
...(isGL
? [
isLocal
? SCRIPT_URLS.localEChartsGLDir + '/dist/echarts-gl.js'
: getScriptURL(SCRIPT_URLS.echartsGLJS)
]
: []),
// echarts theme
...(!store.darkMode && store.theme
? [echartsDir + `/theme/${store.theme}.js`]
: []),
// echarts bmap extension
...(hasBMap || /coordinateSystem.*:.*['"]bmap['"]/g.test(code)
? [
SCRIPT_URLS.bmapLibJS,
echartsDir + getScriptURL(SCRIPT_URLS.echartsBMapJS)
]
: []),
// echarts stat
...(code.indexOf('ecStat.') > -1
? [getScriptURL(SCRIPT_URLS.echartsStatJS)]
: []),
// echarts map
...(/map.*:.*['"]world['"]/g.test(code)
? [SCRIPT_URLS.echartsWorldMapJS]
: []),
// data gui
...(code.indexOf('app.config') > -1 ? [SCRIPT_URLS.datGUIMinJS] : [])
].map((url) => ({ src: url }));
}
Expand Down Expand Up @@ -385,7 +395,7 @@ export default {
renderer: isCanvas ? null : store.renderer,
useDirtyRect: store.useDirtyRect && isCanvas ? 1 : null,
decal: store.enableDecal ? 1 : null,
theme: store.darkMode ? 'dark' : null
theme: store.darkMode ? 'dark' : store.theme || null
};
}
},
Expand Down
12 changes: 10 additions & 2 deletions src/editor/downloadExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ export function download(sourceHeader) {
const jqueryScriptCode = hasJQueryJS
? `<script type="text/javascript" src="${SCRIPT_URLS.jQueryJS}"></script>`
: '';

const echartsDir = SCRIPT_URLS[
store.echartsVersion.indexOf('dev') > -1
? 'echartsNightlyDir'
: 'echartsDir'
].replace('{{version}}', store.echartsVersion);
const echarts4Dir = SCRIPT_URLS.echartsDir.replace('{{version}}', '4.9.0');

const themeScriptCode =
!store.darkMode && store.theme
? `<!-- theme -->\n <script type="text/javascript" src="${echartsDir}/theme/${store.theme}.js"></script>`
: '';

const code = `<!--
${sourceHeader}
-->
Expand All @@ -35,6 +40,7 @@ ${sourceHeader}
<script type="text/javascript" src="${echartsDir}${
SCRIPT_URLS.echartsJS
}"></script>
${themeScriptCode}
<!-- Uncomment this line if you want to dataTool extension
<script type="text/javascript" src="${echartsDir}/dist/extension/dataTool.min.js"></script>
-->
Expand All @@ -57,7 +63,9 @@ ${sourceHeader}
<script type="text/javascript">
var dom = document.getElementById('container');
var myChart = echarts.init(dom, ${store.darkMode ? "'dark'" : 'null'}, {
var myChart = echarts.init(dom, ${
store.darkMode ? "'dark'" : store.theme ? `'${store.theme}'` : 'null'
}, {
renderer: '${store.renderer}',
useDirtyRect: ${store.useDirtyRect}
});
Expand Down
4 changes: 3 additions & 1 deletion src/editor/sandbox/openwith/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export function getTemplates(title, scripts, css) {
</html>`;

const jsTpl = `var dom = document.getElementById('chart-container');
var myChart = echarts.init(dom, ${store.darkMode ? "'dark'" : 'null'}, {
var myChart = echarts.init(dom, ${
store.darkMode ? "'dark'" : store.theme ? `'${store.theme}'` : 'null'
}, {
renderer: '${store.renderer}',
useDirtyRect: ${store.useDirtyRect}
});
Expand Down
2 changes: 1 addition & 1 deletion src/editor/sandbox/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function setup(isShared) {
this.dispose();
chartInstance = echarts.init(
document.getElementById('chart-container'),
store.darkMode ? 'dark' : '',
store.darkMode ? 'dark' : store.theme,
{
renderer: store.renderer,
useDirtyRect: store.useDirtyRect
Expand Down

0 comments on commit c9bd8b9

Please sign in to comment.