diff --git a/docs/3.0.0-beta.x/guides/registering-a-field-in-admin.md b/docs/3.0.0-beta.x/guides/registering-a-field-in-admin.md index b5a3fdd89f..503d938e88 100644 --- a/docs/3.0.0-beta.x/guides/registering-a-field-in-admin.md +++ b/docs/3.0.0-beta.x/guides/registering-a-field-in-admin.md @@ -40,21 +40,99 @@ Once this step is over all we need to do is to create our new WYSIWYG which will ### Creating the WYSIWYG -In this part we will create two components: +In this part we will create three components: +- MediaLib which will be used to insert media in the editor - Wysiwyg which will wrap the CKEditor with a label and the errors - CKEditor which will be the implementation of the new WYSIWYG +### Creating the MediaLib + +**Path —** `./plugins/wysiwyg/admin/src/components/MediaLib/index.js` + +```js +import React, { useEffect, useState } from 'react'; +import { useStrapi, prefixFileUrlWithBackendUrl } from 'strapi-helper-plugin'; +import PropTypes from 'prop-types'; + +const MediaLib = ({ isOpen, onChange, onToggle }) => { + const { + strapi: { + componentApi: { getComponent }, + }, + } = useStrapi(); + const [data, setData] = useState(null); + const [isDisplayed, setIsDisplayed] = useState(false); + + useEffect(() => { + if (isOpen) { + setIsDisplayed(true); + } + }, [isOpen]); + + const Component = getComponent('media-library').Component; + + const handleInputChange = data => { + if (data) { + const { url } = data; + + setData({ ...data, url: prefixFileUrlWithBackendUrl(url) }); + } + }; + + const handleClosed = () => { + if (data) { + onChange(data); + } + + setData(null); + setIsDisplayed(false); + }; + + if (Component && isDisplayed) { + return ( + + ); + } + + return null; +}; + +MediaLib.defaultProps = { + isOpen: false, + onChange: () => {}, + onToggle: () => {}, +}; + +MediaLib.propTypes = { + isOpen: PropTypes.bool, + onChange: PropTypes.func, + onToggle: PropTypes.func, +}; + +export default MediaLib; +``` + #### Creating the WYSIWYG Wrapper **Path —** `./plugins/wysiwyg/admin/src/components/Wysiwyg/index.js` ```js -import React from 'react'; +iimport React, { useState } from 'react'; import PropTypes from 'prop-types'; import { isEmpty } from 'lodash'; +import { Button } from '@buffetjs/core'; import { Label, InputDescription, InputErrors } from 'strapi-helper-plugin'; import Editor from '../CKEditor'; +import MediaLib from '../MediaLib'; const Wysiwyg = ({ inputDescription, @@ -65,12 +143,28 @@ const Wysiwyg = ({ onChange, value, }) => { + const [isOpen, setIsOpen] = useState(false); let spacer = !isEmpty(inputDescription) ?
:
; if (!noErrorsDescription && !isEmpty(errors)) { spacer =
; } + const handleChange = data => { + console.log(data); + + if (data.mime.includes('image')) { + const imgTag = `

${data.alternativeText}

`; + const newValue = value ? `${value}${imgTag}` : imgTag; + + onChange({ target: { name, value: newValue } }); + } + + // Handle videos and other type of files + }; + + const handleToggle = () => setIsOpen(prev => !prev); + return (
); }; @@ -124,6 +224,7 @@ Wysiwyg.propTypes = { }; export default Wysiwyg; + ``` #### Implementing CKEditor @@ -146,11 +247,33 @@ const Wrapper = styled.div` } `; +const configuration = { + toolbar: [ + 'heading', + '|', + 'bold', + 'italic', + 'link', + 'bulletedList', + 'numberedList', + '|', + 'indent', + 'outdent', + '|', + 'blockQuote', + 'insertTable', + 'mediaEmbed', + 'undo', + 'redo', + ], +}; + const Editor = ({ onChange, name, value }) => { return ( { const data = editor.getData(); @@ -164,7 +287,7 @@ const Editor = ({ onChange, name, value }) => { Editor.propTypes = { onChange: PropTypes.func.isRequired, name: PropTypes.string.isRequired, - value: PropTypes.string.isRequired, + value: PropTypes.string, }; export default Editor;