|
| 1 | +/** |
| 2 | + * @file media.js |
| 3 | + */ |
| 4 | +import React, { useContext } from "react" |
| 5 | +import PropTypes from 'prop-types' |
| 6 | +import "./media.css" |
| 7 | +import NotesContext from "../../Context/NotesContext" |
| 8 | +import Modal, { ModalProps } from "../../Shared/Components/Modal/Modal" |
| 9 | + |
| 10 | +const MAX_PAYLOAD_SIZE = 100 * 1024 |
| 11 | + |
| 12 | +/** |
| 13 | + * компонент палитры |
| 14 | + * @param {*} param0 |
| 15 | + * |
| 16 | + */ |
| 17 | +function Media({ setNoteMedia, mediaList = [], style, className, disabled, noteId }) { |
| 18 | + const { addMedia, removeMedia, getMediaById, getNoteById } = useContext(NotesContext) |
| 19 | + |
| 20 | + const limited = getNoteById(noteId).media.length >= 3 |
| 21 | + |
| 22 | + /**хук состояния формы */ |
| 23 | + const [showForm, setShowForm] = React.useState(false) |
| 24 | + |
| 25 | + /**создание параметров модального окна*/ |
| 26 | + const modalProps = new ModalProps() |
| 27 | + modalProps.isOpen = showForm |
| 28 | + modalProps.setOpenState = setShowForm |
| 29 | + modalProps.sideClose = true |
| 30 | + |
| 31 | + /**открытие окна */ |
| 32 | + function open() { |
| 33 | + setShowForm(true) |
| 34 | + } |
| 35 | + |
| 36 | + /**закрытие окна */ |
| 37 | + function close() { |
| 38 | + setShowForm(false) |
| 39 | + } |
| 40 | + |
| 41 | + function encodeImageFileAsURLAndPost(e) { |
| 42 | + var file = e.target.files[0] |
| 43 | + var reader = new FileReader() |
| 44 | + |
| 45 | + reader.onloadend = function () { |
| 46 | + const res = reader.result |
| 47 | + if (res.length < MAX_PAYLOAD_SIZE) { |
| 48 | + console.log("readed suc", res.length) |
| 49 | + const mediaId = addMedia(res, noteId) |
| 50 | + Array.isArray(mediaList) ? mediaList.push(mediaId) : (mediaList = [mediaId]) |
| 51 | + setNoteMedia(mediaList) |
| 52 | + } else { |
| 53 | + console.error("readed unsuc", res.length) |
| 54 | + } |
| 55 | + e.target.value = null |
| 56 | + } |
| 57 | + |
| 58 | + if (file !== undefined) reader.readAsDataURL(file) |
| 59 | + } |
| 60 | + |
| 61 | + function delImg(imgId, index) { |
| 62 | + removeMedia(imgId) |
| 63 | + mediaList.splice(index, 1) |
| 64 | + setNoteMedia(mediaList) |
| 65 | + } |
| 66 | + |
| 67 | + return ( |
| 68 | + <React.Fragment> |
| 69 | + {/**Кнопка вызова media */} |
| 70 | + <button |
| 71 | + disabled={disabled} |
| 72 | + className={`btn ${className}`} |
| 73 | + style={style} |
| 74 | + type="button" |
| 75 | + onClick={open} |
| 76 | + > |
| 77 | + <i className="bi bi-image" ></i> |
| 78 | + </button> |
| 79 | + |
| 80 | + {/**Форма media */} |
| 81 | + <Modal {...modalProps.bind()} > |
| 82 | + <div className="p-1 d-flex flex-row flex-wrap justify-content-center align-items-center"> |
| 83 | + |
| 84 | + <div className="form-group container d-flex flex-row flex-wrap mb-0"> |
| 85 | + {Array.isArray(mediaList) ? (mediaList.map((imgId, index) => { |
| 86 | + const media = getMediaById(imgId) |
| 87 | + const src = typeof media === "object" && media && media.data |
| 88 | + return ( |
| 89 | + <div className="card p-1 m-1" key={imgId} style={{ position: "relative" }}> |
| 90 | + <img style={{ maxWidth: "8em", maxHeight: "8em" }} src={src} alt="note img"></img> |
| 91 | + <button |
| 92 | + style={{ position: "absolute", bottom: "0", right: "0", lineHeight: "1em", padding: "0.05em" }} |
| 93 | + className={`btn btn-danger m-1`} |
| 94 | + onClick={() => delImg(imgId, index)} |
| 95 | + >✗</button> |
| 96 | + </div> |
| 97 | + ) |
| 98 | + })) : null} |
| 99 | + </div> |
| 100 | + |
| 101 | + <div className="form-group container d-flex flex-wrap justify-content-between mb-0"> |
| 102 | + <div className="custom-file mb-0 m-1" style={{ maxWidth: "14em" }}> |
| 103 | + <input disabled={limited} onChange={encodeImageFileAsURLAndPost} type="file" className="custom-file-input" id="noteImgFile" accept=".jpg, .jpeg, .png" /> |
| 104 | + <label className="custom-file-label" htmlFor="noteImgFile">{"Img - 100Kb max"}</label> |
| 105 | + </div> |
| 106 | + <button |
| 107 | + className="btn btn-light m-1" |
| 108 | + style={{ boxShadow: "none" }} |
| 109 | + onClick={close} |
| 110 | + >Close</button> |
| 111 | + </div> |
| 112 | + |
| 113 | + </div> |
| 114 | + </Modal> |
| 115 | + |
| 116 | + </React.Fragment> |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | +// Валидация |
| 121 | +Media.propTypes = { |
| 122 | + setNoteMedia: PropTypes.func, |
| 123 | + style: PropTypes.object, |
| 124 | + className: PropTypes.string, |
| 125 | +} |
| 126 | + |
| 127 | +export default Media; |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
0 commit comments