excalidraw/src/element/resizeElements.ts

887 lines
26 KiB
TypeScript
Raw Normal View History

import { BOUND_TEXT_PADDING, SHIFT_LOCKING_ANGLE } from "../constants";
import { rescalePoints } from "../points";
import {
rotate,
adjustXYWithRotation,
centerPoint,
rotatePoint,
} from "../math";
import {
ExcalidrawLinearElement,
ExcalidrawTextElement,
NonDeletedExcalidrawElement,
NonDeleted,
ExcalidrawElement,
} from "./types";
import {
getElementAbsoluteCoords,
getCommonBounds,
getResizedElementAbsoluteCoords,
getCommonBoundingBox,
} from "./bounds";
import {
isFreeDrawElement,
isLinearElement,
isTextElement,
} from "./typeChecks";
import { mutateElement } from "./mutateElement";
import { getPerfectElementSize } from "./sizeHelpers";
feat: bind text to shapes when pressing enter and support sticky notes 🎉 (#4343) * feat: Word wrap inside rect and increase height when size exceeded * fixes for auto increase in height * fix height * respect newlines when wrapping text * shift text area when height increases beyond mid rect height until it reaches to the top * select bound text if present when rect selected * mutate y coord after text submit * Add padding of 30px and update dimensions acordingly * Don't allow selecting bound text element directly * support deletion of bound text element when rect deleted * trim text * Support autoshrink and improve algo * calculate approx line height instead of hardcoding * use textContainerId instead of storing textContainer element itself * rename boundTextElement -> boundTextElementId * fix text properties not getting reflected after edit inside rect * Support resizing * remove ts ignore * increase height of container when text height increases while resizing * use original text when editing/resizing so it adjusts based on original text * fix tests * add util isRectangleElement * use isTextElement util everywhere * disable selecting text inside rect when selectAll * Bind text to circle and diamond as well * fix tests * vertically center align the text always * better vertical align * Disable binding arrows for text inside shapes * set min width for text container when text is binded to container * update dimensions of container if its less than min width/ min height * Allow selecting of text container for transparent containers when clicked inside * fix test * preserve whitespaces between long word exceeding width and next word Use word break instead of whitespace no wrap for better readability and support safari * Perf improvements for measuring text width and resizing * Use canvas measureText instead of our algo. This has reduced the perf ~ 10 times * Rewrite wrapText algo to break in words appropriately and for longer words calculate the char width in order unless max width reached. This makes the the number of runs linear (max text length times) which was earlier textLength * textLength-1/2 as I was slicing the chars from end until max width reached for each run * Add a util to calculate getApproxCharsToFitInWidth to calculate min chars to fit in a line * use console.info so eslint doesnt warn :p * cache char width and don't call resize unless min width exceeded * update line height and height correctly when text properties inside container updated * improve vertical centering when text properties updated, not yet perfect though * when double clicked inside a conatiner take the cursor to end of text same as what happens when enter is pressed * Add hint when container selected * Select container when escape key is pressed after submitting text * fix copy/paste when using copy/paste action * fix copy when dragged with alt pressed * fix export to svg/png * fix add to library * Fix copy as png/svg * Don't allow selecting text when using selection tool and support resizing when multiple elements include ones with binded text selectec * fix rotation jump * moove all text utils to textElement.ts * resize text element only after container resized so that width doesnt change when editing * insert the remaining chars for long words once it goes beyond line * fix typo, use string for character type * renaming * fix bugs in word wrap algo * make grouping work * set boundTextElementId only when text present else unset it * rename textContainerId to containerId * fix * fix snap * use originalText in redrawTextBoundingBox so height is calculated properly and center align works after props updated * use boundElementIds and also support binding text in images 🎉 * fix the sw/se ends when resizing from ne/nw * fix y coord when resizing from north * bind when enter is pressed, double click/text tool willl edit the binded text if present else create a new text * bind when clicked on center of container * use pre-wrap instead of normal so it works in ff * use container boundTextElement when container present and trying to edit text * review fixes * make getBoundTextElementId type safe and check for existence when using this function * fix * don't duplicate boundElementIds when text submitted * only remove last trailing space if present which we have added when joining words * set width correctly when resizing to fix alignment issues * make duplication work using cmd/ctrl+d * set X coord correctly during resize * don't allow resize to negative dimensions when text is bounded to container * fix, check last char is space * remove logs * make sure text editor doesn't go beyond viewport and set container dimensions in case it overflows * add a util isTextBindableContainer to check if the container could bind text
2021-12-16 21:14:03 +05:30
import { getFontString } from "../utils";
Allow binding linear elements to other elements (#1899) * Refactor: simplify linear element type * Refactor: dedupe scrollbar handling * First step towards binding - establish relationship and basic test for dragged lines * Refactor: use zoom from appstate * Refactor: generalize getElementAtPosition * Only consider bindable elements in hit test * Refactor: pull out pieces of hit test for reuse later * Refactor: pull out diamond from hit test for reuse later * Refactor: pull out text from hit test for reuse later * Suggest binding when hovering * Give shapes in regression test real size * Give shapes in undo/redo test real size * Keep bound element highlighted * Show binding suggestion for multi-point elements * Move binding to its on module with functions so that I can use it from actions, add support for binding end of multi-point elements * Use Id instead of ID * Improve boundary offset for non-squarish elements * Fix localStorage for binding on linear elements * Simplify dragging code and fix elements bound twice to the same shape * Fix binding for rectangles * Bind both ends at the end of the linear element creation, needed for focus points * wip * Refactor: Renames and reshapes for next commit * Calculate and store focus points and gaps, but dont use them yet * Focus points for rectangles * Dont blow up when canceling linear element * Stop suggesting binding when a non-compatible tool is selected * Clean up collision code * Using Geometric Algebra for hit tests * Correct binding for all shapes * Constant gap around polygon corners * Fix rotation handling * Generalize update and fix hit test for rotated elements * Handle rotation realtime * Handle scaling * Remove vibration when moving bound and binding element together * Handle simultenous scaling * Allow binding and unbinding when editing linear elements * Dont delete binding when the end point wasnt touched * Bind on enter/escape when editing * Support multiple suggested bindable elements in preparation for supporting linear elements dragging * Update binding when moving linear elements * Update binding when resizing linear elements * Dont re-render UI on binding hints * Update both ends when one is moved * Use distance instead of focus point for binding * Complicated approach for posterity, ignore this commit * Revert the complicated approach * Better focus point strategy, working for all shapes * Update snapshots * Dont break binding gap when mirroring shape * Dont break binding gap when grid mode pushes it inside * Dont bind draw elements * Support alt duplication * Fix alt duplication to * Support cmd+D duplication * All copy mechanisms are supported * Allow binding shapes to arrows, having arrows created first * Prevent arrows from disappearing for ellipses * Better binding suggestion highlight for shapes * Dont suggest second binding for simple elements when editing or moving them * Dont steal already bound linear elements when moving shapes * Fix highlighting diamonds and more precisely highlight other shapes * Highlight linear element edges for binding * Highlight text binding too * Handle deletion * Dont suggest second binding for simple linear elements when creating them * Dont highlight bound element during creation * Fix binding for rotated linear elements * Fix collision check for ellipses * Dont show suggested bindings for selected pairs * Bind multi-point linear elements when the tool is switched - important for mobile * Handle unbinding one of two bound edges correctly * Rename boundElement in state to startBoundElement * Dont double account for zoom when rendering binding highlight * Fix rendering of edited linear element point handles * Suggest binding when adding new point to a linear element * Bind when adding a new point to a linear element and dont unbind when moving middle elements * Handle deleting points * Add cmd modifier key to disable binding * Use state for enabling binding, fix not binding for linear elements during creation * Drop support for binding lines, only arrows are bindable * Reset binding mode on blur * Fix not binding lines
2020-08-08 21:04:15 -07:00
import { updateBoundElements } from "./binding";
2020-08-10 14:16:39 +02:00
import {
TransformHandleType,
MaybeTransformHandleType,
TransformHandleDirection,
2020-08-10 14:16:39 +02:00
} from "./transformHandles";
import { Point, PointerDownState } from "../types";
feat: bind text to shapes when pressing enter and support sticky notes 🎉 (#4343) * feat: Word wrap inside rect and increase height when size exceeded * fixes for auto increase in height * fix height * respect newlines when wrapping text * shift text area when height increases beyond mid rect height until it reaches to the top * select bound text if present when rect selected * mutate y coord after text submit * Add padding of 30px and update dimensions acordingly * Don't allow selecting bound text element directly * support deletion of bound text element when rect deleted * trim text * Support autoshrink and improve algo * calculate approx line height instead of hardcoding * use textContainerId instead of storing textContainer element itself * rename boundTextElement -> boundTextElementId * fix text properties not getting reflected after edit inside rect * Support resizing * remove ts ignore * increase height of container when text height increases while resizing * use original text when editing/resizing so it adjusts based on original text * fix tests * add util isRectangleElement * use isTextElement util everywhere * disable selecting text inside rect when selectAll * Bind text to circle and diamond as well * fix tests * vertically center align the text always * better vertical align * Disable binding arrows for text inside shapes * set min width for text container when text is binded to container * update dimensions of container if its less than min width/ min height * Allow selecting of text container for transparent containers when clicked inside * fix test * preserve whitespaces between long word exceeding width and next word Use word break instead of whitespace no wrap for better readability and support safari * Perf improvements for measuring text width and resizing * Use canvas measureText instead of our algo. This has reduced the perf ~ 10 times * Rewrite wrapText algo to break in words appropriately and for longer words calculate the char width in order unless max width reached. This makes the the number of runs linear (max text length times) which was earlier textLength * textLength-1/2 as I was slicing the chars from end until max width reached for each run * Add a util to calculate getApproxCharsToFitInWidth to calculate min chars to fit in a line * use console.info so eslint doesnt warn :p * cache char width and don't call resize unless min width exceeded * update line height and height correctly when text properties inside container updated * improve vertical centering when text properties updated, not yet perfect though * when double clicked inside a conatiner take the cursor to end of text same as what happens when enter is pressed * Add hint when container selected * Select container when escape key is pressed after submitting text * fix copy/paste when using copy/paste action * fix copy when dragged with alt pressed * fix export to svg/png * fix add to library * Fix copy as png/svg * Don't allow selecting text when using selection tool and support resizing when multiple elements include ones with binded text selectec * fix rotation jump * moove all text utils to textElement.ts * resize text element only after container resized so that width doesnt change when editing * insert the remaining chars for long words once it goes beyond line * fix typo, use string for character type * renaming * fix bugs in word wrap algo * make grouping work * set boundTextElementId only when text present else unset it * rename textContainerId to containerId * fix * fix snap * use originalText in redrawTextBoundingBox so height is calculated properly and center align works after props updated * use boundElementIds and also support binding text in images 🎉 * fix the sw/se ends when resizing from ne/nw * fix y coord when resizing from north * bind when enter is pressed, double click/text tool willl edit the binded text if present else create a new text * bind when clicked on center of container * use pre-wrap instead of normal so it works in ff * use container boundTextElement when container present and trying to edit text * review fixes * make getBoundTextElementId type safe and check for existence when using this function * fix * don't duplicate boundElementIds when text submitted * only remove last trailing space if present which we have added when joining words * set width correctly when resizing to fix alignment issues * make duplication work using cmd/ctrl+d * set X coord correctly during resize * don't allow resize to negative dimensions when text is bounded to container * fix, check last char is space * remove logs * make sure text editor doesn't go beyond viewport and set container dimensions in case it overflows * add a util isTextBindableContainer to check if the container could bind text
2021-12-16 21:14:03 +05:30
import Scene from "../scene/Scene";
import {
getApproxMinLineHeight,
feat: bind text to shapes when pressing enter and support sticky notes 🎉 (#4343) * feat: Word wrap inside rect and increase height when size exceeded * fixes for auto increase in height * fix height * respect newlines when wrapping text * shift text area when height increases beyond mid rect height until it reaches to the top * select bound text if present when rect selected * mutate y coord after text submit * Add padding of 30px and update dimensions acordingly * Don't allow selecting bound text element directly * support deletion of bound text element when rect deleted * trim text * Support autoshrink and improve algo * calculate approx line height instead of hardcoding * use textContainerId instead of storing textContainer element itself * rename boundTextElement -> boundTextElementId * fix text properties not getting reflected after edit inside rect * Support resizing * remove ts ignore * increase height of container when text height increases while resizing * use original text when editing/resizing so it adjusts based on original text * fix tests * add util isRectangleElement * use isTextElement util everywhere * disable selecting text inside rect when selectAll * Bind text to circle and diamond as well * fix tests * vertically center align the text always * better vertical align * Disable binding arrows for text inside shapes * set min width for text container when text is binded to container * update dimensions of container if its less than min width/ min height * Allow selecting of text container for transparent containers when clicked inside * fix test * preserve whitespaces between long word exceeding width and next word Use word break instead of whitespace no wrap for better readability and support safari * Perf improvements for measuring text width and resizing * Use canvas measureText instead of our algo. This has reduced the perf ~ 10 times * Rewrite wrapText algo to break in words appropriately and for longer words calculate the char width in order unless max width reached. This makes the the number of runs linear (max text length times) which was earlier textLength * textLength-1/2 as I was slicing the chars from end until max width reached for each run * Add a util to calculate getApproxCharsToFitInWidth to calculate min chars to fit in a line * use console.info so eslint doesnt warn :p * cache char width and don't call resize unless min width exceeded * update line height and height correctly when text properties inside container updated * improve vertical centering when text properties updated, not yet perfect though * when double clicked inside a conatiner take the cursor to end of text same as what happens when enter is pressed * Add hint when container selected * Select container when escape key is pressed after submitting text * fix copy/paste when using copy/paste action * fix copy when dragged with alt pressed * fix export to svg/png * fix add to library * Fix copy as png/svg * Don't allow selecting text when using selection tool and support resizing when multiple elements include ones with binded text selectec * fix rotation jump * moove all text utils to textElement.ts * resize text element only after container resized so that width doesnt change when editing * insert the remaining chars for long words once it goes beyond line * fix typo, use string for character type * renaming * fix bugs in word wrap algo * make grouping work * set boundTextElementId only when text present else unset it * rename textContainerId to containerId * fix * fix snap * use originalText in redrawTextBoundingBox so height is calculated properly and center align works after props updated * use boundElementIds and also support binding text in images 🎉 * fix the sw/se ends when resizing from ne/nw * fix y coord when resizing from north * bind when enter is pressed, double click/text tool willl edit the binded text if present else create a new text * bind when clicked on center of container * use pre-wrap instead of normal so it works in ff * use container boundTextElement when container present and trying to edit text * review fixes * make getBoundTextElementId type safe and check for existence when using this function * fix * don't duplicate boundElementIds when text submitted * only remove last trailing space if present which we have added when joining words * set width correctly when resizing to fix alignment issues * make duplication work using cmd/ctrl+d * set X coord correctly during resize * don't allow resize to negative dimensions when text is bounded to container * fix, check last char is space * remove logs * make sure text editor doesn't go beyond viewport and set container dimensions in case it overflows * add a util isTextBindableContainer to check if the container could bind text
2021-12-16 21:14:03 +05:30
getApproxMinLineWidth,
getBoundTextElement,
feat: bind text to shapes when pressing enter and support sticky notes 🎉 (#4343) * feat: Word wrap inside rect and increase height when size exceeded * fixes for auto increase in height * fix height * respect newlines when wrapping text * shift text area when height increases beyond mid rect height until it reaches to the top * select bound text if present when rect selected * mutate y coord after text submit * Add padding of 30px and update dimensions acordingly * Don't allow selecting bound text element directly * support deletion of bound text element when rect deleted * trim text * Support autoshrink and improve algo * calculate approx line height instead of hardcoding * use textContainerId instead of storing textContainer element itself * rename boundTextElement -> boundTextElementId * fix text properties not getting reflected after edit inside rect * Support resizing * remove ts ignore * increase height of container when text height increases while resizing * use original text when editing/resizing so it adjusts based on original text * fix tests * add util isRectangleElement * use isTextElement util everywhere * disable selecting text inside rect when selectAll * Bind text to circle and diamond as well * fix tests * vertically center align the text always * better vertical align * Disable binding arrows for text inside shapes * set min width for text container when text is binded to container * update dimensions of container if its less than min width/ min height * Allow selecting of text container for transparent containers when clicked inside * fix test * preserve whitespaces between long word exceeding width and next word Use word break instead of whitespace no wrap for better readability and support safari * Perf improvements for measuring text width and resizing * Use canvas measureText instead of our algo. This has reduced the perf ~ 10 times * Rewrite wrapText algo to break in words appropriately and for longer words calculate the char width in order unless max width reached. This makes the the number of runs linear (max text length times) which was earlier textLength * textLength-1/2 as I was slicing the chars from end until max width reached for each run * Add a util to calculate getApproxCharsToFitInWidth to calculate min chars to fit in a line * use console.info so eslint doesnt warn :p * cache char width and don't call resize unless min width exceeded * update line height and height correctly when text properties inside container updated * improve vertical centering when text properties updated, not yet perfect though * when double clicked inside a conatiner take the cursor to end of text same as what happens when enter is pressed * Add hint when container selected * Select container when escape key is pressed after submitting text * fix copy/paste when using copy/paste action * fix copy when dragged with alt pressed * fix export to svg/png * fix add to library * Fix copy as png/svg * Don't allow selecting text when using selection tool and support resizing when multiple elements include ones with binded text selectec * fix rotation jump * moove all text utils to textElement.ts * resize text element only after container resized so that width doesnt change when editing * insert the remaining chars for long words once it goes beyond line * fix typo, use string for character type * renaming * fix bugs in word wrap algo * make grouping work * set boundTextElementId only when text present else unset it * rename textContainerId to containerId * fix * fix snap * use originalText in redrawTextBoundingBox so height is calculated properly and center align works after props updated * use boundElementIds and also support binding text in images 🎉 * fix the sw/se ends when resizing from ne/nw * fix y coord when resizing from north * bind when enter is pressed, double click/text tool willl edit the binded text if present else create a new text * bind when clicked on center of container * use pre-wrap instead of normal so it works in ff * use container boundTextElement when container present and trying to edit text * review fixes * make getBoundTextElementId type safe and check for existence when using this function * fix * don't duplicate boundElementIds when text submitted * only remove last trailing space if present which we have added when joining words * set width correctly when resizing to fix alignment issues * make duplication work using cmd/ctrl+d * set X coord correctly during resize * don't allow resize to negative dimensions when text is bounded to container * fix, check last char is space * remove logs * make sure text editor doesn't go beyond viewport and set container dimensions in case it overflows * add a util isTextBindableContainer to check if the container could bind text
2021-12-16 21:14:03 +05:30
getBoundTextElementId,
handleBindTextResize,
measureText,
} from "./textElement";
export const normalizeAngle = (angle: number): number => {
if (angle >= 2 * Math.PI) {
return angle - 2 * Math.PI;
}
return angle;
};
// Returns true when transform (resizing/rotation) happened
export const transformElements = (
pointerDownState: PointerDownState,
2020-08-10 14:16:39 +02:00
transformHandleType: MaybeTransformHandleType,
selectedElements: readonly NonDeletedExcalidrawElement[],
resizeArrowDirection: "origin" | "end",
shouldRotateWithDiscreteAngle: boolean,
shouldResizeFromCenter: boolean,
shouldMaintainAspectRatio: boolean,
pointerX: number,
pointerY: number,
centerX: number,
centerY: number,
) => {
if (selectedElements.length === 1) {
const [element] = selectedElements;
2020-08-10 14:16:39 +02:00
if (transformHandleType === "rotation") {
rotateSingleElement(
element,
pointerX,
pointerY,
shouldRotateWithDiscreteAngle,
);
Allow binding linear elements to other elements (#1899) * Refactor: simplify linear element type * Refactor: dedupe scrollbar handling * First step towards binding - establish relationship and basic test for dragged lines * Refactor: use zoom from appstate * Refactor: generalize getElementAtPosition * Only consider bindable elements in hit test * Refactor: pull out pieces of hit test for reuse later * Refactor: pull out diamond from hit test for reuse later * Refactor: pull out text from hit test for reuse later * Suggest binding when hovering * Give shapes in regression test real size * Give shapes in undo/redo test real size * Keep bound element highlighted * Show binding suggestion for multi-point elements * Move binding to its on module with functions so that I can use it from actions, add support for binding end of multi-point elements * Use Id instead of ID * Improve boundary offset for non-squarish elements * Fix localStorage for binding on linear elements * Simplify dragging code and fix elements bound twice to the same shape * Fix binding for rectangles * Bind both ends at the end of the linear element creation, needed for focus points * wip * Refactor: Renames and reshapes for next commit * Calculate and store focus points and gaps, but dont use them yet * Focus points for rectangles * Dont blow up when canceling linear element * Stop suggesting binding when a non-compatible tool is selected * Clean up collision code * Using Geometric Algebra for hit tests * Correct binding for all shapes * Constant gap around polygon corners * Fix rotation handling * Generalize update and fix hit test for rotated elements * Handle rotation realtime * Handle scaling * Remove vibration when moving bound and binding element together * Handle simultenous scaling * Allow binding and unbinding when editing linear elements * Dont delete binding when the end point wasnt touched * Bind on enter/escape when editing * Support multiple suggested bindable elements in preparation for supporting linear elements dragging * Update binding when moving linear elements * Update binding when resizing linear elements * Dont re-render UI on binding hints * Update both ends when one is moved * Use distance instead of focus point for binding * Complicated approach for posterity, ignore this commit * Revert the complicated approach * Better focus point strategy, working for all shapes * Update snapshots * Dont break binding gap when mirroring shape * Dont break binding gap when grid mode pushes it inside * Dont bind draw elements * Support alt duplication * Fix alt duplication to * Support cmd+D duplication * All copy mechanisms are supported * Allow binding shapes to arrows, having arrows created first * Prevent arrows from disappearing for ellipses * Better binding suggestion highlight for shapes * Dont suggest second binding for simple elements when editing or moving them * Dont steal already bound linear elements when moving shapes * Fix highlighting diamonds and more precisely highlight other shapes * Highlight linear element edges for binding * Highlight text binding too * Handle deletion * Dont suggest second binding for simple linear elements when creating them * Dont highlight bound element during creation * Fix binding for rotated linear elements * Fix collision check for ellipses * Dont show suggested bindings for selected pairs * Bind multi-point linear elements when the tool is switched - important for mobile * Handle unbinding one of two bound edges correctly * Rename boundElement in state to startBoundElement * Dont double account for zoom when rendering binding highlight * Fix rendering of edited linear element point handles * Suggest binding when adding new point to a linear element * Bind when adding a new point to a linear element and dont unbind when moving middle elements * Handle deleting points * Add cmd modifier key to disable binding * Use state for enabling binding, fix not binding for linear elements during creation * Drop support for binding lines, only arrows are bindable * Reset binding mode on blur * Fix not binding lines
2020-08-08 21:04:15 -07:00
updateBoundElements(element);
} else if (
isLinearElement(element) &&
element.points.length === 2 &&
2020-08-10 14:16:39 +02:00
(transformHandleType === "nw" ||
transformHandleType === "ne" ||
transformHandleType === "sw" ||
transformHandleType === "se")
) {
reshapeSingleTwoPointElement(
element,
resizeArrowDirection,
shouldRotateWithDiscreteAngle,
pointerX,
pointerY,
);
} else if (
isTextElement(element) &&
2020-08-10 14:16:39 +02:00
(transformHandleType === "nw" ||
transformHandleType === "ne" ||
transformHandleType === "sw" ||
transformHandleType === "se")
) {
resizeSingleTextElement(
element,
2020-08-10 14:16:39 +02:00
transformHandleType,
shouldResizeFromCenter,
pointerX,
pointerY,
);
updateBoundElements(element);
2020-08-10 14:16:39 +02:00
} else if (transformHandleType) {
resizeSingleElement(
pointerDownState.originalElements,
shouldMaintainAspectRatio,
element,
transformHandleType,
shouldResizeFromCenter,
pointerX,
pointerY,
);
}
return true;
} else if (selectedElements.length > 1) {
2020-08-10 14:16:39 +02:00
if (transformHandleType === "rotation") {
rotateMultipleElements(
pointerDownState,
selectedElements,
pointerX,
pointerY,
shouldRotateWithDiscreteAngle,
centerX,
centerY,
);
return true;
} else if (
2020-08-10 14:16:39 +02:00
transformHandleType === "nw" ||
transformHandleType === "ne" ||
transformHandleType === "sw" ||
transformHandleType === "se"
) {
resizeMultipleElements(
pointerDownState,
selectedElements,
2020-08-10 14:16:39 +02:00
transformHandleType,
shouldResizeFromCenter,
pointerX,
pointerY,
);
return true;
}
}
return false;
};
const rotateSingleElement = (
element: NonDeletedExcalidrawElement,
pointerX: number,
pointerY: number,
shouldRotateWithDiscreteAngle: boolean,
) => {
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
const cx = (x1 + x2) / 2;
const cy = (y1 + y2) / 2;
let angle = (5 * Math.PI) / 2 + Math.atan2(pointerY - cy, pointerX - cx);
if (shouldRotateWithDiscreteAngle) {
angle += SHIFT_LOCKING_ANGLE / 2;
angle -= angle % SHIFT_LOCKING_ANGLE;
}
angle = normalizeAngle(angle);
mutateElement(element, { angle });
feat: bind text to shapes when pressing enter and support sticky notes 🎉 (#4343) * feat: Word wrap inside rect and increase height when size exceeded * fixes for auto increase in height * fix height * respect newlines when wrapping text * shift text area when height increases beyond mid rect height until it reaches to the top * select bound text if present when rect selected * mutate y coord after text submit * Add padding of 30px and update dimensions acordingly * Don't allow selecting bound text element directly * support deletion of bound text element when rect deleted * trim text * Support autoshrink and improve algo * calculate approx line height instead of hardcoding * use textContainerId instead of storing textContainer element itself * rename boundTextElement -> boundTextElementId * fix text properties not getting reflected after edit inside rect * Support resizing * remove ts ignore * increase height of container when text height increases while resizing * use original text when editing/resizing so it adjusts based on original text * fix tests * add util isRectangleElement * use isTextElement util everywhere * disable selecting text inside rect when selectAll * Bind text to circle and diamond as well * fix tests * vertically center align the text always * better vertical align * Disable binding arrows for text inside shapes * set min width for text container when text is binded to container * update dimensions of container if its less than min width/ min height * Allow selecting of text container for transparent containers when clicked inside * fix test * preserve whitespaces between long word exceeding width and next word Use word break instead of whitespace no wrap for better readability and support safari * Perf improvements for measuring text width and resizing * Use canvas measureText instead of our algo. This has reduced the perf ~ 10 times * Rewrite wrapText algo to break in words appropriately and for longer words calculate the char width in order unless max width reached. This makes the the number of runs linear (max text length times) which was earlier textLength * textLength-1/2 as I was slicing the chars from end until max width reached for each run * Add a util to calculate getApproxCharsToFitInWidth to calculate min chars to fit in a line * use console.info so eslint doesnt warn :p * cache char width and don't call resize unless min width exceeded * update line height and height correctly when text properties inside container updated * improve vertical centering when text properties updated, not yet perfect though * when double clicked inside a conatiner take the cursor to end of text same as what happens when enter is pressed * Add hint when container selected * Select container when escape key is pressed after submitting text * fix copy/paste when using copy/paste action * fix copy when dragged with alt pressed * fix export to svg/png * fix add to library * Fix copy as png/svg * Don't allow selecting text when using selection tool and support resizing when multiple elements include ones with binded text selectec * fix rotation jump * moove all text utils to textElement.ts * resize text element only after container resized so that width doesnt change when editing * insert the remaining chars for long words once it goes beyond line * fix typo, use string for character type * renaming * fix bugs in word wrap algo * make grouping work * set boundTextElementId only when text present else unset it * rename textContainerId to containerId * fix * fix snap * use originalText in redrawTextBoundingBox so height is calculated properly and center align works after props updated * use boundElementIds and also support binding text in images 🎉 * fix the sw/se ends when resizing from ne/nw * fix y coord when resizing from north * bind when enter is pressed, double click/text tool willl edit the binded text if present else create a new text * bind when clicked on center of container * use pre-wrap instead of normal so it works in ff * use container boundTextElement when container present and trying to edit text * review fixes * make getBoundTextElementId type safe and check for existence when using this function * fix * don't duplicate boundElementIds when text submitted * only remove last trailing space if present which we have added when joining words * set width correctly when resizing to fix alignment issues * make duplication work using cmd/ctrl+d * set X coord correctly during resize * don't allow resize to negative dimensions when text is bounded to container * fix, check last char is space * remove logs * make sure text editor doesn't go beyond viewport and set container dimensions in case it overflows * add a util isTextBindableContainer to check if the container could bind text
2021-12-16 21:14:03 +05:30
const boundTextElementId = getBoundTextElementId(element);
if (boundTextElementId) {
const textElement = Scene.getScene(element)!.getElement(boundTextElementId);
mutateElement(textElement!, { angle });
}
};
// used in DEV only
const validateTwoPointElementNormalized = (
element: NonDeleted<ExcalidrawLinearElement>,
) => {
if (
element.points.length !== 2 ||
element.points[0][0] !== 0 ||
element.points[0][1] !== 0 ||
Math.abs(element.points[1][0]) !== element.width ||
Math.abs(element.points[1][1]) !== element.height
) {
throw new Error("Two-point element is not normalized");
}
};
const getPerfectElementSizeWithRotation = (
elementType: ExcalidrawElement["type"],
width: number,
height: number,
angle: number,
): [number, number] => {
const size = getPerfectElementSize(
elementType,
...rotate(width, height, 0, 0, angle),
);
return rotate(size.width, size.height, 0, 0, -angle);
};
export const reshapeSingleTwoPointElement = (
element: NonDeleted<ExcalidrawLinearElement>,
resizeArrowDirection: "origin" | "end",
shouldRotateWithDiscreteAngle: boolean,
pointerX: number,
pointerY: number,
) => {
if (process.env.NODE_ENV !== "production") {
validateTwoPointElementNormalized(element);
}
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
const cx = (x1 + x2) / 2;
const cy = (y1 + y2) / 2;
// rotation pointer with reverse angle
const [rotatedX, rotatedY] = rotate(
pointerX,
pointerY,
cx,
cy,
-element.angle,
);
let [width, height] =
resizeArrowDirection === "end"
? [rotatedX - element.x, rotatedY - element.y]
: [
element.x + element.points[1][0] - rotatedX,
element.y + element.points[1][1] - rotatedY,
];
if (shouldRotateWithDiscreteAngle) {
[width, height] = getPerfectElementSizeWithRotation(
element.type,
width,
height,
element.angle,
);
}
const [nextElementX, nextElementY] = adjustXYWithRotation(
resizeArrowDirection === "end"
? { s: true, e: true }
: { n: true, w: true },
element.x,
element.y,
element.angle,
0,
0,
(element.points[1][0] - width) / 2,
(element.points[1][1] - height) / 2,
);
mutateElement(element, {
x: nextElementX,
y: nextElementY,
points: [
[0, 0],
[width, height],
],
});
};
const rescalePointsInElement = (
element: NonDeletedExcalidrawElement,
width: number,
height: number,
normalizePoints: boolean,
) =>
isLinearElement(element) || isFreeDrawElement(element)
? {
points: rescalePoints(
0,
width,
rescalePoints(1, height, element.points, normalizePoints),
normalizePoints,
),
}
: {};
const MIN_FONT_SIZE = 1;
const measureFontSizeFromWH = (
element: NonDeleted<ExcalidrawTextElement>,
nextWidth: number,
nextHeight: number,
): { size: number; baseline: number } | null => {
// We only use width to scale font on resize
const nextFontSize = element.fontSize * (nextWidth / element.width);
if (nextFontSize < MIN_FONT_SIZE) {
return null;
}
const metrics = measureText(
element.text,
getFontString({ fontSize: nextFontSize, fontFamily: element.fontFamily }),
feat: bind text to shapes when pressing enter and support sticky notes 🎉 (#4343) * feat: Word wrap inside rect and increase height when size exceeded * fixes for auto increase in height * fix height * respect newlines when wrapping text * shift text area when height increases beyond mid rect height until it reaches to the top * select bound text if present when rect selected * mutate y coord after text submit * Add padding of 30px and update dimensions acordingly * Don't allow selecting bound text element directly * support deletion of bound text element when rect deleted * trim text * Support autoshrink and improve algo * calculate approx line height instead of hardcoding * use textContainerId instead of storing textContainer element itself * rename boundTextElement -> boundTextElementId * fix text properties not getting reflected after edit inside rect * Support resizing * remove ts ignore * increase height of container when text height increases while resizing * use original text when editing/resizing so it adjusts based on original text * fix tests * add util isRectangleElement * use isTextElement util everywhere * disable selecting text inside rect when selectAll * Bind text to circle and diamond as well * fix tests * vertically center align the text always * better vertical align * Disable binding arrows for text inside shapes * set min width for text container when text is binded to container * update dimensions of container if its less than min width/ min height * Allow selecting of text container for transparent containers when clicked inside * fix test * preserve whitespaces between long word exceeding width and next word Use word break instead of whitespace no wrap for better readability and support safari * Perf improvements for measuring text width and resizing * Use canvas measureText instead of our algo. This has reduced the perf ~ 10 times * Rewrite wrapText algo to break in words appropriately and for longer words calculate the char width in order unless max width reached. This makes the the number of runs linear (max text length times) which was earlier textLength * textLength-1/2 as I was slicing the chars from end until max width reached for each run * Add a util to calculate getApproxCharsToFitInWidth to calculate min chars to fit in a line * use console.info so eslint doesnt warn :p * cache char width and don't call resize unless min width exceeded * update line height and height correctly when text properties inside container updated * improve vertical centering when text properties updated, not yet perfect though * when double clicked inside a conatiner take the cursor to end of text same as what happens when enter is pressed * Add hint when container selected * Select container when escape key is pressed after submitting text * fix copy/paste when using copy/paste action * fix copy when dragged with alt pressed * fix export to svg/png * fix add to library * Fix copy as png/svg * Don't allow selecting text when using selection tool and support resizing when multiple elements include ones with binded text selectec * fix rotation jump * moove all text utils to textElement.ts * resize text element only after container resized so that width doesnt change when editing * insert the remaining chars for long words once it goes beyond line * fix typo, use string for character type * renaming * fix bugs in word wrap algo * make grouping work * set boundTextElementId only when text present else unset it * rename textContainerId to containerId * fix * fix snap * use originalText in redrawTextBoundingBox so height is calculated properly and center align works after props updated * use boundElementIds and also support binding text in images 🎉 * fix the sw/se ends when resizing from ne/nw * fix y coord when resizing from north * bind when enter is pressed, double click/text tool willl edit the binded text if present else create a new text * bind when clicked on center of container * use pre-wrap instead of normal so it works in ff * use container boundTextElement when container present and trying to edit text * review fixes * make getBoundTextElementId type safe and check for existence when using this function * fix * don't duplicate boundElementIds when text submitted * only remove last trailing space if present which we have added when joining words * set width correctly when resizing to fix alignment issues * make duplication work using cmd/ctrl+d * set X coord correctly during resize * don't allow resize to negative dimensions when text is bounded to container * fix, check last char is space * remove logs * make sure text editor doesn't go beyond viewport and set container dimensions in case it overflows * add a util isTextBindableContainer to check if the container could bind text
2021-12-16 21:14:03 +05:30
element.containerId ? element.width : null,
);
return {
size: nextFontSize,
baseline: metrics.baseline + (nextHeight - metrics.height),
};
};
2020-08-10 14:16:39 +02:00
const getSidesForTransformHandle = (
transformHandleType: TransformHandleType,
shouldResizeFromCenter: boolean,
) => {
return {
n:
2020-08-10 14:16:39 +02:00
/^(n|ne|nw)$/.test(transformHandleType) ||
(shouldResizeFromCenter && /^(s|se|sw)$/.test(transformHandleType)),
s:
2020-08-10 14:16:39 +02:00
/^(s|se|sw)$/.test(transformHandleType) ||
(shouldResizeFromCenter && /^(n|ne|nw)$/.test(transformHandleType)),
w:
2020-08-10 14:16:39 +02:00
/^(w|nw|sw)$/.test(transformHandleType) ||
(shouldResizeFromCenter && /^(e|ne|se)$/.test(transformHandleType)),
e:
2020-08-10 14:16:39 +02:00
/^(e|ne|se)$/.test(transformHandleType) ||
(shouldResizeFromCenter && /^(w|nw|sw)$/.test(transformHandleType)),
};
};
const resizeSingleTextElement = (
element: NonDeleted<ExcalidrawTextElement>,
2020-08-10 14:16:39 +02:00
transformHandleType: "nw" | "ne" | "sw" | "se",
shouldResizeFromCenter: boolean,
pointerX: number,
pointerY: number,
) => {
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
const cx = (x1 + x2) / 2;
const cy = (y1 + y2) / 2;
// rotation pointer with reverse angle
const [rotatedX, rotatedY] = rotate(
pointerX,
pointerY,
cx,
cy,
-element.angle,
);
2020-11-06 22:06:30 +02:00
let scale: number;
2020-08-10 14:16:39 +02:00
switch (transformHandleType) {
case "se":
scale = Math.max(
(rotatedX - x1) / (x2 - x1),
(rotatedY - y1) / (y2 - y1),
);
break;
case "nw":
scale = Math.max(
(x2 - rotatedX) / (x2 - x1),
(y2 - rotatedY) / (y2 - y1),
);
break;
case "ne":
scale = Math.max(
(rotatedX - x1) / (x2 - x1),
(y2 - rotatedY) / (y2 - y1),
);
break;
case "sw":
scale = Math.max(
(x2 - rotatedX) / (x2 - x1),
(rotatedY - y1) / (y2 - y1),
);
break;
}
if (scale > 0) {
const nextWidth = element.width * scale;
const nextHeight = element.height * scale;
const nextFont = measureFontSizeFromWH(element, nextWidth, nextHeight);
if (nextFont === null) {
return;
}
const [nextX1, nextY1, nextX2, nextY2] = getResizedElementAbsoluteCoords(
element,
nextWidth,
nextHeight,
false,
);
const deltaX1 = (x1 - nextX1) / 2;
const deltaY1 = (y1 - nextY1) / 2;
const deltaX2 = (x2 - nextX2) / 2;
const deltaY2 = (y2 - nextY2) / 2;
const [nextElementX, nextElementY] = adjustXYWithRotation(
getSidesForTransformHandle(transformHandleType, shouldResizeFromCenter),
element.x,
element.y,
element.angle,
deltaX1,
deltaY1,
deltaX2,
deltaY2,
);
mutateElement(element, {
fontSize: nextFont.size,
width: nextWidth,
height: nextHeight,
baseline: nextFont.baseline,
x: nextElementX,
y: nextElementY,
});
}
};
export const resizeSingleElement = (
originalElements: PointerDownState["originalElements"],
shouldMaintainAspectRatio: boolean,
element: NonDeletedExcalidrawElement,
transformHandleDirection: TransformHandleDirection,
shouldResizeFromCenter: boolean,
pointerX: number,
pointerY: number,
) => {
const stateAtResizeStart = originalElements.get(element.id)!;
// Gets bounds corners
const [x1, y1, x2, y2] = getResizedElementAbsoluteCoords(
stateAtResizeStart,
stateAtResizeStart.width,
stateAtResizeStart.height,
true,
);
const startTopLeft: Point = [x1, y1];
const startBottomRight: Point = [x2, y2];
const startCenter: Point = centerPoint(startTopLeft, startBottomRight);
// Calculate new dimensions based on cursor position
const rotatedPointer = rotatePoint(
[pointerX, pointerY],
startCenter,
-stateAtResizeStart.angle,
);
// Get bounds corners rendered on screen
const [esx1, esy1, esx2, esy2] = getResizedElementAbsoluteCoords(
element,
element.width,
element.height,
true,
);
feat: bind text to shapes when pressing enter and support sticky notes 🎉 (#4343) * feat: Word wrap inside rect and increase height when size exceeded * fixes for auto increase in height * fix height * respect newlines when wrapping text * shift text area when height increases beyond mid rect height until it reaches to the top * select bound text if present when rect selected * mutate y coord after text submit * Add padding of 30px and update dimensions acordingly * Don't allow selecting bound text element directly * support deletion of bound text element when rect deleted * trim text * Support autoshrink and improve algo * calculate approx line height instead of hardcoding * use textContainerId instead of storing textContainer element itself * rename boundTextElement -> boundTextElementId * fix text properties not getting reflected after edit inside rect * Support resizing * remove ts ignore * increase height of container when text height increases while resizing * use original text when editing/resizing so it adjusts based on original text * fix tests * add util isRectangleElement * use isTextElement util everywhere * disable selecting text inside rect when selectAll * Bind text to circle and diamond as well * fix tests * vertically center align the text always * better vertical align * Disable binding arrows for text inside shapes * set min width for text container when text is binded to container * update dimensions of container if its less than min width/ min height * Allow selecting of text container for transparent containers when clicked inside * fix test * preserve whitespaces between long word exceeding width and next word Use word break instead of whitespace no wrap for better readability and support safari * Perf improvements for measuring text width and resizing * Use canvas measureText instead of our algo. This has reduced the perf ~ 10 times * Rewrite wrapText algo to break in words appropriately and for longer words calculate the char width in order unless max width reached. This makes the the number of runs linear (max text length times) which was earlier textLength * textLength-1/2 as I was slicing the chars from end until max width reached for each run * Add a util to calculate getApproxCharsToFitInWidth to calculate min chars to fit in a line * use console.info so eslint doesnt warn :p * cache char width and don't call resize unless min width exceeded * update line height and height correctly when text properties inside container updated * improve vertical centering when text properties updated, not yet perfect though * when double clicked inside a conatiner take the cursor to end of text same as what happens when enter is pressed * Add hint when container selected * Select container when escape key is pressed after submitting text * fix copy/paste when using copy/paste action * fix copy when dragged with alt pressed * fix export to svg/png * fix add to library * Fix copy as png/svg * Don't allow selecting text when using selection tool and support resizing when multiple elements include ones with binded text selectec * fix rotation jump * moove all text utils to textElement.ts * resize text element only after container resized so that width doesnt change when editing * insert the remaining chars for long words once it goes beyond line * fix typo, use string for character type * renaming * fix bugs in word wrap algo * make grouping work * set boundTextElementId only when text present else unset it * rename textContainerId to containerId * fix * fix snap * use originalText in redrawTextBoundingBox so height is calculated properly and center align works after props updated * use boundElementIds and also support binding text in images 🎉 * fix the sw/se ends when resizing from ne/nw * fix y coord when resizing from north * bind when enter is pressed, double click/text tool willl edit the binded text if present else create a new text * bind when clicked on center of container * use pre-wrap instead of normal so it works in ff * use container boundTextElement when container present and trying to edit text * review fixes * make getBoundTextElementId type safe and check for existence when using this function * fix * don't duplicate boundElementIds when text submitted * only remove last trailing space if present which we have added when joining words * set width correctly when resizing to fix alignment issues * make duplication work using cmd/ctrl+d * set X coord correctly during resize * don't allow resize to negative dimensions when text is bounded to container * fix, check last char is space * remove logs * make sure text editor doesn't go beyond viewport and set container dimensions in case it overflows * add a util isTextBindableContainer to check if the container could bind text
2021-12-16 21:14:03 +05:30
const boundsCurrentWidth = esx2 - esx1;
const boundsCurrentHeight = esy2 - esy1;
// It's important we set the initial scale value based on the width and height at resize start,
// otherwise previous dimensions affected by modifiers will be taken into account.
const atStartBoundsWidth = startBottomRight[0] - startTopLeft[0];
const atStartBoundsHeight = startBottomRight[1] - startTopLeft[1];
let scaleX = atStartBoundsWidth / boundsCurrentWidth;
let scaleY = atStartBoundsHeight / boundsCurrentHeight;
let boundTextFont: { fontSize?: number; baseline?: number } = {};
const boundTextElement = getBoundTextElement(element);
if (transformHandleDirection.includes("e")) {
scaleX = (rotatedPointer[0] - startTopLeft[0]) / boundsCurrentWidth;
}
if (transformHandleDirection.includes("s")) {
scaleY = (rotatedPointer[1] - startTopLeft[1]) / boundsCurrentHeight;
}
if (transformHandleDirection.includes("w")) {
scaleX = (startBottomRight[0] - rotatedPointer[0]) / boundsCurrentWidth;
}
if (transformHandleDirection.includes("n")) {
scaleY = (startBottomRight[1] - rotatedPointer[1]) / boundsCurrentHeight;
}
// Linear elements dimensions differ from bounds dimensions
const eleInitialWidth = stateAtResizeStart.width;
const eleInitialHeight = stateAtResizeStart.height;
// We have to use dimensions of element on screen, otherwise the scaling of the
// dimensions won't match the cursor for linear elements.
let eleNewWidth = element.width * scaleX;
let eleNewHeight = element.height * scaleY;
// adjust dimensions for resizing from center
if (shouldResizeFromCenter) {
eleNewWidth = 2 * eleNewWidth - eleInitialWidth;
eleNewHeight = 2 * eleNewHeight - eleInitialHeight;
}
// adjust dimensions to keep sides ratio
if (shouldMaintainAspectRatio) {
const widthRatio = Math.abs(eleNewWidth) / eleInitialWidth;
const heightRatio = Math.abs(eleNewHeight) / eleInitialHeight;
if (transformHandleDirection.length === 1) {
eleNewHeight *= widthRatio;
eleNewWidth *= heightRatio;
}
if (transformHandleDirection.length === 2) {
const ratio = Math.max(widthRatio, heightRatio);
eleNewWidth = eleInitialWidth * ratio * Math.sign(eleNewWidth);
eleNewHeight = eleInitialHeight * ratio * Math.sign(eleNewHeight);
}
}
if (boundTextElement) {
const stateOfBoundTextElementAtResize = originalElements.get(
boundTextElement.id,
) as typeof boundTextElement | undefined;
if (stateOfBoundTextElementAtResize) {
boundTextFont = {
fontSize: stateOfBoundTextElementAtResize.fontSize,
baseline: stateOfBoundTextElementAtResize.baseline,
};
}
if (shouldMaintainAspectRatio) {
const nextFont = measureFontSizeFromWH(
boundTextElement,
eleNewWidth - BOUND_TEXT_PADDING * 2,
eleNewHeight - BOUND_TEXT_PADDING * 2,
);
if (nextFont === null) {
return;
}
boundTextFont = {
fontSize: nextFont.size,
baseline: nextFont.baseline,
};
} else {
const minWidth = getApproxMinLineWidth(getFontString(boundTextElement));
const minHeight = getApproxMinLineHeight(getFontString(boundTextElement));
eleNewWidth = Math.ceil(Math.max(eleNewWidth, minWidth));
eleNewHeight = Math.ceil(Math.max(eleNewHeight, minHeight));
}
}
const [newBoundsX1, newBoundsY1, newBoundsX2, newBoundsY2] =
getResizedElementAbsoluteCoords(
stateAtResizeStart,
eleNewWidth,
eleNewHeight,
true,
);
const newBoundsWidth = newBoundsX2 - newBoundsX1;
const newBoundsHeight = newBoundsY2 - newBoundsY1;
// Calculate new topLeft based on fixed corner during resize
let newTopLeft = [...startTopLeft] as [number, number];
if (["n", "w", "nw"].includes(transformHandleDirection)) {
newTopLeft = [
startBottomRight[0] - Math.abs(newBoundsWidth),
startBottomRight[1] - Math.abs(newBoundsHeight),
];
}
if (transformHandleDirection === "ne") {
const bottomLeft = [startTopLeft[0], startBottomRight[1]];
newTopLeft = [bottomLeft[0], bottomLeft[1] - Math.abs(newBoundsHeight)];
}
if (transformHandleDirection === "sw") {
const topRight = [startBottomRight[0], startTopLeft[1]];
newTopLeft = [topRight[0] - Math.abs(newBoundsWidth), topRight[1]];
}
// Keeps opposite handle fixed during resize
if (shouldMaintainAspectRatio) {
if (["s", "n"].includes(transformHandleDirection)) {
newTopLeft[0] = startCenter[0] - newBoundsWidth / 2;
}
if (["e", "w"].includes(transformHandleDirection)) {
newTopLeft[1] = startCenter[1] - newBoundsHeight / 2;
}
}
// Flip horizontally
if (eleNewWidth < 0) {
if (transformHandleDirection.includes("e")) {
newTopLeft[0] -= Math.abs(newBoundsWidth);
}
if (transformHandleDirection.includes("w")) {
newTopLeft[0] += Math.abs(newBoundsWidth);
}
}
// Flip vertically
if (eleNewHeight < 0) {
if (transformHandleDirection.includes("s")) {
newTopLeft[1] -= Math.abs(newBoundsHeight);
}
if (transformHandleDirection.includes("n")) {
newTopLeft[1] += Math.abs(newBoundsHeight);
}
}
if (shouldResizeFromCenter) {
newTopLeft[0] = startCenter[0] - Math.abs(newBoundsWidth) / 2;
newTopLeft[1] = startCenter[1] - Math.abs(newBoundsHeight) / 2;
}
// adjust topLeft to new rotation point
const angle = stateAtResizeStart.angle;
const rotatedTopLeft = rotatePoint(newTopLeft, startCenter, angle);
const newCenter: Point = [
newTopLeft[0] + Math.abs(newBoundsWidth) / 2,
newTopLeft[1] + Math.abs(newBoundsHeight) / 2,
];
const rotatedNewCenter = rotatePoint(newCenter, startCenter, angle);
newTopLeft = rotatePoint(rotatedTopLeft, rotatedNewCenter, -angle);
// Readjust points for linear elements
const rescaledPoints = rescalePointsInElement(
stateAtResizeStart,
eleNewWidth,
eleNewHeight,
true,
);
// For linear elements (x,y) are the coordinates of the first drawn point not the top-left corner
// So we need to readjust (x,y) to be where the first point should be
const newOrigin = [...newTopLeft];
newOrigin[0] += stateAtResizeStart.x - newBoundsX1;
newOrigin[1] += stateAtResizeStart.y - newBoundsY1;
const resizedElement = {
width: Math.abs(eleNewWidth),
height: Math.abs(eleNewHeight),
x: newOrigin[0],
y: newOrigin[1],
...rescaledPoints,
};
if ("scale" in element && "scale" in stateAtResizeStart) {
mutateElement(element, {
scale: [
// defaulting because scaleX/Y can be 0/-0
(Math.sign(scaleX) || stateAtResizeStart.scale[0]) *
stateAtResizeStart.scale[0],
(Math.sign(scaleY) || stateAtResizeStart.scale[1]) *
stateAtResizeStart.scale[1],
],
});
}
if (
resizedElement.width !== 0 &&
resizedElement.height !== 0 &&
Number.isFinite(resizedElement.x) &&
Number.isFinite(resizedElement.y)
) {
updateBoundElements(element, {
newSize: { width: resizedElement.width, height: resizedElement.height },
});
mutateElement(element, resizedElement);
if (boundTextElement && boundTextFont) {
mutateElement(boundTextElement, { fontSize: boundTextFont.fontSize });
}
handleBindTextResize(element, transformHandleDirection);
}
};
const resizeMultipleElements = (
pointerDownState: PointerDownState,
selectedElements: readonly NonDeletedExcalidrawElement[],
2020-08-10 14:16:39 +02:00
transformHandleType: "nw" | "ne" | "sw" | "se",
shouldResizeFromCenter: boolean,
pointerX: number,
pointerY: number,
) => {
// map selected elements to the original elements. While it never should
// happen that pointerDownState.originalElements won't contain the selected
// elements during resize, this coupling isn't guaranteed, so to ensure
// type safety we need to transform only those elements we filter.
const targetElements = selectedElements.reduce(
(
acc: {
/** element at resize start */
orig: NonDeletedExcalidrawElement;
/** latest element */
latest: NonDeletedExcalidrawElement;
}[],
element,
) => {
const origElement = pointerDownState.originalElements.get(element.id);
if (origElement) {
acc.push({ orig: origElement, latest: element });
}
return acc;
},
[],
);
const { minX, minY, maxX, maxY, midX, midY } = getCommonBoundingBox(
targetElements.map(({ orig }) => orig),
);
const direction = transformHandleType;
const mapDirectionsToAnchors: Record<typeof direction, Point> = {
ne: [minX, maxY],
se: [minX, minY],
sw: [maxX, minY],
nw: [maxX, maxY],
};
// anchor point must be on the opposite side of the dragged selection handle
// or be the center of the selection if alt is pressed
const [anchorX, anchorY]: Point = shouldResizeFromCenter
? [midX, midY]
: mapDirectionsToAnchors[direction];
const mapDirectionsToPointerSides: Record<
typeof direction,
[x: boolean, y: boolean]
> = {
ne: [pointerX >= anchorX, pointerY <= anchorY],
se: [pointerX >= anchorX, pointerY >= anchorY],
sw: [pointerX <= anchorX, pointerY >= anchorY],
nw: [pointerX <= anchorX, pointerY <= anchorY],
};
// pointer side relative to anchor
const [pointerSideX, pointerSideY] = mapDirectionsToPointerSides[
direction
].map((condition) => (condition ? 1 : -1));
// stop resizing if a pointer is on the other side of selection
if (pointerSideX < 0 && pointerSideY < 0) {
return;
}
const scale =
Math.max(
(pointerSideX * Math.abs(pointerX - anchorX)) / (maxX - minX),
(pointerSideY * Math.abs(pointerY - anchorY)) / (maxY - minY),
) * (shouldResizeFromCenter ? 2 : 1);
if (scale === 1) {
return;
}
targetElements.forEach((element) => {
const width = element.orig.width * scale;
const height = element.orig.height * scale;
const x = anchorX + (element.orig.x - anchorX) * scale;
const y = anchorY + (element.orig.y - anchorY) * scale;
// readjust points for linear & free draw elements
const rescaledPoints = rescalePointsInElement(
element.orig,
width,
height,
false,
);
const update: {
width: number;
height: number;
x: number;
y: number;
points?: Point[];
fontSize?: number;
baseline?: number;
} = {
width,
height,
x,
y,
...rescaledPoints,
};
let boundTextUpdates: { fontSize: number; baseline: number } | null = null;
const boundTextElement = getBoundTextElement(element.latest);
if (boundTextElement || isTextElement(element.orig)) {
const optionalPadding = boundTextElement ? BOUND_TEXT_PADDING * 2 : 0;
const textMeasurements = measureFontSizeFromWH(
boundTextElement ?? (element.orig as ExcalidrawTextElement),
width - optionalPadding,
height - optionalPadding,
);
if (textMeasurements) {
if (isTextElement(element.orig)) {
update.fontSize = textMeasurements.size;
update.baseline = textMeasurements.baseline;
}
if (boundTextElement) {
boundTextUpdates = {
fontSize: textMeasurements.size,
baseline: textMeasurements.baseline,
};
}
}
}
mutateElement(element.latest, update);
if (boundTextElement && boundTextUpdates) {
mutateElement(boundTextElement, boundTextUpdates);
handleBindTextResize(element.latest, transformHandleType);
}
});
};
const rotateMultipleElements = (
pointerDownState: PointerDownState,
elements: readonly NonDeletedExcalidrawElement[],
pointerX: number,
pointerY: number,
shouldRotateWithDiscreteAngle: boolean,
centerX: number,
centerY: number,
) => {
let centerAngle =
(5 * Math.PI) / 2 + Math.atan2(pointerY - centerY, pointerX - centerX);
if (shouldRotateWithDiscreteAngle) {
centerAngle += SHIFT_LOCKING_ANGLE / 2;
centerAngle -= centerAngle % SHIFT_LOCKING_ANGLE;
}
elements.forEach((element, index) => {
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
const cx = (x1 + x2) / 2;
const cy = (y1 + y2) / 2;
const origAngle =
pointerDownState.originalElements.get(element.id)?.angle ?? element.angle;
const [rotatedCX, rotatedCY] = rotate(
cx,
cy,
centerX,
centerY,
centerAngle + origAngle - element.angle,
);
mutateElement(element, {
x: element.x + (rotatedCX - cx),
y: element.y + (rotatedCY - cy),
angle: normalizeAngle(centerAngle + origAngle),
});
feat: bind text to shapes when pressing enter and support sticky notes 🎉 (#4343) * feat: Word wrap inside rect and increase height when size exceeded * fixes for auto increase in height * fix height * respect newlines when wrapping text * shift text area when height increases beyond mid rect height until it reaches to the top * select bound text if present when rect selected * mutate y coord after text submit * Add padding of 30px and update dimensions acordingly * Don't allow selecting bound text element directly * support deletion of bound text element when rect deleted * trim text * Support autoshrink and improve algo * calculate approx line height instead of hardcoding * use textContainerId instead of storing textContainer element itself * rename boundTextElement -> boundTextElementId * fix text properties not getting reflected after edit inside rect * Support resizing * remove ts ignore * increase height of container when text height increases while resizing * use original text when editing/resizing so it adjusts based on original text * fix tests * add util isRectangleElement * use isTextElement util everywhere * disable selecting text inside rect when selectAll * Bind text to circle and diamond as well * fix tests * vertically center align the text always * better vertical align * Disable binding arrows for text inside shapes * set min width for text container when text is binded to container * update dimensions of container if its less than min width/ min height * Allow selecting of text container for transparent containers when clicked inside * fix test * preserve whitespaces between long word exceeding width and next word Use word break instead of whitespace no wrap for better readability and support safari * Perf improvements for measuring text width and resizing * Use canvas measureText instead of our algo. This has reduced the perf ~ 10 times * Rewrite wrapText algo to break in words appropriately and for longer words calculate the char width in order unless max width reached. This makes the the number of runs linear (max text length times) which was earlier textLength * textLength-1/2 as I was slicing the chars from end until max width reached for each run * Add a util to calculate getApproxCharsToFitInWidth to calculate min chars to fit in a line * use console.info so eslint doesnt warn :p * cache char width and don't call resize unless min width exceeded * update line height and height correctly when text properties inside container updated * improve vertical centering when text properties updated, not yet perfect though * when double clicked inside a conatiner take the cursor to end of text same as what happens when enter is pressed * Add hint when container selected * Select container when escape key is pressed after submitting text * fix copy/paste when using copy/paste action * fix copy when dragged with alt pressed * fix export to svg/png * fix add to library * Fix copy as png/svg * Don't allow selecting text when using selection tool and support resizing when multiple elements include ones with binded text selectec * fix rotation jump * moove all text utils to textElement.ts * resize text element only after container resized so that width doesnt change when editing * insert the remaining chars for long words once it goes beyond line * fix typo, use string for character type * renaming * fix bugs in word wrap algo * make grouping work * set boundTextElementId only when text present else unset it * rename textContainerId to containerId * fix * fix snap * use originalText in redrawTextBoundingBox so height is calculated properly and center align works after props updated * use boundElementIds and also support binding text in images 🎉 * fix the sw/se ends when resizing from ne/nw * fix y coord when resizing from north * bind when enter is pressed, double click/text tool willl edit the binded text if present else create a new text * bind when clicked on center of container * use pre-wrap instead of normal so it works in ff * use container boundTextElement when container present and trying to edit text * review fixes * make getBoundTextElementId type safe and check for existence when using this function * fix * don't duplicate boundElementIds when text submitted * only remove last trailing space if present which we have added when joining words * set width correctly when resizing to fix alignment issues * make duplication work using cmd/ctrl+d * set X coord correctly during resize * don't allow resize to negative dimensions when text is bounded to container * fix, check last char is space * remove logs * make sure text editor doesn't go beyond viewport and set container dimensions in case it overflows * add a util isTextBindableContainer to check if the container could bind text
2021-12-16 21:14:03 +05:30
const boundTextElementId = getBoundTextElementId(element);
if (boundTextElementId) {
const textElement =
Scene.getScene(element)!.getElement(boundTextElementId)!;
mutateElement(textElement, {
x: textElement.x + (rotatedCX - cx),
y: textElement.y + (rotatedCY - cy),
angle: normalizeAngle(centerAngle + origAngle),
});
}
});
};
export const getResizeOffsetXY = (
2020-08-10 14:16:39 +02:00
transformHandleType: MaybeTransformHandleType,
selectedElements: NonDeletedExcalidrawElement[],
x: number,
y: number,
): [number, number] => {
const [x1, y1, x2, y2] =
selectedElements.length === 1
? getElementAbsoluteCoords(selectedElements[0])
: getCommonBounds(selectedElements);
const cx = (x1 + x2) / 2;
const cy = (y1 + y2) / 2;
const angle = selectedElements.length === 1 ? selectedElements[0].angle : 0;
[x, y] = rotate(x, y, cx, cy, -angle);
2020-08-10 14:16:39 +02:00
switch (transformHandleType) {
case "n":
return rotate(x - (x1 + x2) / 2, y - y1, 0, 0, angle);
case "s":
return rotate(x - (x1 + x2) / 2, y - y2, 0, 0, angle);
case "w":
return rotate(x - x1, y - (y1 + y2) / 2, 0, 0, angle);
case "e":
return rotate(x - x2, y - (y1 + y2) / 2, 0, 0, angle);
case "nw":
return rotate(x - x1, y - y1, 0, 0, angle);
case "ne":
return rotate(x - x2, y - y1, 0, 0, angle);
case "sw":
return rotate(x - x1, y - y2, 0, 0, angle);
case "se":
return rotate(x - x2, y - y2, 0, 0, angle);
default:
return [0, 0];
}
};
export const getResizeArrowDirection = (
2020-08-10 14:16:39 +02:00
transformHandleType: MaybeTransformHandleType,
element: NonDeleted<ExcalidrawLinearElement>,
): "origin" | "end" => {
const [, [px, py]] = element.points;
const isResizeEnd =
2020-08-10 14:16:39 +02:00
(transformHandleType === "nw" && (px < 0 || py < 0)) ||
(transformHandleType === "ne" && px >= 0) ||
(transformHandleType === "sw" && px <= 0) ||
(transformHandleType === "se" && (px > 0 || py > 0));
return isResizeEnd ? "end" : "origin";
};