|
| 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 | +import { downscaleImage } from "../../Shared/downscaleImage" |
| 10 | + |
| 11 | +const MAX_PAYLOAD_SIZE = 100 * 1024 |
| 12 | + |
| 13 | +/** |
| 14 | + * Сжатие url изображения c проверкой размера |
| 15 | + * @param {String} uncompressed |
| 16 | + * @param {String} type |
| 17 | + */ |
| 18 | +async function getCompressed(uncompressed, type) { |
| 19 | + if (uncompressed.length < MAX_PAYLOAD_SIZE) return uncompressed |
| 20 | + const smallcompressedRes = await downscaleImage(uncompressed, type, 480) |
| 21 | + if (smallcompressedRes.length < MAX_PAYLOAD_SIZE) return smallcompressedRes |
| 22 | + const mediumcompressedRes = await downscaleImage(uncompressed, type, 360) |
| 23 | + if (mediumcompressedRes.length < MAX_PAYLOAD_SIZE) return mediumcompressedRes |
| 24 | + const extracompressedRes = await downscaleImage(uncompressed, type, 240) |
| 25 | + if (extracompressedRes.length < MAX_PAYLOAD_SIZE) return extracompressedRes |
| 26 | + console.error("compressed unsuc, too long url") |
| 27 | + return null |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * компонент палитры |
| 32 | + * @param {object} props |
| 33 | + * @param {void} props.setNoteMedia |
| 34 | + * @param {Array<String>} props.mediaList |
| 35 | + * @param {{}} props.style |
| 36 | + * @param {String} props.className |
| 37 | + * @param {Boolean} props.disabled |
| 38 | + * @param {String} props.noteId |
| 39 | + * @param {{}} props.sizeData |
| 40 | + */ |
| 41 | +function Media({ setNoteMedia, mediaList = [], style, className, disabled, noteId, sizeData }) { |
| 42 | + const { addMedia, removeMedia, getMediaById, getNoteById } = useContext(NotesContext) |
| 43 | + |
| 44 | + const limited = getNoteById(noteId).media.length >= 1 |
| 45 | + |
| 46 | + /**хук состояния формы */ |
| 47 | + const [showForm, setShowForm] = React.useState(false) |
| 48 | + |
| 49 | + /**создание параметров модального окна*/ |
| 50 | + const modalProps = new ModalProps() |
| 51 | + modalProps.isOpen = showForm |
| 52 | + modalProps.setOpenState = setShowForm |
| 53 | + modalProps.sideClose = true |
| 54 | + |
| 55 | + /**открытие окна */ |
| 56 | + function open() { |
| 57 | + setShowForm(true) |
| 58 | + } |
| 59 | + |
| 60 | + /**закрытие окна */ |
| 61 | + function close() { |
| 62 | + setShowForm(false) |
| 63 | + } |
| 64 | + |
| 65 | + function encodeImageFileAsURLAndPost(e) { |
| 66 | + var file = e.target.files[0] |
| 67 | + var reader = new FileReader() |
| 68 | + |
| 69 | + reader.onloadend = async function () { |
| 70 | + const uncompressedReaderRes = reader.result |
| 71 | + const compressedRes = await getCompressed(uncompressedReaderRes, file.type) |
| 72 | + |
| 73 | + if (compressedRes) { |
| 74 | + const mediaId = addMedia(compressedRes, noteId) |
| 75 | + Array.isArray(mediaList) ? mediaList.push(mediaId) : (mediaList = [mediaId]) |
| 76 | + setNoteMedia(mediaList) |
| 77 | + } |
| 78 | + |
| 79 | + e.target.value = null |
| 80 | + } |
| 81 | + |
| 82 | + if (file !== undefined) reader.readAsDataURL(file) |
| 83 | + } |
| 84 | + |
| 85 | + function delImg(imgId, index) { |
| 86 | + removeMedia(imgId) |
| 87 | + mediaList.splice(index, 1) |
| 88 | + setNoteMedia(mediaList) |
| 89 | + } |
| 90 | + |
| 91 | + return ( |
| 92 | + <React.Fragment> |
| 93 | + {/**Кнопка вызова media */} |
| 94 | + <button disabled={disabled} className={`btn ${className}`} style={style} type="button" onClick={open} > |
| 95 | + <i className="bi bi-image" ></i> |
| 96 | + </button> |
| 97 | + |
| 98 | + {/**Форма media */} |
| 99 | + <Modal {...modalProps.bind()} > |
| 100 | + <div style={{ minHeight: `${sizeData.current ? sizeData.current.parentElement.clientHeight : 100}px` }} className="p-1 d-flex flex-wrap align-content-between align-items-center justify-content-center"> |
| 101 | + |
| 102 | + <div className="form-group container d-flex flex-row flex-wrap align-items-start justify-content-around mb-0"> |
| 103 | + {Array.isArray(mediaList) && mediaList.length ? (mediaList.map((imgId, index) => { |
| 104 | + const media = getMediaById(imgId) |
| 105 | + const src = typeof media === "object" && media && media.data |
| 106 | + return ( |
| 107 | + <div className="card p-1 m-1" key={imgId} style={{ position: "relative" }}> |
| 108 | + <img className="img-fluid" style={{ maxWidth: "35em", maxHeight: "15em" }} src={src} alt="note img"></img> |
| 109 | + <button |
| 110 | + style={{ position: "absolute", bottom: "0", right: "0", lineHeight: "1em", padding: "0.05em" }} |
| 111 | + className={`btn btn-danger m-1`} |
| 112 | + onClick={() => delImg(imgId, index)} |
| 113 | + >✗</button> |
| 114 | + </div> |
| 115 | + ) |
| 116 | + })) : ( |
| 117 | + <div className="p-1 m-1 text-center" style={{ minWidth: '100%' }}> |
| 118 | + No images |
| 119 | + </div> |
| 120 | + )} |
| 121 | + </div> |
| 122 | + |
| 123 | + <div className="form-group container row mb-0"> |
| 124 | + <div className="custom-file p-0 col m-1" style={{ minWidth: "7.6em" }}> |
| 125 | + <input style={{ cursor: "pointer" }} disabled={limited} onChange={encodeImageFileAsURLAndPost} type="file" className="custom-file-input" id="noteImgFile" accept=".jpg, .jpeg, .png" /> |
| 126 | + <label style={{ boxShadow: "none", border: "lightgray 1px solid" }} className="custom-file-label" htmlFor="noteImgFile">{"Img"}</label> |
| 127 | + </div> |
| 128 | + <button className="btn btn-light col-3 col-sm-2 m-1 ml-auto" style={{ boxShadow: "none", minWidth: "4em" }} onClick={close} > |
| 129 | + Close |
| 130 | + </button> |
| 131 | + </div> |
| 132 | + |
| 133 | + </div> |
| 134 | + </Modal> |
| 135 | + </React.Fragment> |
| 136 | + ); |
| 137 | +} |
| 138 | + |
| 139 | +// Валидация |
| 140 | +Media.propTypes = { |
| 141 | + setNoteMedia: PropTypes.func, |
| 142 | + style: PropTypes.object, |
| 143 | + className: PropTypes.string, |
| 144 | +} |
| 145 | + |
| 146 | +export default Media; |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
0 commit comments