mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-12-03 18:11:17 +03:00
* Initial commit * Segments detection * Remove aspect ratio * Refactor * Fix tests * Toggle shapeSnap * Fix linting * debug: live preview * Rebase fixes Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * Bind to elements (no hover background) * Shape closed * Fix rad calc + rename segments * Comments * Fix binding highlighting + lints * Rename to convert to shape * fix: Binding Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Refactor spline simplification Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Remove unused doBoundsIntersect Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Restore type signature Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Propose new button shapes Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Remove unused types Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Icon shapes Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Element cloning Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Element cloning Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Remove unused Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Shape settings in recognition mode Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Remove debug Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Actions Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Lint Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Arrowheads for freedraw Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Draw shape tool instead of freedraw Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Test Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Lint Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Keyboard shortcut visibility Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Unique cursor for draw shape Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Remove selection Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Shape selection Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Improve shape recognition Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Further refine shape recognition Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Preview Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Add hint Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Refined shape recognition Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Less ellipse more rectanguloid Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Refined arrow recognition Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Refinerecognition Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Lower linear threshold Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Rectangle head arrow Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: More arrowhead sizes Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Arrow remains an arrow Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Draw line between elements converts to arrow Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * remove pendingDrawShapeElement * fix: Arrow start point binding Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Different arrow endpoint heuristic Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Point type Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Various small fixes Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Avoid recompute Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Type issues Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * test(editor): cover the drawShape (snap-to-shape) tool The drawShape tool had no dedicated tests, which let master's toolbar / active-tool rewrite silently break it during the merge: actionFinalize reverted to the selection tool after every sketch, because the drawShape flow finalizes without a `newElement` and so failed master's new `&& element` guard. Covers the behavior the merge had to preserve: - sketch recognition (rectangle, ellipse) and the too-small no-op - the tool stays active after finalizing, so shapes can be sketched consecutively (regression guard for the above) - activation via the S shortcut and the extra-tools dropdown - the tool's own cursor Points are fed into the trail directly rather than through pointermove, which is rAF-throttled and would drop all but one point per frame — the same approach lasso.test.tsx uses. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat: Add test Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Breakout from App.tsx Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Shortcut is now Shift+X Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: No selecting arrows for shape recog Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Double history Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: PCA + feature extraction recognition Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * reduce and normalize min-size gates * never select newElement when shape recog tool active * simplify lifecycle & remove double finalize * make arrows bindings orbit * funnel though actionFinalize & fix two unrelated cleanup cases * use crosshair cursor * show strokeWidth * switch icons for drawShape & extraTools * dedupe getBoundsFromPoints * remove unused helpers * jsdoc cleanup * dynamic trail size * improve rectangle recognition * disallow linears to deviate too much from a straight line * put under freedraw tool on tablet/mobile * tweak label * rename `drawShape` -> `autoshape` --------- Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Co-authored-by: dwelle <5153846+dwelle@users.noreply.github.com> Co-authored-by: Mark Tolmacs <mark@lazycat.hu> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
309 lines
8.9 KiB
TypeScript
309 lines
8.9 KiB
TypeScript
import { arrayToMap, isShallowEqual, type Bounds } from "@excalidraw/common";
|
|
|
|
import type {
|
|
AppState,
|
|
BoxSelectionMode,
|
|
InteractiveCanvasAppState,
|
|
} from "@excalidraw/excalidraw/types";
|
|
|
|
import { elementsOverlappingBBox, getElementAbsoluteCoords } from "./bounds";
|
|
import { isElementInViewport } from "./sizeHelpers";
|
|
import {
|
|
isBoundToContainer,
|
|
isFrameLikeElement,
|
|
isLinearElement,
|
|
isTextElement,
|
|
} from "./typeChecks";
|
|
import { getFrameChildren } from "./frame";
|
|
|
|
import { LinearElementEditor } from "./linearElementEditor";
|
|
import { selectGroupsForSelectedElements } from "./groups";
|
|
|
|
import { isNonDeletedElement } from ".";
|
|
|
|
import type {
|
|
ElementsMap,
|
|
ElementsMapOrArray,
|
|
ExcalidrawElement,
|
|
ExcalidrawFrameLikeElement,
|
|
NonDeleted,
|
|
NonDeletedExcalidrawElement,
|
|
} from "./types";
|
|
|
|
const shouldIgnoreElementFromSelection = (element: ExcalidrawElement) =>
|
|
element.locked || isBoundToContainer(element);
|
|
|
|
const excludeElementsFromFrames = <T extends ExcalidrawElement>(
|
|
selectedElements: readonly T[],
|
|
framesInSelection: Set<ExcalidrawFrameLikeElement["id"]>,
|
|
) => {
|
|
return selectedElements.filter((element) => {
|
|
if (element.frameId && framesInSelection.has(element.frameId)) {
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Frames and their containing elements are not to be selected at the same time.
|
|
* Given an array of selected elements, if there are frames and their containing elements
|
|
* we only keep the frames.
|
|
* @param selectedElements
|
|
*/
|
|
export const excludeElementsInFramesFromSelection = <
|
|
T extends ExcalidrawElement,
|
|
>(
|
|
selectedElements: readonly T[],
|
|
) => {
|
|
const framesInSelection = new Set<T["id"]>();
|
|
|
|
selectedElements.forEach((element) => {
|
|
if (isFrameLikeElement(element)) {
|
|
framesInSelection.add(element.id);
|
|
}
|
|
});
|
|
|
|
return excludeElementsFromFrames(selectedElements, framesInSelection);
|
|
};
|
|
|
|
export const getElementsWithinSelection = <T extends ExcalidrawElement>(
|
|
elements: readonly T[],
|
|
selection: ExcalidrawElement,
|
|
elementsMap: ElementsMap,
|
|
// TODO remove (this flag is effectively unused AFAIK)
|
|
excludeElementsInFrames: boolean = true,
|
|
boxSelectionMode: BoxSelectionMode = "contain",
|
|
): T[] => {
|
|
const [selectionStartX, selectionStartY, selectionEndX, selectionEndY] =
|
|
getElementAbsoluteCoords(selection, elementsMap);
|
|
const selectionX1 = Math.min(selectionStartX, selectionEndX);
|
|
const selectionY1 = Math.min(selectionStartY, selectionEndY);
|
|
const selectionX2 = Math.max(selectionStartX, selectionEndX);
|
|
const selectionY2 = Math.max(selectionStartY, selectionEndY);
|
|
const selectionBounds = [
|
|
selectionX1,
|
|
selectionY1,
|
|
selectionX2,
|
|
selectionY2,
|
|
] as Bounds;
|
|
|
|
return elementsOverlappingBBox({
|
|
elements,
|
|
bounds: selectionBounds,
|
|
elementsMap,
|
|
type: boxSelectionMode,
|
|
shouldIgnoreElementFromSelection,
|
|
excludeElementsInFrames,
|
|
});
|
|
};
|
|
|
|
export const getVisibleAndNonSelectedElements = <
|
|
T extends NonDeletedExcalidrawElement,
|
|
>(
|
|
elements: readonly T[],
|
|
selectedElements: readonly ExcalidrawElement[],
|
|
appState: AppState,
|
|
elementsMap: ElementsMap,
|
|
) => {
|
|
const selectedElementsSet = new Set(
|
|
selectedElements.map((element) => element.id),
|
|
);
|
|
return elements.filter((element) => {
|
|
const isVisible = isElementInViewport(
|
|
element,
|
|
appState.width,
|
|
appState.height,
|
|
appState,
|
|
elementsMap,
|
|
);
|
|
|
|
return !selectedElementsSet.has(element.id) && isVisible;
|
|
});
|
|
};
|
|
|
|
// FIXME move this into the editor instance to keep utility methods stateless
|
|
export const isSomeElementSelected = (function () {
|
|
let lastElements: readonly NonDeletedExcalidrawElement[] | null = null;
|
|
let lastSelectedElementIds: AppState["selectedElementIds"] | null = null;
|
|
let isSelected: boolean | null = null;
|
|
|
|
const ret = (
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
|
appState: Pick<AppState, "selectedElementIds">,
|
|
): boolean => {
|
|
if (
|
|
isSelected != null &&
|
|
elements === lastElements &&
|
|
appState.selectedElementIds === lastSelectedElementIds
|
|
) {
|
|
return isSelected;
|
|
}
|
|
|
|
isSelected = elements.some(
|
|
(element) => appState.selectedElementIds[element.id],
|
|
);
|
|
lastElements = elements;
|
|
lastSelectedElementIds = appState.selectedElementIds;
|
|
|
|
return isSelected;
|
|
};
|
|
|
|
ret.clearCache = () => {
|
|
lastElements = null;
|
|
lastSelectedElementIds = null;
|
|
isSelected = null;
|
|
};
|
|
|
|
return ret;
|
|
})();
|
|
|
|
export const getSelectedElements = (
|
|
elements: ElementsMapOrArray,
|
|
appState: Pick<InteractiveCanvasAppState, "selectedElementIds">,
|
|
opts?: {
|
|
includeBoundTextElement?: boolean;
|
|
includeElementsInFrames?: boolean;
|
|
},
|
|
) => {
|
|
const addedElements = new Set<ExcalidrawElement["id"]>();
|
|
// selection can only contain non-deleted elements
|
|
const selectedElements: NonDeletedExcalidrawElement[] = [];
|
|
for (const element of elements.values()) {
|
|
if (appState.selectedElementIds[element.id]) {
|
|
if (isNonDeletedElement(element)) {
|
|
selectedElements.push(element as NonDeletedExcalidrawElement);
|
|
addedElements.add(element.id);
|
|
} else {
|
|
console.error(
|
|
"[NONDELETED][INVARIANT] getSelectedElements skipping deleted selected element which should not be in the selection",
|
|
);
|
|
}
|
|
continue;
|
|
}
|
|
if (
|
|
opts?.includeBoundTextElement &&
|
|
isBoundToContainer(element) &&
|
|
isNonDeletedElement(element) &&
|
|
appState.selectedElementIds[element?.containerId]
|
|
) {
|
|
selectedElements.push(element as NonDeletedExcalidrawElement);
|
|
addedElements.add(element.id);
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if (opts?.includeElementsInFrames) {
|
|
const elementsToInclude: NonDeletedExcalidrawElement[] = [];
|
|
selectedElements.forEach((element) => {
|
|
if (isFrameLikeElement(element)) {
|
|
getFrameChildren(elements, element.id).forEach(
|
|
(e) =>
|
|
!addedElements.has(e.id) &&
|
|
elementsToInclude.push(e as NonDeletedExcalidrawElement),
|
|
);
|
|
}
|
|
elementsToInclude.push(element);
|
|
});
|
|
|
|
return elementsToInclude;
|
|
}
|
|
|
|
return selectedElements;
|
|
};
|
|
|
|
export const getTargetElements = (
|
|
elements: ElementsMapOrArray,
|
|
appState: Pick<
|
|
AppState,
|
|
"selectedElementIds" | "editingTextElement" | "newElement" | "activeTool"
|
|
>,
|
|
) =>
|
|
appState.editingTextElement
|
|
? [appState.editingTextElement]
|
|
: // the drawShape recognition preview in `newElement` is not a real
|
|
// in-progress element — targeting it would make the styles panel track
|
|
// whatever shape is currently recognized instead of the tool defaults
|
|
appState.newElement && appState.activeTool.type !== "autoshape"
|
|
? [appState.newElement]
|
|
: getSelectedElements(elements, appState, {
|
|
includeBoundTextElement: true,
|
|
});
|
|
|
|
/**
|
|
* returns prevState's selectedElementids if no change from previous, so as to
|
|
* retain reference identity for memoization
|
|
*/
|
|
export const makeNextSelectedElementIds = (
|
|
nextSelectedElementIds: AppState["selectedElementIds"],
|
|
prevState: Pick<AppState, "selectedElementIds">,
|
|
) => {
|
|
if (isShallowEqual(prevState.selectedElementIds, nextSelectedElementIds)) {
|
|
return prevState.selectedElementIds;
|
|
}
|
|
|
|
return nextSelectedElementIds;
|
|
};
|
|
|
|
const _getLinearElementEditor = (
|
|
targetElements: readonly NonDeletedExcalidrawElement[],
|
|
allElements: readonly NonDeletedExcalidrawElement[],
|
|
) => {
|
|
const linears = targetElements.filter(isLinearElement);
|
|
if (linears.length === 1) {
|
|
const linear = linears[0];
|
|
const boundElements = linear.boundElements?.map((def) => def.id) ?? [];
|
|
const onlySingleLinearSelected = targetElements.every(
|
|
(el) => el.id === linear.id || boundElements.includes(el.id),
|
|
);
|
|
|
|
if (onlySingleLinearSelected) {
|
|
return new LinearElementEditor(linear, arrayToMap(allElements));
|
|
}
|
|
}
|
|
|
|
return null;
|
|
};
|
|
|
|
export const getSelectionStateForElements = (
|
|
targetElements: readonly NonDeletedExcalidrawElement[],
|
|
allElements: readonly NonDeletedExcalidrawElement[],
|
|
appState: AppState,
|
|
) => {
|
|
return {
|
|
selectedLinearElement: _getLinearElementEditor(targetElements, allElements),
|
|
...selectGroupsForSelectedElements(
|
|
{
|
|
editingGroupId: appState.editingGroupId,
|
|
selectedElementIds: excludeElementsInFramesFromSelection(
|
|
targetElements,
|
|
).reduce((acc: Record<ExcalidrawElement["id"], true>, element) => {
|
|
if (!isBoundToContainer(element)) {
|
|
acc[element.id] = true;
|
|
}
|
|
return acc;
|
|
}, {}),
|
|
},
|
|
allElements,
|
|
appState,
|
|
null,
|
|
),
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Returns editing or single-selected text element, if any.
|
|
*/
|
|
export const getActiveTextElement = (
|
|
selectedElements: readonly NonDeleted<ExcalidrawElement>[],
|
|
appState: Pick<AppState, "editingTextElement">,
|
|
) => {
|
|
const activeTextElement =
|
|
appState.editingTextElement ||
|
|
(selectedElements.length === 1 &&
|
|
isTextElement(selectedElements[0]) &&
|
|
selectedElements[0]);
|
|
|
|
return activeTextElement || null;
|
|
};
|