mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-12-05 03:55:06 +01:00
add transform to the element package
This commit is contained in:
2720
packages/element/src/__tests__/__snapshots__/transform.test.ts.snap
Normal file
2720
packages/element/src/__tests__/__snapshots__/transform.test.ts.snap
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,11 +1,11 @@
|
|||||||
import { pointFrom } from "@excalidraw/math";
|
import { pointFrom } from "@excalidraw/math";
|
||||||
import { vi } from "vitest";
|
import { vi } from "vitest";
|
||||||
|
|
||||||
import type { ExcalidrawArrowElement } from "@excalidraw/element/types";
|
import { convertToExcalidrawElements } from "../transform";
|
||||||
|
|
||||||
import { convertToExcalidrawElements } from "./transform";
|
import type { ExcalidrawArrowElement } from "../types";
|
||||||
|
|
||||||
import type { ExcalidrawElementSkeleton } from "./transform";
|
import type { ExcalidrawElementSkeleton } from "../types";
|
||||||
|
|
||||||
const opts = { regenerateIds: false };
|
const opts = { regenerateIds: false };
|
||||||
|
|
||||||
@@ -92,6 +92,7 @@ export * from "./store";
|
|||||||
export * from "./textElement";
|
export * from "./textElement";
|
||||||
export * from "./textMeasurements";
|
export * from "./textMeasurements";
|
||||||
export * from "./textWrapping";
|
export * from "./textWrapping";
|
||||||
|
export * from "./transform";
|
||||||
export * from "./transformHandles";
|
export * from "./transformHandles";
|
||||||
export * from "./typeChecks";
|
export * from "./typeChecks";
|
||||||
export * from "./utils";
|
export * from "./utils";
|
||||||
|
|||||||
@@ -16,7 +16,9 @@ import {
|
|||||||
getLineHeight,
|
getLineHeight,
|
||||||
} from "@excalidraw/common";
|
} from "@excalidraw/common";
|
||||||
|
|
||||||
import { bindBindingElement } from "@excalidraw/element";
|
import type { MarkOptional } from "@excalidraw/common/utility-types";
|
||||||
|
|
||||||
|
import { bindBindingElement } from "./binding";
|
||||||
import {
|
import {
|
||||||
newArrowElement,
|
newArrowElement,
|
||||||
newElement,
|
newElement,
|
||||||
@@ -25,189 +27,31 @@ import {
|
|||||||
newLinearElement,
|
newLinearElement,
|
||||||
newMagicFrameElement,
|
newMagicFrameElement,
|
||||||
newTextElement,
|
newTextElement,
|
||||||
} from "@excalidraw/element";
|
type ElementConstructorOpts,
|
||||||
import { measureText, normalizeText } from "@excalidraw/element";
|
} from "./newElement";
|
||||||
import { isArrowElement } from "@excalidraw/element";
|
import { measureText, normalizeText } from "./textMeasurements";
|
||||||
|
import { isArrowElement } from "./typeChecks";
|
||||||
|
|
||||||
import { syncInvalidIndices } from "@excalidraw/element";
|
import { syncInvalidIndices } from "./fractionalIndex";
|
||||||
|
|
||||||
import { redrawTextBoundingBox } from "@excalidraw/element";
|
import { redrawTextBoundingBox } from "./textElement";
|
||||||
|
|
||||||
import { LinearElementEditor } from "@excalidraw/element";
|
import { LinearElementEditor } from "./linearElementEditor";
|
||||||
|
|
||||||
import { getCommonBounds } from "@excalidraw/element";
|
import { getCommonBounds } from "./bounds";
|
||||||
|
|
||||||
import { Scene } from "@excalidraw/element";
|
import { Scene } from "./Scene";
|
||||||
|
|
||||||
import type { ElementConstructorOpts } from "@excalidraw/element";
|
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
ExcalidrawArrowElement,
|
ExcalidrawArrowElement,
|
||||||
ExcalidrawBindableElement,
|
ExcalidrawBindableElement,
|
||||||
ExcalidrawElement,
|
ExcalidrawElement,
|
||||||
ExcalidrawFrameElement,
|
ExcalidrawElementSkeleton,
|
||||||
ExcalidrawFreeDrawElement,
|
|
||||||
ExcalidrawGenericElement,
|
|
||||||
ExcalidrawIframeLikeElement,
|
|
||||||
ExcalidrawImageElement,
|
|
||||||
ExcalidrawLinearElement,
|
ExcalidrawLinearElement,
|
||||||
ExcalidrawMagicFrameElement,
|
|
||||||
ExcalidrawSelectionElement,
|
|
||||||
ExcalidrawTextElement,
|
ExcalidrawTextElement,
|
||||||
FileId,
|
|
||||||
FontFamilyValues,
|
|
||||||
NonDeletedSceneElementsMap,
|
NonDeletedSceneElementsMap,
|
||||||
TextAlign,
|
ValidLinearElement,
|
||||||
VerticalAlign,
|
} from "./types";
|
||||||
} from "@excalidraw/element/types";
|
|
||||||
|
|
||||||
import type { MarkOptional } from "@excalidraw/common/utility-types";
|
|
||||||
|
|
||||||
export type ValidLinearElement = {
|
|
||||||
type: "arrow" | "line";
|
|
||||||
x: number;
|
|
||||||
y: number;
|
|
||||||
label?: {
|
|
||||||
text: string;
|
|
||||||
fontSize?: number;
|
|
||||||
fontFamily?: FontFamilyValues;
|
|
||||||
textAlign?: TextAlign;
|
|
||||||
verticalAlign?: VerticalAlign;
|
|
||||||
} & MarkOptional<ElementConstructorOpts, "x" | "y">;
|
|
||||||
end?:
|
|
||||||
| (
|
|
||||||
| (
|
|
||||||
| {
|
|
||||||
type: Exclude<
|
|
||||||
ExcalidrawBindableElement["type"],
|
|
||||||
| "image"
|
|
||||||
| "text"
|
|
||||||
| "frame"
|
|
||||||
| "magicframe"
|
|
||||||
| "embeddable"
|
|
||||||
| "iframe"
|
|
||||||
>;
|
|
||||||
id?: ExcalidrawGenericElement["id"];
|
|
||||||
}
|
|
||||||
| {
|
|
||||||
id: ExcalidrawGenericElement["id"];
|
|
||||||
type?: Exclude<
|
|
||||||
ExcalidrawBindableElement["type"],
|
|
||||||
| "image"
|
|
||||||
| "text"
|
|
||||||
| "frame"
|
|
||||||
| "magicframe"
|
|
||||||
| "embeddable"
|
|
||||||
| "iframe"
|
|
||||||
>;
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| ((
|
|
||||||
| {
|
|
||||||
type: "text";
|
|
||||||
text: string;
|
|
||||||
}
|
|
||||||
| {
|
|
||||||
type?: "text";
|
|
||||||
id: ExcalidrawTextElement["id"];
|
|
||||||
text: string;
|
|
||||||
}
|
|
||||||
) &
|
|
||||||
Partial<ExcalidrawTextElement>)
|
|
||||||
) &
|
|
||||||
MarkOptional<ElementConstructorOpts, "x" | "y">;
|
|
||||||
start?:
|
|
||||||
| (
|
|
||||||
| (
|
|
||||||
| {
|
|
||||||
type: Exclude<
|
|
||||||
ExcalidrawBindableElement["type"],
|
|
||||||
| "image"
|
|
||||||
| "text"
|
|
||||||
| "frame"
|
|
||||||
| "magicframe"
|
|
||||||
| "embeddable"
|
|
||||||
| "iframe"
|
|
||||||
>;
|
|
||||||
id?: ExcalidrawGenericElement["id"];
|
|
||||||
}
|
|
||||||
| {
|
|
||||||
id: ExcalidrawGenericElement["id"];
|
|
||||||
type?: Exclude<
|
|
||||||
ExcalidrawBindableElement["type"],
|
|
||||||
| "image"
|
|
||||||
| "text"
|
|
||||||
| "frame"
|
|
||||||
| "magicframe"
|
|
||||||
| "embeddable"
|
|
||||||
| "iframe"
|
|
||||||
>;
|
|
||||||
}
|
|
||||||
)
|
|
||||||
| ((
|
|
||||||
| {
|
|
||||||
type: "text";
|
|
||||||
text: string;
|
|
||||||
}
|
|
||||||
| {
|
|
||||||
type?: "text";
|
|
||||||
id: ExcalidrawTextElement["id"];
|
|
||||||
text: string;
|
|
||||||
}
|
|
||||||
) &
|
|
||||||
Partial<ExcalidrawTextElement>)
|
|
||||||
) &
|
|
||||||
MarkOptional<ElementConstructorOpts, "x" | "y">;
|
|
||||||
} & Partial<ExcalidrawLinearElement>;
|
|
||||||
|
|
||||||
export type ValidContainer =
|
|
||||||
| {
|
|
||||||
type: Exclude<ExcalidrawGenericElement["type"], "selection">;
|
|
||||||
id?: ExcalidrawGenericElement["id"];
|
|
||||||
label?: {
|
|
||||||
text: string;
|
|
||||||
fontSize?: number;
|
|
||||||
fontFamily?: FontFamilyValues;
|
|
||||||
textAlign?: TextAlign;
|
|
||||||
verticalAlign?: VerticalAlign;
|
|
||||||
} & MarkOptional<ElementConstructorOpts, "x" | "y">;
|
|
||||||
} & ElementConstructorOpts;
|
|
||||||
|
|
||||||
export type ExcalidrawElementSkeleton =
|
|
||||||
| Extract<
|
|
||||||
Exclude<ExcalidrawElement, ExcalidrawSelectionElement>,
|
|
||||||
ExcalidrawIframeLikeElement | ExcalidrawFreeDrawElement
|
|
||||||
>
|
|
||||||
| ({
|
|
||||||
type: Extract<ExcalidrawLinearElement["type"], "line">;
|
|
||||||
x: number;
|
|
||||||
y: number;
|
|
||||||
} & Partial<ExcalidrawLinearElement>)
|
|
||||||
| ValidContainer
|
|
||||||
| ValidLinearElement
|
|
||||||
| ({
|
|
||||||
type: "text";
|
|
||||||
text: string;
|
|
||||||
x: number;
|
|
||||||
y: number;
|
|
||||||
id?: ExcalidrawTextElement["id"];
|
|
||||||
} & Partial<ExcalidrawTextElement>)
|
|
||||||
| ({
|
|
||||||
type: Extract<ExcalidrawImageElement["type"], "image">;
|
|
||||||
x: number;
|
|
||||||
y: number;
|
|
||||||
fileId: FileId;
|
|
||||||
} & Partial<ExcalidrawImageElement>)
|
|
||||||
| ({
|
|
||||||
type: "frame";
|
|
||||||
children: readonly ExcalidrawElement["id"][];
|
|
||||||
name?: string;
|
|
||||||
} & Partial<ExcalidrawFrameElement>)
|
|
||||||
| ({
|
|
||||||
type: "magicframe";
|
|
||||||
children: readonly ExcalidrawElement["id"][];
|
|
||||||
name?: string;
|
|
||||||
} & Partial<ExcalidrawMagicFrameElement>);
|
|
||||||
|
|
||||||
const DEFAULT_LINEAR_ELEMENT_PROPS = {
|
const DEFAULT_LINEAR_ELEMENT_PROPS = {
|
||||||
width: 100,
|
width: 100,
|
||||||
@@ -11,10 +11,13 @@ import type {
|
|||||||
import type {
|
import type {
|
||||||
MakeBrand,
|
MakeBrand,
|
||||||
MarkNonNullable,
|
MarkNonNullable,
|
||||||
|
MarkOptional,
|
||||||
Merge,
|
Merge,
|
||||||
ValueOf,
|
ValueOf,
|
||||||
} from "@excalidraw/common/utility-types";
|
} from "@excalidraw/common/utility-types";
|
||||||
|
|
||||||
|
import type { ElementConstructorOpts } from "./newElement";
|
||||||
|
|
||||||
export type ChartType = "bar" | "line";
|
export type ChartType = "bar" | "line";
|
||||||
export type FillStyle = "hachure" | "cross-hatch" | "solid" | "zigzag";
|
export type FillStyle = "hachure" | "cross-hatch" | "solid" | "zigzag";
|
||||||
export type FontFamilyKeys = keyof typeof FONT_FAMILY;
|
export type FontFamilyKeys = keyof typeof FONT_FAMILY;
|
||||||
@@ -433,3 +436,149 @@ export type ExcalidrawLinearElementSubType =
|
|||||||
export type ConvertibleGenericTypes = "rectangle" | "diamond" | "ellipse";
|
export type ConvertibleGenericTypes = "rectangle" | "diamond" | "ellipse";
|
||||||
export type ConvertibleLinearTypes = ExcalidrawLinearElementSubType;
|
export type ConvertibleLinearTypes = ExcalidrawLinearElementSubType;
|
||||||
export type ConvertibleTypes = ConvertibleGenericTypes | ConvertibleLinearTypes;
|
export type ConvertibleTypes = ConvertibleGenericTypes | ConvertibleLinearTypes;
|
||||||
|
|
||||||
|
export type ValidLinearElement = {
|
||||||
|
type: "arrow" | "line";
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
label?: {
|
||||||
|
text: string;
|
||||||
|
fontSize?: number;
|
||||||
|
fontFamily?: FontFamilyValues;
|
||||||
|
textAlign?: TextAlign;
|
||||||
|
verticalAlign?: VerticalAlign;
|
||||||
|
} & MarkOptional<ElementConstructorOpts, "x" | "y">;
|
||||||
|
end?:
|
||||||
|
| (
|
||||||
|
| (
|
||||||
|
| {
|
||||||
|
type: Exclude<
|
||||||
|
ExcalidrawBindableElement["type"],
|
||||||
|
| "image"
|
||||||
|
| "text"
|
||||||
|
| "frame"
|
||||||
|
| "magicframe"
|
||||||
|
| "embeddable"
|
||||||
|
| "iframe"
|
||||||
|
>;
|
||||||
|
id?: ExcalidrawGenericElement["id"];
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
id: ExcalidrawGenericElement["id"];
|
||||||
|
type?: Exclude<
|
||||||
|
ExcalidrawBindableElement["type"],
|
||||||
|
| "image"
|
||||||
|
| "text"
|
||||||
|
| "frame"
|
||||||
|
| "magicframe"
|
||||||
|
| "embeddable"
|
||||||
|
| "iframe"
|
||||||
|
>;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
| ((
|
||||||
|
| {
|
||||||
|
type: "text";
|
||||||
|
text: string;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
type?: "text";
|
||||||
|
id: ExcalidrawTextElement["id"];
|
||||||
|
text: string;
|
||||||
|
}
|
||||||
|
) &
|
||||||
|
Partial<ExcalidrawTextElement>)
|
||||||
|
) &
|
||||||
|
MarkOptional<ElementConstructorOpts, "x" | "y">;
|
||||||
|
start?:
|
||||||
|
| (
|
||||||
|
| (
|
||||||
|
| {
|
||||||
|
type: Exclude<
|
||||||
|
ExcalidrawBindableElement["type"],
|
||||||
|
| "image"
|
||||||
|
| "text"
|
||||||
|
| "frame"
|
||||||
|
| "magicframe"
|
||||||
|
| "embeddable"
|
||||||
|
| "iframe"
|
||||||
|
>;
|
||||||
|
id?: ExcalidrawGenericElement["id"];
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
id: ExcalidrawGenericElement["id"];
|
||||||
|
type?: Exclude<
|
||||||
|
ExcalidrawBindableElement["type"],
|
||||||
|
| "image"
|
||||||
|
| "text"
|
||||||
|
| "frame"
|
||||||
|
| "magicframe"
|
||||||
|
| "embeddable"
|
||||||
|
| "iframe"
|
||||||
|
>;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
| ((
|
||||||
|
| {
|
||||||
|
type: "text";
|
||||||
|
text: string;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
type?: "text";
|
||||||
|
id: ExcalidrawTextElement["id"];
|
||||||
|
text: string;
|
||||||
|
}
|
||||||
|
) &
|
||||||
|
Partial<ExcalidrawTextElement>)
|
||||||
|
) &
|
||||||
|
MarkOptional<ElementConstructorOpts, "x" | "y">;
|
||||||
|
} & Partial<ExcalidrawLinearElement>;
|
||||||
|
|
||||||
|
export type ValidContainer =
|
||||||
|
| {
|
||||||
|
type: Exclude<ExcalidrawGenericElement["type"], "selection">;
|
||||||
|
id?: ExcalidrawGenericElement["id"];
|
||||||
|
label?: {
|
||||||
|
text: string;
|
||||||
|
fontSize?: number;
|
||||||
|
fontFamily?: FontFamilyValues;
|
||||||
|
textAlign?: TextAlign;
|
||||||
|
verticalAlign?: VerticalAlign;
|
||||||
|
} & MarkOptional<ElementConstructorOpts, "x" | "y">;
|
||||||
|
} & ElementConstructorOpts;
|
||||||
|
|
||||||
|
export type ExcalidrawElementSkeleton =
|
||||||
|
| Extract<
|
||||||
|
Exclude<ExcalidrawElement, ExcalidrawSelectionElement>,
|
||||||
|
ExcalidrawIframeLikeElement | ExcalidrawFreeDrawElement
|
||||||
|
>
|
||||||
|
| ({
|
||||||
|
type: Extract<ExcalidrawLinearElement["type"], "line">;
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
} & Partial<ExcalidrawLinearElement>)
|
||||||
|
| ValidContainer
|
||||||
|
| ValidLinearElement
|
||||||
|
| ({
|
||||||
|
type: "text";
|
||||||
|
text: string;
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
id?: ExcalidrawTextElement["id"];
|
||||||
|
} & Partial<ExcalidrawTextElement>)
|
||||||
|
| ({
|
||||||
|
type: Extract<ExcalidrawImageElement["type"], "image">;
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
fileId: FileId;
|
||||||
|
} & Partial<ExcalidrawImageElement>)
|
||||||
|
| ({
|
||||||
|
type: "frame";
|
||||||
|
children: readonly ExcalidrawElement["id"][];
|
||||||
|
name?: string;
|
||||||
|
} & Partial<ExcalidrawFrameElement>)
|
||||||
|
| ({
|
||||||
|
type: "magicframe";
|
||||||
|
children: readonly ExcalidrawElement["id"][];
|
||||||
|
name?: string;
|
||||||
|
} & Partial<ExcalidrawMagicFrameElement>);
|
||||||
|
|||||||
@@ -248,6 +248,7 @@ import {
|
|||||||
doBoundsIntersect,
|
doBoundsIntersect,
|
||||||
isPointInElement,
|
isPointInElement,
|
||||||
maxBindingDistance_simple,
|
maxBindingDistance_simple,
|
||||||
|
convertToExcalidrawElements
|
||||||
} from "@excalidraw/element";
|
} from "@excalidraw/element";
|
||||||
|
|
||||||
import type { GlobalPoint, LocalPoint, Radians } from "@excalidraw/math";
|
import type { GlobalPoint, LocalPoint, Radians } from "@excalidraw/math";
|
||||||
@@ -276,6 +277,7 @@ import type {
|
|||||||
ExcalidrawElbowArrowElement,
|
ExcalidrawElbowArrowElement,
|
||||||
SceneElementsMap,
|
SceneElementsMap,
|
||||||
ExcalidrawBindableElement,
|
ExcalidrawBindableElement,
|
||||||
|
ExcalidrawElementSkeleton,
|
||||||
} from "@excalidraw/element/types";
|
} from "@excalidraw/element/types";
|
||||||
|
|
||||||
import type { Mutable, ValueOf } from "@excalidraw/common/utility-types";
|
import type { Mutable, ValueOf } from "@excalidraw/common/utility-types";
|
||||||
@@ -395,7 +397,6 @@ import {
|
|||||||
SnapCache,
|
SnapCache,
|
||||||
isGridModeEnabled,
|
isGridModeEnabled,
|
||||||
} from "../snapping";
|
} from "../snapping";
|
||||||
import { convertToExcalidrawElements } from "../data/transform";
|
|
||||||
import { Renderer } from "../scene/Renderer";
|
import { Renderer } from "../scene/Renderer";
|
||||||
import {
|
import {
|
||||||
setEraserCursor,
|
setEraserCursor,
|
||||||
@@ -457,7 +458,7 @@ import type { ClipboardData, PastedMixedContent } from "../clipboard";
|
|||||||
import type { ExportedElements } from "../data";
|
import type { ExportedElements } from "../data";
|
||||||
import type { ContextMenuItems } from "./ContextMenu";
|
import type { ContextMenuItems } from "./ContextMenu";
|
||||||
import type { FileSystemHandle } from "../data/filesystem";
|
import type { FileSystemHandle } from "../data/filesystem";
|
||||||
import type { ExcalidrawElementSkeleton } from "../data/transform";
|
|
||||||
import type {
|
import type {
|
||||||
AppClassProperties,
|
AppClassProperties,
|
||||||
AppProps,
|
AppProps,
|
||||||
|
|||||||
@@ -293,8 +293,11 @@ export { TTDDialog } from "./components/TTDDialog/TTDDialog";
|
|||||||
export { TTDDialogTrigger } from "./components/TTDDialog/TTDDialogTrigger";
|
export { TTDDialogTrigger } from "./components/TTDDialog/TTDDialogTrigger";
|
||||||
|
|
||||||
export { zoomToFitBounds } from "./actions/actionCanvas";
|
export { zoomToFitBounds } from "./actions/actionCanvas";
|
||||||
export { convertToExcalidrawElements } from "./data/transform";
|
export {
|
||||||
export { getCommonBounds, getVisibleSceneBounds } from "@excalidraw/element";
|
getCommonBounds,
|
||||||
|
getVisibleSceneBounds,
|
||||||
|
convertToExcalidrawElements,
|
||||||
|
} from "@excalidraw/element";
|
||||||
|
|
||||||
export {
|
export {
|
||||||
elementsOverlappingBBox,
|
elementsOverlappingBBox,
|
||||||
|
|||||||
@@ -3,6 +3,6 @@
|
|||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"outDir": "./dist/types"
|
"outDir": "./dist/types"
|
||||||
},
|
},
|
||||||
"include": ["**/*"],
|
"include": ["**/*", "../element/src/__tests__/transform.test.ts", "../element/src/transform.ts"],
|
||||||
"exclude": ["**/*.test.*", "tests", "types", "examples", "dist"]
|
"exclude": ["**/*.test.*", "tests", "types", "examples", "dist"]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user