From faec098e305b0ddea7c584f0e2581d5cb317abe8 Mon Sep 17 00:00:00 2001 From: Aakansha Doshi Date: Wed, 7 Jun 2023 15:27:39 +0530 Subject: [PATCH] fix offset in editor --- src/element/textElement.ts | 19 +++++++++++++++++++ src/element/textWysiwyg.tsx | 15 +++++++++++++-- src/renderer/renderElement.ts | 24 ++++++------------------ 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/element/textElement.ts b/src/element/textElement.ts index 19c28aa0d..768ce0081 100644 --- a/src/element/textElement.ts +++ b/src/element/textElement.ts @@ -977,3 +977,22 @@ export const getDefaultLineHeight = (fontFamily: FontFamilyValues) => { } return DEFAULT_LINE_HEIGHT[DEFAULT_FONT_FAMILY]; }; + +export const getSpacesOffsetForLine = ( + element: ExcalidrawTextElement, + line: string, + font: FontString, +) => { + const container = getContainerElement(element); + const trailingSpacesWidth = + getLineWidth(line, font) - getLineWidth(line.trimEnd(), font); + const maxWidth = container ? getBoundTextMaxWidth(container) : element.width; + const availableWidth = maxWidth - getLineWidth(line.trimEnd(), font); + let spacesOffset = 0; + if (element.textAlign === TEXT_ALIGN.CENTER) { + spacesOffset = -Math.min(trailingSpacesWidth / 2, availableWidth / 2); + } else if (element.textAlign === TEXT_ALIGN.RIGHT) { + spacesOffset = -Math.min(availableWidth, trailingSpacesWidth); + } + return spacesOffset; +}; diff --git a/src/element/textWysiwyg.tsx b/src/element/textWysiwyg.tsx index a61d559f8..e23cc4127 100644 --- a/src/element/textWysiwyg.tsx +++ b/src/element/textWysiwyg.tsx @@ -11,7 +11,7 @@ import { isBoundToContainer, isTextElement, } from "./typeChecks"; -import { CLASSES, isSafari } from "../constants"; +import { CLASSES, TEXT_ALIGN, isSafari } from "../constants"; import { ExcalidrawElement, ExcalidrawLinearElement, @@ -196,6 +196,8 @@ export const textWysiwyg = ({ } maxWidth = getBoundTextMaxWidth(container); + textElementWidth = Math.min(textElementWidth, maxWidth); + maxHeight = getBoundTextMaxHeight( container, updatedTextElement as ExcalidrawTextElementWithContainer, @@ -230,7 +232,16 @@ export const textWysiwyg = ({ coordY = y; } } - const [viewportX, viewportY] = getViewportCoords(coordX, coordY); + let spacesOffset = 0; + if (updatedTextElement.textAlign === TEXT_ALIGN.CENTER) { + spacesOffset = Math.max(0, updatedTextElement.width / 2 - maxWidth / 2); + } else if (updatedTextElement.textAlign === TEXT_ALIGN.RIGHT) { + spacesOffset = Math.max(0, updatedTextElement.width - maxWidth); + } + const [viewportX, viewportY] = getViewportCoords( + coordX + spacesOffset, + coordY, + ); const initialSelectionStart = editable.selectionStart; const initialSelectionEnd = editable.selectionEnd; const initialLength = editable.value.length; diff --git a/src/renderer/renderElement.ts b/src/renderer/renderElement.ts index bb6f7c65e..76921bd50 100644 --- a/src/renderer/renderElement.ts +++ b/src/renderer/renderElement.ts @@ -48,6 +48,7 @@ import { getBoundTextMaxHeight, getBoundTextMaxWidth, getLineWidth, + getSpacesOffsetForLine, } from "../element/textElement"; import { LinearElementEditor } from "../element/linearElementEditor"; @@ -338,27 +339,14 @@ const drawElementOnCanvas = ( element.fontSize, element.lineHeight, ); - const container = getContainerElement(element); const verticalOffset = element.height - element.baseline; for (let index = 0; index < lines.length; index++) { - const trailingSpacesWidth = - getLineWidth(lines[index], font) - - getLineWidth(lines[index].trimEnd(), font); - const maxWidth = container - ? getBoundTextMaxWidth(container) - : element.width; - const availableWidth = - maxWidth - getLineWidth(lines[index].trimEnd(), font); - let spacesOffset = 0; - if (element.textAlign === TEXT_ALIGN.CENTER) { - spacesOffset = -Math.min( - trailingSpacesWidth / 2, - availableWidth / 2, - ); - } else if (element.textAlign === TEXT_ALIGN.RIGHT) { - spacesOffset = -Math.min(availableWidth, trailingSpacesWidth); - } + const spacesOffset = getSpacesOffsetForLine( + element, + lines[index], + font, + ); context.fillText( lines[index].trimEnd(), horizontalOffset + spacesOffset,