@@ -4,7 +4,6 @@ use web_sys::HtmlInputElement;
4
4
use yew:: { html, Callback , Component , Context , Event , Html , Properties , SubmitEvent , TargetCast } ;
5
5
6
6
use crate :: color:: palette:: palette_from_abbr;
7
- use crate :: image:: tile;
8
7
use crate :: parser:: ParserType ;
9
8
use crate :: { file_data, png} ;
10
9
@@ -20,7 +19,7 @@ pub struct ImageComponent {
20
19
}
21
20
22
21
pub enum Msg {
23
- Width ( Event ) ,
22
+ Width ( usize ) ,
24
23
}
25
24
26
25
impl ImageComponent {
@@ -40,7 +39,7 @@ impl ImageComponent {
40
39
format ! ( "data:application/png;base64,{}" , STANDARD . encode( data) )
41
40
}
42
41
43
- pub fn previews ( & self , file : & FileUpload ) -> Html {
42
+ pub fn previews ( & self , ctx : & Context < Self > , file : & FileUpload ) -> Html {
44
43
if file. mime_type . contains ( "image" ) {
45
44
"" . into ( )
46
45
} else {
@@ -54,9 +53,12 @@ impl ImageComponent {
54
53
55
54
let _ = png:: write_to ( & mut bytes, p. data ( ) , palette. clone ( ) ) ;
56
55
let src = format ! ( "data:application/png;base64,{}" , STANDARD . encode( bytes) ) ;
56
+ let width = p. width ( ) . clone ( ) ;
57
57
html ! {
58
- <div class="preview-tile" >
59
- <h3>{ p. width( ) } </h3>
58
+ <div class="preview-tile" onclick={ ctx. link( ) . callback( move |_|{
59
+ Msg :: Width ( width)
60
+ } ) } >
61
+ <h3>{ & width. to_string( ) } </h3>
60
62
<img src={ src } />
61
63
</div>
62
64
}
@@ -71,16 +73,13 @@ impl Component for ImageComponent {
71
73
type Properties = Props ;
72
74
73
75
fn create ( _ctx : & Context < Self > ) -> Self {
74
- Self {
75
- width : 320 ,
76
- }
76
+ Self { width : 320 }
77
77
}
78
78
79
79
fn update ( & mut self , _ctx : & Context < Self > , msg : Self :: Message ) -> bool {
80
80
match msg {
81
- Msg :: Width ( e) => {
82
- let input: HtmlInputElement = e. target_unchecked_into ( ) ;
83
- self . width = input. value ( ) . parse ( ) . expect ( "fail to parse width" ) ;
81
+ Msg :: Width ( w) => {
82
+ self . width = w;
84
83
}
85
84
}
86
85
true
@@ -90,6 +89,10 @@ impl Component for ImageComponent {
90
89
let noop = Callback :: from ( |e : SubmitEvent | {
91
90
e. prevent_default ( ) ;
92
91
} ) ;
92
+ let onchange = ctx. link ( ) . callback ( |e : Event | {
93
+ let input: HtmlInputElement = e. target_unchecked_into ( ) ;
94
+ Msg :: Width ( input. value ( ) . parse ( ) . expect ( "fail to parse width" ) )
95
+ } ) ;
93
96
let file = & ctx. props ( ) . file ;
94
97
95
98
html ! {
@@ -101,11 +104,11 @@ impl Component for ImageComponent {
101
104
</div>
102
105
<form onsubmit={ noop} >
103
106
<label for ="width" >{ "Width" } </label>
104
- <input name="width" type ="number" value={ self . width. to_string( ) } onchange= { ctx . link ( ) . callback ( Msg :: Width ) } />
107
+ <input name="width" type ="number" value={ self . width. to_string( ) } { onchange } />
105
108
</form>
106
109
</div>
107
110
<div class="preview-row" >
108
- { self . previews( file) }
111
+ { self . previews( ctx , file) }
109
112
</div>
110
113
</>
111
114
}
0 commit comments