2020-02-21 14:34:18 -05:00
|
|
|
import { ExcalidrawElement, PointerType } from "./types";
|
2020-02-15 21:03:32 +01:00
|
|
|
|
2020-08-08 21:04:15 -07:00
|
|
|
import { getElementAbsoluteCoords, Bounds } from "./bounds";
|
2020-04-02 17:40:26 +09:00
|
|
|
import { rotate } from "../math";
|
2020-01-06 19:34:22 +04:00
|
|
|
|
2020-08-10 14:16:39 +02:00
|
|
|
export type TransformHandleType =
|
|
|
|
| "n"
|
|
|
|
| "s"
|
|
|
|
| "w"
|
|
|
|
| "e"
|
|
|
|
| "nw"
|
|
|
|
| "ne"
|
|
|
|
| "sw"
|
|
|
|
| "se"
|
|
|
|
| "rotation";
|
|
|
|
|
|
|
|
export type TransformHandle = [number, number, number, number];
|
|
|
|
export type TransformHandles = Partial<
|
|
|
|
{ [T in TransformHandleType]: TransformHandle }
|
2020-08-08 21:04:15 -07:00
|
|
|
>;
|
2020-08-10 14:16:39 +02:00
|
|
|
export type MaybeTransformHandleType = TransformHandleType | false;
|
2020-08-08 21:04:15 -07:00
|
|
|
|
2020-08-10 14:16:39 +02:00
|
|
|
const transformHandleSizes: { [k in PointerType]: number } = {
|
2020-02-21 14:34:18 -05:00
|
|
|
mouse: 8,
|
|
|
|
pen: 16,
|
|
|
|
touch: 28,
|
|
|
|
};
|
2020-02-01 15:49:18 +04:00
|
|
|
|
2020-08-10 14:16:39 +02:00
|
|
|
const ROTATION_RESIZE_HANDLE_GAP = 16;
|
2020-04-02 17:40:26 +09:00
|
|
|
|
2020-04-07 17:49:59 +09:00
|
|
|
export const OMIT_SIDES_FOR_MULTIPLE_ELEMENTS = {
|
|
|
|
e: true,
|
|
|
|
s: true,
|
|
|
|
n: true,
|
|
|
|
w: true,
|
|
|
|
};
|
|
|
|
|
2020-05-28 07:17:15 +09:00
|
|
|
const OMIT_SIDES_FOR_TEXT_ELEMENT = {
|
|
|
|
e: true,
|
|
|
|
s: true,
|
|
|
|
n: true,
|
|
|
|
w: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const OMIT_SIDES_FOR_LINE_SLASH = {
|
|
|
|
e: true,
|
|
|
|
s: true,
|
|
|
|
n: true,
|
|
|
|
w: true,
|
|
|
|
nw: true,
|
|
|
|
se: true,
|
|
|
|
rotation: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const OMIT_SIDES_FOR_LINE_BACKSLASH = {
|
|
|
|
e: true,
|
|
|
|
s: true,
|
|
|
|
n: true,
|
|
|
|
w: true,
|
|
|
|
ne: true,
|
|
|
|
sw: true,
|
|
|
|
rotation: true,
|
|
|
|
};
|
|
|
|
|
2020-08-10 14:16:39 +02:00
|
|
|
const generateTransformHandle = (
|
2020-04-02 17:40:26 +09:00
|
|
|
x: number,
|
|
|
|
y: number,
|
|
|
|
width: number,
|
|
|
|
height: number,
|
|
|
|
cx: number,
|
|
|
|
cy: number,
|
|
|
|
angle: number,
|
2020-08-10 14:16:39 +02:00
|
|
|
): TransformHandle => {
|
2020-04-02 17:40:26 +09:00
|
|
|
const [xx, yy] = rotate(x + width / 2, y + height / 2, cx, cy, angle);
|
|
|
|
return [xx - width / 2, yy - height / 2, width, height];
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-04-02 17:40:26 +09:00
|
|
|
|
2020-08-10 14:16:39 +02:00
|
|
|
export const getTransformHandlesFromCoords = (
|
2020-08-08 21:04:15 -07:00
|
|
|
[x1, y1, x2, y2]: Bounds,
|
2020-04-07 17:49:59 +09:00
|
|
|
angle: number,
|
2020-02-21 14:34:18 -05:00
|
|
|
zoom: number,
|
2020-08-10 14:16:39 +02:00
|
|
|
pointerType: PointerType = "touch",
|
|
|
|
omitSides: { [T in TransformHandleType]?: boolean } = {},
|
|
|
|
): TransformHandles => {
|
|
|
|
const size = transformHandleSizes[pointerType];
|
|
|
|
const handleWidth = size / zoom;
|
|
|
|
const handleHeight = size / zoom;
|
2020-02-21 14:34:18 -05:00
|
|
|
|
2020-08-10 14:16:39 +02:00
|
|
|
const handleMarginX = size / zoom;
|
|
|
|
const handleMarginY = size / zoom;
|
2020-02-01 15:49:18 +04:00
|
|
|
|
2020-04-07 17:49:59 +09:00
|
|
|
const width = x2 - x1;
|
|
|
|
const height = y2 - y1;
|
|
|
|
const cx = (x1 + x2) / 2;
|
|
|
|
const cy = (y1 + y2) / 2;
|
2020-02-15 21:03:32 +01:00
|
|
|
|
|
|
|
const dashedLineMargin = 4 / zoom;
|
2020-01-06 19:34:22 +04:00
|
|
|
|
2020-08-10 14:16:39 +02:00
|
|
|
const centeringOffset = 0;
|
2020-02-21 14:34:18 -05:00
|
|
|
|
2020-08-10 14:16:39 +02:00
|
|
|
const transformHandles: TransformHandles = {
|
2020-04-07 17:49:59 +09:00
|
|
|
nw: omitSides["nw"]
|
|
|
|
? undefined
|
2020-08-10 14:16:39 +02:00
|
|
|
: generateTransformHandle(
|
|
|
|
x1 - dashedLineMargin - handleMarginX + centeringOffset,
|
|
|
|
y1 - dashedLineMargin - handleMarginY + centeringOffset,
|
|
|
|
handleWidth,
|
|
|
|
handleHeight,
|
2020-04-07 17:49:59 +09:00
|
|
|
cx,
|
|
|
|
cy,
|
|
|
|
angle,
|
|
|
|
),
|
|
|
|
ne: omitSides["ne"]
|
|
|
|
? undefined
|
2020-08-10 14:16:39 +02:00
|
|
|
: generateTransformHandle(
|
2020-04-07 17:49:59 +09:00
|
|
|
x2 + dashedLineMargin - centeringOffset,
|
2020-08-10 14:16:39 +02:00
|
|
|
y1 - dashedLineMargin - handleMarginY + centeringOffset,
|
|
|
|
handleWidth,
|
|
|
|
handleHeight,
|
2020-04-07 17:49:59 +09:00
|
|
|
cx,
|
|
|
|
cy,
|
|
|
|
angle,
|
|
|
|
),
|
|
|
|
sw: omitSides["sw"]
|
|
|
|
? undefined
|
2020-08-10 14:16:39 +02:00
|
|
|
: generateTransformHandle(
|
|
|
|
x1 - dashedLineMargin - handleMarginX + centeringOffset,
|
2020-04-07 17:49:59 +09:00
|
|
|
y2 + dashedLineMargin - centeringOffset,
|
2020-08-10 14:16:39 +02:00
|
|
|
handleWidth,
|
|
|
|
handleHeight,
|
2020-04-07 17:49:59 +09:00
|
|
|
cx,
|
|
|
|
cy,
|
|
|
|
angle,
|
|
|
|
),
|
|
|
|
se: omitSides["se"]
|
|
|
|
? undefined
|
2020-08-10 14:16:39 +02:00
|
|
|
: generateTransformHandle(
|
2020-04-07 17:49:59 +09:00
|
|
|
x2 + dashedLineMargin - centeringOffset,
|
|
|
|
y2 + dashedLineMargin - centeringOffset,
|
2020-08-10 14:16:39 +02:00
|
|
|
handleWidth,
|
|
|
|
handleHeight,
|
2020-04-07 17:49:59 +09:00
|
|
|
cx,
|
|
|
|
cy,
|
|
|
|
angle,
|
|
|
|
),
|
|
|
|
rotation: omitSides["rotation"]
|
|
|
|
? undefined
|
2020-08-10 14:16:39 +02:00
|
|
|
: generateTransformHandle(
|
|
|
|
x1 + width / 2 - handleWidth / 2,
|
2020-04-07 17:49:59 +09:00
|
|
|
y1 -
|
|
|
|
dashedLineMargin -
|
2020-08-10 14:16:39 +02:00
|
|
|
handleMarginY +
|
2020-04-07 17:49:59 +09:00
|
|
|
centeringOffset -
|
2020-08-10 14:16:39 +02:00
|
|
|
ROTATION_RESIZE_HANDLE_GAP / zoom,
|
|
|
|
handleWidth,
|
|
|
|
handleHeight,
|
2020-04-07 17:49:59 +09:00
|
|
|
cx,
|
|
|
|
cy,
|
|
|
|
angle,
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
2020-08-10 14:16:39 +02:00
|
|
|
// We only want to show height handles (all cardinal directions) above a certain size
|
|
|
|
const minimumSizeForEightHandles = (5 * size) / zoom;
|
|
|
|
if (Math.abs(width) > minimumSizeForEightHandles) {
|
2020-04-07 17:49:59 +09:00
|
|
|
if (!omitSides["n"]) {
|
2020-08-10 14:16:39 +02:00
|
|
|
transformHandles["n"] = generateTransformHandle(
|
|
|
|
x1 + width / 2 - handleWidth / 2,
|
|
|
|
y1 - dashedLineMargin - handleMarginY + centeringOffset,
|
|
|
|
handleWidth,
|
|
|
|
handleHeight,
|
2020-04-06 14:21:07 +03:00
|
|
|
cx,
|
|
|
|
cy,
|
|
|
|
angle,
|
2020-04-07 17:49:59 +09:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!omitSides["s"]) {
|
2020-08-10 14:16:39 +02:00
|
|
|
transformHandles["s"] = generateTransformHandle(
|
|
|
|
x1 + width / 2 - handleWidth / 2,
|
2020-04-07 17:49:59 +09:00
|
|
|
y2 + dashedLineMargin - centeringOffset,
|
2020-08-10 14:16:39 +02:00
|
|
|
handleWidth,
|
|
|
|
handleHeight,
|
2020-04-06 14:21:07 +03:00
|
|
|
cx,
|
|
|
|
cy,
|
|
|
|
angle,
|
2020-04-07 17:49:59 +09:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-08-10 14:16:39 +02:00
|
|
|
if (Math.abs(height) > minimumSizeForEightHandles) {
|
2020-04-07 17:49:59 +09:00
|
|
|
if (!omitSides["w"]) {
|
2020-08-10 14:16:39 +02:00
|
|
|
transformHandles["w"] = generateTransformHandle(
|
|
|
|
x1 - dashedLineMargin - handleMarginX + centeringOffset,
|
|
|
|
y1 + height / 2 - handleHeight / 2,
|
|
|
|
handleWidth,
|
|
|
|
handleHeight,
|
2020-04-06 14:21:07 +03:00
|
|
|
cx,
|
|
|
|
cy,
|
|
|
|
angle,
|
2020-04-07 17:49:59 +09:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!omitSides["e"]) {
|
2020-08-10 14:16:39 +02:00
|
|
|
transformHandles["e"] = generateTransformHandle(
|
2020-04-07 17:49:59 +09:00
|
|
|
x2 + dashedLineMargin - centeringOffset,
|
2020-08-10 14:16:39 +02:00
|
|
|
y1 + height / 2 - handleHeight / 2,
|
|
|
|
handleWidth,
|
|
|
|
handleHeight,
|
2020-04-06 14:21:07 +03:00
|
|
|
cx,
|
|
|
|
cy,
|
|
|
|
angle,
|
2020-04-07 17:49:59 +09:00
|
|
|
);
|
|
|
|
}
|
2020-01-06 19:34:22 +04:00
|
|
|
}
|
|
|
|
|
2020-08-10 14:16:39 +02:00
|
|
|
return transformHandles;
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-04-07 17:49:59 +09:00
|
|
|
|
2020-08-10 14:16:39 +02:00
|
|
|
export const getTransformHandles = (
|
2020-04-07 17:49:59 +09:00
|
|
|
element: ExcalidrawElement,
|
|
|
|
zoom: number,
|
2020-08-10 14:16:39 +02:00
|
|
|
pointerType: PointerType = "touch",
|
|
|
|
): TransformHandles => {
|
|
|
|
let omitSides: { [T in TransformHandleType]?: boolean } = {};
|
2020-05-12 20:10:11 +01:00
|
|
|
if (
|
|
|
|
element.type === "arrow" ||
|
|
|
|
element.type === "line" ||
|
|
|
|
element.type === "draw"
|
|
|
|
) {
|
2020-02-01 15:49:18 +04:00
|
|
|
if (element.points.length === 2) {
|
|
|
|
// only check the last point because starting point is always (0,0)
|
|
|
|
const [, p1] = element.points;
|
|
|
|
if (p1[0] === 0 || p1[1] === 0) {
|
2020-05-28 07:17:15 +09:00
|
|
|
omitSides = OMIT_SIDES_FOR_LINE_BACKSLASH;
|
|
|
|
} else if (p1[0] > 0 && p1[1] < 0) {
|
|
|
|
omitSides = OMIT_SIDES_FOR_LINE_SLASH;
|
|
|
|
} else if (p1[0] > 0 && p1[1] > 0) {
|
|
|
|
omitSides = OMIT_SIDES_FOR_LINE_BACKSLASH;
|
|
|
|
} else if (p1[0] < 0 && p1[1] > 0) {
|
|
|
|
omitSides = OMIT_SIDES_FOR_LINE_SLASH;
|
|
|
|
} else if (p1[0] < 0 && p1[1] < 0) {
|
|
|
|
omitSides = OMIT_SIDES_FOR_LINE_BACKSLASH;
|
2020-02-01 15:49:18 +04:00
|
|
|
}
|
|
|
|
}
|
2020-05-28 07:17:15 +09:00
|
|
|
} else if (element.type === "text") {
|
|
|
|
omitSides = OMIT_SIDES_FOR_TEXT_ELEMENT;
|
2020-01-06 19:34:22 +04:00
|
|
|
}
|
|
|
|
|
2020-08-10 14:16:39 +02:00
|
|
|
return getTransformHandlesFromCoords(
|
2020-05-28 07:17:15 +09:00
|
|
|
getElementAbsoluteCoords(element),
|
|
|
|
element.angle,
|
|
|
|
zoom,
|
|
|
|
pointerType,
|
|
|
|
omitSides,
|
|
|
|
);
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|