mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
improve & granularize ExcalidrawElement types (#991)
* improve & granularize ExcalidrawElement types * fix incorrectly passing type * fix tests * fix more tests * fix unnecessary spreads & refactor * add comments
This commit is contained in:
parent
1c545c1d47
commit
373d16abe6
22 changed files with 430 additions and 272 deletions
|
@ -1,13 +1,14 @@
|
|||
import { ExcalidrawElement } from "./types";
|
||||
import { ExcalidrawElement, ExcalidrawLinearElement } from "./types";
|
||||
import { rotate } from "../math";
|
||||
import { Drawable } from "roughjs/bin/core";
|
||||
import { Point } from "../types";
|
||||
import { getShapeForElement } from "../renderer/renderElement";
|
||||
import { isLinearElement } from "./typeChecks";
|
||||
|
||||
// If the element is created from right to left, the width is going to be negative
|
||||
// This set of functions retrieves the absolute position of the 4 points.
|
||||
export function getElementAbsoluteCoords(element: ExcalidrawElement) {
|
||||
if (element.type === "arrow" || element.type === "line") {
|
||||
if (isLinearElement(element)) {
|
||||
return getLinearElementAbsoluteBounds(element);
|
||||
}
|
||||
return [
|
||||
|
@ -33,7 +34,9 @@ export function getDiamondPoints(element: ExcalidrawElement) {
|
|||
return [topX, topY, rightX, rightY, bottomX, bottomY, leftX, leftY];
|
||||
}
|
||||
|
||||
export function getLinearElementAbsoluteBounds(element: ExcalidrawElement) {
|
||||
export function getLinearElementAbsoluteBounds(
|
||||
element: ExcalidrawLinearElement,
|
||||
) {
|
||||
if (element.points.length < 2 || !getShapeForElement(element)) {
|
||||
const { minX, minY, maxX, maxY } = element.points.reduce(
|
||||
(limits, [x, y]) => {
|
||||
|
@ -119,7 +122,10 @@ export function getLinearElementAbsoluteBounds(element: ExcalidrawElement) {
|
|||
];
|
||||
}
|
||||
|
||||
export function getArrowPoints(element: ExcalidrawElement, shape: Drawable[]) {
|
||||
export function getArrowPoints(
|
||||
element: ExcalidrawLinearElement,
|
||||
shape: Drawable[],
|
||||
) {
|
||||
const ops = shape[0].sets[0].ops;
|
||||
|
||||
const data = ops[ops.length - 1].data;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue