From e4edda455541134d98a55e4995a297bc10a38fe9 Mon Sep 17 00:00:00 2001 From: zsviczian Date: Sat, 29 Jan 2022 14:27:03 +0100 Subject: [PATCH] fix: sceneCoordsToViewportCoords, jumping text when there is an offset (#4413) (#4630) Co-authored-by: Aakansha Doshi Co-authored-by: David Luzar Co-authored-by: thxnder --- src/element/textWysiwyg.tsx | 33 +++++++-------------------------- src/utils.ts | 4 ++-- 2 files changed, 9 insertions(+), 28 deletions(-) diff --git a/src/element/textWysiwyg.tsx b/src/element/textWysiwyg.tsx index b82513f6..53b56c42 100644 --- a/src/element/textWysiwyg.tsx +++ b/src/element/textWysiwyg.tsx @@ -45,17 +45,15 @@ const getTransform = ( maxWidth: number, maxHeight: number, ) => { - const { zoom, offsetTop, offsetLeft } = appState; + const { zoom } = appState; const degree = (180 * angle) / Math.PI; - // offsets must be multiplied by 2 to account for the division by 2 of - // the whole expression afterwards - let translateX = ((width - offsetLeft * 2) * (zoom.value - 1)) / 2; - let translateY = ((height - offsetTop * 2) * (zoom.value - 1)) / 2; + let translateX = (width * (zoom.value - 1)) / 2; + let translateY = (height * (zoom.value - 1)) / 2; if (width > maxWidth && zoom.value !== 1) { - translateX = (maxWidth / 2) * (zoom.value - 1); + translateX = (maxWidth * (zoom.value - 1)) / 2; } if (height > maxHeight && zoom.value !== 1) { - translateY = ((maxHeight - offsetTop * 2) * (zoom.value - 1)) / 2; + translateY = (maxHeight * (zoom.value - 1)) / 2; } return `translate(${translateX}px, ${translateY}px) scale(${zoom.value}) rotate(${degree}deg)`; }; @@ -173,29 +171,12 @@ export const textWysiwyg = ({ ? approxLineHeight : updatedElement.height / lines.length; if (!container) { - maxWidth = - (appState.offsetLeft + appState.width - viewportX - 8) / - appState.zoom.value - - // margin-right of parent if any - Number( - getComputedStyle( - excalidrawContainer?.parentNode as Element, - ).marginRight.slice(0, -2), - ); + maxWidth = (appState.width - 8 - viewportX) / appState.zoom.value; } // Make sure text editor height doesn't go beyond viewport const editorMaxHeight = - (appState.height - - viewportY - - // There is a ~14px difference which keeps on increasing - // with every zoom step when offset present hence I am subtracting it here - // However this is not the best fix and breaks in - // few scenarios - (appState.offsetTop - ? ((appState.zoom.value * 100 - 100) / 10) * 14 - : 0)) / - appState.zoom.value; + (appState.height - viewportY) / appState.zoom.value; const angle = container ? container.angle : updatedElement.angle; Object.assign(editable.style, { font: getFontString(updatedElement), diff --git a/src/utils.ts b/src/utils.ts index 6766de40..991bb0c4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -244,8 +244,8 @@ export const sceneCoordsToViewportCoords = ( scrollY: number; }, ) => { - const x = (sceneX + scrollX + offsetLeft) * zoom.value + zoom.translation.x; - const y = (sceneY + scrollY + offsetTop) * zoom.value + zoom.translation.y; + const x = (sceneX + scrollX) * zoom.value + zoom.translation.x + offsetLeft; + const y = (sceneY + scrollY) * zoom.value + zoom.translation.y + offsetTop; return { x, y }; };