2020-03-10 20:11:02 -07:00
|
|
|
import { ExcalidrawElement } from "./types";
|
2020-03-09 22:34:50 -07:00
|
|
|
import { mutateElement } from "./mutateElement";
|
2020-03-17 20:55:40 +01:00
|
|
|
import { isLinearElement } from "./typeChecks";
|
2020-04-04 12:55:22 +01:00
|
|
|
import { SHIFT_LOCKING_ANGLE } from "../constants";
|
2020-01-23 09:21:04 +00:00
|
|
|
|
|
|
|
export function isInvisiblySmallElement(element: ExcalidrawElement): boolean {
|
2020-03-17 20:55:40 +01:00
|
|
|
if (isLinearElement(element)) {
|
2020-02-05 22:47:10 +04:00
|
|
|
return element.points.length < 2;
|
2020-02-04 13:45:22 +04:00
|
|
|
}
|
2020-01-23 09:21:04 +00:00
|
|
|
return element.width === 0 && element.height === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a perfect shape or diagonal/horizontal/vertical line
|
|
|
|
*/
|
|
|
|
export function getPerfectElementSize(
|
|
|
|
elementType: string,
|
|
|
|
width: number,
|
2020-01-24 12:04:54 +02:00
|
|
|
height: number,
|
2020-01-23 09:21:04 +00:00
|
|
|
): { width: number; height: number } {
|
|
|
|
const absWidth = Math.abs(width);
|
|
|
|
const absHeight = Math.abs(height);
|
|
|
|
|
|
|
|
if (elementType === "line" || elementType === "arrow") {
|
2020-04-04 12:55:22 +01:00
|
|
|
const lockedAngle =
|
|
|
|
Math.round(Math.atan(absHeight / absWidth) / SHIFT_LOCKING_ANGLE) *
|
|
|
|
SHIFT_LOCKING_ANGLE;
|
|
|
|
if (lockedAngle === 0) {
|
2020-01-23 09:21:04 +00:00
|
|
|
height = 0;
|
2020-04-04 12:55:22 +01:00
|
|
|
} else if (lockedAngle === Math.PI / 2) {
|
2020-01-23 09:21:04 +00:00
|
|
|
width = 0;
|
|
|
|
} else {
|
2020-04-04 12:55:22 +01:00
|
|
|
height =
|
|
|
|
Math.round(absWidth * Math.tan(lockedAngle)) * Math.sign(height) ||
|
|
|
|
height;
|
2020-01-23 09:21:04 +00:00
|
|
|
}
|
|
|
|
} else if (elementType !== "selection") {
|
|
|
|
height = absWidth * Math.sign(height);
|
|
|
|
}
|
|
|
|
return { width, height };
|
|
|
|
}
|
|
|
|
|
|
|
|
export function resizePerfectLineForNWHandler(
|
2020-03-10 20:11:02 -07:00
|
|
|
element: ExcalidrawElement,
|
2020-01-23 09:21:04 +00:00
|
|
|
x: number,
|
2020-01-24 12:04:54 +02:00
|
|
|
y: number,
|
2020-01-23 09:21:04 +00:00
|
|
|
) {
|
|
|
|
const anchorX = element.x + element.width;
|
|
|
|
const anchorY = element.y + element.height;
|
|
|
|
const distanceToAnchorX = x - anchorX;
|
|
|
|
const distanceToAnchorY = y - anchorY;
|
|
|
|
if (Math.abs(distanceToAnchorX) < Math.abs(distanceToAnchorY) / 2) {
|
2020-03-10 20:11:02 -07:00
|
|
|
mutateElement(element, {
|
|
|
|
x: anchorX,
|
|
|
|
width: 0,
|
|
|
|
y,
|
|
|
|
height: -distanceToAnchorY,
|
|
|
|
});
|
2020-01-23 09:21:04 +00:00
|
|
|
} else if (Math.abs(distanceToAnchorY) < Math.abs(element.width) / 2) {
|
2020-03-10 20:11:02 -07:00
|
|
|
mutateElement(element, {
|
|
|
|
y: anchorY,
|
|
|
|
height: 0,
|
|
|
|
});
|
2020-01-23 09:21:04 +00:00
|
|
|
} else {
|
2020-03-10 20:11:02 -07:00
|
|
|
const nextHeight =
|
2020-01-23 09:21:04 +00:00
|
|
|
Math.sign(distanceToAnchorY) *
|
|
|
|
Math.sign(distanceToAnchorX) *
|
|
|
|
element.width;
|
2020-03-10 20:11:02 -07:00
|
|
|
mutateElement(element, {
|
|
|
|
x,
|
|
|
|
y: anchorY - nextHeight,
|
|
|
|
width: -distanceToAnchorX,
|
|
|
|
height: nextHeight,
|
|
|
|
});
|
2020-01-23 09:21:04 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-24 20:45:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {boolean} whether element was normalized
|
|
|
|
*/
|
|
|
|
export function normalizeDimensions(
|
|
|
|
element: ExcalidrawElement | null,
|
|
|
|
): element is ExcalidrawElement {
|
2020-04-09 18:53:12 +09:00
|
|
|
if (!element || (element.width >= 0 && element.height >= 0)) {
|
2020-01-24 20:45:52 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (element.width < 0) {
|
2020-03-10 20:11:02 -07:00
|
|
|
const nextWidth = Math.abs(element.width);
|
|
|
|
mutateElement(element, {
|
|
|
|
width: nextWidth,
|
|
|
|
x: element.x - nextWidth,
|
2020-03-09 22:34:50 -07:00
|
|
|
});
|
2020-01-24 20:45:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (element.height < 0) {
|
2020-03-10 20:11:02 -07:00
|
|
|
const nextHeight = Math.abs(element.height);
|
|
|
|
mutateElement(element, {
|
|
|
|
height: nextHeight,
|
|
|
|
y: element.y - nextHeight,
|
2020-03-09 22:34:50 -07:00
|
|
|
});
|
2020-01-24 20:45:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|