Skip to content

Commit abb8068

Browse files
authored
Merge pull request #6437 from railway17/han-fix-magnifying-glass
fix issue 6326: Chat - Magnifying glass displayed anywhere
2 parents 5e781be + 6d37102 commit abb8068

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

src/components/ImageView/index.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ImageView extends PureComponent {
1919
this.scrollableRef = null;
2020
this.canUseTouchScreen = canUseTouchScreen();
2121
this.state = {
22+
containerHeight: 0,
2223
isZoomed: false,
2324
isDragging: false,
2425
isMouseDown: false,
@@ -41,8 +42,7 @@ class ImageView extends PureComponent {
4142
return;
4243
}
4344
Image.getSize(this.props.url, (width, height) => {
44-
const scale = Math.max(this.props.windowWidth / width, this.props.windowHeight / height);
45-
this.setImageRegion(width, height, scale);
45+
this.setImageRegion(width, height);
4646
});
4747
document.addEventListener('mousemove', this.trackMovement.bind(this));
4848
}
@@ -59,9 +59,8 @@ class ImageView extends PureComponent {
5959
* When open image, set image left/right/top/bottom point and width, height
6060
* @param {Boolean} width image width
6161
* @param {Boolean} height image height
62-
* @param {Boolean} scale zoomscale when click zoom
6362
*/
64-
setImageRegion(width, height, scale) {
63+
setImageRegion(width, height) {
6564
let imgLeft = (this.props.windowWidth - width) / 2;
6665
let imgRight = ((this.props.windowWidth - width) / 2) + width;
6766
let imgTop = (this.props.windowHeight - height) / 2;
@@ -86,7 +85,7 @@ class ImageView extends PureComponent {
8685
}
8786

8887
this.setState({
89-
imgWidth: width, imgHeight: height, zoomScale: scale, imageLeft: imgLeft, imageTop: imgTop, imageRight: imgRight, imageBottom: imgBottom,
88+
imgWidth: width, imgHeight: height, imageLeft: imgLeft, imageTop: imgTop, imageRight: imgRight, imageBottom: imgBottom,
9089
});
9190
}
9291

@@ -162,19 +161,30 @@ class ImageView extends PureComponent {
162161
return (
163162
<View
164163
ref={el => this.scrollableRef = el}
164+
onLayout={(e) => {
165+
const {width, height} = e.nativeEvent.layout;
166+
const imageWidth = this.state.imgWidth;
167+
const imageHeight = this.state.imgHeight;
168+
const scale = imageHeight && imageWidth ? Math.min(width / imageWidth, height / imageHeight) : 0;
169+
this.setState({
170+
containerHeight: height,
171+
zoomScale: scale,
172+
});
173+
}}
165174
style={[
166175
styles.imageViewContainer,
167176
styles.overflowScroll,
168177
styles.noScrollbars,
178+
styles.pRelative,
169179
]}
170180
>
171181
<Pressable
172-
style={[
173-
styles.w100,
174-
styles.h100,
175-
styles.flex1,
176-
getZoomCursorStyle(this.state.isZoomed, this.state.isDragging),
177-
]}
182+
style={{
183+
...getZoomSizingStyle(this.state.isZoomed, this.state.imgWidth, this.state.imgHeight, this.state.zoomScale, this.state.containerHeight),
184+
...getZoomCursorStyle(this.state.isZoomed, this.state.isDragging),
185+
...this.state.isZoomed ? styles.pRelative : styles.pAbsolute,
186+
...styles.flex1,
187+
}}
178188
onPressIn={(e) => {
179189
const {pageX, pageY} = e.nativeEvent;
180190
this.setState({
@@ -212,7 +222,10 @@ class ImageView extends PureComponent {
212222
>
213223
<Image
214224
source={{uri: this.props.url}}
215-
style={getZoomSizingStyle(this.state.isZoomed, this.state.imgWidth, this.state.imgHeight, this.state.zoomScale)}
225+
style={[
226+
styles.h100,
227+
styles.w100,
228+
]}
216229
resizeMode={this.state.isZoomed ? 'contain' : 'center'}
217230
/>
218231
</Pressable>

src/styles/styles.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2337,18 +2337,30 @@ function getZoomCursorStyle(isZoomed, isDragging) {
23372337
* @param {Number} imgWidth
23382338
* @param {Number} imgHeight
23392339
* @param {Number} zoomScale
2340+
* @param {Number} containerHeight
23402341
* @return {Object}
23412342
*/
2342-
function getZoomSizingStyle(isZoomed, imgWidth, imgHeight, zoomScale) {
2343+
function getZoomSizingStyle(isZoomed, imgWidth, imgHeight, zoomScale, containerHeight) {
23432344
if (imgWidth === 0 || imgHeight === 0) {
23442345
return {
23452346
height: isZoomed ? '250%' : '100%',
23462347
width: isZoomed ? '250%' : '100%',
23472348
};
23482349
}
2350+
if (isZoomed) {
2351+
return {
2352+
height: `${(imgHeight * zoomScale)}px`,
2353+
width: `${(imgWidth * zoomScale)}px`,
2354+
};
2355+
}
2356+
2357+
const top = `${(containerHeight - imgHeight) / 2}px`;
2358+
const left = `calc(50% - ${imgWidth / 2}px)`;
23492359
return {
2350-
height: isZoomed ? `${(imgHeight * zoomScale)}px` : '100%',
2351-
width: isZoomed ? `${(imgWidth * zoomScale)}px` : '100%',
2360+
height: `${imgHeight}px`,
2361+
width: `${imgWidth}px`,
2362+
top,
2363+
left,
23522364
};
23532365
}
23542366

0 commit comments

Comments
 (0)