@@ -3,7 +3,6 @@ import { RapidElement } from '../RapidElement';
3
3
import { property } from 'lit/decorators.js' ;
4
4
import { getClasses } from '../utils' ;
5
5
import { Lightbox } from '../lightbox/Lightbox' ;
6
- import { styleMap } from 'lit-html/directives/style-map.js' ;
7
6
import { WebChatIcon } from '../webchat' ;
8
7
9
8
enum ThumbnailContentType {
@@ -48,17 +47,15 @@ export class Thumbnail extends RapidElement {
48
47
.zoom .thumb {
49
48
border-radius: 0px !important;
50
49
width: calc(var(--thumb-size, 4em) + 0.8em);
51
- height: calc(var(--thumb-size, 4em) + 0.8em);
52
- max-height: calc(var(--thumb-size, 4em) + 0.8em);
50
+ max-height: calc(90vh - 10em);
53
51
}
54
52
55
53
.thumb {
56
54
background-size: cover;
57
55
background-position: center;
58
56
background-repeat: no-repeat;
59
57
border-radius: var(--curvature);
60
- max-height: var(--thumb-size, 4em);
61
- height: var(--thumb-size, 4em);
58
+ max-height: calc(var(--thumb-size, 4em) * 2);
62
59
width: var(--thumb-size, 4em);
63
60
display: flex;
64
61
align-items: center;
@@ -84,8 +81,24 @@ export class Thumbnail extends RapidElement {
84
81
display: block;
85
82
}
86
83
87
- .zoom temba-icon {
84
+ .download {
88
85
display: none;
86
+ position: absolute;
87
+ right: 0em;
88
+ bottom: 0em;
89
+ border-radius: var(--curvature);
90
+ transform: scale(0.2) translate(3em, 3em);
91
+ padding: 0.4em;
92
+ }
93
+
94
+ .zoom .download {
95
+ display: block;
96
+ background: rgba(0, 0, 0, 0.5);
97
+ }
98
+
99
+ .zoom .download:hover {
100
+ background: rgba(0, 0, 0, 0.6);
101
+ cursor: pointer;
89
102
}
90
103
` ;
91
104
}
@@ -96,6 +109,12 @@ export class Thumbnail extends RapidElement {
96
109
@property ( { type : String } )
97
110
attachment : string ;
98
111
112
+ @property ( { type : Number } )
113
+ ratio : number = 0 ;
114
+
115
+ @property ( { type : Boolean } )
116
+ preview : boolean = true ;
117
+
99
118
@property ( { type : Boolean , attribute : false } )
100
119
zoom : boolean ;
101
120
@@ -107,6 +126,23 @@ export class Thumbnail extends RapidElement {
107
126
) : void {
108
127
super . updated ( changes ) ;
109
128
129
+ if (
130
+ changes . has ( 'contentType' ) &&
131
+ this . contentType === ThumbnailContentType . IMAGE
132
+ ) {
133
+ const toObserve = this . shadowRoot . querySelector ( '.observe' ) ;
134
+ if ( toObserve ) {
135
+ new ResizeObserver ( ( e , observer ) => {
136
+ if ( toObserve . clientHeight > 0 && toObserve . clientWidth > 0 ) {
137
+ this . ratio = toObserve . clientHeight / toObserve . clientWidth ;
138
+ this . preview =
139
+ this . ratio === 0 || ( this . ratio > 0.25 && this . ratio <= 1.5 ) ;
140
+ observer . disconnect ( ) ;
141
+ }
142
+ } ) . observe ( toObserve ) ;
143
+ }
144
+ }
145
+
110
146
// convert our attachment to a url and label
111
147
if ( changes . has ( 'attachment' ) ) {
112
148
if ( this . attachment ) {
@@ -134,7 +170,7 @@ export class Thumbnail extends RapidElement {
134
170
}
135
171
136
172
public handleThumbnailClicked ( ) {
137
- if ( this . contentType === ThumbnailContentType . IMAGE ) {
173
+ if ( this . contentType === ThumbnailContentType . IMAGE && this . preview ) {
138
174
window . setTimeout ( ( ) => {
139
175
const lightbox = document . querySelector ( 'temba-lightbox' ) as Lightbox ;
140
176
lightbox . showElement ( this ) ;
@@ -144,28 +180,36 @@ export class Thumbnail extends RapidElement {
144
180
}
145
181
}
146
182
183
+ public handleDownload ( e : Event ) {
184
+ e . stopPropagation ( ) ;
185
+ e . preventDefault ( ) ;
186
+
187
+ // open this.url in another tab
188
+ window . open ( this . url , '_blank' ) ;
189
+ }
190
+
147
191
public render ( ) {
148
192
return html `
149
193
< div
150
194
@click =${ this . handleThumbnailClicked . bind ( this ) }
151
195
class ="${ getClasses ( { wrapper : true , zoom : this . zoom } ) } "
152
196
url=${ this . url }
153
197
>
154
- < div
155
- class ="thumb ${ this . contentType } "
156
- style ="${ this . url
157
- ? styleMap ( {
158
- backgroundImage : `url(${ this . url } )`
159
- } )
160
- : '' } "
161
- >
162
- ${ this . contentType !== ThumbnailContentType . IMAGE
163
- ? html `< temba-icon
198
+ ${ this . contentType === ThumbnailContentType . IMAGE && this . preview
199
+ ? html `< div class =""> < div class ="download " @click =${ this . handleDownload . bind (
200
+ this
201
+ ) } > < temba-icon size ="1 " style ="color:#fff; " name ="download "> </ temba-icon > </ div > < img
202
+ class ="observe thumb ${ this . contentType } "
203
+ src ="${ this . url } "
204
+ > </ img > </ div > `
205
+ : html `< div
206
+ style ="padding:1em; background:rgba(0,0,0,.05);border-radius:var(--curvature); "
207
+ >
208
+ < temba-icon
164
209
size ="1.5 "
165
210
name ="${ ThumbnailIcons [ this . contentType ] } "
166
- > </ temba-icon > `
167
- : null }
168
- </ div >
211
+ > </ temba-icon >
212
+ </ div > ` }
169
213
</ div >
170
214
` ;
171
215
}
0 commit comments