Skip to content

Commit aa66e0b

Browse files
authored
Merge pull request #40 from kwinyyyc/fix/v5
fix: upgrade to be compatible with strapi v5 while the media lib and …
2 parents b9198cb + ea67c22 commit aa66e0b

File tree

17 files changed

+549
-776
lines changed

17 files changed

+549
-776
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Default it uses below commands in sequence:
3030
]`
3131

3232
You can customize the value in plugins/config.ts file.
33-
```javascript
33+
```js
3434
export default {
3535
"wysiwyg-react-md-editor": {
3636
enabled: true,

admin/src/components/MediaLib.tsx

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
1-
import {FC as FunctionComponent} from 'react';
2-
3-
import {useStrapiApp} from '@strapi/admin/strapi-admin';
4-
import type {Schema} from '@strapi/types';
1+
import { FC as FunctionComponent } from "react";
52

3+
import { useStrapiApp } from "@strapi/admin/strapi-admin";
4+
import type { Schema } from "@strapi/types";
65

76
const prefixFileUrlWithBackendUrl = (fileURL: string) => {
87
return !!fileURL &&
9-
fileURL.startsWith('/') &&
10-
'strapi' in window &&
8+
fileURL.startsWith("/") &&
9+
"strapi" in window &&
1110
window.strapi instanceof Object &&
12-
'backendURL' in window.strapi &&
13-
window.strapi.backendURL ?
14-
`${window.strapi.backendURL}${fileURL}` :
15-
fileURL;
11+
"backendURL" in window.strapi &&
12+
window.strapi.backendURL
13+
? `${window.strapi.backendURL}${fileURL}`
14+
: fileURL;
1615
};
1716

1817
interface MediaLibComponentProps {
19-
isOpen: boolean,
20-
onChange: (files: Schema.Attribute.MediaValue<true>) => void,
21-
onToggle: () => void,
18+
isOpen: boolean;
19+
onChange: (files: Schema.Attribute.MediaValue<true>) => void;
20+
onToggle: () => void;
2221
}
2322

2423
const MediaLib: FunctionComponent<MediaLibComponentProps> = ({
2524
isOpen,
2625
onChange,
2726
onToggle,
2827
}) => {
29-
const components = useStrapiApp('ImageDialog', (state) => state.components);
28+
const components = useStrapiApp("ImageDialog", (state) => state.components);
3029
if (!components || !isOpen) return null;
3130

32-
const MediaLibraryDialog = components['media-library'] as FunctionComponent<{
33-
onClose: () => void,
34-
onSelectAssets: (_images: Schema.Attribute.MediaValue<true>) => void,
31+
const MediaLibraryDialog = components["media-library"] as FunctionComponent<{
32+
onClose: () => void;
33+
onSelectAssets: (_images: Schema.Attribute.MediaValue<true>) => void;
3534
}>;
3635

3736
const handleSelectAssets = (files: Schema.Attribute.MediaValue<true>) => {
@@ -58,4 +57,4 @@ MediaLib.defaultProps = {
5857
onToggle: () => {},
5958
};
6059

61-
export {MediaLib};
60+
export { MediaLib };
Lines changed: 66 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import {FC as FunctionComponent, useState, useEffect, useMemo} from 'react';
1+
import { FC as FunctionComponent, useState, useEffect, useMemo } from "react";
22

3-
import {Box, Flex, Typography} from '@strapi/design-system';
4-
import type {Schema} from '@strapi/types';
5-
import MDEditor, {commands, ICommand} from '@uiw/react-md-editor';
6-
import {useIntl} from 'react-intl';
7-
import {styled} from 'styled-components';
3+
import { Box, Flex, Typography } from "@strapi/design-system";
4+
import type { Schema } from "@strapi/types";
5+
import MDEditor, { commands, ICommand } from "@uiw/react-md-editor";
6+
import { useIntl } from "react-intl";
7+
import { styled } from "styled-components";
88

9-
import '@uiw/react-markdown-preview/markdown.css';
10-
11-
import pluginId from '../pluginId';
12-
import {MediaLib} from './MediaLib';
9+
import "@uiw/react-markdown-preview/markdown.css";
1310

11+
import pluginId from "../pluginId";
12+
import { MediaLib } from "./MediaLib";
13+
import { useField } from "@strapi/strapi/admin";
1414

1515
const Wrapper = styled.div`
1616
> div:nth-child(2) {
@@ -58,80 +58,76 @@ const Wrapper = styled.div`
5858
`;
5959

6060
interface EditorProps {
61-
name: string,
62-
onChange: (e: {target: {name: string; value: string}}) => void,
63-
value: string,
61+
name: string;
62+
onChange: (e: { target: { name: string; value: string } }) => void;
63+
value: string;
6464
intlLabel: {
65-
id: string,
66-
defaultMessage: string,
67-
},
68-
disabled?: boolean,
69-
error?: string,
65+
id: string;
66+
defaultMessage: string;
67+
};
68+
disabled?: boolean;
69+
error?: string;
7070
description?: {
71-
id: string,
72-
defaultMessage: string,
73-
},
74-
required?: boolean,
71+
id: string;
72+
defaultMessage: string;
73+
};
74+
required?: boolean;
7575
}
7676

