fix: Refactor linearElementEditor to not use types outside the package

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
This commit is contained in:
Mark Tolmacs committed 2026-11-17 16:11:16 +01:00
1 parent fa581b819b
commit 28053c00a5
8 files changed
+68 -54

No files matched your search

+6 -2
View File
@@ -1,3 +1,7 @@
/// <reference types="vite/client" />
import "@excalidraw/excalidraw/global";
import "@excalidraw/excalidraw/css";
/// <reference path="../common/global.d.ts" />
interface Window {
// set to debug fractional index validation in the scene
DEBUG_FRACTIONAL_INDICES: boolean | undefined;
}
-1
View File
@@ -95,7 +95,6 @@
"roughjs": "4.6.4"
},
"devDependencies": {
"@excalidraw/excalidraw": "workspace:*",
"@excalidraw/laser-pointer": "workspace:*",
"@testing-library/dom": "10.4.0",
"@testing-library/react": "16.2.0",
+4 -15
View File
@@ -1,27 +1,16 @@
import type { NullableGridSize } from "@excalidraw/common";
import { LinearElementEditor } from "../linearElementEditor";
import { handleFocusPointDrag } from "./focus";
import type { Scene } from "../Scene";
import type { ElementsState } from "../types";
import type { LinearElementEditorHost } from "../linearElementEditor";
/**
* What arrow point dragging needs from the editor. Point dragging itself
* (`LinearElementEditor.handlePointDragging`) still takes the whole editor.
* What arrow point dragging needs from the editor: point dragging's own needs
* (`LinearElementEditorHost`) plus the last pointer position and event.
*/
export type ArrowPointDragHost = Parameters<
typeof LinearElementEditor.handlePointDragging
>[1] & {
state: Pick<
ElementsState,
"selectedLinearElement" | "isBindingEnabled" | "zoom"
>;
scene: Scene;
export type ArrowPointDragHost = LinearElementEditorHost & {
lastPointerMoveCoords: { x: number; y: number } | null;
lastPointerMoveEvent: PointerEvent | null;
getEffectiveGridSize(): NullableGridSize;
};
export const maybeHandleArrowPointlikeDrag = ({
+53 -23
View File
@@ -30,15 +30,8 @@ import {
import type { Radians } from "@excalidraw/math";
import type {
AppState,
InteractiveCanvasAppState,
AppClassProperties,
} from "@excalidraw/excalidraw/types";
import type { Bounds } from "@excalidraw/common";
import type { Store } from "@excalidraw/excalidraw/store";
import type { NullableGridSize, Zoom } from "@excalidraw/common";
import { getCurvePathOps } from "./geometricShapes";
@@ -96,6 +89,7 @@ import type {
} from "./types";
import type { PointerCoords } from "./types";
import type { ElementsState } from "./types";
import type { RenderEnvironment } from "./renderEnvironment";
/**
* Normalizes line points so that the start point is at [0,0]. This is
@@ -141,6 +135,40 @@ const BoundTextPositionCache = new WeakMap<
}
>();
/**
* What the linear element pointer handlers need from the editor. The editor
* (`App`) satisfies it.
*/
export type LinearElementEditorHost = {
state: Pick<
ElementsState,
| "bindMode"
| "gridModeEnabled"
| "gridSize"
| "isBindingEnabled"
| "isMidpointSnappingEnabled"
| "newElement"
| "selectedLinearElement"
| "suggestedBinding"
| "zoom"
>;
scene: Scene;
renderEnvironment: RenderEnvironment;
getEffectiveGridSize(): NullableGridSize;
arrowText: {
isBoundTextGrabbable(
element: NonDeletedExcalidrawElement,
x: number,
y: number,
): boolean;
};
};
/** The history store, as far as point editing is concerned. */
export type LinearElementEditorStore = {
scheduleCapture(): void;
};
export class LinearElementEditor {
public readonly elementId: ExcalidrawElement["id"] & {
_brand: "excalidrawLinearElementId";
@@ -248,8 +276,8 @@ export class LinearElementEditor {
static handleBoxSelection(
event: PointerEvent,
appState: AppState,
setState: React.Component<any, AppState>["setState"],
appState: Pick<ElementsState, "selectedLinearElement" | "selectionElement">,
setState: (state: Pick<ElementsState, "selectedLinearElement">) => void,
elementsMap: NonDeletedSceneElementsMap,
) {
if (
@@ -311,7 +339,7 @@ export class LinearElementEditor {
static handlePointerMove(
event: PointerEvent,
app: AppClassProperties,
app: LinearElementEditorHost,
scenePointerX: number,
scenePointerY: number,
linearElementEditor: LinearElementEditor,
@@ -470,7 +498,7 @@ export class LinearElementEditor {
static handlePointDragging(
event: PointerEvent,
app: AppClassProperties,
app: LinearElementEditorHost,
scenePointerX: number,
scenePointerY: number,
linearElementEditor: LinearElementEditor,
@@ -726,7 +754,7 @@ export class LinearElementEditor {
static handlePointerUp(
event: PointerEvent,
editingLinearElement: LinearElementEditor,
appState: AppState,
appState: Pick<ElementsState, "zoom">,
scene: Scene,
): LinearElementEditor {
const elementsMap = scene.getNonDeletedElementsMap();
@@ -818,7 +846,7 @@ export class LinearElementEditor {
static getEditorMidPoints = (
element: ExcalidrawLinearElement,
elementsMap: ElementsMap,
appState: InteractiveCanvasAppState,
appState: Pick<ElementsState, "selectedLinearElement" | "zoom">,
): (GlobalPoint | null)[] => {
const boundText = getBoundTextElement(element, elementsMap);
@@ -869,7 +897,7 @@ export class LinearElementEditor {
static getSegmentMidpointHitCoords = (
linearElementEditor: LinearElementEditor,
scenePointer: { x: number; y: number },
appState: AppState,
appState: Pick<ElementsState, "selectedLinearElement" | "zoom">,
elementsMap: ElementsMap,
): GlobalPoint | null => {
const { elementId } = linearElementEditor;
@@ -1026,7 +1054,7 @@ export class LinearElementEditor {
static getSegmentMidPointIndex(
linearElementEditor: LinearElementEditor,
appState: AppState,
appState: Pick<ElementsState, "selectedLinearElement" | "zoom">,
midPoint: GlobalPoint,
elementsMap: ElementsMap,
) {
@@ -1054,8 +1082,8 @@ export class LinearElementEditor {
static handlePointerDown(
event: React.PointerEvent<HTMLElement>,
app: AppClassProperties,
store: Store,
app: LinearElementEditorHost,
store: LinearElementEditorStore,
scenePointer: { x: number; y: number },
linearElementEditor: LinearElementEditor,
scene: Scene,
@@ -1266,7 +1294,7 @@ export class LinearElementEditor {
event: React.PointerEvent<HTMLCanvasElement>,
scenePointerX: number,
scenePointerY: number,
app: AppClassProperties,
app: LinearElementEditorHost,
): LinearElementEditor | null {
const appState = app.state;
if (!appState.selectedLinearElement?.isEditing) {
@@ -1504,7 +1532,9 @@ export class LinearElementEditor {
// element-mutating methods
// ---------------------------------------------------------------------------
static duplicateSelectedPoints(appState: AppState, scene: Scene): AppState {
static duplicateSelectedPoints<
T extends Pick<ElementsState, "selectedLinearElement">,
>(appState: T, scene: Scene): T {
invariant(
appState.selectedLinearElement?.isEditing,
"Not currently editing a linear element",
@@ -1582,7 +1612,7 @@ export class LinearElementEditor {
static deletePoints(
element: NonDeleted<ExcalidrawLinearElement>,
app: AppClassProperties,
app: LinearElementEditorHost,
pointIndices: readonly number[],
) {
const isUncommittedPoint =
@@ -1743,7 +1773,7 @@ export class LinearElementEditor {
static shouldAddMidpoint(
linearElementEditor: LinearElementEditor,
pointerCoords: PointerCoords,
appState: AppState,
appState: Pick<ElementsState, "selectedLinearElement" | "zoom">,
elementsMap: ElementsMap,
) {
const element = LinearElementEditor.getElement(
@@ -1788,7 +1818,7 @@ export class LinearElementEditor {
static addMidpoint(
linearElementEditor: LinearElementEditor,
pointerCoords: PointerCoords,
app: AppClassProperties,
app: LinearElementEditorHost,
snapToGrid: boolean,
scene: Scene,
) {
@@ -2410,7 +2440,7 @@ const pointDraggingUpdates = (
elementsMap: NonDeletedSceneElementsMap,
element: NonDeleted<ExcalidrawLinearElement>,
elements: readonly Ordered<NonDeletedExcalidrawElement>[],
app: AppClassProperties,
app: LinearElementEditorHost,
angleLocked: boolean,
altKey: boolean,
linearElementEditor: LinearElementEditor,
+5
View File
@@ -719,6 +719,11 @@ export interface ElementsState {
gridSize: number;
gridModeEnabled: boolean;
shouldCacheIgnoreZoom: boolean;
/**
* decoupled from newElement, dragging selection only creates selectionElement
* - set on pointer down, updated during pointer move
*/
selectionElement: NonDeletedExcalidrawElement | null;
/** the open dialog, if any (`AppState` narrows this to its dialogs) */
openDialog: { name: string } | null;
}
-5
View File
@@ -339,11 +339,6 @@ export interface AppState extends ElementsState {
* - when set and present, the editor will handle linear element creation logic accordingly
*/
multiElement: NonDeleted<ExcalidrawLinearElement> | null;
/**
* decoupled from newElement, dragging selection only creates selectionElement
* - set on pointer down, updated during pointer move
*/
selectionElement: NonDeletedExcalidrawElement | null;
/** user box selection preference; defaults to "contain" when unset */
boxSelectionMode: BoxSelectionMode;
/** user arrow binding preference */
-3
View File
@@ -337,9 +337,6 @@ importers:
specifier: 4.6.4
version: 4.6.4
devDependencies:
'@excalidraw/excalidraw':
specifier: workspace:*
version: link:../excalidraw
'@excalidraw/laser-pointer':
specifier: workspace:*
version: link:../laser-pointer
-5
View File
@@ -12,8 +12,3 @@ onlyBuiltDependencies:
- core-js
- esbuild
- protobufjs
# Type- and test-only edges close a cycle (element imports editor types from
# excalidraw, and its tests use excalidraw's test harness). Runtime deps are
# acyclic.
ignoreWorkspaceCycles: true