|
| 1 | +import { FC as FunctionComponent, useState, useEffect, useMemo } from "react"; |
| 2 | +import { Flex, Field } from "@strapi/design-system"; |
| 3 | +import type { Schema } from "@strapi/types"; |
| 4 | +import { commands, ICommand } from "@uiw/react-md-editor"; |
| 5 | +import { useIntl } from "react-intl"; |
| 6 | +import { styled } from "styled-components"; |
| 7 | +import "@uiw/react-markdown-preview/markdown.css"; |
| 8 | +import { PLUGIN_ID } from '../utils/pluginId'; |
| 9 | +import MediaLib from "./MediaLib"; |
| 10 | +import { useField } from "@strapi/strapi/admin"; |
| 11 | +import assetsToMarkdown from "../utils/assetsToMarkdown"; |
| 12 | +import CustomMDEditor from "./CustomMDEditor"; |
| 13 | + |
| 14 | +const Wrapper = styled.div` |
| 15 | + flex-basis: 100%; |
| 16 | + > div:nth-child(2) { |
| 17 | + display: none; |
| 18 | + } |
| 19 | + .w-md-editor-bar { |
| 20 | + display: none; |
| 21 | + } |
| 22 | + .w-md-editor { |
| 23 | + border: 1px solid #dcdce4; |
| 24 | + border-radius: 4px; |
| 25 | + box-shadow: none; |
| 26 | + &:focus-within { |
| 27 | + border: 1px solid #4945ff; |
| 28 | + box-shadow: #4945ff 0px 0px 0px 2px; |
| 29 | + } |
| 30 | + min-height: 400px; |
| 31 | + display: flex; |
| 32 | + flex-direction: column; |
| 33 | + img { |
| 34 | + max-width: 100%; |
| 35 | + } |
| 36 | + ul,ol{ |
| 37 | + list-style:inherit; |
| 38 | + } |
| 39 | + .w-md-editor-preview { |
| 40 | + display: block; |
| 41 | + strong { |
| 42 | + font-weight: bold; |
| 43 | + } |
| 44 | + em { |
| 45 | + font-style: italic; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + .w-md-editor-content { |
| 50 | + flex: 1 1 auto; |
| 51 | + } |
| 52 | + .w-md-editor-fullscreen { |
| 53 | + z-index: 11; |
| 54 | + } |
| 55 | + .w-md-editor-text { |
| 56 | + margin: 0; |
| 57 | + } |
| 58 | + .w-md-editor-preview ol { |
| 59 | + list-style: auto; |
| 60 | + } |
| 61 | +`; |
| 62 | + |
| 63 | +interface FieldProps { |
| 64 | + name: string; |
| 65 | + onChange: (e: { target: { name: string; value: string } }) => void; |
| 66 | + value: string; |
| 67 | + intlLabel: { |
| 68 | + id: string; |
| 69 | + defaultMessage: string; |
| 70 | + }; |
| 71 | + disabled?: boolean; |
| 72 | + error?: string; |
| 73 | + description?: { |
| 74 | + id: string; |
| 75 | + defaultMessage: string; |
| 76 | + }; |
| 77 | + required?: boolean; |
| 78 | + attribute?: any; // TO FIX |
| 79 | + labelAction?: React.ReactNode; //TO FIX TO CHECK |
| 80 | +} |
| 81 | + |
| 82 | +interface CursorPosition { |
| 83 | + start: number; |
| 84 | + end: number; |
| 85 | +} |
| 86 | + |
| 87 | +const CustomField: FunctionComponent<FieldProps> = ({ |
| 88 | + attribute, |
| 89 | + name, |
| 90 | + disabled, |
| 91 | + labelAction, |
| 92 | + required, |
| 93 | + description, |
| 94 | + error, |
| 95 | + intlLabel, |
| 96 | +}) => { |
| 97 | + // const { formatMessage } = useIntl(); |
| 98 | + const field: any = useField(name); |
| 99 | + |
| 100 | + const formatMessage = (message: { id: string; defaultMessage: string }) => |
| 101 | + message?.defaultMessage ?? ""; |
| 102 | + const [mediaLibVisible, setMediaLibVisible] = useState(false); |
| 103 | + const [cursorPosition, setCursorPosition] = useState<CursorPosition | null>(null); |
| 104 | + |
| 105 | + const handleToggleMediaLib = () => setMediaLibVisible((prev) => !prev); |
| 106 | + |
| 107 | + const updateFieldValue = (value:any) => { |
| 108 | + field.onChange({ target: { name, value: value } }); |
| 109 | + } |
| 110 | + |
| 111 | + const handleChangeAssets = (assets: Schema.Attribute.MediaValue<true>) => { |
| 112 | + |
| 113 | + let output; |
| 114 | + const assetsString = assetsToMarkdown(assets); |
| 115 | + |
| 116 | + if (cursorPosition) { |
| 117 | + output = field.value.slice(0, cursorPosition.start) + assetsString + field.value.slice(cursorPosition.end); |
| 118 | + }else{ |
| 119 | + output = field.value + assetsString; |
| 120 | + } |
| 121 | + |
| 122 | + updateFieldValue(output); |
| 123 | + handleToggleMediaLib(); |
| 124 | + }; |
| 125 | + |
| 126 | + const [config, setConfig] = useState<{ toolbarCommands?: string[] }>({}); |
| 127 | + |
| 128 | + const toolbarCommands = useMemo(() => { |
| 129 | + const mediaLibraryButton: ICommand = { |
| 130 | + name: "media", |
| 131 | + keyCommand: "media", |
| 132 | + buttonProps: { "aria-label": "Insert media" }, |
| 133 | + icon: ( |
| 134 | + <svg width="12" height="12" viewBox="0 0 20 20"> |
| 135 | + <path |
| 136 | + fill="currentColor" |
| 137 | + d="M15 9c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm4-7H1c-.55 0-1 .45-1 1v14c0 .55.45 1 1 1h18c.55 0 1-.45 1-1V3c0-.55-.45-1-1-1zm-1 13l-6-5-2 2-4-5-4 8V4h16v11z" |
| 138 | + ></path> |
| 139 | + </svg> |
| 140 | + ), |
| 141 | + execute: (state, _api) => { |
| 142 | + setCursorPosition(state.selection); |
| 143 | + handleToggleMediaLib(); |
| 144 | + }, |
| 145 | + }; |
| 146 | + if (!config?.toolbarCommands) { |
| 147 | + return [...commands.getCommands(), |
| 148 | + commands.divider, |
| 149 | + mediaLibraryButton |
| 150 | + ] as ICommand[]; |
| 151 | + } |
| 152 | + const customCommands = config?.toolbarCommands |
| 153 | + ?.map((config) => { |
| 154 | + if (config === "mediaLibraryButton") return mediaLibraryButton; |
| 155 | + if ( |
| 156 | + config in commands && |
| 157 | + commands[config as unknown as keyof typeof commands] |
| 158 | + ) { |
| 159 | + return commands[ |
| 160 | + config as unknown as keyof typeof commands |
| 161 | + ] as ICommand; |
| 162 | + } |
| 163 | + }) |
| 164 | + .filter((command): command is ICommand => command !== undefined); |
| 165 | + |
| 166 | + return customCommands; |
| 167 | + }, [JSON.stringify(config)]); |
| 168 | + |
| 169 | + useEffect(() => { |
| 170 | + fetch(`/${PLUGIN_ID}`) |
| 171 | + .then((response) => response.json()) |
| 172 | + .then((data) => { |
| 173 | + setConfig(data); |
| 174 | + }); |
| 175 | + }, []); |
| 176 | + |
| 177 | + return ( |
| 178 | + <Field.Root |
| 179 | + name={name} |
| 180 | + id={name} |
| 181 | + error={error} |
| 182 | + hint={description && formatMessage(description)} |
| 183 | + > |
| 184 | + <Flex spacing={1} alignItems="normal" style={{ flexDirection: "column" }}> |
| 185 | + <Field.Label action={labelAction} required={required}> |
| 186 | + {intlLabel ? formatMessage(intlLabel) : name} |
| 187 | + </Field.Label> |
| 188 | + <Wrapper> |
| 189 | + <CustomMDEditor |
| 190 | + hidden={disabled} |
| 191 | + value={field.value} |
| 192 | + onChange={updateFieldValue} |
| 193 | + commands={toolbarCommands} |
| 194 | + /> |
| 195 | + </Wrapper> |
| 196 | + <Field.Hint /> |
| 197 | + <Field.Error /> |
| 198 | + </Flex> |
| 199 | + <MediaLib |
| 200 | + /*allowedTypes={['images']}*/ |
| 201 | + isOpen={mediaLibVisible} |
| 202 | + onChange={handleChangeAssets} |
| 203 | + onToggle={handleToggleMediaLib} |
| 204 | + /> |
| 205 | + </Field.Root> |
| 206 | + ); |
| 207 | +}; |
| 208 | + |
| 209 | +export { CustomField }; |
0 commit comments