refactor: don't pass array to handleBindTextResize (#4826)

This commit is contained in:
Aakansha Doshi 2022-02-21 17:15:29 +05:30 committed by GitHub
parent f224e4d596
commit e203203993
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 55 deletions

View File

@ -602,7 +602,7 @@ export const resizeSingleElement = (
newSize: { width: resizedElement.width, height: resizedElement.height }, newSize: { width: resizedElement.width, height: resizedElement.height },
}); });
mutateElement(element, resizedElement); mutateElement(element, resizedElement);
handleBindTextResize([element], transformHandleDirection); handleBindTextResize(element, transformHandleDirection);
} }
}; };
@ -742,7 +742,7 @@ const resizeMultipleElements = (
fontSize: updates[index].fontSize, fontSize: updates[index].fontSize,
baseline: updates[index].baseline, baseline: updates[index].baseline,
}); });
handleBindTextResize([element], transformHandleType); handleBindTextResize(element, transformHandleType);
} }
}); });
} }

View File

@ -92,71 +92,69 @@ export const bindTextToShapeAfterDuplication = (
}; };
export const handleBindTextResize = ( export const handleBindTextResize = (
elements: readonly NonDeletedExcalidrawElement[], element: NonDeletedExcalidrawElement,
transformHandleType: MaybeTransformHandleType, transformHandleType: MaybeTransformHandleType,
) => { ) => {
elements.forEach((element) => { const boundTextElementId = getBoundTextElementId(element);
const boundTextElementId = getBoundTextElementId(element); if (boundTextElementId) {
if (boundTextElementId) { const textElement = Scene.getScene(element)!.getElement(
const textElement = Scene.getScene(element)!.getElement( boundTextElementId,
boundTextElementId, ) as ExcalidrawTextElement;
) as ExcalidrawTextElement; if (textElement && textElement.text) {
if (textElement && textElement.text) { if (!element) {
if (!element) { return;
return; }
} let text = textElement.text;
let text = textElement.text; let nextHeight = textElement.height;
let nextHeight = textElement.height; let containerHeight = element.height;
let containerHeight = element.height; let nextBaseLine = textElement.baseline;
let nextBaseLine = textElement.baseline; if (transformHandleType !== "n" && transformHandleType !== "s") {
if (transformHandleType !== "n" && transformHandleType !== "s") { if (text) {
if (text) { text = wrapText(
text = wrapText( textElement.originalText,
textElement.originalText,
getFontString(textElement),
element.width,
);
}
const dimensions = measureText(
text,
getFontString(textElement), getFontString(textElement),
element.width, element.width,
); );
nextHeight = dimensions.height;
nextBaseLine = dimensions.baseline;
}
// increase height in case text element height exceeds
if (nextHeight > element.height - BOUND_TEXT_PADDING * 2) {
containerHeight = nextHeight + BOUND_TEXT_PADDING * 2;
const diff = containerHeight - element.height;
// fix the y coord when resizing from ne/nw/n
const updatedY =
transformHandleType === "ne" ||
transformHandleType === "nw" ||
transformHandleType === "n"
? element.y - diff
: element.y;
mutateElement(element, {
height: containerHeight,
y: updatedY,
});
} }
const updatedY = element.y + containerHeight / 2 - nextHeight / 2; const dimensions = measureText(
mutateElement(textElement, {
text, text,
// preserve padding and set width correctly getFontString(textElement),
width: element.width - BOUND_TEXT_PADDING * 2, element.width,
height: nextHeight, );
x: element.x + BOUND_TEXT_PADDING, nextHeight = dimensions.height;
nextBaseLine = dimensions.baseline;
}
// increase height in case text element height exceeds
if (nextHeight > element.height - BOUND_TEXT_PADDING * 2) {
containerHeight = nextHeight + BOUND_TEXT_PADDING * 2;
const diff = containerHeight - element.height;
// fix the y coord when resizing from ne/nw/n
const updatedY =
transformHandleType === "ne" ||
transformHandleType === "nw" ||
transformHandleType === "n"
? element.y - diff
: element.y;
mutateElement(element, {
height: containerHeight,
y: updatedY, y: updatedY,
baseline: nextBaseLine,
}); });
} }
const updatedY = element.y + containerHeight / 2 - nextHeight / 2;
mutateElement(textElement, {
text,
// preserve padding and set width correctly
width: element.width - BOUND_TEXT_PADDING * 2,
height: nextHeight,
x: element.x + BOUND_TEXT_PADDING,
y: updatedY,
baseline: nextBaseLine,
});
} }
}); }
}; };
// https://github.com/grassator/canvas-text-editor/blob/master/lib/FontMetrics.js // https://github.com/grassator/canvas-text-editor/blob/master/lib/FontMetrics.js