Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,6 +1,6 @@
@use '../../../styles/mixins.scss';
@use '../../styles/mixins.scss';

.ydb-connect-to-db-syntax-highlighter {
.ydb-syntax-highlighter {
&__wrapper {
position: relative;
z-index: 0;
Expand Down
79 changes: 79 additions & 0 deletions src/components/SyntaxHighlighter/YDBSyntaxHighlighter.tsx
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);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe its possible to register needed language on demand inside components? Or its library requirement to register on top level?

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated component, now it registers languages on render


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,
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>
);
}
25 changes: 25 additions & 0 deletions src/components/SyntaxHighlighter/YqlHighlighter.tsx
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) {
Copy link
Member Author

@artemmufazalov artemmufazalov Mar 6, 2025

Choose a reason for hiding this comment

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

Previous YqlHighlighter with little changes to render queries in tables and overview. The reason - we load a lot of other languages in YDBSyntaxHighlighter, so it's loaded with lazy load. However, in tables lazy load works not very good, since there are may loaders and row height changes. So to make it lazy in tables we need first create some loader for queries - it should display query with proper size but without highlight

const style = useSyntaxHighlighterStyle(true);

return (
<div className={b(null, className)}>
<SyntaxHighlighter language="yql" style={style}>
{children}
</SyntaxHighlighter>
</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