From 3bc25e7fb93b0b38059209ea87320aeed0486f70 Mon Sep 17 00:00:00 2001 From: kwin Date: Sat, 26 Mar 2022 12:27:54 +0800 Subject: [PATCH] add support for medialib --- admin/src/components/MediaLib/index.js | 43 ++++++++++++ admin/src/components/ReactMdEditor/index.js | 77 ++++++++++++++++++--- package.json | 2 +- 3 files changed, 113 insertions(+), 9 deletions(-) create mode 100644 admin/src/components/MediaLib/index.js diff --git a/admin/src/components/MediaLib/index.js b/admin/src/components/MediaLib/index.js new file mode 100644 index 0000000..486f914 --- /dev/null +++ b/admin/src/components/MediaLib/index.js @@ -0,0 +1,43 @@ +import React from "react"; +import { prefixFileUrlWithBackendUrl, useLibrary } from "@strapi/helper-plugin"; +import PropTypes from "prop-types"; + +const MediaLib = ({ isOpen, onChange, onToggle }) => { + const { components } = useLibrary(); + const MediaLibraryDialog = components["media-library"]; + + const handleSelectAssets = (files) => { + const formattedFiles = files.map((f) => ({ + alt: f.alternativeText || f.name, + url: prefixFileUrlWithBackendUrl(f.url), + mime: f.mime, + })); + + onChange(formattedFiles); + }; + + if (!isOpen) { + return null; + } + + return ( + + ); +}; + +MediaLib.defaultProps = { + isOpen: false, + onChange: () => {}, + onToggle: () => {}, +}; + +MediaLib.propTypes = { + isOpen: PropTypes.bool, + onChange: PropTypes.func, + onToggle: PropTypes.func, +}; + +export default MediaLib; diff --git a/admin/src/components/ReactMdEditor/index.js b/admin/src/components/ReactMdEditor/index.js index 964de64..4891277 100644 --- a/admin/src/components/ReactMdEditor/index.js +++ b/admin/src/components/ReactMdEditor/index.js @@ -1,9 +1,9 @@ -import React from 'react'; -import PropTypes from 'prop-types'; -import MDEditor from '@uiw/react-md-editor'; - -import styled from 'styled-components'; -import '@uiw/react-md-editor/dist/markdown-editor.css'; +import React, { useState } from "react"; +import PropTypes from "prop-types"; +import MDEditor, { commands } from "@uiw/react-md-editor"; +import MediaLib from "../MediaLib"; +import styled from "styled-components"; +import "@uiw/react-md-editor/dist/markdown-editor.css"; const Wrapper = styled.div` .w-md-editor { @@ -29,16 +29,77 @@ const Wrapper = styled.div` `; const Editor = ({ onChange, name, value }) => { + const [mediaLibVisible, setMediaLibVisible] = useState(false); + + const handleToggleMediaLib = () => setMediaLibVisible((prev) => !prev); + + const handleChangeAssets = (assets) => { + let newValue = value ? value : ""; + + assets.map((asset) => { + if (asset.mime.includes("image")) { + const imgTag = `![](${asset.url})`; + + newValue = `${newValue}${imgTag}`; + } + + // Handle videos and other type of files by adding some code + }); + + onChange({ target: { name, value: newValue || "" } }); + handleToggleMediaLib(); + }; return ( + + + ), + execute: (state, api) => { + handleToggleMediaLib(); + }, + }, + commands.unorderedListCommand, + commands.orderedListCommand, + commands.checkedListCommand, + commands.codeEdit, + commands.codeLive, + commands.codePreview, + commands.fullscreen, + ]} value={value || ""} onChange={(newValue) => { - onChange({ target: { name, value: newValue || ""} }); + onChange({ target: { name, value: newValue || "" } }); }} /> -
+
+ ); }; diff --git a/package.json b/package.json index 160ac72..4790217 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "strapi-plugin-wysiwsg-react-md-editor", - "version": "4.0.1", + "version": "4.1.0", "description": "This is a strapi rich text editor plugin using react md editor.", "strapi": { "name": "wysiwsg-react-md-editor",