-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: highlight add pro wrapper mode
- Loading branch information
1 parent
6119c9d
commit f7b6ece
Showing
9 changed files
with
331 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import classNames from 'classnames'; | ||
import { createRef } from 'react'; | ||
import { getPrefixCls } from '../theme'; | ||
import CopyButton from './components/CopyButton'; | ||
import HighLighter from './components/HighLighter'; | ||
import { useKeyDownCopyEvent } from './hooks/useKeyDownCopyEvent'; | ||
import { useStyles } from './style'; | ||
import { THEME_LIGHT, ThemeType } from './theme'; | ||
|
||
export interface HighlightProps { | ||
/** | ||
* @description 样式 | ||
* @ignore | ||
*/ | ||
style?: React.CSSProperties; | ||
/** | ||
* @description className 类名 | ||
* @ignore | ||
*/ | ||
className?: string; | ||
/** | ||
* @description 类名前缀 | ||
* @ignore | ||
*/ | ||
prefixCls?: string; | ||
/** | ||
* @title 指定语言 | ||
* @description 指定语言 | ||
* @renderType select | ||
* @default "typescript" | ||
*/ | ||
language: string; | ||
/** | ||
* @title 主题 | ||
* @description 主题颜色, dark 黑色主题,light 白色主题 | ||
* @default "light" | ||
*/ | ||
theme?: ThemeType; | ||
/** | ||
* @title 高亮内容 | ||
* @description 高亮内容 | ||
*/ | ||
children?: any; | ||
/** | ||
* @title 是否使用要使用行号 | ||
* @description 是否需要展示代码块左侧的行号 | ||
* @default false | ||
*/ | ||
lineNumber?: boolean; | ||
/** | ||
* @title 是否展示复制按钮 | ||
* @description 是否需要展示复制按钮 | ||
* @default true | ||
*/ | ||
copyable?: boolean; | ||
/** | ||
* @title 复制按钮点击后回调 | ||
*/ | ||
onCopy?: (children: any) => void; | ||
/** | ||
* 高亮类型 | ||
*/ | ||
type?: 'pure' | 'block'; | ||
/** | ||
* 是否需要默认外层 wrapper | ||
*/ | ||
containerWrapper?: boolean; | ||
} | ||
|
||
const HighlightBase: React.FC<HighlightProps> = (props) => { | ||
const { | ||
children, | ||
style, | ||
className, | ||
lineNumber = false, | ||
copyable = true, | ||
theme = THEME_LIGHT, | ||
language, | ||
prefixCls: customPrefixCls, | ||
type = 'block', | ||
onCopy, | ||
} = props; | ||
const prefixCls = getPrefixCls('highlight', customPrefixCls); | ||
const { styles } = useStyles({ prefixCls, theme, type }); | ||
const codeRef = createRef<HTMLDivElement>(); | ||
useKeyDownCopyEvent(codeRef, onCopy); | ||
|
||
return ( | ||
<> | ||
<div | ||
ref={codeRef} | ||
tabIndex={-1} | ||
style={style} | ||
className={classNames(styles.container, className)} | ||
> | ||
{copyable && ( | ||
<CopyButton prefixCls={prefixCls} onCopy={onCopy} theme={theme} content={children} /> | ||
)} | ||
<HighLighter | ||
lineNumber={lineNumber} | ||
language={language} | ||
theme={theme} | ||
prefixCls={prefixCls} | ||
> | ||
{children} | ||
</HighLighter> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export { HighlightBase }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* title: wrapper used | ||
*/ | ||
|
||
import { Highlight } from '@ant-design/pro-editor'; | ||
import { Space } from 'antd'; | ||
|
||
export default () => ( | ||
<Space direction="vertical"> | ||
<Highlight | ||
language="java" | ||
theme="dark" | ||
containerWrapper | ||
onCopy={(children) => { | ||
console.log('复制代码', children); | ||
}} | ||
> | ||
{`public class HelloWorld { | ||
public static void main(String[] args) { | ||
System.out.println("Hello World!"); | ||
} | ||
}`} | ||
</Highlight> | ||
<Highlight | ||
language="java" | ||
theme="light" | ||
containerWrapper | ||
onCopy={(children) => { | ||
console.log('复制代码', children); | ||
}} | ||
> | ||
{`public class HelloWorld { | ||
public static void main(String[] args) { | ||
System.out.println("Hello World!"); | ||
} | ||
}`} | ||
</Highlight> | ||
</Space> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,15 @@ | ||
import classNames from 'classnames'; | ||
import { createRef } from 'react'; | ||
import { getPrefixCls } from '../theme'; | ||
import CopyButton from './components/CopyButton'; | ||
import HighLighter from './components/HighLighter'; | ||
import { useKeyDownCopyEvent } from './hooks/useKeyDownCopyEvent'; | ||
import { useStyles } from './style'; | ||
import { THEME_LIGHT, ThemeType } from './theme'; | ||
|
||
export interface HighlightProps { | ||
/** | ||
* @description 样式 | ||
* @ignore | ||
*/ | ||
style?: React.CSSProperties; | ||
/** | ||
* @description className 类名 | ||
* @ignore | ||
*/ | ||
className?: string; | ||
/** | ||
* @description 类名前缀 | ||
* @ignore | ||
*/ | ||
prefixCls?: string; | ||
/** | ||
* @title 指定语言 | ||
* @description 指定语言 | ||
* @renderType select | ||
* @default "typescript" | ||
*/ | ||
language: string; | ||
/** | ||
* @title 主题 | ||
* @description 主题颜色, dark 黑色主题,light 白色主题 | ||
* @default "light" | ||
*/ | ||
theme?: ThemeType; | ||
/** | ||
* @title 高亮内容 | ||
* @description 高亮内容 | ||
*/ | ||
children?: any; | ||
/** | ||
* @title 是否使用要使用行号 | ||
* @description 是否需要展示代码块左侧的行号 | ||
* @default false | ||
*/ | ||
lineNumber?: boolean; | ||
/** | ||
* @title 是否展示复制按钮 | ||
* @description 是否需要展示复制按钮 | ||
* @default true | ||
*/ | ||
copyable?: boolean; | ||
/** | ||
* @title 复制按钮点击后回调 | ||
*/ | ||
onCopy?: (children: any) => void; | ||
/** | ||
* 高亮类型 | ||
*/ | ||
type?: 'pure' | 'block'; | ||
} | ||
|
||
const Highlight: React.FC<HighlightProps> = (props) => { | ||
const { | ||
children, | ||
style, | ||
className, | ||
lineNumber = false, | ||
copyable = true, | ||
theme = THEME_LIGHT, | ||
language, | ||
prefixCls: customPrefixCls, | ||
type = 'block', | ||
onCopy, | ||
} = props; | ||
|
||
const prefixCls = getPrefixCls('highlight', customPrefixCls); | ||
const { styles } = useStyles({ prefixCls, theme, type }); | ||
const codeRef = createRef<HTMLDivElement>(); | ||
useKeyDownCopyEvent(codeRef, onCopy); | ||
|
||
return ( | ||
<> | ||
<div | ||
ref={codeRef} | ||
tabIndex={-1} | ||
style={style} | ||
className={classNames(styles.container, className)} | ||
> | ||
{copyable && ( | ||
<CopyButton prefixCls={prefixCls} onCopy={onCopy} theme={theme} content={children} /> | ||
)} | ||
<HighLighter | ||
lineNumber={lineNumber} | ||
language={language} | ||
theme={theme} | ||
prefixCls={prefixCls} | ||
> | ||
{children} | ||
</HighLighter> | ||
</div> | ||
</> | ||
); | ||
import { HighlightBase, HighlightProps } from './defalut'; | ||
import FullFeatureWrapper from './wrapper'; | ||
|
||
const Highlight = ( | ||
props: HighlightProps & { | ||
containerWrapper: boolean; | ||
}, | ||
) => { | ||
if (props?.containerWrapper) { | ||
return <FullFeatureWrapper {...props} />; | ||
} | ||
return <HighlightBase {...props} />; | ||
}; | ||
|
||
export { Highlight }; |
Oops, something went wrong.