Skip to content

Commit 5d0e9f4

Browse files
authored
Merge pull request #15 from Webbrother/develop
2.0.0 => main
2 parents 27929b7 + d295cf4 commit 5d0e9f4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+638
-670
lines changed

README.md

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,26 @@ React-mde-headless has no 3rd party dependencies.
1212
## Using
1313

1414
```jsx
15-
import { bold, italic, link, useTextAreaMarkdownEditor } from 'react-mde-headless';
15+
import { boldCommand, italicCommand, linkCommand, useTextAreaMarkdownEditor } from 'react-mde-headless';
1616

1717
export const MarkdownEditor = () => {
18-
const { ref } = useTextAreaMarkdownEditor();
18+
const { ref, commandController } = useTextAreaMarkdownEditor({
19+
commandMap: {
20+
bold: boldCommand,
21+
italic: italicCommand,
22+
link: linkCommand,
23+
},
24+
});
1925

2026
return (
2127
<div>
22-
<button onClick={bold}>B</button>
28+
<button
29+
onClick={() => {
30+
commandController.executeCommand('bold');
31+
}}
32+
>
33+
B
34+
</button>
2335

2436
<textarea ref={ref} />
2537
</div>
@@ -29,31 +41,40 @@ export const MarkdownEditor = () => {
2941

3042
## Supported commands
3143

32-
- bold
33-
- italic
34-
- strikethrough
3544
- headingLevel1
3645
- headingLevel2
3746
- headingLevel3
3847
- headingLevel4
3948
- headingLevel5
4049
- headingLevel6
41-
- link
4250
- quote
43-
- code
44-
- codeBlock
4551
- checkedList
4652
- orderedList
4753
- unorderedList
54+
- bold
55+
- code
56+
- codeBlock
57+
- italic
58+
- strikethrough
4859
- image
49-
- attachment
60+
- link
5061

5162
## Todo
5263

5364
- heading undo
5465
- undo/redo
5566
- Check execution on SSR (For example, Next.js) and, if necessary, regenerate eslint config, taking into account execution on node.js
5667
- peerDependencies React?
68+
- undo for
69+
- 1st priority
70+
- all headings
71+
- quote
72+
- strikethrough
73+
- 2nd priority
74+
- orderedList
75+
- unorderedList
76+
- checkedList
77+
- codeBlock
5778

5879
PR's are welcome!
5980

@@ -69,7 +90,7 @@ this has been taken from [their documentation](<https://github.com/showdownjs/sh
6990
> Cross-side scripting is a well known technique to gain access to private information of the users
7091
> of a website. The attacker injects spurious HTML content (a script) on the web page which will read
7192
> the user’s cookies and do something bad with it (like steal credentials). As a countermeasure,
72-
> you should filter any suspicious content coming from user input. Showdown doesn’t include an
93+
> you should filter any suspicious content coming from user input. Showdown does not include an
7394
> XSS filter, so you must provide your own. But be careful in how you do it…
7495
7596
You might want to take a look at [showdown-xss-filter](https://github.com/VisionistInc/showdown-xss-filter).

demo/index.tsx

Lines changed: 111 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,132 @@
11
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, faFile } from '@fortawesome/free-solid-svg-icons';
5-
import { attachment, bold, code, headingLevel1, italic, useTextAreaMarkdownEditor } from '../src';
4+
import { faBold, faItalic, faCode, faHeading, faImage, faLink } from '@fortawesome/free-solid-svg-icons';
5+
import {
6+
boldCommand,
7+
codeCommand,
8+
codeBlockCommand,
9+
headingLevel1Command,
10+
italicCommand,
11+
strikethroughCommand,
12+
useTextAreaMarkdownEditor,
13+
quoteCommand,
14+
checkedListCommand,
15+
orderedListCommand,
16+
unorderedListCommand,
17+
imageCommand,
18+
linkCommand,
19+
} from '../src';
620
import { ToolbarButton } from './toolbar-button';
721
import ReactMarkdown from 'react-markdown';
822

9-
const uploadMock = async () =>
10-
await new Promise<string>(resolve => {
11-
setTimeout(() => {
12-
resolve('https://example.com');
13-
}, 1000);
14-
});
15-
1623
export const Demo = () => {
17-
const { ref } = useTextAreaMarkdownEditor();
24+
const { ref, commandController } = useTextAreaMarkdownEditor({
25+
commandMap: {
26+
// word logic
27+
bold: boldCommand,
28+
code: codeCommand,
29+
codeBlock: codeBlockCommand,
30+
italic: italicCommand,
31+
strikethrough: strikethroughCommand,
32+
33+
// word complex
34+
image: imageCommand,
35+
link: linkCommand,
36+
37+
// line logic
38+
headingLevel1: headingLevel1Command,
39+
quote: quoteCommand,
40+
41+
// list
42+
orderedList: orderedListCommand,
43+
unorderedList: unorderedListCommand,
44+
checkedList: checkedListCommand,
45+
},
46+
});
1847
const [isPreview, setIsPreview] = useState(false);
1948
const [value, setValue] = useState('');
2049

2150
return (
2251
<ChakraProvider>
2352
<Box p={3}>
2453
<HStack py={2}>
25-
<ToolbarButton onClick={bold} icon={faBold} />
26-
<ToolbarButton onClick={italic} icon={faItalic} />
27-
<ToolbarButton onClick={code} icon={faCode} />
28-
<ToolbarButton onClick={headingLevel1} icon={faHeading} />
2954
<ToolbarButton
3055
onClick={() => {
31-
attachment({ fileName: 'file-name.txt', fileUrlPromise: uploadMock() });
56+
commandController.executeCommand('bold');
57+
}}
58+
icon={faBold}
59+
/>
60+
<ToolbarButton
61+
onClick={() => {
62+
commandController.executeCommand('italic');
63+
}}
64+
icon={faItalic}
65+
/>
66+
<ToolbarButton
67+
onClick={() => {
68+
commandController.executeCommand('code');
69+
}}
70+
icon={faCode}
71+
/>
72+
73+
<ToolbarButton
74+
onClick={() => {
75+
commandController.executeCommand('image');
76+
}}
77+
icon={faImage}
78+
/>
79+
<ToolbarButton
80+
onClick={() => {
81+
commandController.executeCommand('link');
82+
}}
83+
icon={faLink}
84+
/>
85+
86+
<ToolbarButton
87+
onClick={() => {
88+
commandController.executeCommand('headingLevel1');
3289
}}
33-
icon={faFile}
90+
icon={faHeading}
3491
/>
92+
<ToolbarButton
93+
onClick={() => {
94+
commandController.executeCommand('quote');
95+
}}
96+
>
97+
{'>'}
98+
</ToolbarButton>
99+
100+
<ToolbarButton
101+
onClick={() => {
102+
commandController.executeCommand('codeBlock');
103+
}}
104+
icon={faCode}
105+
/>
106+
107+
<ToolbarButton
108+
onClick={() => {
109+
commandController.executeCommand('orderedList');
110+
}}
111+
>
112+
ol
113+
</ToolbarButton>
114+
115+
<ToolbarButton
116+
onClick={() => {
117+
commandController.executeCommand('unorderedList');
118+
}}
119+
>
120+
ul
121+
</ToolbarButton>
122+
123+
<ToolbarButton
124+
onClick={() => {
125+
commandController.executeCommand('checkedList');
126+
}}
127+
>
128+
xi
129+
</ToolbarButton>
35130

36131
<ToolbarButton
37132
onClick={() => {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-headless-mde",
3-
"version": "1.0.4",
3+
"version": "2.0.0",
44
"description": "React Headless Markdown Editor",
55
"main": "dist/cjs/index.js",
66
"module": "dist/esm/index.js",

src/commands/command-controller.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/commands/command.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/commands/commands-service.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/commands/line/heading-level1.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { type ICommand } from '../../types/command';
2+
import { setHeader } from '../../utils/header';
3+
4+
export const headingLevel1Command: ICommand = {
5+
do: textCtrl => {
6+
const initialState = textCtrl.getState();
7+
8+
setHeader(initialState, textCtrl, '# ');
9+
},
10+
};

src/commands/line/heading-level2.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { type ICommand } from '../../types/command';
2+
import { setHeader } from '../../utils/header';
3+
4+
export const headingLevel2Command: ICommand = {
5+
do: textCtrl => {
6+
const initialState = textCtrl.getState();
7+
8+
setHeader(initialState, textCtrl, '## ');
9+
},
10+
};

src/commands/line/heading-level3.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { type ICommand } from '../../types/command';
2+
import { setHeader } from '../../utils/header';
3+
4+
export const headingLevel3Command: ICommand = {
5+
do: textCtrl => {
6+
const initialState = textCtrl.getState();
7+
8+
setHeader(initialState, textCtrl, '### ');
9+
},
10+
};

src/commands/line/heading-level4.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { type ICommand } from '../../types/command';
2+
import { setHeader } from '../../utils/header';
3+
4+
export const headingLevel4Command: ICommand = {
5+
do: textCtrl => {
6+
const initialState = textCtrl.getState();
7+
8+
setHeader(initialState, textCtrl, '#### ');
9+
},
10+
};

src/commands/line/heading-level5.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { type ICommand } from '../../types/command';
2+
import { setHeader } from '../../utils/header';
3+
4+
export const headingLevel5Command: ICommand = {
5+
do: textCtrl => {
6+
const initialState = textCtrl.getState();
7+
8+
setHeader(initialState, textCtrl, '##### ');
9+
},
10+
};

src/commands/line/heading-level6.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { type ICommand } from '../../types/command';
2+
import { setHeader } from '../../utils/header';
3+
4+
export const headingLevel6Command: ICommand = {
5+
do: textCtrl => {
6+
const initialState = textCtrl.getState();
7+
8+
setHeader(initialState, textCtrl, '###### ');
9+
},
10+
};

0 commit comments

Comments
 (0)