7777
const Editor: FunctionComponent<EditorProps> = ({
7878
name,
79-
onChange,
80-
value,
8179
intlLabel,
8280
disabled,
8381
error,
8482
description,
8583
required,
8684
}) => {
87-
const {formatMessage} = useIntl();
85+
// const { formatMessage } = useIntl();
86+
const { onChange, value }: any = useField(name);
87+
const formatMessage = (message: { id: string; defaultMessage: string }) =>
88+
message?.defaultMessage ?? "";
8889
const [mediaLibVisible, setMediaLibVisible] = useState(false);
8990
const [mediaLibSelection, setMediaLibSelection] = useState(-1);
9091

9192
const handleToggleMediaLib = () => setMediaLibVisible((prev) => !prev);
9293

9394
const handleChangeAssets = (assets: Schema.Attribute.MediaValue<true>) => {
94-
let newValue = value ? value : '';
95+
let newValue = value ? value : "";
9596
assets.map((asset) => {
96-
if (asset.mime.includes('image')) {
97+
if (asset.mime.includes("image")) {
9798
const imgTag = `![${asset.alt}](${asset.url})`;
9899
if (mediaLibSelection > -1) {
99-
const preValue = value?.substring(0, mediaLibSelection) ?? '';
100-
const postValue = value?.substring(mediaLibSelection) ?? '';
100+
const preValue = value?.substring(0, mediaLibSelection) ?? "";
101+
const postValue = value?.substring(mediaLibSelection) ?? "";
101102
newValue = `${
102-
preValue && !preValue.endsWith(' ') ? preValue + ' ' : preValue
103+
preValue && !preValue.endsWith(" ") ? preValue + " " : preValue
103104
}${imgTag}${
104-
postValue && !postValue.startsWith(' ') ?
105-
' ' + postValue :
106-
postValue
105+
postValue && !postValue.startsWith(" ")
106+
? " " + postValue
107+
: postValue
107108
}`;
108109
} else {
109110
newValue = `${newValue}${imgTag}`;
110111
}
111112
}
112113
// Handle videos and other type of files by adding some code
113114
});
114-
onChange({target: {name,
115-
value: newValue ?? ''}});
115+
onChange({ target: { name, value: newValue ?? "" } });
116116
handleToggleMediaLib();
117117
};
118118

119-
const [configs, setConfigs] = useState<{toolbarCommands?: string[]}>({});
119+
const [configs, setConfigs] = useState<{ toolbarCommands?: string[] }>({});
120120

121121
const toolbarCommands = useMemo(() => {
122122
const strapiMediaLibrary: ICommand = {
123-
name: 'image',
124-
keyCommand: 'image',
125-
buttonProps: {'aria-label': 'Insert title3'},
123+
name: "image",
124+
keyCommand: "image",
125+
buttonProps: { "aria-label": "Insert title3" },
126126
icon: (
127-
<svg
128-
width='12'
129-
height='12'
130-
viewBox='0 0 20 20'
131-
>
127+
<svg width="12" height="12" viewBox="0 0 20 20">
132128
<path
133-
fill='currentColor'
134-
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'
129+
fill="currentColor"
130+
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"
135131
></path>
136132
</svg>
137133
),
@@ -164,15 +160,19 @@ const Editor: FunctionComponent<EditorProps> = ({
164160
commands.checkedListCommand,
165161
] as ICommand[];
166162
}
167-
const customCommands = configs?.toolbarCommands?.map((config) => {
168-
if (config === 'strapiMediaLibrary') return strapiMediaLibrary;
169-
if (
170-
config in commands &&
171-
commands[config as unknown as keyof typeof commands]
172-
) {
173-
return commands[config as unknown as keyof typeof commands] as ICommand;
174-
};
175-
}).filter((command): command is ICommand => command !== undefined);
163+
const customCommands = configs?.toolbarCommands
164+
?.map((config) => {
165+
if (config === "strapiMediaLibrary") return strapiMediaLibrary;
166+
if (
167+
config in commands &&
168+
commands[config as unknown as keyof typeof commands]
169+
) {
170+
return commands[
171+
config as unknown as keyof typeof commands
172+
] as ICommand;
173+
}
174+
})
175+
.filter((command): command is ICommand => command !== undefined);
176176

177177
return customCommands;
178178
}, [JSON.stringify(configs)]);
@@ -188,18 +188,11 @@ const Editor: FunctionComponent<EditorProps> = ({
188188
return (
189189
<Flex gap={4}>
190190
<Box>
191-
<Typography
192-
variant='pi'
193-
fontWeight='bold'
194-
>
191+
<Typography variant="pi" fontWeight="bold">
195192
{formatMessage(intlLabel)}
196193
</Typography>
197194
{required && (
198-
<Typography
199-
variant='pi'
200-
fontWeight='bold'
201-
textColor='danger600'
202-
>
195+
<Typography variant="pi" fontWeight="bold" textColor="danger600">
203196
*
204197
</Typography>
205198
)}
@@ -208,33 +201,28 @@ const Editor: FunctionComponent<EditorProps> = ({
208201
<MDEditor
209202
hidden={disabled}
210203
commands={toolbarCommands}
211-
value={value || ''}
204+
value={value || ""}
212205
onChange={(newValue) => {
213-
onChange({target: {name,
214-
value: newValue || ''}});
206+
onChange({ target: { name, value: newValue || "" } });
215207
}}
216208
/>
217-
<div style={{padding: '50px 0 0 0'}}/>
218-
<MediaLib
209+
<div style={{ padding: "50px 0 0 0" }} />
210+
{/* <MediaLib
219211
isOpen={mediaLibVisible}
220212
onChange={handleChangeAssets}
221213
onToggle={handleToggleMediaLib}
222-
/>
214+
/> */}
223215
</Wrapper>
224216
{error && (
225-
<Typography
226-
variant='pi'
227-
textColor='danger600'
228-
>
229-
{formatMessage({id: error,
230-
defaultMessage: error})}
217+
<Typography variant="pi" textColor="danger600">
218+
{formatMessage({ id: error, defaultMessage: error })}
231219
</Typography>
232220
)}
233221
{description && (
234-
<Typography variant='pi'>{formatMessage(description)}</Typography>
222+
<Typography variant="pi">{formatMessage(description)}</Typography>
235223
)}
236224
</Flex>
237225
);
238226
};
239227

240-
export {Editor};
228+
export { Editor };

admin/src/index.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import pluginPkg from "../../package.json";
22
import pluginId from "./pluginId";
3-
import {Initializer} from "./components/Initializer";
4-
import {Editor as ReactMdEditor} from "./components/ReactMdEditor";
5-
import {getTranslation} from "./utils/getTranslation";
3+
import { Initializer } from "./components/Initializer";
4+
import { Editor as ReactMdEditor } from "./components/ReactMdEditor";
5+
import { getTranslation } from "./utils/getTranslation";
66

77
const name = pluginPkg.strapi.name;
88

99
export default {
1010
register(app: any) {
11-
app.addFields({ type: "wysiwyg", Component: ReactMdEditor });
11+
app.addFields({ type: "richtext", Component: ReactMdEditor });
1212
const plugin = {
1313
id: pluginId,
1414
initializer: Initializer,
@@ -18,18 +18,13 @@ export default {
1818

1919
app.registerPlugin(plugin);
2020
},
21-
22-
bootstrap(app: any) {},
23-
24-
async registerTrads(app: any) {
25-
const { locales } = app;
26-
27-
const importedTranslations = await Promise.all(
28-
(locales as string[]).map((locale) => {
21+
async registerTrads({ locales }: any) {
22+
const importedTrads = await Promise.all(
23+
locales.map((locale: any) => {
2924
return import(`./translations/${locale}.json`)
3025
.then(({ default: data }) => {
3126
return {
32-
data: getTranslation(data),
27+
data: data,
3328
locale,
3429
};
3530
})
@@ -42,6 +37,6 @@ export default {
4237
})
4338
);
4439

45-
return importedTranslations;
40+
return Promise.resolve(importedTrads);
4641
},
4742
};

admin/src/translations/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
{}
1+
{
2+
"test": ""
3+
}

admin/src/translations/fr.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
{}
1+
{
2+
"test": ""
3+
}

0 commit comments

Comments
 (0)