Skip to content

Commit

Permalink
add check if is image element
Browse files Browse the repository at this point in the history
  • Loading branch information
przemvs committed May 21, 2024
1 parent e74fb5f commit d9ac23e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/script/components/ZoomableImage/ZoomableImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import React, {HTMLProps, RefObject, useEffect, useRef, useState} from 'react';

import {containerStyle, imageStyle} from './ZoomableImage.style';

import {isHTMLImageElement} from '../../guards/HTMLElement';

type Offset = {
x: number;
y: number;
Expand Down Expand Up @@ -54,12 +56,9 @@ function calculateMaxOffset(containerRef: RefObject<HTMLDivElement>, imgRef: Ref

const containerRect = containerRef.current.getBoundingClientRect();

const maxXOffset = (containerRect.width - imgRef.current.naturalWidth) / 2;
const maxYOffset = (containerRect.height - imgRef.current.naturalHeight) / 2;

return {
maxXOffset: maxXOffset,
maxYOffset: maxYOffset,
maxXOffset: (containerRect.width - imgRef.current.naturalWidth) / 2,
maxYOffset: (containerRect.height - imgRef.current.naturalHeight) / 2,
};
}

Expand All @@ -82,7 +81,11 @@ export const ZoomableImage = (props: ZoomableImageProps) => {
const zoomScale = isZoomEnabled ? 1 : imageRatio;

const handleMouseClick = (event: React.MouseEvent<HTMLDivElement>) => {
const element = event.target as HTMLImageElement;
const element = event.target;

if (!isHTMLImageElement(element)) {
return;
}

if (!canZoomImage && !isZoomEnabled) {
return;
Expand Down
20 changes: 20 additions & 0 deletions src/script/guards/HTMLElement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*/

export const isHTMLImageElement = (element: any): element is HTMLImageElement => element.nodeName === 'IMG';

0 comments on commit d9ac23e

Please sign in to comment.