Skip to content

Commit fcf1180

Browse files
committed
feat: add previewWidth props (#192).
1 parent c7afc07 commit fcf1180

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

src/commands/preview.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { IMarkdownEditor, ToolBarProps } from '../';
44

55
const Preview: React.FC<{ command: ICommand; editorProps: IMarkdownEditor & ToolBarProps }> = (props) => {
66
const { editorProps } = props;
7-
const { containerEditor, preview } = editorProps;
7+
const { containerEditor, preview, previewWidth = '50%' } = editorProps;
88
const [visible, setVisible] = useState(props.editorProps.visible);
99
useEffect(() => setVisible(props.editorProps.visible), [props.editorProps.visible]);
1010
useEffect(() => {
@@ -14,12 +14,14 @@ const Preview: React.FC<{ command: ICommand; editorProps: IMarkdownEditor & Tool
1414
$preview.style.borderBottomRightRadius = '3px';
1515
}
1616
if ($preview && visible) {
17-
$preview.style.width = '50%';
17+
$preview.style.width = previewWidth;
1818
$preview.style.overflow = 'auto';
19-
$preview.style.borderLeft = '1px solid var(--color-border-muted)';
19+
if (previewWidth !== '100%') {
20+
$preview.style.borderLeft = '1px solid var(--color-border-muted)';
21+
}
2022
$preview.style.padding = '20px';
2123
if (containerEditor.current) {
22-
containerEditor.current.style.width = '50%';
24+
containerEditor.current.style.width = `calc(100% - ${previewWidth})`;
2325
}
2426
} else if ($preview) {
2527
$preview.style.width = '0%';
@@ -31,7 +33,7 @@ const Preview: React.FC<{ command: ICommand; editorProps: IMarkdownEditor & Tool
3133
}
3234
}
3335
}
34-
}, [visible, containerEditor, preview]);
36+
}, [visible, containerEditor, preview, previewWidth]);
3537

3638
return (
3739
<button onClick={() => setVisible(!visible)} type="button" className={visible ? 'active' : ''}>

src/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export interface IMarkdownEditor extends ReactCodeMirrorProps {
3232
hideToolbar?: boolean;
3333
/** Override the default preview component */
3434
renderPreview?: (props: MarkdownPreviewProps, initVisible: boolean) => React.ReactNode;
35+
/** Preview expanded width @default `50%` */
36+
previewWidth?: string;
3537
/** Tool display settings. */
3638
toolbars?: IToolBarProps['toolbars'];
3739
/** Tool display settings. */
@@ -82,6 +84,7 @@ function MarkdownEditorInternal(
8284
hideToolbar = true,
8385
previewProps = {},
8486
extensions = [],
87+
previewWidth = '50%',
8588
reExtensions,
8689
...codemirrorProps
8790
} = props;
@@ -105,7 +108,7 @@ function MarkdownEditorInternal(
105108
editor: codeMirror,
106109
container: container,
107110
containerEditor: containerEditor,
108-
editorProps: props,
111+
editorProps: { ...props, previewWidth },
109112
};
110113
const height = typeof codemirrorProps.height === 'number' ? `${codemirrorProps.height}px` : codemirrorProps.height;
111114
const extensionsData: IMarkdownEditor['extensions'] = reExtensions

website/Example.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function Example() {
1212
const [hideToolbar, setHideToolbar] = useState(true);
1313
return (
1414
<div className={styles.editor}>
15-
<MarkdownEditor visible={visible} height="500px" value={mdstr} hideToolbar={hideToolbar} />
15+
<MarkdownEditor visible={visible} previewWidth="100%" height="500px" value={mdstr} hideToolbar={hideToolbar} />
1616
<div style={{ marginTop: 10, display: 'flex', gap: '10px' }}>
1717
<button
1818
onClick={() => {

0 commit comments

Comments
 (0)