* feat: sharpness * feat: fill sharp lines, et al. * fix: rotated positioning * chore: simplify path with Q * fix: hit test inside sharp elements * make sharp / round buttons work properly * fix tsc tests * update snapshots * update snapshots * fix: sharp arrow creation error * fix merge and test * avoid type assertion * remove duplicate helper Co-authored-by: dwelle <luzar.david@gmail.com>
70 lines
1.7 KiB
TypeScript
70 lines
1.7 KiB
TypeScript
import {
|
|
ExcalidrawElement,
|
|
NonDeletedExcalidrawElement,
|
|
} from "../element/types";
|
|
|
|
import { getElementAbsoluteCoords } from "../element";
|
|
|
|
export const hasBackground = (type: string) =>
|
|
type === "rectangle" ||
|
|
type === "ellipse" ||
|
|
type === "diamond" ||
|
|
type === "draw" ||
|
|
type === "line";
|
|
|
|
export const hasStroke = (type: string) =>
|
|
type === "rectangle" ||
|
|
type === "ellipse" ||
|
|
type === "diamond" ||
|
|
type === "arrow" ||
|
|
type === "draw" ||
|
|
type === "line";
|
|
|
|
export const canChangeSharpness = (type: string) =>
|
|
type === "rectangle" ||
|
|
type === "arrow" ||
|
|
type === "draw" ||
|
|
type === "line";
|
|
|
|
export const hasText = (type: string) => type === "text";
|
|
|
|
export const getElementAtPosition = (
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
|
isAtPositionFn: (element: NonDeletedExcalidrawElement) => boolean,
|
|
) => {
|
|
let hitElement = null;
|
|
// We need to to hit testing from front (end of the array) to back (beginning of the array)
|
|
for (let i = elements.length - 1; i >= 0; --i) {
|
|
const element = elements[i];
|
|
if (element.isDeleted) {
|
|
continue;
|
|
}
|
|
if (isAtPositionFn(element)) {
|
|
hitElement = element;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return hitElement;
|
|
};
|
|
|
|
export const getElementContainingPosition = (
|
|
elements: readonly ExcalidrawElement[],
|
|
x: number,
|
|
y: number,
|
|
) => {
|
|
let hitElement = null;
|
|
// We need to to hit testing from front (end of the array) to back (beginning of the array)
|
|
for (let i = elements.length - 1; i >= 0; --i) {
|
|
if (elements[i].isDeleted) {
|
|
continue;
|
|
}
|
|
const [x1, y1, x2, y2] = getElementAbsoluteCoords(elements[i]);
|
|
if (x1 < x && x < x2 && y1 < y && y < y2) {
|
|
hitElement = elements[i];
|
|
break;
|
|
}
|
|
}
|
|
return hitElement;
|
|
};
|