From 2df4b352a191021d025aca444b9f372f848515b5 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 3 Nov 2022 20:16:56 -0400 Subject: [PATCH] fix: Hard to resize image larger than documen width immediately after uploading --- shared/editor/nodes/Image.tsx | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/shared/editor/nodes/Image.tsx b/shared/editor/nodes/Image.tsx index 944a58cd1..1cb7d72b0 100644 --- a/shared/editor/nodes/Image.tsx +++ b/shared/editor/nodes/Image.tsx @@ -599,6 +599,11 @@ const ImageComponent = ( const documentWidth = props.view?.dom.clientWidth; const maxWidth = layoutClass ? documentWidth / 3 : documentWidth; + const constrainWidth = (width: number) => { + const minWidth = documentWidth * 0.1; + return Math.round(Math.min(maxWidth, Math.max(width, minWidth))); + }; + const handlePointerMove = (event: PointerEvent) => { event.preventDefault(); @@ -610,12 +615,9 @@ const ImageComponent = ( } const grid = documentWidth / 10; - const minWidth = documentWidth * 0.1; const newWidth = sizeAtDragStart.width + diff * 2; const widthOnGrid = Math.round(newWidth / grid) * grid; - const constrainedWidth = Math.round( - Math.min(maxWidth, Math.max(widthOnGrid, minWidth)) - ); + const constrainedWidth = constrainWidth(widthOnGrid); const aspectRatio = naturalHeight / naturalWidth; setSize({ @@ -642,7 +644,10 @@ const ImageComponent = ( ) => { event.preventDefault(); event.stopPropagation(); - setSizeAtDragStart(size); + setSizeAtDragStart({ + width: constrainWidth(size.width), + height: size.height, + }); setOffset(event.pageX); setDragging(dragging); };