-
Notifications
You must be signed in to change notification settings - Fork 17
feat(YDBSyntaxHighlighter): separate component, load languages on demand #2004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import {ClipboardButton} from '@gravity-ui/uikit'; | ||
| import {PrismLight as ReactSyntaxHighlighter} from 'react-syntax-highlighter'; | ||
| import bash from 'react-syntax-highlighter/dist/esm/languages/prism/bash'; | ||
| import cpp from 'react-syntax-highlighter/dist/esm/languages/prism/cpp'; | ||
| import csharp from 'react-syntax-highlighter/dist/esm/languages/prism/csharp'; | ||
| import go from 'react-syntax-highlighter/dist/esm/languages/prism/go'; | ||
| import java from 'react-syntax-highlighter/dist/esm/languages/prism/java'; | ||
| import javascript from 'react-syntax-highlighter/dist/esm/languages/prism/javascript'; | ||
| import php from 'react-syntax-highlighter/dist/esm/languages/prism/php'; | ||
| import python from 'react-syntax-highlighter/dist/esm/languages/prism/python'; | ||
|
|
||
| import i18n from './i18n'; | ||
| import {b} from './shared'; | ||
| import {useSyntaxHighlighterStyle} from './themes'; | ||
| import type {Language} from './types'; | ||
| import {yql} from './yql'; | ||
|
|
||
| import './YDBSyntaxHighlighter.scss'; | ||
|
|
||
| ReactSyntaxHighlighter.registerLanguage('bash', bash); | ||
| ReactSyntaxHighlighter.registerLanguage('cpp', cpp); | ||
| ReactSyntaxHighlighter.registerLanguage('csharp', csharp); | ||
| ReactSyntaxHighlighter.registerLanguage('go', go); | ||
| ReactSyntaxHighlighter.registerLanguage('java', java); | ||
| ReactSyntaxHighlighter.registerLanguage('javascript', javascript); | ||
| ReactSyntaxHighlighter.registerLanguage('php', php); | ||
| ReactSyntaxHighlighter.registerLanguage('python', python); | ||
| ReactSyntaxHighlighter.registerLanguage('yql', yql); | ||
|
||
|
|
||
| type YDBSyntaxHighlighterProps = { | ||
| text: string; | ||
| language: Language; | ||
| className?: string; | ||
| transparentBackground?: boolean; | ||
| withCopy?: boolean; | ||
|
Comment on lines
+30
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added these props to make component customisable |
||
| }; | ||
|
|
||
| export function YDBSyntaxHighlighter({ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Named it |
||
| text, | ||
| language, | ||
| className, | ||
| transparentBackground, | ||
| withCopy, | ||
| }: YDBSyntaxHighlighterProps) { | ||
| const style = useSyntaxHighlighterStyle(transparentBackground); | ||
|
|
||
| const renderCopyButton = () => { | ||
| if (withCopy) { | ||
| return ( | ||
| <div className={b('sticky-container')}> | ||
| <ClipboardButton | ||
| view="flat-secondary" | ||
| size="s" | ||
| className={b('copy')} | ||
| text={text} | ||
| > | ||
| {i18n('copy')} | ||
| </ClipboardButton> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return null; | ||
| }; | ||
|
|
||
| return ( | ||
| <div className={b('wrapper', className)}> | ||
| {renderCopyButton()} | ||
|
|
||
| <ReactSyntaxHighlighter | ||
| language={language} | ||
| style={style} | ||
| customStyle={{height: '100%'}} | ||
| > | ||
| {text} | ||
| </ReactSyntaxHighlighter> | ||
| </div> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import {PrismLight as SyntaxHighlighter} from 'react-syntax-highlighter'; | ||
|
|
||
| import {b} from './shared'; | ||
| import {useSyntaxHighlighterStyle} from './themes'; | ||
| import {yql} from './yql'; | ||
|
|
||
| SyntaxHighlighter.registerLanguage('yql', yql); | ||
|
|
||
| interface YqlHighlighterProps { | ||
| children: string; | ||
| className?: string; | ||
| } | ||
|
|
||
| /** SyntaxHighlighter with just YQL for sync load */ | ||
| export function YqlHighlighter({children, className}: YqlHighlighterProps) { | ||
|
||
| const style = useSyntaxHighlighterStyle(true); | ||
|
|
||
| return ( | ||
| <div className={b(null, className)}> | ||
| <SyntaxHighlighter language="yql" style={style}> | ||
| {children} | ||
| </SyntaxHighlighter> | ||
| </div> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "copy": "Copy" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import {registerKeysets} from '../../../utils/i18n'; | ||
|
|
||
| import en from './en.json'; | ||
|
|
||
| const COMPONENT = 'ydb-syntax-highlighter'; | ||
|
|
||
| export default registerKeysets(COMPONENT, {en}); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import {lazyComponent} from '../../utils/lazyComponent'; | ||
|
|
||
| export const YDBSyntaxHighlighterLazy = lazyComponent( | ||
| () => import('./YDBSyntaxHighlighter'), | ||
| 'YDBSyntaxHighlighter', | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import {cn} from '../../utils/cn'; | ||
|
|
||
| export const b = cn('ydb-syntax-highlighter'); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import {useThemeValue} from '@gravity-ui/uikit'; | ||
| import {materialLight, vscDarkPlus} from 'react-syntax-highlighter/dist/esm/styles/prism'; | ||
|
|
||
| export const lightTransparent = { | ||
| ...materialLight, | ||
| 'pre[class*="language-"]': { | ||
| ...materialLight['pre[class*="language-"]'], | ||
| background: 'transparent', | ||
| margin: 0, | ||
| }, | ||
| 'code[class*="language-"]': { | ||
| ...materialLight['code[class*="language-"]'], | ||
| background: 'transparent', | ||
| color: 'var(--g-color-text-primary)', | ||
| whiteSpace: 'pre-wrap' as const, | ||
| }, | ||
| comment: { | ||
| color: '#969896', | ||
| }, | ||
| string: { | ||
| color: '#a31515', | ||
| }, | ||
| tablepath: { | ||
| color: '#338186', | ||
| }, | ||
| function: { | ||
| color: '#7a3e9d', | ||
| }, | ||
| udf: { | ||
| color: '#7a3e9d', | ||
| }, | ||
| type: { | ||
| color: '#4d932d', | ||
| }, | ||
| boolean: { | ||
| color: '#608b4e', | ||
| }, | ||
| constant: { | ||
| color: '#608b4e', | ||
| }, | ||
| variable: { | ||
| color: '#001188', | ||
| }, | ||
| }; | ||
|
|
||
| export const darkTransparent = { | ||
| ...vscDarkPlus, | ||
| 'pre[class*="language-"]': { | ||
| ...vscDarkPlus['pre[class*="language-"]'], | ||
| background: 'transparent', | ||
| margin: 0, | ||
| }, | ||
| 'code[class*="language-"]': { | ||
| ...vscDarkPlus['code[class*="language-"]'], | ||
| background: 'transparent', | ||
| color: 'var(--g-color-text-primary)', | ||
| whiteSpace: 'pre-wrap' as const, | ||
| }, | ||
| comment: { | ||
| color: '#969896', | ||
| }, | ||
| string: { | ||
| color: '#ce9178', | ||
| }, | ||
| tablepath: { | ||
| color: '#338186', | ||
| }, | ||
| function: { | ||
| color: '#9e7bb0', | ||
| }, | ||
| udf: { | ||
| color: '#9e7bb0', | ||
| }, | ||
| type: { | ||
| color: '#6A8759', | ||
| }, | ||
| boolean: { | ||
| color: '#608b4e', | ||
| }, | ||
| constant: { | ||
| color: '#608b4e', | ||
| }, | ||
| variable: { | ||
| color: '#74b0df', | ||
| }, | ||
| }; | ||
|
|
||
| const dark: Record<string, React.CSSProperties> = { | ||
| ...darkTransparent, | ||
| 'pre[class*="language-"]': { | ||
| ...darkTransparent['pre[class*="language-"]'], | ||
| background: vscDarkPlus['pre[class*="language-"]'].background, | ||
| scrollbarColor: `var(--g-color-scroll-handle) transparent`, | ||
| }, | ||
| 'code[class*="language-"]': { | ||
| ...darkTransparent['code[class*="language-"]'], | ||
| whiteSpace: 'pre', | ||
| }, | ||
| }; | ||
|
|
||
| const light: Record<string, React.CSSProperties> = { | ||
| ...lightTransparent, | ||
| 'pre[class*="language-"]': { | ||
| ...lightTransparent['pre[class*="language-"]'], | ||
| background: 'var(--g-color-base-misc-light)', | ||
| scrollbarColor: `var(--g-color-scroll-handle) transparent`, | ||
| }, | ||
| 'code[class*="language-"]': { | ||
| ...lightTransparent['code[class*="language-"]'], | ||
| whiteSpace: 'pre', | ||
| }, | ||
| }; | ||
|
|
||
| export function useSyntaxHighlighterStyle(transparentBackground?: boolean) { | ||
| const themeValue = useThemeValue(); | ||
| const isDark = themeValue === 'dark' || themeValue === 'dark-hc'; | ||
|
|
||
| if (transparentBackground) { | ||
| return isDark ? darkTransparent : lightTransparent; | ||
| } | ||
|
|
||
| return isDark ? dark : light; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| export type Language = | ||
| | 'bash' | ||
| | 'cpp' | ||
| | 'csharp' | ||
| | 'go' | ||
| | 'java' | ||
| | 'javascript' | ||
| | 'php' | ||
| | 'python' | ||
| | 'yql'; |
Uh oh!
There was an error while loading. Please reload this page.