Skip to content

Commit

Permalink
pluginId --> PLUGIN_ID
Browse files Browse the repository at this point in the history
  • Loading branch information
gordielachance committed Oct 14, 2024
1 parent fbd5e8d commit 5964441
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 51 deletions.
4 changes: 2 additions & 2 deletions admin/src/components/Initializer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { useEffect, useRef } from 'react';
import pluginId from '../pluginId';
import {PLUGIN_ID} from '../utils/pluginId';

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

useEffect(() => {
ref.current(pluginId);
ref.current(PLUGIN_ID);
}, []);

return null;
Expand Down
61 changes: 23 additions & 38 deletions admin/src/components/MediaLib.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { FC as FunctionComponent } from "react";

import { useStrapiApp } from "@strapi/admin/strapi-admin";
import type { Schema } from "@strapi/types";
import React from 'react';
import PropTypes from 'prop-types';
import { useStrapiApp } from '@strapi/strapi/admin';

const prefixFileUrlWithBackendUrl = (fileURL: string) => {
return !!fileURL &&
Expand All @@ -14,47 +13,33 @@ const prefixFileUrlWithBackendUrl = (fileURL: string) => {
: fileURL;
};

interface MediaLibComponentProps {
isOpen: boolean;
onChange: (files: Schema.Attribute.MediaValue<true>) => void;
onToggle: () => void;
}

const MediaLib: FunctionComponent<MediaLibComponentProps> = ({
isOpen,
onChange,
onToggle,
}) => {
const components = useStrapiApp("ImageDialog", (state) => state.components);
if (!components || !isOpen) return null;

const MediaLibraryDialog = components["media-library"] as FunctionComponent<{
onClose: () => void;
onSelectAssets: (_images: Schema.Attribute.MediaValue<true>) => void;
}>;

const handleSelectAssets = (files: Schema.Attribute.MediaValue<true>) => {
const formattedFiles = files.map((f) => ({
const MediaLib = ( { isOpen = false, onChange = () => {}, onToggle = () => {} } ) => {
const { components } = useStrapiApp( 'library', app => app );
const MediaLibraryDialog = components[ 'media-library' ];

const handleSelectAssets = files => {
const formattedFiles = files.map(f => ( {
alt: f.alternativeText || f.name,
url: prefixFileUrlWithBackendUrl(f.url),
url: prefixFileUrlWithBackendUrl( f.url ),
mime: f.mime,
}));
} ) );

onChange( formattedFiles );
};

onChange(formattedFiles);
if ( !isOpen ) {
return null
};

return (
<MediaLibraryDialog
onClose={onToggle}
onSelectAssets={handleSelectAssets}
/>
return(
<MediaLibraryDialog onClose={ onToggle } onSelectAssets={ handleSelectAssets } />
);
};

MediaLib.defaultProps = {
isOpen: false,
onChange: (_files: Schema.Attribute.MediaValue<true>) => {},
onToggle: () => {},
MediaLib.propTypes = {
isOpen: PropTypes.bool,
onChange: PropTypes.func,
onToggle: PropTypes.func,
};

export { MediaLib };
export default MediaLib;
4 changes: 2 additions & 2 deletions admin/src/components/ReactMdEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { styled } from "styled-components";

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

import pluginId from "../pluginId";
import {PLUGIN_ID} from '../utils/pluginId';
import { MediaLib } from "./MediaLib";
import { useField } from "@strapi/strapi/admin";

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

useEffect(() => {
fetch(`/${pluginId}`)
fetch(`/${PLUGIN_ID}`)
.then((response) => response.json())
.then((data) => {
setConfigs(data);
Expand Down
4 changes: 2 additions & 2 deletions admin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pluginPkg from "../../package.json";
import pluginId from "./pluginId";
import {PLUGIN_ID} from './utils/pluginId';
import { Initializer } from "./components/Initializer";
import { Editor as ReactMdEditor } from "./components/ReactMdEditor";
import { getTranslation } from "./utils/getTranslation";
Expand All @@ -10,7 +10,7 @@ export default {
register(app: any) {
app.addFields({ type: "richtext", Component: ReactMdEditor });
const plugin = {
id: pluginId,
id: PLUGIN_ID,
initializer: Initializer,
isReady: false,
name,
Expand Down
5 changes: 0 additions & 5 deletions admin/src/pluginId.ts

This file was deleted.

4 changes: 2 additions & 2 deletions admin/src/utils/getTranslation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pluginId from '../pluginId';
import {PLUGIN_ID} from '../utils/pluginId';

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

export { getTranslation };
3 changes: 3 additions & 0 deletions admin/src/utils/pluginId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pluginPkg from '../../../package.json';

export const PLUGIN_ID = pluginPkg.strapi.name || pluginPkg.name.replace(/^(@[^-,.][\w,-]+\/|strapi-)plugin-/i, '');

0 comments on commit 5964441

Please sign in to comment.