From 539505affd68fdb75b12e65fb5b22dcad7040c60 Mon Sep 17 00:00:00 2001 From: David Luzar Date: Sun, 18 Dec 2022 23:06:01 +0100 Subject: [PATCH] fix: resize sometimes throwing on missing null-checks (#6013) --- src/element/resizeElements.ts | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/element/resizeElements.ts b/src/element/resizeElements.ts index 46e1c0d1..3782c232 100644 --- a/src/element/resizeElements.ts +++ b/src/element/resizeElements.ts @@ -162,11 +162,12 @@ const rotateSingleElement = ( mutateElement(element, { angle }); if (boundTextElementId) { - const textElement = Scene.getScene(element)!.getElement( - boundTextElementId, - ) as ExcalidrawTextElementWithContainer; + const textElement = + Scene.getScene(element)?.getElement( + boundTextElementId, + ); - if (!isArrowElement(element)) { + if (textElement && !isArrowElement(element)) { mutateElement(textElement, { angle }); } } @@ -201,8 +202,10 @@ const measureFontSizeFromWH = ( const hasContainer = isBoundToContainer(element); if (hasContainer) { - const container = getContainerElement(element)!; - width = getMaxContainerWidth(container); + const container = getContainerElement(element); + if (container) { + width = getMaxContainerWidth(container); + } } const nextFontSize = element.fontSize * (nextWidth / width); if (nextFontSize < MIN_FONT_SIZE) { @@ -211,7 +214,7 @@ const measureFontSizeFromWH = ( const metrics = measureText( element.text, getFontString({ fontSize: nextFontSize, fontFamily: element.fontFamily }), - hasContainer ? width : null, + element.containerId ? width : null, ); return { size: nextFontSize, @@ -765,10 +768,11 @@ const rotateMultipleElements = ( }); const boundTextElementId = getBoundTextElementId(element); if (boundTextElementId) { - const textElement = Scene.getScene(element)!.getElement( - boundTextElementId, - ) as ExcalidrawTextElementWithContainer; - if (!isArrowElement(element)) { + const textElement = + Scene.getScene(element)?.getElement( + boundTextElementId, + ); + if (textElement && !isArrowElement(element)) { mutateElement(textElement, { x: textElement.x + (rotatedCX - cx), y: textElement.y + (rotatedCY - cy),