Skip to content

Commit

Permalink
chore: Unify math types, utils and functions (excalidraw#8389)
Browse files Browse the repository at this point in the history
Co-authored-by: dwelle <[email protected]>
  • Loading branch information
mtolmacs and dwelle authored Sep 2, 2024
1 parent e3d1dee commit f4dd23f
Show file tree
Hide file tree
Showing 98 changed files with 4,291 additions and 3,661 deletions.
18 changes: 13 additions & 5 deletions excalidraw-app/components/DebugCanvas.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { forwardRef, useCallback, useImperativeHandle, useRef } from "react";
import { type AppState } from "../../packages/excalidraw/types";
import { throttleRAF } from "../../packages/excalidraw/utils";
import type { LineSegment } from "../../packages/utils";
import {
bootstrapCanvas,
getNormalizedCanvasDimensions,
Expand All @@ -13,12 +12,16 @@ import {
TrashIcon,
} from "../../packages/excalidraw/components/icons";
import { STORAGE_KEYS } from "../app_constants";
import { isLineSegment } from "../../packages/excalidraw/element/typeChecks";
import {
isLineSegment,
type GlobalPoint,
type LineSegment,
} from "../../packages/math";

const renderLine = (
context: CanvasRenderingContext2D,
zoom: number,
segment: LineSegment,
segment: LineSegment<GlobalPoint>,
color: string,
) => {
context.save();
Expand Down Expand Up @@ -47,10 +50,15 @@ const render = (
context: CanvasRenderingContext2D,
appState: AppState,
) => {
frame.forEach((el) => {
frame.forEach((el: DebugElement) => {
switch (true) {
case isLineSegment(el.data):
renderLine(context, appState.zoom.value, el.data, el.color);
renderLine(
context,
appState.zoom.value,
el.data as LineSegment<GlobalPoint>,
el.color,
);
break;
}
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"excalidraw-app",
"packages/excalidraw",
"packages/utils",
"packages/math",
"examples/excalidraw",
"examples/excalidraw/*"
],
Expand Down
2 changes: 1 addition & 1 deletion packages/excalidraw/actions/actionCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { DEFAULT_CANVAS_BACKGROUND_PICKS } from "../colors";
import type { SceneBounds } from "../element/bounds";
import { setCursor } from "../cursor";
import { StoreAction } from "../store";
import { clamp } from "../math";
import { clamp } from "../../math";

export const actionChangeViewBackgroundColor = register({
name: "changeViewBackgroundColor",
Expand Down
23 changes: 12 additions & 11 deletions packages/excalidraw/actions/actionDuplicateSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,21 @@ export const actionDuplicateSelection = register({
perform: (elements, appState, formData, app) => {
// duplicate selected point(s) if editing a line
if (appState.editingLinearElement) {
const ret = LinearElementEditor.duplicateSelectedPoints(
appState,
app.scene.getNonDeletedElementsMap(),
);
// TODO: Invariants should be checked here instead of duplicateSelectedPoints()
try {
const newAppState = LinearElementEditor.duplicateSelectedPoints(
appState,
app.scene.getNonDeletedElementsMap(),
);

if (!ret) {
return {
elements,
appState: newAppState,
storeAction: StoreAction.CAPTURE,
};
} catch {
return false;
}

return {
elements,
appState: ret.appState,
storeAction: StoreAction.CAPTURE,
};
}

return {
Expand Down
9 changes: 5 additions & 4 deletions packages/excalidraw/actions/actionFinalize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { done } from "../components/icons";
import { t } from "../i18n";
import { register } from "./register";
import { mutateElement } from "../element/mutateElement";
import { isPathALoop } from "../math";
import { LinearElementEditor } from "../element/linearElementEditor";
import {
maybeBindLinearElement,
Expand All @@ -16,6 +15,8 @@ import { isBindingElement, isLinearElement } from "../element/typeChecks";
import type { AppState } from "../types";
import { resetCursor } from "../cursor";
import { StoreAction } from "../store";
import { point } from "../../math";
import { isPathALoop } from "../shapes";

export const actionFinalize = register({
name: "finalize",
Expand Down Expand Up @@ -112,10 +113,10 @@ export const actionFinalize = register({
const linePoints = multiPointElement.points;
const firstPoint = linePoints[0];
mutateElement(multiPointElement, {
points: linePoints.map((point, index) =>
points: linePoints.map((p, index) =>
index === linePoints.length - 1
? ([firstPoint[0], firstPoint[1]] as const)
: point,
? point(firstPoint[0], firstPoint[1])
: p,
),
});
}
Expand Down
10 changes: 6 additions & 4 deletions packages/excalidraw/actions/actionProperties.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useMemo, useRef, useState } from "react";
import type { AppClassProperties, AppState, Point, Primitive } from "../types";
import type { AppClassProperties, AppState, Primitive } from "../types";
import type { StoreActionType } from "../store";
import {
DEFAULT_ELEMENT_BACKGROUND_COLOR_PALETTE,
Expand Down Expand Up @@ -115,6 +115,8 @@ import {
} from "../element/binding";
import { mutateElbowArrow } from "../element/routing";
import { LinearElementEditor } from "../element/linearElementEditor";
import type { LocalPoint } from "../../math";
import { point, vector } from "../../math";

const FONT_SIZE_RELATIVE_INCREASE_STEP = 0.1;

Expand Down Expand Up @@ -1648,10 +1650,10 @@ export const actionChangeArrowType = register({
newElement,
elementsMap,
[finalStartPoint, finalEndPoint].map(
(point) =>
[point[0] - newElement.x, point[1] - newElement.y] as Point,
(p): LocalPoint =>
point(p[0] - newElement.x, p[1] - newElement.y),
),
[0, 0],
vector(0, 0),
{
...(startElement && newElement.startBinding
? {
Expand Down
24 changes: 7 additions & 17 deletions packages/excalidraw/charts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Radians } from "../math";
import { point } from "../math";
import {
COLOR_PALETTE,
DEFAULT_CHART_COLOR_INDEX,
Expand Down Expand Up @@ -203,7 +205,7 @@ const chartXLabels = (
x: x + index * (BAR_WIDTH + BAR_GAP) + BAR_GAP * 2,
y: y + BAR_GAP / 2,
width: BAR_WIDTH,
angle: 5.87,
angle: 5.87 as Radians,
fontSize: 16,
textAlign: "center",
verticalAlign: "top",
Expand Down Expand Up @@ -258,10 +260,7 @@ const chartLines = (
x,
y,
width: chartWidth,
points: [
[0, 0],
[chartWidth, 0],
],
points: [point(0, 0), point(chartWidth, 0)],
});

const yLine = newLinearElement({
Expand All @@ -272,10 +271,7 @@ const chartLines = (
x,
y,
height: chartHeight,
points: [
[0, 0],
[0, -chartHeight],
],
points: [point(0, 0), point(0, -chartHeight)],
});

const maxLine = newLinearElement({
Expand All @@ -288,10 +284,7 @@ const chartLines = (
strokeStyle: "dotted",
width: chartWidth,
opacity: GRID_OPACITY,
points: [
[0, 0],
[chartWidth, 0],
],
points: [point(0, 0), point(chartWidth, 0)],
});

return [xLine, yLine, maxLine];
Expand Down Expand Up @@ -448,10 +441,7 @@ const chartTypeLine = (
height: cy,
strokeStyle: "dotted",
opacity: GRID_OPACITY,
points: [
[0, 0],
[0, cy],
],
points: [point(0, 0), point(0, cy)],
});
});

Expand Down
Loading

0 comments on commit f4dd23f

Please sign in to comment.