@@ -6,13 +6,37 @@ import PropTypes from 'prop-types'
6
6
import "./media.css"
7
7
import NotesContext from "../../Context/NotesContext"
8
8
import Modal , { ModalProps } from "../../Shared/Components/Modal/Modal"
9
+ import { downscaleImage } from "../../Shared/downscaleImage"
9
10
10
11
const MAX_PAYLOAD_SIZE = 100 * 1024
11
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
+
12
30
/**
13
31
* компонент палитры
14
- * @param {* } param0
15
- *
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
16
40
*/
17
41
function Media ( { setNoteMedia, mediaList = [ ] , style, className, disabled, noteId, sizeData } ) {
18
42
const { addMedia, removeMedia, getMediaById, getNoteById } = useContext ( NotesContext )
@@ -42,16 +66,16 @@ function Media({ setNoteMedia, mediaList = [], style, className, disabled, noteI
42
66
var file = e . target . files [ 0 ]
43
67
var reader = new FileReader ( )
44
68
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 )
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 )
50
75
Array . isArray ( mediaList ) ? mediaList . push ( mediaId ) : ( mediaList = [ mediaId ] )
51
76
setNoteMedia ( mediaList )
52
- } else {
53
- console . error ( "readed unsuc" , res . length )
54
77
}
78
+
55
79
e . target . value = null
56
80
}
57
81
@@ -67,13 +91,7 @@ function Media({ setNoteMedia, mediaList = [], style, className, disabled, noteI
67
91
return (
68
92
< React . Fragment >
69
93
{ /**Кнопка вызова media */ }
70
- < button
71
- disabled = { disabled }
72
- className = { `btn ${ className } ` }
73
- style = { style }
74
- type = "button"
75
- onClick = { open }
76
- >
94
+ < button disabled = { disabled } className = { `btn ${ className } ` } style = { style } type = "button" onClick = { open } >
77
95
< i className = "bi bi-image" > </ i >
78
96
</ button >
79
97
@@ -102,21 +120,18 @@ function Media({ setNoteMedia, mediaList = [], style, className, disabled, noteI
102
120
) }
103
121
</ div >
104
122
105
- < div className = "form-group container d-flex flex-wrap justify-content-between mb-0" >
106
- < div className = "custom-file mb -0 m-1" style = { { maxWidth : "14em " } } >
107
- < input disabled = { limited } onChange = { encodeImageFileAsURLAndPost } type = "file" className = "custom-file-input" id = "noteImgFile" accept = ".jpg, .jpeg, .png" />
108
- < label className = "custom-file-label" htmlFor = "noteImgFile" > { "Img - 100Kb max " } </ label >
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 >
109
127
</ div >
110
- < button
111
- className = "btn btn-light m-1"
112
- style = { { boxShadow : "none" } }
113
- onClick = { close }
114
- > Close</ button >
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 >
115
131
</ div >
116
132
117
133
</ div >
118
134
</ Modal >
119
-
120
135
</ React . Fragment >
121
136
) ;
122
137
}
0 commit comments