mirror of
https://github.com/diced/zipline.git
synced 2025-12-05 20:40:12 -08:00
@@ -1,12 +1,16 @@
|
|||||||
import { ActionIcon, CopyButton, Paper, ScrollArea, Text, useMantineTheme } from '@mantine/core';
|
import { ActionIcon, Button, CopyButton, Paper, ScrollArea, Text, useMantineTheme } from '@mantine/core';
|
||||||
import { IconCheck, IconClipboardCopy } from '@tabler/icons-react';
|
import { IconCheck, IconClipboardCopy, IconChevronDown, IconChevronUp } from '@tabler/icons-react';
|
||||||
import hljs from 'highlight.js';
|
import hljs from 'highlight.js';
|
||||||
|
import { useState } from 'react';
|
||||||
|
|
||||||
export default function HighlightCode({ language, code }: { language: string; code: string }) {
|
export default function HighlightCode({ language, code }: { language: string; code: string }) {
|
||||||
const theme = useMantineTheme();
|
const theme = useMantineTheme();
|
||||||
|
const [expanded, setExpanded] = useState(false);
|
||||||
|
|
||||||
const lines = code.split('\n');
|
const lines = code.split('\n');
|
||||||
const lineNumbers = lines.map((_, i) => i + 1);
|
const lineNumbers = lines.map((_, i) => i + 1);
|
||||||
|
const displayLines = expanded ? lines : lines.slice(0, 50);
|
||||||
|
const displayLineNumbers = expanded ? lineNumbers : lineNumbers.slice(0, 50);
|
||||||
|
|
||||||
if (!hljs.getLanguage(language)) {
|
if (!hljs.getLanguage(language)) {
|
||||||
language = 'text';
|
language = 'text';
|
||||||
@@ -35,7 +39,7 @@ export default function HighlightCode({ language, code }: { language: string; co
|
|||||||
<ScrollArea type='auto' dir='ltr' offsetScrollbars={false}>
|
<ScrollArea type='auto' dir='ltr' offsetScrollbars={false}>
|
||||||
<pre style={{ margin: 0 }} className='theme'>
|
<pre style={{ margin: 0 }} className='theme'>
|
||||||
<code className='theme'>
|
<code className='theme'>
|
||||||
{lines.map((line, i) => (
|
{displayLines.map((line, i) => (
|
||||||
<div key={i}>
|
<div key={i}>
|
||||||
<Text
|
<Text
|
||||||
component='span'
|
component='span'
|
||||||
@@ -44,7 +48,7 @@ export default function HighlightCode({ language, code }: { language: string; co
|
|||||||
mr='md'
|
mr='md'
|
||||||
style={{ userSelect: 'none', fontFamily: 'monospace' }}
|
style={{ userSelect: 'none', fontFamily: 'monospace' }}
|
||||||
>
|
>
|
||||||
{lineNumbers[i]}
|
{displayLineNumbers[i]}
|
||||||
</Text>
|
</Text>
|
||||||
<span
|
<span
|
||||||
className='line'
|
className='line'
|
||||||
@@ -57,6 +61,18 @@ export default function HighlightCode({ language, code }: { language: string; co
|
|||||||
</code>
|
</code>
|
||||||
</pre>
|
</pre>
|
||||||
</ScrollArea>
|
</ScrollArea>
|
||||||
|
|
||||||
|
{lines.length > 50 && (
|
||||||
|
<Button
|
||||||
|
variant='outline'
|
||||||
|
size='compact-sm'
|
||||||
|
onClick={() => setExpanded(!expanded)}
|
||||||
|
leftSection={expanded ? <IconChevronUp size='1rem' /> : <IconChevronDown size='1rem' />}
|
||||||
|
style={{ position: 'absolute', bottom: '0.5rem', right: '0.5rem' }}
|
||||||
|
>
|
||||||
|
{expanded ? 'Show Less' : `Show More (${lines.length - 50} more lines)`}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
</Paper>
|
</Paper>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user