@@ -11,61 +11,39 @@ use crate::png;
11
11
12
12
use crate :: wasm:: FileUpload ;
13
13
14
- pub struct ImageFile < ' a > {
15
- file_input : & ' a FileUpload ,
14
+ #[ derive( Clone , PartialEq , Properties ) ]
15
+ pub struct Props {
16
+ pub file : FileUpload ,
17
+ }
16
18
19
+ pub struct ImageComponent {
17
20
width : usize ,
18
21
height : usize ,
19
22
}
20
23
21
- impl ImageFile < ' _ > {
22
- pub fn name ( & self ) -> String {
23
- if self . file_input . mime_type . contains ( "image" ) {
24
- self . file_input . name . to_string ( )
25
- } else {
26
- format ! ( "{}{}" , self . file_input. name, ".png" )
27
- }
28
- }
29
-
30
- pub fn mime_type ( & self ) -> String {
31
- if self . file_input . mime_type . contains ( "image" ) {
32
- self . file_input . mime_type . to_string ( )
33
- } else {
34
- "image/png" . to_string ( )
35
- }
36
- }
24
+ pub enum Msg {
25
+ Width ( Event ) ,
26
+ Height ( Event ) ,
27
+ }
37
28
38
- pub fn data ( & self ) -> Vec < u8 > {
39
- if self . file_input . mime_type . contains ( "image" ) {
40
- self . file_input . data . clone ( )
29
+ impl ImageComponent {
30
+ pub fn src ( & self , file : & FileUpload ) -> String {
31
+ let data = if file. mime_type . contains ( "image" ) {
32
+ file. data . clone ( )
41
33
} else {
42
- let file_data = file_data:: Raw :: new ( & self . file_input . data ) ;
34
+ let file_data = file_data:: Raw :: new ( & file . data ) ;
43
35
let parser = ParserType :: CGA ;
44
36
let image = file_data. parse ( parser, self . width ) ;
45
37
let palette = palette_from_abbr ( "cga0" ) ;
46
38
let mut bytes: Vec < u8 > = Vec :: new ( ) ;
47
39
48
40
let _ = png:: write_to ( & mut bytes, tile ( image. data ( ) , self . height ) , palette. clone ( ) ) ;
49
41
bytes
50
- }
42
+ } ;
43
+ format ! ( "data:application/png;base64,{}" , STANDARD . encode( data) )
51
44
}
52
45
}
53
46
54
- #[ derive( Clone , PartialEq , Properties ) ]
55
- pub struct Props {
56
- pub file : FileUpload ,
57
- }
58
-
59
- pub struct ImageComponent {
60
- width : usize ,
61
- height : usize ,
62
- }
63
-
64
- pub enum Msg {
65
- Width ( Event ) ,
66
- Height ( Event ) ,
67
- }
68
-
69
47
impl Component for ImageComponent {
70
48
type Message = Msg ;
71
49
type Properties = Props ;
@@ -92,33 +70,22 @@ impl Component for ImageComponent {
92
70
}
93
71
94
72
fn view ( & self , ctx : & Context < Self > ) -> Html {
95
- let image = ImageFile {
96
- file_input : & ctx. props ( ) . file ,
97
- width : self . width ,
98
- height : self . height ,
99
- } ;
100
-
101
- let output = format ! (
102
- "data:{};base64,{}" ,
103
- image. mime_type( ) ,
104
- STANDARD . encode( image. data( ) )
105
- ) ;
106
-
107
73
let noop = Callback :: from ( |e : SubmitEvent | {
108
74
e. prevent_default ( ) ;
109
75
} ) ;
76
+ let file = & ctx. props ( ) . file ;
110
77
111
78
html ! {
112
79
<div class="preview-tile " >
113
80
<form onsubmit={ noop} >
114
81
<label for ="width" >{ "[Tile] Width" } </label>
115
- <input name="width" type ="number" value={ image . width. to_string( ) } onchange={ ctx. link( ) . callback( Msg :: Width ) } />
82
+ <input name="width" type ="number" value={ self . width. to_string( ) } onchange={ ctx. link( ) . callback( Msg :: Width ) } />
116
83
<label for ="height" >{ "[Tile] Height" } </label>
117
- <input name="height" type ="number" value={ image . height. to_string( ) } onchange={ ctx. link( ) . callback( Msg :: Height ) } />
84
+ <input name="height" type ="number" value={ self . height. to_string( ) } onchange={ ctx. link( ) . callback( Msg :: Height ) } />
118
85
</form>
119
- <p class="preview-name" >{ & image . name( ) } </p>
86
+ <p class="preview-name" >{ file . name. to_string ( ) } </p>
120
87
<div class=".preview-media" >
121
- <img src={ output } />
88
+ <img src={ self . src ( file ) } />
122
89
</div>
123
90
</div>
124
91
}
0 commit comments