Skip to content

Commit 27929b7

Browse files
authored
Merge pull request #13 from Webbrother/develop
v1.0.4 develop => main
2 parents 54d0f06 + 16f3141 commit 27929b7

File tree

7 files changed

+1909
-267
lines changed

7 files changed

+1909
-267
lines changed

demo/index.tsx

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,60 @@
1-
import * as React from 'react';
1+
import React, { useState } from 'react';
22
import ReactDOM from 'react-dom';
33
import { Box, ChakraProvider, HStack, Textarea } from '@chakra-ui/react';
4-
import { faBold, faItalic, faCode, faHeading } from '@fortawesome/free-solid-svg-icons';
5-
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
4+
import { faBold, faItalic, faCode, faHeading, faFile } from '@fortawesome/free-solid-svg-icons';
65
import { attachment, bold, code, headingLevel1, italic, useTextAreaMarkdownEditor } from '../src';
76
import { ToolbarButton } from './toolbar-button';
7+
import ReactMarkdown from 'react-markdown';
88

99
const uploadMock = async () =>
1010
await new Promise<string>(resolve => {
1111
setTimeout(() => {
12-
resolve('https://google.com');
13-
}, 5000);
12+
resolve('https://example.com');
13+
}, 1000);
1414
});
1515

1616
export const Demo = () => {
1717
const { ref } = useTextAreaMarkdownEditor();
18+
const [isPreview, setIsPreview] = useState(false);
19+
const [value, setValue] = useState('');
1820

1921
return (
2022
<ChakraProvider>
2123
<Box p={3}>
2224
<HStack py={2}>
23-
<ToolbarButton onClick={bold}>
24-
<FontAwesomeIcon icon={faBold} />
25-
</ToolbarButton>
26-
<ToolbarButton onClick={italic}>
27-
<FontAwesomeIcon icon={faItalic} />
28-
</ToolbarButton>
29-
<ToolbarButton onClick={code}>
30-
<FontAwesomeIcon icon={faCode} />
31-
</ToolbarButton>
32-
<ToolbarButton onClick={headingLevel1}>
33-
<FontAwesomeIcon icon={faHeading} />
34-
</ToolbarButton>
25+
<ToolbarButton onClick={bold} icon={faBold} />
26+
<ToolbarButton onClick={italic} icon={faItalic} />
27+
<ToolbarButton onClick={code} icon={faCode} />
28+
<ToolbarButton onClick={headingLevel1} icon={faHeading} />
29+
<ToolbarButton
30+
onClick={() => {
31+
attachment({ fileName: 'file-name.txt', fileUrlPromise: uploadMock() });
32+
}}
33+
icon={faFile}
34+
/>
35+
3536
<ToolbarButton
3637
onClick={() => {
37-
attachment({ fileName: 'file-name', fileUrlPromise: uploadMock() });
38+
setIsPreview(val => !val);
3839
}}
3940
>
40-
+
41+
Preview
4142
</ToolbarButton>
4243
</HStack>
43-
<Textarea ref={ref} placeholder="I'm a markdown editor" fontFamily={'monospace'} />
44+
45+
{isPreview ? (
46+
<ReactMarkdown>{ref.current?.value ?? ''}</ReactMarkdown>
47+
) : (
48+
<Textarea
49+
ref={ref}
50+
value={value}
51+
onChange={e => {
52+
setValue(e.target.value);
53+
}}
54+
placeholder="I'm a markdown editor"
55+
fontFamily={'monospace'}
56+
/>
57+
)}
4458
</Box>
4559
</ChakraProvider>
4660
);

demo/toolbar-button.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
import * as React from "react";
2-
import { Button } from "@chakra-ui/react";
1+
import React, { type FC } from 'react';
2+
import { Button } from '@chakra-ui/react';
3+
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
4+
import { type IconDefinition } from '@fortawesome/fontawesome-common-types';
35

4-
export type ToolbarButtonProps = {
6+
export interface ToolbarButtonProps {
57
onClick: () => void;
6-
};
8+
icon?: IconDefinition;
9+
}
710

8-
export const ToolbarButton: React.FC<ToolbarButtonProps> = props => {
11+
export const ToolbarButton: FC<ToolbarButtonProps> = ({ onClick, icon, children }) => {
912
return (
10-
<Button
11-
variant={"outline"}
12-
size={"sm"}
13-
color={"gray.600"}
14-
onClick={props.onClick}
15-
>
16-
{props.children}
13+
<Button variant={'outline'} size={'sm'} color={'gray.600'} onClick={onClick}>
14+
{icon && <FontAwesomeIcon icon={icon} />}
15+
{children}
1716
</Button>
1817
);
1918
};

0 commit comments

Comments
 (0)