Skip to content

Commit b3e29ad

Browse files
fixed media library popup
1 parent 0a5478e commit b3e29ad

File tree

2 files changed

+82
-67
lines changed

2 files changed

+82
-67
lines changed

admin/src/components/MediaLib.tsx

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,49 @@
11
import React from 'react';
2-
import PropTypes from 'prop-types';
3-
import { useStrapiApp } from '@strapi/strapi/admin';
42
import prefixFileUrlWithBackendUrl from '../utils/prefixFileUrlWithBackendUrl';
3+
import { useStrapiApp } from '@strapi/strapi/admin';
4+
import type { Schema } from '@strapi/types';
5+
6+
const MediaLibComponent: React.FC<any> = ({
7+
isOpen = false,
8+
onChange,
9+
onToggle,
10+
allowedTypes
11+
}) => {
12+
13+
const components = useStrapiApp('ImageDialog', (state) => state.components);
14+
if (!components || !isOpen) return null;
515

16+
const MediaLibraryDialog = components['media-library'] as React.ComponentType<{
17+
allowedTypes?: Schema.Attribute.MediaKind[]; // 'images' | 'videos' | 'files' | 'audios'
18+
onClose: () => void;
19+
onSelectAssets: (_images: Schema.Attribute.MediaValue<true>) => void;
20+
}>;
621

7-
const MediaLib = ( { isOpen = false, onChange = () => {}, onToggle = () => {} } ) => {
8-
const { components } = useStrapiApp( 'library', app => app );
9-
const MediaLibraryDialog = components[ 'media-library' ];
1022

11-
const handleSelectAssets = files => {
12-
const formattedFiles = files.map(f => ( {
23+
const handleSelectAssets = (assets: Schema.Attribute.MediaValue<true>) => {
24+
const formattedFiles = assets.map((f) => ({
1325
alt: f.alternativeText || f.name,
14-
url: prefixFileUrlWithBackendUrl( f.url ),
26+
url: prefixFileUrlWithBackendUrl(f.url),
1527
mime: f.mime,
16-
} ) );
17-
18-
onChange( formattedFiles );
28+
//width: f.width,
29+
//height: f.height,
30+
//size: f.size,
31+
//formats:f.formats,
32+
}));
33+
onChange(formattedFiles);
1934
};
2035

21-
if ( !isOpen ) {
22-
return null
23-
};
36+
if (!isOpen) {
37+
return null;
38+
}
2439

25-
return(
26-
<MediaLibraryDialog onClose={ onToggle } onSelectAssets={ handleSelectAssets } />
40+
return (
41+
<MediaLibraryDialog
42+
allowedTypes={allowedTypes}
43+
onClose={onToggle}
44+
onSelectAssets={handleSelectAssets}
45+
/>
2746
);
2847
};
2948

30-
MediaLib.propTypes = {
31-
isOpen: PropTypes.bool,
32-
onChange: PropTypes.func,
33-
onToggle: PropTypes.func,
34-
};
35-
36-
export default MediaLib;
49+
export default MediaLibComponent;

admin/src/components/ReactMdEditor.tsx

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FC as FunctionComponent, useState, useEffect, useMemo } from "react";
22

3-
import { Box, Flex, Typography } from "@strapi/design-system";
3+
import { Flex, Field } from "@strapi/design-system";
44
import type { Schema } from "@strapi/types";
55
import MDEditor, { commands, ICommand } from "@uiw/react-md-editor";
66
import { useIntl } from "react-intl";
@@ -9,7 +9,7 @@ import { styled } from "styled-components";
99
import "@uiw/react-markdown-preview/markdown.css";
1010

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

1515
const Wrapper = styled.div`
@@ -34,6 +34,9 @@ const Wrapper = styled.div`
3434
img {
3535
max-width: 100%;
3636
}
37+
ul,ol{
38+
list-style:inherit;
39+
}
3740
.w-md-editor-preview {
3841
display: block;
3942
strong {
@@ -73,15 +76,19 @@ interface EditorProps {
7376
defaultMessage: string;
7477
};
7578
required?: boolean;
79+
attribute?: any; // TO FIX
80+
labelAction?: React.ReactNode; //TO FIX TO CHECK
7681
}
7782

7883
const Editor: FunctionComponent<EditorProps> = ({
84+
attribute,
7985
name,
80-
intlLabel,
8186
disabled,
82-
error,
83-
description,
87+
labelAction,
8488
required,
89+
description,
90+
error,
91+
intlLabel,
8592
}) => {
8693
// const { formatMessage } = useIntl();
8794
const { onChange, value }: any = useField(name);
@@ -94,6 +101,7 @@ const Editor: FunctionComponent<EditorProps> = ({
94101

95102
const handleChangeAssets = (assets: Schema.Attribute.MediaValue<true>) => {
96103
let newValue = value ? value : "";
104+
97105
assets.map((asset) => {
98106
if (asset.mime.includes("image")) {
99107
const imgTag = `![${asset.alt}](${asset.url})`;
@@ -121,9 +129,9 @@ const Editor: FunctionComponent<EditorProps> = ({
121129

122130
const toolbarCommands = useMemo(() => {
123131
const strapiMediaLibrary: ICommand = {
124-
name: "image",
125-
keyCommand: "image",
126-
buttonProps: { "aria-label": "Insert image" },
132+
name: "media",
133+
keyCommand: "media",
134+
buttonProps: { "aria-label": "Insert media" },
127135
icon: (
128136
<svg width="12" height="12" viewBox="0 0 20 20">
129137
<path
@@ -187,42 +195,36 @@ const Editor: FunctionComponent<EditorProps> = ({
187195
}, []);
188196

189197
return (
190-
<Flex gap={0} style={{ margin: 0, padding: 0 }}>
191-
<Box>
192-
<Typography variant="pi" fontWeight="bold">
193-
{formatMessage(intlLabel)}
194-
</Typography>
195-
{required && (
196-
<Typography variant="pi" fontWeight="bold" textColor="danger600">
197-
*
198-
</Typography>
199-
)}
200-
</Box>
201-
<Wrapper>
202-
<MDEditor
203-
hidden={disabled}
204-
commands={toolbarCommands}
205-
value={value || ""}
206-
onChange={(newValue) => {
207-
onChange({ target: { name, value: newValue || "" } });
208-
}}
209-
/>
210-
<div style={{ padding: "50px 0 0 0" }} />
211-
{/* <MediaLib
212-
isOpen={mediaLibVisible}
213-
onChange={handleChangeAssets}
214-
onToggle={handleToggleMediaLib}
215-
/> */}
216-
</Wrapper>
217-
{error && (
218-
<Typography variant="pi" textColor="danger600">
219-
{formatMessage({ id: error, defaultMessage: error })}
220-
</Typography>
221-
)}
222-
{description && (
223-
<Typography variant="pi">{formatMessage(description)}</Typography>
224-
)}
225-
</Flex>
198+
<Field.Root
199+
name= {name }
200+
id={ name }
201+
error={ error }
202+
hint={ description && formatMessage( description ) }
203+
>
204+
<Flex spacing={ 1 } alignItems="normal" style={ { 'flexDirection': 'column' } }>
205+
<Field.Label action={ labelAction } required={ required }>
206+
{ intlLabel ? formatMessage( intlLabel ) : name }
207+
</Field.Label>
208+
<Wrapper>
209+
<MDEditor
210+
hidden={disabled}
211+
commands={toolbarCommands}
212+
value={value || ""}
213+
onChange={(newValue) => {
214+
onChange({ target: { name, value: newValue || "" } });
215+
}}
216+
/>
217+
</Wrapper>
218+
<Field.Hint />
219+
<Field.Error />
220+
</Flex>
221+
<MediaLib
222+
/*allowedTypes={['images']}*/
223+
isOpen={ mediaLibVisible }
224+
onChange={ handleChangeAssets }
225+
onToggle={ handleToggleMediaLib }
226+
/>
227+
</Field.Root>
226228
);
227229
};
228230

0 commit comments

Comments
 (0)