This repository has been archived by the owner on Nov 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added vega-lite diagrams Update CHANGELOG.md Co-authored-by: mrdrogdrog <mr.drogdrog@gmail.com>
- Loading branch information
1 parent
0f6ea38
commit 553cd35
Showing
9 changed files
with
690 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/components/markdown-renderer/markdown-it-plugins/highlighted-code.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/components/markdown-renderer/replace-components/vega-lite/vega-chart.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import React, { Fragment, useCallback, useEffect, useRef, useState } from 'react' | ||
import { Alert } from 'react-bootstrap' | ||
import { useTranslation } from 'react-i18next' | ||
import embed, { VisualizationSpec } from 'vega-embed' | ||
import { ShowIf } from '../../../common/show-if/show-if' | ||
|
||
export interface VegaChartProps { | ||
code: string | ||
} | ||
|
||
export const VegaChart: React.FC<VegaChartProps> = ({ code }) => { | ||
const diagramContainer = useRef<HTMLDivElement>(null) | ||
const [error, setError] = useState<string>() | ||
const { t } = useTranslation() | ||
|
||
const showError = useCallback((error: string) => { | ||
if (!diagramContainer.current) { | ||
return | ||
} | ||
console.error(error) | ||
setError(error) | ||
}, []) | ||
|
||
useEffect(() => { | ||
if (!diagramContainer.current) { | ||
return | ||
} | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const spec: VisualizationSpec = JSON.parse(code) | ||
showError('') | ||
embed(diagramContainer.current, spec, { | ||
actions: { | ||
export: true, | ||
source: false, | ||
compiled: false, | ||
editor: false | ||
}, | ||
i18n: { | ||
PNG_ACTION: t('renderer.vega-lite.png'), | ||
SVG_ACTION: t('renderer.vega-lite.svg') | ||
} | ||
}) | ||
.then(result => console.log(result)) | ||
.catch(err => showError(err)) | ||
} catch (err) { | ||
showError(t('renderer.vega-lite.errorJson')) | ||
} | ||
}, [code, showError, t]) | ||
|
||
return <Fragment> | ||
<ShowIf condition={!!error}> | ||
<Alert variant={'danger'}>{error}</Alert> | ||
</ShowIf> | ||
<div className={'text-center'}> | ||
<div ref={diagramContainer}/> | ||
</div> | ||
</Fragment> | ||
} |
16 changes: 16 additions & 0 deletions
16
src/components/markdown-renderer/replace-components/vega-lite/vega-replacer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { DomElement } from 'domhandler' | ||
import React from 'react' | ||
import { ComponentReplacer } from '../ComponentReplacer' | ||
import { VegaChart } from './vega-chart' | ||
|
||
export class VegaReplacer implements ComponentReplacer { | ||
getReplacement (codeNode: DomElement): React.ReactElement | undefined { | ||
if (codeNode.name !== 'code' || !codeNode.attribs || !codeNode.attribs['data-highlight-language'] || codeNode.attribs['data-highlight-language'] !== 'vega-lite' || !codeNode.children || !codeNode.children[0]) { | ||
return | ||
} | ||
|
||
const code = codeNode.children[0].data as string | ||
|
||
return <VegaChart code={code}/> | ||
} | ||
} |
Oops, something went wrong.