You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.7 KiB
TypeScript
93 lines
2.7 KiB
TypeScript
1 year ago
|
import { isIframeElement } from "../element/typeChecks";
|
||
2 years ago
|
import {
|
||
1 year ago
|
ExcalidrawIframeElement,
|
||
2 years ago
|
NonDeletedExcalidrawElement,
|
||
|
} from "../element/types";
|
||
1 year ago
|
import { ElementOrToolType } from "../types";
|
||
5 years ago
|
|
||
1 year ago
|
export const hasBackground = (type: ElementOrToolType) =>
|
||
5 years ago
|
type === "rectangle" ||
|
||
1 year ago
|
type === "iframe" ||
|
||
2 years ago
|
type === "embeddable" ||
|
||
5 years ago
|
type === "ellipse" ||
|
||
|
type === "diamond" ||
|
||
3 years ago
|
type === "line" ||
|
||
|
type === "freedraw";
|
||
5 years ago
|
|
||
1 year ago
|
export const hasStrokeColor = (type: ElementOrToolType) =>
|
||
|
type !== "image" && type !== "frame" && type !== "magicframe";
|
||
3 years ago
|
|
||
1 year ago
|
export const hasStrokeWidth = (type: ElementOrToolType) =>
|
||
5 years ago
|
type === "rectangle" ||
|
||
1 year ago
|
type === "iframe" ||
|
||
2 years ago
|
type === "embeddable" ||
|
||
5 years ago
|
type === "ellipse" ||
|
||
|
type === "diamond" ||
|
||
4 years ago
|
type === "freedraw" ||
|
||
5 years ago
|
type === "arrow" ||
|
||
|
type === "line";
|
||
5 years ago
|
|
||
1 year ago
|
export const hasStrokeStyle = (type: ElementOrToolType) =>
|
||
5 years ago
|
type === "rectangle" ||
|
||
1 year ago
|
type === "iframe" ||
|
||
2 years ago
|
type === "embeddable" ||
|
||
4 years ago
|
type === "ellipse" ||
|
||
|
type === "diamond" ||
|
||
5 years ago
|
type === "arrow" ||
|
||
|
type === "line";
|
||
|
|
||
1 year ago
|
export const canChangeRoundness = (type: ElementOrToolType) =>
|
||
3 years ago
|
type === "rectangle" ||
|
||
1 year ago
|
type === "iframe" ||
|
||
2 years ago
|
type === "embeddable" ||
|
||
3 years ago
|
type === "arrow" ||
|
||
|
type === "line" ||
|
||
|
type === "diamond";
|
||
4 years ago
|
|
||
1 year ago
|
export const canHaveArrowheads = (type: ElementOrToolType) => type === "arrow";
|
||
4 years ago
|
|
||
5 years ago
|
export const getElementAtPosition = (
|
||
5 years ago
|
elements: readonly NonDeletedExcalidrawElement[],
|
||
5 years ago
|
isAtPositionFn: (element: NonDeletedExcalidrawElement) => boolean,
|
||
5 years ago
|
) => {
|
||
5 years ago
|
let hitElement = null;
|
||
|
// We need to to hit testing from front (end of the array) to back (beginning of the array)
|
||
5 years ago
|
// because array is ordered from lower z-index to highest and we want element z-index
|
||
|
// with higher z-index
|
||
4 years ago
|
for (let index = elements.length - 1; index >= 0; --index) {
|
||
|
const element = elements[index];
|
||
5 years ago
|
if (element.isDeleted) {
|
||
5 years ago
|
continue;
|
||
|
}
|
||
5 years ago
|
if (isAtPositionFn(element)) {
|
||
|
hitElement = element;
|
||
5 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return hitElement;
|
||
5 years ago
|
};
|
||
5 years ago
|
|
||
5 years ago
|
export const getElementsAtPosition = (
|
||
|
elements: readonly NonDeletedExcalidrawElement[],
|
||
|
isAtPositionFn: (element: NonDeletedExcalidrawElement) => boolean,
|
||
|
) => {
|
||
1 year ago
|
const iframeLikes: ExcalidrawIframeElement[] = [];
|
||
5 years ago
|
// The parameter elements comes ordered from lower z-index to higher.
|
||
|
// We want to preserve that order on the returned array.
|
||
2 years ago
|
// Exception being embeddables which should be on top of everything else in
|
||
|
// terms of hit testing.
|
||
|
const elsAtPos = elements.filter((element) => {
|
||
|
const hit = !element.isDeleted && isAtPositionFn(element);
|
||
|
if (hit) {
|
||
1 year ago
|
if (isIframeElement(element)) {
|
||
|
iframeLikes.push(element);
|
||
2 years ago
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
});
|
||
1 year ago
|
return elsAtPos.concat(iframeLikes);
|
||
5 years ago
|
};
|