2024-01-09 22:51:55 +08:00
|
|
|
import type { ComponentProps, FC } from 'react';
|
2024-07-11 10:02:04 +08:00
|
|
|
import React from 'react';
|
2024-01-09 22:51:55 +08:00
|
|
|
import { createStyles } from 'antd-style';
|
|
|
|
import SourceCodeEditor from 'dumi/theme-default/slots/SourceCodeEditor';
|
|
|
|
|
|
|
|
import LiveError from '../slots/LiveError';
|
2023-10-23 22:49:49 +08:00
|
|
|
|
|
|
|
const useStyle = createStyles(({ token, css }) => {
|
2024-07-11 10:02:04 +08:00
|
|
|
const { colorBgContainer } = token;
|
2023-10-23 22:49:49 +08:00
|
|
|
return {
|
|
|
|
editor: css`
|
2024-01-09 22:51:55 +08:00
|
|
|
// override dumi editor styles
|
|
|
|
.dumi-default-source-code-editor {
|
2024-01-24 14:43:03 +08:00
|
|
|
.dumi-default-source-code {
|
|
|
|
background: ${colorBgContainer};
|
|
|
|
}
|
2024-01-09 22:51:55 +08:00
|
|
|
.dumi-default-source-code > pre,
|
2024-01-24 14:43:03 +08:00
|
|
|
.dumi-default-source-code-scroll-content > pre,
|
2024-01-09 22:51:55 +08:00
|
|
|
.dumi-default-source-code-editor-textarea {
|
2024-04-06 16:11:17 +08:00
|
|
|
padding: ${token.paddingSM}px ${token.padding}px;
|
2024-01-09 22:51:55 +08:00
|
|
|
}
|
|
|
|
|
2024-01-24 14:43:03 +08:00
|
|
|
.dumi-default-source-code > pre,
|
|
|
|
.dumi-default-source-code-scroll-content > pre {
|
2024-04-15 10:27:51 +08:00
|
|
|
font-size: ${token.fontSize}px;
|
2024-01-09 22:51:55 +08:00
|
|
|
line-height: 2;
|
|
|
|
font-family: 'Lucida Console', Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
|
|
|
|
}
|
|
|
|
|
|
|
|
// disable dumi default copy button
|
|
|
|
.dumi-default-source-code-copy {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
|
2024-01-24 14:43:03 +08:00
|
|
|
&::after {
|
|
|
|
border-radius: 0 !important;
|
2024-01-09 22:51:55 +08:00
|
|
|
}
|
|
|
|
|
2024-01-24 14:43:03 +08:00
|
|
|
&:hover:not(:focus-within) {
|
|
|
|
&::after {
|
|
|
|
box-shadow: 0 0 0 1px ${token.colorPrimaryBorderHover} inset;
|
|
|
|
}
|
2024-01-09 22:51:55 +08:00
|
|
|
}
|
|
|
|
}
|
2023-10-23 22:49:49 +08:00
|
|
|
`,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2024-01-23 13:30:15 +08:00
|
|
|
const LiveCode: FC<
|
|
|
|
{
|
|
|
|
error: Error | null;
|
|
|
|
} & Pick<ComponentProps<typeof SourceCodeEditor>, 'lang' | 'initialValue' | 'onChange'>
|
|
|
|
> = (props) => {
|
2023-10-23 22:49:49 +08:00
|
|
|
const { styles } = useStyle();
|
|
|
|
return (
|
2024-07-11 10:02:04 +08:00
|
|
|
<div className={styles.editor}>
|
|
|
|
<SourceCodeEditor
|
|
|
|
lang={props.lang}
|
|
|
|
initialValue={props.initialValue}
|
|
|
|
onChange={props.onChange}
|
|
|
|
/>
|
|
|
|
<LiveError error={props.error} />
|
|
|
|
</div>
|
2023-10-23 22:49:49 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default LiveCode;
|