Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: ✨ Supports echarts #19

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.