From 66c92fc65a905133b0ab219691f5bfde63e257d2 Mon Sep 17 00:00:00 2001 From: Aakansha Doshi Date: Mon, 7 Feb 2022 19:54:39 +0530 Subject: [PATCH] feat: Make whole element clickable in view mode when it has hyperlink (#4735) * feat: Make whole element clickable in view mode when it has hyperlink * redirect to link if pointerup and pointer down is exactly same point * don't make element clickable in mobile --- src/components/App.tsx | 28 ++++++++++++++++++++-------- src/element/Hyperlink.tsx | 9 ++++++++- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index 1c60b8f7..8d2fc895 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -2366,16 +2366,24 @@ class App extends React.Component { } return ( element.link && - isPointHittingLinkIcon(element, this.state, [ - scenePointer.x, - scenePointer.y, - ]) && + isPointHittingLinkIcon( + element, + this.state, + [scenePointer.x, scenePointer.y], + this.isMobile, + ) && index <= hitElementIndex ); }); }; private redirectToLink = () => { + if ( + this.lastPointerDown!.clientX !== this.lastPointerUp!.clientX || + this.lastPointerDown!.clientY !== this.lastPointerUp!.clientY + ) { + return; + } const lastPointerDownCoords = viewportCoordsToSceneCoords( this.lastPointerDown!, this.state, @@ -2384,6 +2392,7 @@ class App extends React.Component { this.hitLinkElement!, this.state, [lastPointerDownCoords.x, lastPointerDownCoords.y], + this.isMobile, ); const lastPointerUpCoords = viewportCoordsToSceneCoords( this.lastPointerUp!, @@ -2393,6 +2402,7 @@ class App extends React.Component { this.hitLinkElement!, this.state, [lastPointerUpCoords.x, lastPointerUpCoords.y], + this.isMobile, ); if (lastPointerDownHittingLinkIcon && LastPointerUpHittingLinkIcon) { const url = this.hitLinkElement?.link; @@ -3238,10 +3248,12 @@ class App extends React.Component { if (pointerDownState.hit.element) { // Early return if pointer is hitting link icon if ( - isPointHittingLinkIcon(pointerDownState.hit.element, this.state, [ - pointerDownState.origin.x, - pointerDownState.origin.y, - ]) + isPointHittingLinkIcon( + pointerDownState.hit.element, + this.state, + [pointerDownState.origin.x, pointerDownState.origin.y], + this.isMobile, + ) ) { return false; } diff --git a/src/element/Hyperlink.tsx b/src/element/Hyperlink.tsx index e653ba61..36599d2c 100644 --- a/src/element/Hyperlink.tsx +++ b/src/element/Hyperlink.tsx @@ -306,9 +306,16 @@ export const isPointHittingLinkIcon = ( element: NonDeletedExcalidrawElement, appState: AppState, [x, y]: Point, + isMobile: boolean, ) => { const threshold = 4 / appState.zoom.value; - + if ( + !isMobile && + appState.viewModeEnabled && + isPointHittingElementBoundingBox(element, [x, y], threshold) + ) { + return true; + } const [x1, y1, x2, y2] = getElementAbsoluteCoords(element); const [linkX, linkY, linkWidth, linkHeight] = getLinkHandleFromCoords(