Refactor code structure for improved readability and maintainability

This commit is contained in:
Ayesha Imran 2025-04-28 13:22:25 +05:00
parent c8d38e87b0
commit 987668c139
31 changed files with 1517 additions and 2045 deletions

View file

@ -732,4 +732,4 @@ const generateElbowArrowShape = (
d.push(`L ${points[points.length - 1][0]} ${points[points.length - 1][1]}`);
return d.join(" ");
};
};

View file

@ -1,6 +1,11 @@
import rough from "roughjs/bin/rough";
import { rescalePoints, arrayToMap, invariant } from "@excalidraw/common";
import {
rescalePoints,
arrayToMap,
invariant,
sizeOf,
} from "@excalidraw/common";
import {
degreesToRadians,
@ -57,6 +62,7 @@ import type {
ElementsMap,
ExcalidrawRectanguloidElement,
ExcalidrawEllipseElement,
ElementsMapOrArray,
ExcalidrawRegularPolygonElement,
} from "./types";
import type { Drawable, Op } from "roughjs/bin/core";
@ -939,10 +945,10 @@ export const getElementBounds = (
};
export const getCommonBounds = (
elements: readonly ExcalidrawElement[],
elements: ElementsMapOrArray,
elementsMap?: ElementsMap,
): Bounds => {
if (!elements.length) {
if (!sizeOf(elements)) {
return [0, 0, 0, 0];
}
@ -1140,4 +1146,4 @@ export const doBoundsIntersect = (
const [minX2, minY2, maxX2, maxY2] = bounds2;
return minX1 < maxX2 && maxX1 > minX2 && minY1 < maxY2 && maxY1 > minY2;
};
};

View file

@ -7,10 +7,11 @@ export const hasBackground = (type: ElementOrToolType) =>
type === "ellipse" ||
type === "diamond" ||
type === "line" ||
type === "regularPolygon" || // Added regularPolygon
type === "freedraw";
export const hasStrokeColor = (type: ElementOrToolType) =>
type !== "image" && type !== "frame" && type !== "magicframe";
type !== "image" && type !== "frame" && type !== "magicframe"; // regularPolygon already included by not being excluded
export const hasStrokeWidth = (type: ElementOrToolType) =>
type === "rectangle" ||
@ -20,6 +21,7 @@ export const hasStrokeWidth = (type: ElementOrToolType) =>
type === "diamond" ||
type === "freedraw" ||
type === "arrow" ||
type === "regularPolygon" || // Added regularPolygon
type === "line";
export const hasStrokeStyle = (type: ElementOrToolType) =>
@ -29,6 +31,7 @@ export const hasStrokeStyle = (type: ElementOrToolType) =>
type === "ellipse" ||
type === "diamond" ||
type === "arrow" ||
type === "regularPolygon" || // Added regularPolygon
type === "line";
export const canChangeRoundness = (type: ElementOrToolType) =>
@ -37,6 +40,7 @@ export const canChangeRoundness = (type: ElementOrToolType) =>
type === "embeddable" ||
type === "line" ||
type === "diamond" ||
type === "regularPolygon" || // Added regularPolygon
type === "image";
export const toolIsArrow = (type: ElementOrToolType) => type === "arrow";

View file

@ -120,4 +120,4 @@ const distanceToEllipseElement = (
pointRotateRads(p, center, -element.angle as Radians),
ellipse(center, element.width / 2, element.height / 2),
);
};
};

View file

@ -555,4 +555,4 @@ export const newImageElement = (
scale: opts.scale ?? [1, 1],
crop: opts.crop ?? null,
};
};
};

View file

@ -1079,4 +1079,4 @@ function getSvgPathFromStroke(points: number[][]): string {
)
.join(" ")
.replace(TO_FIXED_PRECISION, "$1");
}
}

View file

@ -411,4 +411,4 @@ export const isPathALoop = (
return distance <= LINE_CONFIRM_THRESHOLD / zoomValue;
}
return false;
};
};

View file

@ -348,4 +348,4 @@ export const isBounds = (box: unknown): box is Bounds =>
typeof box[0] === "number" &&
typeof box[1] === "number" &&
typeof box[2] === "number" &&
typeof box[3] === "number";
typeof box[3] === "number";

View file

@ -420,4 +420,4 @@ export type NonDeletedSceneElementsMap = Map<
export type ElementsMapOrArray =
| readonly ExcalidrawElement[]
| Readonly<ElementsMap>;
| Readonly<ElementsMap>;