Skip to content
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

Merged
merged 3 commits into from
Mar 10, 2025
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
9 changes: 7 additions & 2 deletions src/components/ConnectToDB/ConnectToDBDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {Dialog, Tabs} from '@gravity-ui/uikit';

import {cn} from '../../utils/cn';
import {LinkWithIcon} from '../LinkWithIcon/LinkWithIcon';
import {YDBSyntaxHighlighterLazy} from '../SyntaxHighlighter/lazy';

import {ConnectToDBSyntaxHighlighterLazy} from './ConnectToDBSyntaxHighlighter/lazy';
import {getDocsLink} from './getDocsLink';
import i18n from './i18n';
import {getSnippetCode} from './snippets';
Expand Down Expand Up @@ -52,7 +52,12 @@ function ConnectToDBDialog({open, onClose, database, endpoint}: ConnectToDBDialo
className={b('dialog-tabs')}
/>
<div className={b('snippet-container')}>
<ConnectToDBSyntaxHighlighterLazy language={activeTab} text={snippet} />
<YDBSyntaxHighlighterLazy
language={activeTab}
text={snippet}
transparentBackground={false}
withCopy
/>
</div>
{docsLink ? (
<LinkWithIcon
Expand Down

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion src/components/ConnectToDB/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"documentation": "Documentation",

"close": "Close",
"copy": "Copy",

"docs_bash": "https://ydb.tech/docs/en/concepts/connect",
"docs_cpp": "https://ydb.tech/docs/en/dev/example-app/example-cpp",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
@use '../../../styles/mixins.scss';
@use '../../styles/mixins.scss';

.ydb-connect-to-db-syntax-highlighter {
&__wrapper {
position: relative;
z-index: 0;
.ydb-syntax-highlighter {
position: relative;
z-index: 0;

height: 100%;
}
height: 100%;

&__sticky-container {
z-index: 1;
Expand Down
86 changes: 86 additions & 0 deletions src/components/SyntaxHighlighter/YDBSyntaxHighlighter.tsx
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;
Comment on lines +30 to +31
Copy link
Member Author

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

};

export function YDBSyntaxHighlighter({
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Named it YDBSyntaxHighlighter to prevent names collision with SyntaxHighlighter

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());
Copy link
Member Author

Choose a reason for hiding this comment

The 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>
);
}
3 changes: 3 additions & 0 deletions src/components/SyntaxHighlighter/i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"copy": "Copy"
}
7 changes: 7 additions & 0 deletions src/components/SyntaxHighlighter/i18n/index.ts
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});
6 changes: 6 additions & 0 deletions src/components/SyntaxHighlighter/lazy.ts
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',
);
3 changes: 3 additions & 0 deletions src/components/SyntaxHighlighter/shared.ts
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');
123 changes: 123 additions & 0 deletions src/components/SyntaxHighlighter/themes.ts
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;
}
10 changes: 10 additions & 0 deletions src/components/SyntaxHighlighter/types.ts
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';
Loading
Loading