mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-09-26 02:40:07 +02:00

Tests added Fix binding Remove unneeded params Unfinished simple arrow avoidance Fix newly created jumping arrow when gets outside Do not apply the jumping logic to elbow arrows for new elements Existing arrows now jump out Type updates to support fixed binding for simple arrows Fix crash for elbow arrws in mutateElement() Refactored simple arrow creation Updating tests No confirm threshold when inside biding range Fix multi-point arrow grid off Make elbow arrows respect grids Unbind arrow if bound and moved at shaft of arrow key Fix binding test Fix drag unbind when the bound element is in the selection Do not move mid point for simple arrows bound on both ends Add test for mobing mid points for simple arrows when bound on the same element on both ends Fix linear editor bug when both midpoint and endpoint is moved Fix all point multipoint arrow highlight and binding Arrow dragging gets a little drag to avoid accidental unbinding Fixed point binding for simple arrows when the arrow doesn't point to the element Fix binding disabled use-case triggering arrow editor Timed binding mode change for simple arrows Apply fixes Remove code to unbind on drag Update simple arrow fixed point when arrow is dragged or moved by arrow keys Binding highlight fixes Change bind mode timeout logic Fix tests Add Alt bindMode switch No dragging of arrows when bound, similar to elbow Fix timeout not taking effect immediately Bumop z-index for arrows when dragged Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Only transparent bindables allow binding fallthrough Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Fix lint Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Fix point click array creation interaction with fixed point binding Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Restrict new behavior to arrows only Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Allow binding inside images Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Fix already existing fixed binding retention Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Refactor and implement fixed point binding for unfilled elements Restore drag Removed point binding Binding code refactor Added centered focus point Binding & focus point debug Add invariants to check binding integrity in elements Binding fixes Small refactors Completely rewritten binding Include point updates after binding update Fix point updates when endpoint dragged and opposite endpoint orbits centered focus point only for new arrows Make z-index arrow reorder on bind Turn off inside binding mode after leaving a shape Remove invariants from debug feat: expose `applyTo` options, don't commit empty text element (#9744) * Expose applyTo options, skip re-draw for empty text * Don't commit empty text elements test: added test file for distribute (#9754) z-index update Bind mode on precise binding Fix binding to inside element Fix initial arrow not following cursor (white dot) Fix elbow arrow Fix z-index so it works on hover Fix fixed angle orbiting Move point click arrow creation over to common strategy Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Add binding strategy for drag arrow creation Fix elbow arrow Fix point handles Snap to center Fix transparent shape binding Internal arrow creation fix Fix point binding Fix selection bug Fix new arrow focus point Images now always bind inside Flashing arrow creation on binding band Add watchState debug method to window.h Fix debug canvas crash Remove non-needed bind mode Fix restore No keyboard movement when bound Add actionFinalize when arrow in edit mode Add drag to the Stats panel when bound arrow is moved Further simplify curve tracking Add typing to action register() Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Fix point at finalize Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Fix type errors Signed-off-by: Mark Tolmacs <mark@lazycat.hu> New arrow binding rules Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Fix cyclical dep Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Fix jiggly arrows Fix jiggly arrow x2 Long inside-other binding Click-click binding Fix arrows Performance [PERF] Replace in-place Jacobian derivation with analytical version Different approach to inside binding Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Fixes Fix inconsistent arrow start jump out Change how images are bound to on new arrow creation Lower timeout Small insurance fix Fix curve test Signed-off-by: Mark Tolmacs <mark@lazycat.hu> No center focus point 90% inside center binding Fixing tests fix: Elbow arrow fixes fix: More arrow fixes Do not trigger arrow binding for linear elements fix: Linear elements fix: Refactor actionFinalize for linear Binding tests updated fix: Jump when cursor not moved fix: history tests Fix history snapshot Fix undo issue fix(eraser): Remove binding from the other element fix(tests): Update tests chore: Attempt filtering new set state Fix excessive history recording Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Fix all tests Signed-off-by: Mark Tolmacs <mark@lazycat.hu> fix(transform): Fix group resize and rotate fix(binding): Harmonize binding param usage fix: Center focus point Signed-off-by: Mark Tolmacs <mark@lazycat.hu> chore: Trigger build Remove binding gap Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Binding highlight refactor fix: Refactored timeout bind mode handling fix: Center when orbiting feat: Color change on highlight Fix orbit binding highlight fix: hiding arrow Fix arrow binding Fix arrow drag selection logic Binding highlight is now hot pink Change inside binding logic for start point Render focus point in debug mode Fix snap to center Fix actionFinalize for new arrow creation fix: snapToCenter() 80% by length fix: attempt at fixing the dancing arrows feat: No center snap when start is not bound Fix centering for existing arrows tweak binding highlight color change `appState.suggestedBindings` -> `suggestedBinding` & remove unused code Refactor delayed bind mode change Binding highlight rotation support + image support fix(highlight): Overdraw fixes feat: Do not allow drag bound arrow closer to the shape than dragging distance feat: Stroke width adaptive fixed binding distance chore: More point dragging centralization New element behavior Refactor dragging Fix incorrect highlight sizing Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Fix delayed bind mode for multiElement arrows Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Fix multi-point Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Fix elbow arrows Simplify state Small positional fixes Fix jiggly arrows Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Fixes for arrow dragging Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Elbow arrow fixes Highlight fixes Fix elbow arrow binding Frame highlight Fix elbow mid-point binding Fix binding suggestion for disabled binding state Implement Alt Remove debug
354 lines
9.3 KiB
TypeScript
354 lines
9.3 KiB
TypeScript
import {
|
|
TEXT_AUTOWRAP_THRESHOLD,
|
|
getGridPoint,
|
|
getFontString,
|
|
DRAGGING_THRESHOLD,
|
|
} from "@excalidraw/common";
|
|
|
|
import type {
|
|
AppState,
|
|
NormalizedZoomValue,
|
|
NullableGridSize,
|
|
PointerDownState,
|
|
} from "@excalidraw/excalidraw/types";
|
|
|
|
import type { NonDeletedExcalidrawElement } from "@excalidraw/element/types";
|
|
|
|
import { unbindBindingElement, updateBoundElements } from "./binding";
|
|
import { getCommonBounds } from "./bounds";
|
|
import { getPerfectElementSize } from "./sizeHelpers";
|
|
import { getBoundTextElement } from "./textElement";
|
|
import { getMinTextElementWidth } from "./textMeasurements";
|
|
import {
|
|
isArrowElement,
|
|
isElbowArrow,
|
|
isFrameLikeElement,
|
|
isImageElement,
|
|
isTextElement,
|
|
} from "./typeChecks";
|
|
|
|
import type { Scene } from "./Scene";
|
|
|
|
import type { Bounds } from "./bounds";
|
|
import type { ExcalidrawElement } from "./types";
|
|
|
|
export const dragSelectedElements = (
|
|
pointerDownState: PointerDownState,
|
|
_selectedElements: NonDeletedExcalidrawElement[],
|
|
offset: { x: number; y: number },
|
|
scene: Scene,
|
|
snapOffset: {
|
|
x: number;
|
|
y: number;
|
|
},
|
|
gridSize: NullableGridSize,
|
|
) => {
|
|
if (
|
|
_selectedElements.length === 1 &&
|
|
isElbowArrow(_selectedElements[0]) &&
|
|
(_selectedElements[0].startBinding || _selectedElements[0].endBinding)
|
|
) {
|
|
return;
|
|
}
|
|
|
|
const selectedElements = _selectedElements.filter((element) => {
|
|
if (isElbowArrow(element) && element.startBinding && element.endBinding) {
|
|
const startElement = _selectedElements.find(
|
|
(el) => el.id === element.startBinding?.elementId,
|
|
);
|
|
const endElement = _selectedElements.find(
|
|
(el) => el.id === element.endBinding?.elementId,
|
|
);
|
|
|
|
return startElement && endElement;
|
|
}
|
|
|
|
return true;
|
|
});
|
|
|
|
// we do not want a frame and its elements to be selected at the same time
|
|
// but when it happens (due to some bug), we want to avoid updating element
|
|
// in the frame twice, hence the use of set
|
|
const elementsToUpdate = new Set<NonDeletedExcalidrawElement>(
|
|
selectedElements,
|
|
);
|
|
const frames = selectedElements
|
|
.filter((e) => isFrameLikeElement(e))
|
|
.map((f) => f.id);
|
|
|
|
if (frames.length > 0) {
|
|
for (const element of scene.getNonDeletedElements()) {
|
|
if (element.frameId !== null && frames.includes(element.frameId)) {
|
|
elementsToUpdate.add(element);
|
|
}
|
|
}
|
|
}
|
|
|
|
const origElements: ExcalidrawElement[] = [];
|
|
|
|
for (const element of elementsToUpdate) {
|
|
const origElement = pointerDownState.originalElements.get(element.id);
|
|
// if original element is not set (e.g. when you duplicate during a drag
|
|
// operation), exit to avoid undefined behavior
|
|
if (!origElement) {
|
|
return;
|
|
}
|
|
origElements.push(origElement);
|
|
}
|
|
|
|
const adjustedOffset = calculateOffset(
|
|
getCommonBounds(origElements),
|
|
offset,
|
|
snapOffset,
|
|
gridSize,
|
|
);
|
|
|
|
const elementsToUpdateIds = new Set(
|
|
Array.from(elementsToUpdate, (el) => el.id),
|
|
);
|
|
|
|
elementsToUpdate.forEach((element) => {
|
|
const isArrow = !isArrowElement(element);
|
|
const isStartBoundElementSelected =
|
|
isArrow ||
|
|
(element.startBinding
|
|
? elementsToUpdateIds.has(element.startBinding.elementId)
|
|
: false);
|
|
const isEndBoundElementSelected =
|
|
isArrow ||
|
|
(element.endBinding
|
|
? elementsToUpdateIds.has(element.endBinding.elementId)
|
|
: false);
|
|
|
|
if (!isArrowElement(element)) {
|
|
updateElementCoords(pointerDownState, element, scene, adjustedOffset);
|
|
|
|
// skip arrow labels since we calculate its position during render
|
|
const textElement = getBoundTextElement(
|
|
element,
|
|
scene.getNonDeletedElementsMap(),
|
|
);
|
|
if (textElement) {
|
|
updateElementCoords(
|
|
pointerDownState,
|
|
textElement,
|
|
scene,
|
|
adjustedOffset,
|
|
);
|
|
}
|
|
updateBoundElements(element, scene, {
|
|
simultaneouslyUpdated: Array.from(elementsToUpdate),
|
|
});
|
|
} else if (
|
|
// NOTE: Add a little initial drag to the arrow dragging when the arrow
|
|
// is the single element being dragged to avoid accidentally unbinding
|
|
// the arrow when the user just wants to select it.
|
|
|
|
elementsToUpdate.size > 1 ||
|
|
Math.max(Math.abs(adjustedOffset.x), Math.abs(adjustedOffset.y)) >
|
|
DRAGGING_THRESHOLD ||
|
|
(!element.startBinding && !element.endBinding)
|
|
) {
|
|
updateElementCoords(pointerDownState, element, scene, adjustedOffset);
|
|
|
|
const shouldUnbindStart =
|
|
element.startBinding && !isStartBoundElementSelected;
|
|
const shouldUnbindEnd = element.endBinding && !isEndBoundElementSelected;
|
|
if (shouldUnbindStart || shouldUnbindEnd) {
|
|
// NOTE: Moving the bound arrow should unbind it, otherwise we would
|
|
// have weird situations, like 0 lenght arrow when the user moves
|
|
// the arrow outside a filled shape suddenly forcing the arrow start
|
|
// and end point to jump "outside" the shape.
|
|
if (shouldUnbindStart) {
|
|
unbindBindingElement(element, "start", scene);
|
|
}
|
|
if (shouldUnbindEnd) {
|
|
unbindBindingElement(element, "end", scene);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
};
|
|
|
|
const calculateOffset = (
|
|
commonBounds: Bounds,
|
|
dragOffset: { x: number; y: number },
|
|
snapOffset: { x: number; y: number },
|
|
gridSize: NullableGridSize,
|
|
): { x: number; y: number } => {
|
|
const [x, y] = commonBounds;
|
|
let nextX = x + dragOffset.x + snapOffset.x;
|
|
let nextY = y + dragOffset.y + snapOffset.y;
|
|
|
|
if (snapOffset.x === 0 || snapOffset.y === 0) {
|
|
const [nextGridX, nextGridY] = getGridPoint(
|
|
x + dragOffset.x,
|
|
y + dragOffset.y,
|
|
gridSize,
|
|
);
|
|
|
|
if (snapOffset.x === 0) {
|
|
nextX = nextGridX;
|
|
}
|
|
|
|
if (snapOffset.y === 0) {
|
|
nextY = nextGridY;
|
|
}
|
|
}
|
|
return {
|
|
x: nextX - x,
|
|
y: nextY - y,
|
|
};
|
|
};
|
|
|
|
const updateElementCoords = (
|
|
pointerDownState: PointerDownState,
|
|
element: NonDeletedExcalidrawElement,
|
|
scene: Scene,
|
|
dragOffset: { x: number; y: number },
|
|
) => {
|
|
const originalElement =
|
|
pointerDownState.originalElements.get(element.id) ?? element;
|
|
|
|
const nextX = originalElement.x + dragOffset.x;
|
|
const nextY = originalElement.y + dragOffset.y;
|
|
|
|
scene.mutateElement(element, {
|
|
x: nextX,
|
|
y: nextY,
|
|
});
|
|
};
|
|
|
|
export const getDragOffsetXY = (
|
|
selectedElements: NonDeletedExcalidrawElement[],
|
|
x: number,
|
|
y: number,
|
|
): [number, number] => {
|
|
const [x1, y1] = getCommonBounds(selectedElements);
|
|
return [x - x1, y - y1];
|
|
};
|
|
|
|
export const dragNewElement = ({
|
|
newElement,
|
|
elementType,
|
|
originX,
|
|
originY,
|
|
x,
|
|
y,
|
|
width,
|
|
height,
|
|
shouldMaintainAspectRatio,
|
|
shouldResizeFromCenter,
|
|
zoom,
|
|
scene,
|
|
widthAspectRatio = null,
|
|
originOffset = null,
|
|
informMutation = true,
|
|
}: {
|
|
newElement: NonDeletedExcalidrawElement;
|
|
elementType: AppState["activeTool"]["type"];
|
|
originX: number;
|
|
originY: number;
|
|
x: number;
|
|
y: number;
|
|
width: number;
|
|
height: number;
|
|
shouldMaintainAspectRatio: boolean;
|
|
shouldResizeFromCenter: boolean;
|
|
zoom: NormalizedZoomValue;
|
|
scene: Scene;
|
|
/** whether to keep given aspect ratio when `isResizeWithSidesSameLength` is
|
|
true */
|
|
widthAspectRatio?: number | null;
|
|
originOffset?: {
|
|
x: number;
|
|
y: number;
|
|
} | null;
|
|
informMutation?: boolean;
|
|
}) => {
|
|
if (shouldMaintainAspectRatio && newElement.type !== "selection") {
|
|
if (widthAspectRatio) {
|
|
height = width / widthAspectRatio;
|
|
} else {
|
|
// Depending on where the cursor is at (x, y) relative to where the starting point is
|
|
// (originX, originY), we use ONLY width or height to control size increase.
|
|
// This allows the cursor to always "stick" to one of the sides of the bounding box.
|
|
if (Math.abs(y - originY) > Math.abs(x - originX)) {
|
|
({ width, height } = getPerfectElementSize(
|
|
elementType,
|
|
height,
|
|
x < originX ? -width : width,
|
|
));
|
|
} else {
|
|
({ width, height } = getPerfectElementSize(
|
|
elementType,
|
|
width,
|
|
y < originY ? -height : height,
|
|
));
|
|
}
|
|
|
|
if (height < 0) {
|
|
height = -height;
|
|
}
|
|
}
|
|
}
|
|
|
|
let newX = x < originX ? originX - width : originX;
|
|
let newY = y < originY ? originY - height : originY;
|
|
|
|
if (shouldResizeFromCenter) {
|
|
width += width;
|
|
height += height;
|
|
newX = originX - width / 2;
|
|
newY = originY - height / 2;
|
|
}
|
|
|
|
let textAutoResize = null;
|
|
|
|
if (isTextElement(newElement)) {
|
|
height = newElement.height;
|
|
const minWidth = getMinTextElementWidth(
|
|
getFontString({
|
|
fontSize: newElement.fontSize,
|
|
fontFamily: newElement.fontFamily,
|
|
}),
|
|
newElement.lineHeight,
|
|
);
|
|
width = Math.max(width, minWidth);
|
|
|
|
if (Math.abs(x - originX) > TEXT_AUTOWRAP_THRESHOLD / zoom) {
|
|
textAutoResize = {
|
|
autoResize: false,
|
|
};
|
|
}
|
|
|
|
newY = originY;
|
|
if (shouldResizeFromCenter) {
|
|
newX = originX - width / 2;
|
|
}
|
|
}
|
|
|
|
if (width !== 0 && height !== 0) {
|
|
let imageInitialDimension = null;
|
|
if (isImageElement(newElement)) {
|
|
imageInitialDimension = {
|
|
initialWidth: width,
|
|
initialHeight: height,
|
|
};
|
|
}
|
|
|
|
scene.mutateElement(
|
|
newElement,
|
|
{
|
|
x: newX + (originOffset?.x ?? 0),
|
|
y: newY + (originOffset?.y ?? 0),
|
|
width,
|
|
height,
|
|
...textAutoResize,
|
|
...imageInitialDimension,
|
|
},
|
|
{ informMutation, isDragging: false },
|
|
);
|
|
}
|
|
};
|