Skip to content

Commit 5964441

Browse files
pluginId --> PLUGIN_ID
1 parent fbd5e8d commit 5964441

File tree

7 files changed

+34
-51
lines changed

7 files changed

+34
-51
lines changed

admin/src/components/Initializer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import { useEffect, useRef } from 'react';
8-
import pluginId from '../pluginId';
8+
import {PLUGIN_ID} from '../utils/pluginId';
99

1010
type InitializerProps = {
1111
setPlugin: (id: string) => void;
@@ -15,7 +15,7 @@ const Initializer = ({ setPlugin }: InitializerProps) => {
1515
const ref = useRef(setPlugin);
1616

1717
useEffect(() => {
18-
ref.current(pluginId);
18+
ref.current(PLUGIN_ID);
1919
}, []);
2020

2121
return null;

admin/src/components/MediaLib.tsx

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
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 React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { useStrapiApp } from '@strapi/strapi/admin';
54

65
const prefixFileUrlWithBackendUrl = (fileURL: string) => {
76
return !!fileURL &&
@@ -14,47 +13,33 @@ const prefixFileUrlWithBackendUrl = (fileURL: string) => {
1413
: fileURL;
1514
};
1615

17-
interface MediaLibComponentProps {
18-
isOpen: boolean;
19-
onChange: (files: Schema.Attribute.MediaValue<true>) => void;
20-
onToggle: () => void;
21-
}
22-
23-
const MediaLib: FunctionComponent<MediaLibComponentProps> = ({
24-
isOpen,
25-
onChange,
26-
onToggle,
27-
}) => {
28-
const components = useStrapiApp("ImageDialog", (state) => state.components);
29-
if (!components || !isOpen) return null;
30-
31-
const MediaLibraryDialog = components["media-library"] as FunctionComponent<{
32-
onClose: () => void;
33-
onSelectAssets: (_images: Schema.Attribute.MediaValue<true>) => void;
34-
}>;
35-
36-
const handleSelectAssets = (files: Schema.Attribute.MediaValue<true>) => {
37-
const formattedFiles = files.map((f) => ({
16+
const MediaLib = ( { isOpen = false, onChange = () => {}, onToggle = () => {} } ) => {
17+
const { components } = useStrapiApp( 'library', app => app );
18+
const MediaLibraryDialog = components[ 'media-library' ];
19+
20+
const handleSelectAssets = files => {
21+
const formattedFiles = files.map(f => ( {
3822
alt: f.alternativeText || f.name,
39-
url: prefixFileUrlWithBackendUrl(f.url),
23+
url: prefixFileUrlWithBackendUrl( f.url ),
4024
mime: f.mime,
41-
}));
25+
} ) );
26+
27+
onChange( formattedFiles );
28+
};
4229

43-
onChange(formattedFiles);
30+
if ( !isOpen ) {
31+
return null
4432
};
4533

46-
return (
47-
<MediaLibraryDialog
48-
onClose={onToggle}
49-
onSelectAssets={handleSelectAssets}
50-
/>
34+
return(
35+
<MediaLibraryDialog onClose={ onToggle } onSelectAssets={ handleSelectAssets } />
5136
);
5237
};
5338

54-
MediaLib.defaultProps = {
55-
isOpen: false,
56-
onChange: (_files: Schema.Attribute.MediaValue<true>) => {},
57-
onToggle: () => {},
39+
MediaLib.propTypes = {
40+
isOpen: PropTypes.bool,
41+
onChange: PropTypes.func,
42+
onToggle: PropTypes.func,
5843
};
5944

60-
export { MediaLib };
45+
export default MediaLib;

admin/src/components/ReactMdEditor.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { styled } from "styled-components";
88

99
import "@uiw/react-markdown-preview/markdown.css";
1010

11-
import pluginId from "../pluginId";
11+
import {PLUGIN_ID} from '../utils/pluginId';
1212
import { MediaLib } from "./MediaLib";
1313
import { useField } from "@strapi/strapi/admin";
1414

@@ -179,7 +179,7 @@ const Editor: FunctionComponent<EditorProps> = ({
179179
}, [JSON.stringify(configs)]);
180180

181181
useEffect(() => {
182-
fetch(`/${pluginId}`)
182+
fetch(`/${PLUGIN_ID}`)
183183
.then((response) => response.json())
184184
.then((data) => {
185185
setConfigs(data);

admin/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pluginPkg from "../../package.json";
2-
import pluginId from "./pluginId";
2+
import {PLUGIN_ID} from './utils/pluginId';
33
import { Initializer } from "./components/Initializer";
44
import { Editor as ReactMdEditor } from "./components/ReactMdEditor";
55
import { getTranslation } from "./utils/getTranslation";
@@ -10,7 +10,7 @@ export default {
1010
register(app: any) {
1111
app.addFields({ type: "richtext", Component: ReactMdEditor });
1212
const plugin = {
13-
id: pluginId,
13+
id: PLUGIN_ID,
1414
initializer: Initializer,
1515
isReady: false,
1616
name,

admin/src/pluginId.ts

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

admin/src/utils/getTranslation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import pluginId from '../pluginId';
1+
import {PLUGIN_ID} from '../utils/pluginId';
22

3-
const getTranslation = (id: string) => `${pluginId}.${id}`;
3+
const getTranslation = (id: string) => `${PLUGIN_ID}.${id}`;
44

55
export { getTranslation };

admin/src/utils/pluginId.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import pluginPkg from '../../../package.json';
2+
3+
export const PLUGIN_ID = pluginPkg.strapi.name || pluginPkg.name.replace(/^(@[^-,.][\w,-]+\/|strapi-)plugin-/i, '');

0 commit comments

Comments
 (0)