Skip to content

Commit

Permalink
Merge pull request #19 from haydenull/main
Browse files Browse the repository at this point in the history
feat: ✨ Supports echarts
  • Loading branch information
xyhp915 committed Feb 9, 2023
2 parents 1c0f2dc + d1ad86f commit 5c96276
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,6 @@ dist
# TernJS port file
.tern-port
.idea
.parcel-cache/
.parcel-cache/

.DS_Store
57 changes: 57 additions & 0 deletions src/echarts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import '@logseq/libs'
import React from 'react'

export default function (props: { content: string }) {
const { content } = props
const elRef = React.useRef(null)
const echartsRef = React.useRef(null)
const [ready, setReady] = React.useState(false)
const host = logseq.Experiments.ensureHostScope()
const [theme, setTheme] = React.useState<'light' | 'dark'>('light')

React.useEffect(() => {
logseq.App.onThemeModeChanged(({ mode }) => setTheme(mode))
}, [])

React.useEffect(() => {
if (echartsRef.current) {
echartsRef.current.setOption({ darkMode: theme === 'dark' })
}
}, [theme])

React.useEffect(() => {
if (host.echarts) {
return setReady(true)
}

let timer
logseq.Experiments
.loadScripts('./vendors/echarts.min.js')
.then(() => {
timer = setTimeout(() => {
setReady(true)
}, 50)
})

return () => {
timer && clearTimeout(timer)
}
}, [ready])

React.useEffect(() => {
if (!ready || !content?.trim()) return

if (host.echarts) {
if (elRef.current) {
const echarts = (parent.window as any).echarts
const myChart = echarts.init(elRef.current)
echartsRef.current = myChart
const option = JSON.parse(content)
console.log('[faiz:] === myChart', myChart, option)
myChart.setOption(option)
}
}
}, [ready, content, elRef.current])

return (<div style={{ minHeight: '300px' }} className={'echarts'} ref={elRef}>Echarts Loading...</div>)
}
21 changes: 19 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import '@logseq/libs'
import { LSPluginBaseInfo, SettingSchemaDesc } from '@logseq/libs/dist/LSPlugin'
import mermaidRenderer from './mermaid'
import echartsRenderer from './echarts'

const Supports = {
Mermaid: 'mermaid'
Mermaid: 'mermaid',
Echarts: 'echarts',
}

const settingsSchema: Array<SettingSchemaDesc> = [
Expand All @@ -13,7 +15,14 @@ const settingsSchema: Array<SettingSchemaDesc> = [
title: 'Support mermaid?',
description: 'Mermaid lets you create diagrams and visualizations using text and code.',
default: true
}
},
{
key: 'echarts',
type: 'boolean',
title: 'Support echarts?',
description: 'Use Echarts to render the chart.',
default: true
},
]

function main (baseInfo: LSPluginBaseInfo) {
Expand All @@ -27,6 +36,14 @@ function main (baseInfo: LSPluginBaseInfo) {
}
)
}
if (settings.echarts) {
logseq.Experiments.registerFencedCodeRenderer(
Supports.Echarts, {
edit: true,
render: echartsRenderer,
}
)
}
}

// entry
Expand Down
45 changes: 45 additions & 0 deletions vendors/echarts.min.js

Large diffs are not rendered by default.

0 comments on commit 5c96276

Please # to comment.