Skip to content

Commit bb69c08

Browse files
authored
Merge pull request #26 from kwinyyyc/feat/custom-config
feat: allow customizing the toolbar commands
2 parents d0ff5e3 + a6b639d commit bb69c08

File tree

33 files changed

+3533
-861
lines changed

33 files changed

+3533
-861
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ package-lock.json
88
.DS_Store
99
npm-debug.log
1010
.idea
11+
dist/

.npmignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Don't check auto-generated stuff into git
2+
coverage
3+
node_modules
4+
stats.json
5+
package-lock.json
6+
7+
# Cruft
8+
.DS_Store
9+
npm-debug.log
10+
.idea

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,43 @@ This is a [strapi](https://github.com/strapi/strapi) rich text editor plugin bas
44

55
![](screenshot.png)
66

7+
## Customizing the toolbar commands
8+
Default it uses below commands in sequence:
9+
`[
10+
"title2",
11+
"title3",
12+
"title4",
13+
"title5",
14+
"title6",
15+
"divider",
16+
"bold",
17+
"codeBlock",
18+
"italic",
19+
"strikethrough",
20+
"hr",
21+
"group",
22+
"divider",
23+
"link",
24+
"quote",
25+
"code",
26+
"unorderedListCommand",
27+
"orderedListCommand",
28+
"checkedListCommand",
29+
"strapiMediaLibrary"
30+
]`
31+
32+
You can customize the value in plugins/config.ts file.
33+
```json
34+
export default {
35+
"wysiwyg-react-md-editor": {
36+
enabled: true,
37+
config: {
38+
toolbarCommands: ["title1", "strapiMediaLibrary"],
39+
},
40+
}
41+
}
42+
```
43+
744
## Get started
845
With yarn:
946

@@ -19,3 +56,4 @@ With yarn:
1956
$ yarn build
2057
$ yarn run develop
2158
```
59+

admin/src/components/Initializer/index.js

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
*
3+
* Initializer
4+
*
5+
*/
6+
7+
import { useEffect, useRef } from 'react';
8+
import pluginId from '../../pluginId';
9+
10+
type InitializerProps = {
11+
setPlugin: (id: string) => void;
12+
};
13+
14+
const Initializer = ({ setPlugin }: InitializerProps) => {
15+
const ref = useRef(setPlugin);
16+
17+
useEffect(() => {
18+
ref.current(pluginId);
19+
}, []);
20+
21+
return null;
22+
};
23+
24+
export default Initializer;
File renamed without changes.

admin/src/components/ReactMdEditor/index.js renamed to admin/src/components/ReactMdEditor/index.tsx

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from "react";
1+
import React, { useState, useEffect, useMemo } from "react";
22
import PropTypes from "prop-types";
33
import MDEditor, { commands } from "@uiw/react-md-editor";
44
import MediaLib from "../MediaLib";
@@ -8,6 +8,8 @@ import { Stack } from "@strapi/design-system/Stack";
88
import { Box } from "@strapi/design-system/Box";
99
import { Typography } from "@strapi/design-system/Typography";
1010
import { useIntl } from "react-intl";
11+
import pluginId from "../../pluginId";
12+
import { ICommand } from "@uiw/react-md-editor";
1113

1214
const Wrapper = styled.div`
1315
> div:nth-child(2) {
@@ -95,6 +97,65 @@ const Editor = ({
9597
handleToggleMediaLib();
9698
};
9799

100+
const [configs, setConfigs] = useState<{ toolbarCommands?: string[] }>({});
101+
102+
const toolbarCommands: ICommand[] = useMemo(() => {
103+
const strapiMediaLibrary = {
104+
name: "image",
105+
keyCommand: "image",
106+
buttonProps: { "aria-label": "Insert title3" },
107+
icon: (
108+
<svg width="12" height="12" viewBox="0 0 20 20">
109+
<path
110+
fill="currentColor"
111+
d="M15 9c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm4-7H1c-.55 0-1 .45-1 1v14c0 .55.45 1 1 1h18c.55 0 1-.45 1-1V3c0-.55-.45-1-1-1zm-1 13l-6-5-2 2-4-5-4 8V4h16v11z"
112+
></path>
113+
</svg>
114+
),
115+
execute: (state, api) => {
116+
setMediaLibSelection(state.selection.end);
117+
handleToggleMediaLib();
118+
},
119+
};
120+
if (!configs?.toolbarCommands) {
121+
return [
122+
commands.title2,
123+
commands.title3,
124+
commands.title4,
125+
commands.title5,
126+
commands.title6,
127+
commands.divider,
128+
commands.bold,
129+
commands.codeBlock,
130+
commands.italic,
131+
commands.strikethrough,
132+
commands.hr,
133+
commands.group,
134+
commands.divider,
135+
commands.link,
136+
commands.quote,
137+
commands.code,
138+
strapiMediaLibrary,
139+
commands.unorderedListCommand,
140+
commands.orderedListCommand,
141+
commands.checkedListCommand,
142+
];
143+
}
144+
const customCommands = configs?.toolbarCommands?.map((config) => {
145+
if (config === "strapiMediaLibrary") return strapiMediaLibrary;
146+
if (commands[config]) return commands[config];
147+
});
148+
return customCommands;
149+
}, [JSON.stringify(configs)]);
150+
151+
useEffect(() => {
152+
fetch(`/${pluginId}`)
153+
.then((response) => response.json())
154+
.then((data) => {
155+
setConfigs(data);
156+
});
157+
}, []);
158+
98159
return (
99160
<Stack size={1}>
100161
<Box>
@@ -110,44 +171,7 @@ const Editor = ({
110171
<Wrapper>
111172
<MDEditor
112173
disabled={disabled}
113-
commands={[
114-
commands.title2,
115-
commands.title3,
116-
commands.title4,
117-
commands.title5,
118-
commands.title6,
119-
commands.divider,
120-
commands.bold,
121-
commands.codeBlock,
122-
commands.italic,
123-
commands.strikethrough,
124-
commands.hr,
125-
commands.group,
126-
commands.divider,
127-
commands.link,
128-
commands.quote,
129-
commands.code,
130-
{
131-
name: "image",
132-
keyCommand: "image",
133-
buttonProps: { "aria-label": "Insert title3" },
134-
icon: (
135-
<svg width="12" height="12" viewBox="0 0 20 20">
136-
<path
137-
fill="currentColor"
138-
d="M15 9c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm4-7H1c-.55 0-1 .45-1 1v14c0 .55.45 1 1 1h18c.55 0 1-.45 1-1V3c0-.55-.45-1-1-1zm-1 13l-6-5-2 2-4-5-4 8V4h16v11z"
139-
></path>
140-
</svg>
141-
),
142-
execute: (state, api) => {
143-
setMediaLibSelection(state.selection.end);
144-
handleToggleMediaLib();
145-
},
146-
},
147-
commands.unorderedListCommand,
148-
commands.orderedListCommand,
149-
commands.checkedListCommand,
150-
]}
174+
commands={toolbarCommands}
151175
value={value || ""}
152176
onChange={(newValue) => {
153177
onChange({ target: { name, value: newValue || "" } });

admin/src/index.js

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

admin/src/index.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { prefixPluginTranslations } from "@strapi/helper-plugin";
2+
3+
import pluginPkg from "../../package.json";
4+
import pluginId from "./pluginId";
5+
import Initializer from "./components/Initializer";
6+
import PluginIcon from "./components/PluginIcon";
7+
import ReactMdEditor from "./components/ReactMdEditor";
8+
9+
const name = pluginPkg.strapi.name;
10+
11+
export default {
12+
register(app: any) {
13+
app.addFields({ type: "wysiwyg", Component: ReactMdEditor });
14+
const plugin = {
15+
id: pluginId,
16+
initializer: Initializer,
17+
isReady: false,
18+
name,
19+
};
20+
21+
app.registerPlugin(plugin);
22+
},
23+
24+
bootstrap(app: any) {},
25+
26+
async registerTrads(app: any) {
27+
const { locales } = app;
28+
29+
const importedTrads = await Promise.all(
30+
(locales as any[]).map((locale) => {
31+
return import(`./translations/${locale}.json`)
32+
.then(({ default: data }) => {
33+
return {
34+
data: prefixPluginTranslations(data, pluginId),
35+
locale,
36+
};
37+
})
38+
.catch(() => {
39+
return {
40+
data: {},
41+
locale,
42+
};
43+
});
44+
})
45+
);
46+
47+
return Promise.resolve(importedTrads);
48+
},
49+
};

admin/src/pluginId.js

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

0 commit comments

Comments
 (0)