mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-12-05 18:55:28 +03:00
* fix: Undeclared dependencies Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Rationalize package versions Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Remove old react app remnants Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Conversion to pnpm Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Circular deps #0 Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: common package circular dependencies Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Move history and undo/redo to excalidraw/excalidraw Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: AppState broken up across package boundaries Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: renderElement and binding type racionalization Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Refactor linearElementEditor to not use types outside the package Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Do not import self barrel file Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: All circular dependencies except core Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Bump pnpm to 11 Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: SASS deprecation warnings Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Polynomial regular expression used on uncontrolled data Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Vercel deploy Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: excalidraw/history package Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Polynomial regex in SVG payload decoding Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: emit the internal packages' types from the editor again The editor advertises ./common/*, ./element/*, ./history/*, ./math/* and ./utils/* type subpaths, but without the path aliases tsc treats those packages as external libraries and no longer emits them. Include their sources in the editor's type build, and drop the relative cross-package import that made element/ come out half-emitted. * feat: export the history classes of the editor's API from the entry onIncrement hands out StoreIncrement subclasses and applyDeltas takes StoreDelta, so export them next to CaptureUpdateAction. Hosts then need no dependency of their own on @excalidraw/history. * chore: refuse to publish packages with npm or yarn Only pnpm publish rewrites workspace:* dependencies and applies publishConfig.exports, so a package published with another client can't be installed. --------- Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Co-authored-by: dwelle <5153846+dwelle@users.noreply.github.com>
299 lines
7.6 KiB
TypeScript
299 lines
7.6 KiB
TypeScript
import {
|
|
pointFrom,
|
|
pointOnLineSegment,
|
|
pointRotateRads,
|
|
type Radians,
|
|
} from "@excalidraw/math";
|
|
|
|
import {
|
|
SIDE_RESIZING_THRESHOLD,
|
|
type EditorInterface,
|
|
} from "@excalidraw/common";
|
|
|
|
import type { GlobalPoint, LineSegment, LocalPoint } from "@excalidraw/math";
|
|
|
|
import type { Bounds } from "@excalidraw/common";
|
|
|
|
import type { Zoom } from "@excalidraw/common";
|
|
|
|
import { getElementAbsoluteCoords } from "./bounds";
|
|
import {
|
|
getTransformHandlesFromCoords,
|
|
getTransformHandles,
|
|
getOmitSidesForEditorInterface,
|
|
canResizeFromSides,
|
|
} from "./transformHandles";
|
|
import { isImageElement, isLinearElement } from "./typeChecks";
|
|
|
|
import type {
|
|
TransformHandleType,
|
|
TransformHandle,
|
|
MaybeTransformHandleType,
|
|
} from "./transformHandles";
|
|
import type {
|
|
ExcalidrawElement,
|
|
PointerType,
|
|
NonDeletedExcalidrawElement,
|
|
ElementsMap,
|
|
} from "./types";
|
|
import type { ElementsState } from "./types";
|
|
|
|
const isInsideTransformHandle = (
|
|
transformHandle: TransformHandle,
|
|
x: number,
|
|
y: number,
|
|
) =>
|
|
x >= transformHandle[0] &&
|
|
x <= transformHandle[0] + transformHandle[2] &&
|
|
y >= transformHandle[1] &&
|
|
y <= transformHandle[1] + transformHandle[3];
|
|
|
|
export const resizeTest = <Point extends GlobalPoint | LocalPoint>(
|
|
element: NonDeletedExcalidrawElement,
|
|
elementsMap: ElementsMap,
|
|
appState: Pick<ElementsState, "selectedElementIds" | "croppingElementId">,
|
|
x: number,
|
|
y: number,
|
|
zoom: Zoom,
|
|
pointerType: PointerType,
|
|
editorInterface: EditorInterface,
|
|
): MaybeTransformHandleType => {
|
|
if (!appState.selectedElementIds[element.id]) {
|
|
return false;
|
|
}
|
|
|
|
const { rotation: rotationTransformHandle, ...transformHandles } =
|
|
getTransformHandles(
|
|
element,
|
|
zoom,
|
|
elementsMap,
|
|
pointerType,
|
|
getOmitSidesForEditorInterface(editorInterface),
|
|
);
|
|
|
|
if (
|
|
rotationTransformHandle &&
|
|
// (the crop editor has none, nor draws it)
|
|
!appState.croppingElementId &&
|
|
isInsideTransformHandle(rotationTransformHandle, x, y)
|
|
) {
|
|
return "rotation" as TransformHandleType;
|
|
}
|
|
|
|
const filter = Object.keys(transformHandles).filter((key) => {
|
|
const transformHandle =
|
|
transformHandles[key as Exclude<TransformHandleType, "rotation">]!;
|
|
if (!transformHandle) {
|
|
return false;
|
|
}
|
|
return isInsideTransformHandle(transformHandle, x, y);
|
|
});
|
|
|
|
if (filter.length > 0) {
|
|
return filter[0] as TransformHandleType;
|
|
}
|
|
|
|
if (canResizeFromSides(editorInterface)) {
|
|
const [x1, y1, x2, y2, cx, cy] = getElementAbsoluteCoords(
|
|
element,
|
|
elementsMap,
|
|
);
|
|
|
|
// do not resize from the sides for linear elements with only two points
|
|
if (!(isLinearElement(element) && element.points.length <= 2)) {
|
|
const SPACING = isImageElement(element)
|
|
? 0
|
|
: SIDE_RESIZING_THRESHOLD / zoom.value;
|
|
const ZOOMED_SIDE_RESIZING_THRESHOLD =
|
|
SIDE_RESIZING_THRESHOLD / zoom.value;
|
|
const sides = getSelectionBorders(
|
|
pointFrom(x1 - SPACING, y1 - SPACING),
|
|
pointFrom(x2 + SPACING, y2 + SPACING),
|
|
pointFrom(cx, cy),
|
|
element.angle,
|
|
);
|
|
|
|
for (const [dir, side] of Object.entries(sides)) {
|
|
// test to see if x, y are on the line segment
|
|
if (
|
|
pointOnLineSegment(
|
|
pointFrom(x, y),
|
|
side as LineSegment<Point>,
|
|
ZOOMED_SIDE_RESIZING_THRESHOLD,
|
|
)
|
|
) {
|
|
return dir as TransformHandleType;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
};
|
|
|
|
export const getElementWithTransformHandleType = (
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
|
appState: Pick<ElementsState, "selectedElementIds" | "croppingElementId">,
|
|
scenePointerX: number,
|
|
scenePointerY: number,
|
|
zoom: Zoom,
|
|
pointerType: PointerType,
|
|
elementsMap: ElementsMap,
|
|
editorInterface: EditorInterface,
|
|
) => {
|
|
return elements.reduce((result, element) => {
|
|
if (result) {
|
|
return result;
|
|
}
|
|
const transformHandleType = resizeTest(
|
|
element,
|
|
elementsMap,
|
|
appState,
|
|
scenePointerX,
|
|
scenePointerY,
|
|
zoom,
|
|
pointerType,
|
|
editorInterface,
|
|
);
|
|
return transformHandleType ? { element, transformHandleType } : null;
|
|
}, null as { element: NonDeletedExcalidrawElement; transformHandleType: MaybeTransformHandleType } | null);
|
|
};
|
|
|
|
export const getTransformHandleTypeFromCoords = <
|
|
Point extends GlobalPoint | LocalPoint,
|
|
>(
|
|
[x1, y1, x2, y2]: Bounds,
|
|
scenePointerX: number,
|
|
scenePointerY: number,
|
|
zoom: Zoom,
|
|
pointerType: PointerType,
|
|
editorInterface: EditorInterface,
|
|
): MaybeTransformHandleType => {
|
|
const transformHandles = getTransformHandlesFromCoords(
|
|
[x1, y1, x2, y2, (x1 + x2) / 2, (y1 + y2) / 2],
|
|
0 as Radians,
|
|
zoom,
|
|
pointerType,
|
|
getOmitSidesForEditorInterface(editorInterface),
|
|
);
|
|
|
|
const found = Object.keys(transformHandles).find((key) => {
|
|
const transformHandle =
|
|
transformHandles[key as Exclude<TransformHandleType, "rotation">]!;
|
|
return (
|
|
transformHandle &&
|
|
isInsideTransformHandle(transformHandle, scenePointerX, scenePointerY)
|
|
);
|
|
});
|
|
|
|
if (found) {
|
|
return found as MaybeTransformHandleType;
|
|
}
|
|
|
|
if (canResizeFromSides(editorInterface)) {
|
|
const cx = (x1 + x2) / 2;
|
|
const cy = (y1 + y2) / 2;
|
|
|
|
const SPACING = SIDE_RESIZING_THRESHOLD / zoom.value;
|
|
|
|
const sides = getSelectionBorders(
|
|
pointFrom(x1 - SPACING, y1 - SPACING),
|
|
pointFrom(x2 + SPACING, y2 + SPACING),
|
|
pointFrom(cx, cy),
|
|
0 as Radians,
|
|
);
|
|
|
|
for (const [dir, side] of Object.entries(sides)) {
|
|
// test to see if x, y are on the line segment
|
|
if (
|
|
pointOnLineSegment(
|
|
pointFrom(scenePointerX, scenePointerY),
|
|
side as LineSegment<Point>,
|
|
SPACING,
|
|
)
|
|
) {
|
|
return dir as TransformHandleType;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
};
|
|
|
|
const RESIZE_CURSORS = ["ns", "nesw", "ew", "nwse"];
|
|
const rotateResizeCursor = (cursor: string, angle: number) => {
|
|
const index = RESIZE_CURSORS.indexOf(cursor);
|
|
if (index >= 0) {
|
|
const a = Math.round(angle / (Math.PI / 4));
|
|
cursor = RESIZE_CURSORS[(index + a) % RESIZE_CURSORS.length];
|
|
}
|
|
return cursor;
|
|
};
|
|
|
|
/*
|
|
* Returns bi-directional cursor for the element being resized
|
|
*/
|
|
export const getCursorForResizingElement = (resizingElement: {
|
|
element?: ExcalidrawElement;
|
|
transformHandleType: MaybeTransformHandleType;
|
|
}): string => {
|
|
const { element, transformHandleType } = resizingElement;
|
|
const shouldSwapCursors =
|
|
element && Math.sign(element.height) * Math.sign(element.width) === -1;
|
|
let cursor = null;
|
|
|
|
switch (transformHandleType) {
|
|
case "n":
|
|
case "s":
|
|
cursor = "ns";
|
|
break;
|
|
case "w":
|
|
case "e":
|
|
cursor = "ew";
|
|
break;
|
|
case "nw":
|
|
case "se":
|
|
if (shouldSwapCursors) {
|
|
cursor = "nesw";
|
|
} else {
|
|
cursor = "nwse";
|
|
}
|
|
break;
|
|
case "ne":
|
|
case "sw":
|
|
if (shouldSwapCursors) {
|
|
cursor = "nwse";
|
|
} else {
|
|
cursor = "nesw";
|
|
}
|
|
break;
|
|
case "rotation":
|
|
return "grab";
|
|
}
|
|
|
|
if (cursor && element) {
|
|
cursor = rotateResizeCursor(cursor, element.angle);
|
|
}
|
|
|
|
return cursor ? `${cursor}-resize` : "";
|
|
};
|
|
|
|
const getSelectionBorders = <Point extends LocalPoint | GlobalPoint>(
|
|
[x1, y1]: Point,
|
|
[x2, y2]: Point,
|
|
center: Point,
|
|
angle: Radians,
|
|
) => {
|
|
const topLeft = pointRotateRads(pointFrom(x1, y1), center, angle);
|
|
const topRight = pointRotateRads(pointFrom(x2, y1), center, angle);
|
|
const bottomLeft = pointRotateRads(pointFrom(x1, y2), center, angle);
|
|
const bottomRight = pointRotateRads(pointFrom(x2, y2), center, angle);
|
|
|
|
return {
|
|
n: [topLeft, topRight],
|
|
e: [topRight, bottomRight],
|
|
s: [bottomRight, bottomLeft],
|
|
w: [bottomLeft, topLeft],
|
|
};
|
|
};
|