-
Notifications
You must be signed in to change notification settings - Fork 11
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
feat(YDBSyntaxHighlighter): separate component, load languages on demand #2004
Changes from all commits
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,86 @@ | ||
import React from 'react'; | ||
|
||
import {ClipboardButton} from '@gravity-ui/uikit'; | ||
import {nanoid} from '@reduxjs/toolkit'; | ||
import {PrismLight as ReactSyntaxHighlighter} from 'react-syntax-highlighter'; | ||
|
||
import i18n from './i18n'; | ||
import {b} from './shared'; | ||
import {useSyntaxHighlighterStyle} from './themes'; | ||
import type {Language} from './types'; | ||
import {yql} from './yql'; | ||
|
||
import './YDBSyntaxHighlighter.scss'; | ||
|
||
async function registerLanguage(lang: Language) { | ||
if (lang === 'yql') { | ||
ReactSyntaxHighlighter.registerLanguage('yql', yql); | ||
} else { | ||
const {default: syntax} = await import( | ||
`react-syntax-highlighter/dist/esm/languages/prism/${lang}` | ||
); | ||
ReactSyntaxHighlighter.registerLanguage(lang, syntax); | ||
} | ||
} | ||
|
||
type YDBSyntaxHighlighterProps = { | ||
text: string; | ||
language: Language; | ||
className?: string; | ||
transparentBackground?: boolean; | ||
withCopy?: boolean; | ||
}; | ||
|
||
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 = true, | ||
withCopy, | ||
}: YDBSyntaxHighlighterProps) { | ||
const [highlighterKey, setHighlighterKey] = React.useState(''); | ||
|
||
const style = useSyntaxHighlighterStyle(transparentBackground); | ||
|
||
React.useEffect(() => { | ||
async function registerLangAndUpdateKey() { | ||
await registerLanguage(language); | ||
setHighlighterKey(nanoid()); | ||
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. Without component update syntax won't be highlighted |
||
} | ||
registerLangAndUpdateKey(); | ||
}, [language]); | ||
|
||
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(null, className)}> | ||
{renderCopyButton()} | ||
|
||
<ReactSyntaxHighlighter | ||
key={highlighterKey} | ||
language={language} | ||
style={style} | ||
customStyle={{height: '100%'}} | ||
> | ||
{text} | ||
</ReactSyntaxHighlighter> | ||
</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'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added these props to make component customisable