Skip to content

Commit

Permalink
feat(Highlighter): support contentStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
Carrotzpc committed Jul 25, 2024
1 parent 7cb2a85 commit 5963ed6
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/DragPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const DragPanel: React.FC<DragPanelProps> = memo(
const draggleRef = useRef<HTMLDivElement>(null);

const onStart = (_event: DraggableEvent, uiData: DraggableData) => {
// @Todo SSR
const { clientWidth, clientHeight } = window.document.documentElement;
const targetRect = draggleRef.current?.getBoundingClientRect();
if (!targetRect) {
Expand Down
4 changes: 3 additions & 1 deletion src/Highlighter/FullFeatured.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface FullFeaturedHighlighterProps extends DivProps {
* @description The language of the code content
*/
language: string;
contentStyle?: React.CSSProperties;
}

const options: SelectProps['options'] = languageMap.map(item => ({
Expand All @@ -38,6 +39,7 @@ export const FullFeaturedHighlighter = memo<FullFeaturedHighlighterProps>(
allowChangeLanguage = false,
fileName,
icon,
contentStyle,
...rest
}) => {
const [expand, setExpand] = useState(true);
Expand Down Expand Up @@ -78,7 +80,7 @@ export const FullFeaturedHighlighter = memo<FullFeaturedHighlighterProps>(
</Flexbox>
<SyntaxHighlighter
language={lang?.toLowerCase()}
style={expand ? {} : { height: 0, overflow: 'hidden' }}
style={expand ? contentStyle : { ...contentStyle, height: 0, overflow: 'hidden' }}
>
{children}
</SyntaxHighlighter>
Expand Down
6 changes: 5 additions & 1 deletion src/Highlighter/SyntaxHighlighter/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ export const useStyles = createStyles(({ css, token, cx, prefixCls, stylish }) =
shiki: cx(
`${prefix}-shiki`,
css`
overflow: auto;
margin: 0;
padding: 0;
.shiki {
overflow-x: auto;
overflow: unset;
margin: 0;
padding: 0;
background: none !important;
Expand Down Expand Up @@ -110,6 +111,9 @@ export const useStyles = createStyles(({ css, token, cx, prefixCls, stylish }) =
`
),
unshiki: css`
overflow: auto;
margin: 0;
padding: 0;
color: ${token.colorTextDescription};
`,
};
Expand Down
12 changes: 10 additions & 2 deletions src/Highlighter/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import { CopyButton, type CopyButtonProps, Spotlight, Tag } from '@lobehub/ui';
import { ReactNode, memo } from 'react';
import React, { ReactNode, memo } from 'react';

import { DivProps } from '@/types';

Expand Down Expand Up @@ -43,6 +43,10 @@ export interface HighlighterProps extends DivProps {
* @default 'block'
*/
type?: 'ghost' | 'block' | 'pure';
/**
* @description The style of the code content
*/
contentStyle?: React.CSSProperties;
}

export const Highlighter = memo<HighlighterProps>(
Expand All @@ -60,6 +64,7 @@ export const Highlighter = memo<HighlighterProps>(
allowChangeLanguage,
fileName,
icon,
contentStyle,
...rest
}) => {
const { styles, cx } = useStyles(type);
Expand All @@ -70,6 +75,7 @@ export const Highlighter = memo<HighlighterProps>(
<FullFeaturedHighlighter
allowChangeLanguage={allowChangeLanguage}
className={className}
contentStyle={contentStyle}
fileName={fileName}
icon={icon}
language={language}
Expand All @@ -93,7 +99,9 @@ export const Highlighter = memo<HighlighterProps>(
)}
{showLanguage && language && <Tag className={styles.lang}>{language.toLowerCase()}</Tag>}
<div className={styles.scroller}>
<SyntaxHighlighter language={language?.toLowerCase()}>{children}</SyntaxHighlighter>
<SyntaxHighlighter language={language?.toLowerCase()} style={contentStyle}>
{children}
</SyntaxHighlighter>
</div>
</div>
);
Expand Down
8 changes: 4 additions & 4 deletions src/Highlighter/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const useStyles = createStyles(
css`
position: absolute;
z-index: 2;
inset-block-start: ${type === 'pure' ? 0 : '8px'};
inset-inline-end: ${type === 'pure' ? 0 : '8px'};
inset-block-start: ${type === 'pure' ? 0 : '12px'};
inset-inline-end: ${type === 'pure' ? 0 : '12px'};
opacity: 0;
`
Expand Down Expand Up @@ -79,8 +79,8 @@ export const useStyles = createStyles(
css`
position: absolute;
z-index: 2;
inset-block-end: 8px;
inset-inline-end: 0;
inset-block-end: 12px;
inset-inline-end: 4px;
font-family: ${token.fontFamilyCode};
color: ${token.colorTextSecondary};
Expand Down
1 change: 1 addition & 0 deletions src/MonacoEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import React, { useMemo, useState } from 'react';
import { useCdnFn } from '../ConfigProvider';
import { useStyles } from './style';

// @Todo: Controller SSR error: https://github.com/alibaba/lowcode-plugins/blob/main/packages/base-monaco-editor/src/controller.ts#L33
export {
type EditorMeta,
Controller as MonacoController,
Expand Down

0 comments on commit 5963ed6

Please sign in to comment.