Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b3a58b6c2d |
@@ -1,4 +1,4 @@
|
|||||||
FROM node:24-bullseye
|
FROM node:18-bullseye
|
||||||
|
|
||||||
# Vite wants to open the browser using `open`, so we
|
# Vite wants to open the browser using `open`, so we
|
||||||
# need to install those utils.
|
# need to install those utils.
|
||||||
|
|||||||
+2
-2
@@ -1,4 +1,4 @@
|
|||||||
FROM --platform=${BUILDPLATFORM} node:24 AS build
|
FROM --platform=${BUILDPLATFORM} node:18 AS build
|
||||||
|
|
||||||
WORKDIR /opt/node_app
|
WORKDIR /opt/node_app
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ ARG NODE_ENV=production
|
|||||||
|
|
||||||
RUN npm_config_target_arch=${TARGETARCH} yarn build:app:docker
|
RUN npm_config_target_arch=${TARGETARCH} yarn build:app:docker
|
||||||
|
|
||||||
FROM nginx:1.27-alpine
|
FROM --platform=${TARGETPLATFORM} nginx:1.27-alpine
|
||||||
|
|
||||||
COPY --from=build /opt/node_app/excalidraw-app/build /usr/share/nginx/html
|
COPY --from=build /opt/node_app/excalidraw-app/build /usr/share/nginx/html
|
||||||
|
|
||||||
|
|||||||
@@ -97,8 +97,8 @@ const config = {
|
|||||||
href: "https://discord.gg/UexuTaE",
|
href: "https://discord.gg/UexuTaE",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "𝕏",
|
label: "Twitter",
|
||||||
href: "https://x.com/excalidraw",
|
href: "https://twitter.com/excalidraw",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "Linkedin",
|
label: "Linkedin",
|
||||||
|
|||||||
@@ -136,6 +136,7 @@ import { useHandleAppTheme } from "./useHandleAppTheme";
|
|||||||
import { getPreferredLanguage } from "./app-language/language-detector";
|
import { getPreferredLanguage } from "./app-language/language-detector";
|
||||||
import { useAppLangCode } from "./app-language/language-state";
|
import { useAppLangCode } from "./app-language/language-state";
|
||||||
import DebugCanvas, {
|
import DebugCanvas, {
|
||||||
|
ConsoleLogger,
|
||||||
debugRenderer,
|
debugRenderer,
|
||||||
isVisualDebuggerEnabled,
|
isVisualDebuggerEnabled,
|
||||||
loadSavedDebugState,
|
loadSavedDebugState,
|
||||||
@@ -1261,6 +1262,7 @@ const ExcalidrawWrapper = () => {
|
|||||||
ref={debugCanvasRef}
|
ref={debugCanvasRef}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
{isVisualDebuggerEnabled() && <ConsoleLogger />}
|
||||||
</Excalidraw>
|
</Excalidraw>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ export const STORAGE_KEYS = {
|
|||||||
LOCAL_STORAGE_COLLAB: "excalidraw-collab",
|
LOCAL_STORAGE_COLLAB: "excalidraw-collab",
|
||||||
LOCAL_STORAGE_THEME: "excalidraw-theme",
|
LOCAL_STORAGE_THEME: "excalidraw-theme",
|
||||||
LOCAL_STORAGE_DEBUG: "excalidraw-debug",
|
LOCAL_STORAGE_DEBUG: "excalidraw-debug",
|
||||||
|
LOCAL_STORAGE_DEBUG_CONSOLE: "excalidraw-debug-console",
|
||||||
VERSION_DATA_STATE: "version-dataState",
|
VERSION_DATA_STATE: "version-dataState",
|
||||||
VERSION_FILES: "version-files",
|
VERSION_FILES: "version-files",
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ import {
|
|||||||
eyeIcon,
|
eyeIcon,
|
||||||
} from "@excalidraw/excalidraw/components/icons";
|
} from "@excalidraw/excalidraw/components/icons";
|
||||||
import { MainMenu } from "@excalidraw/excalidraw/index";
|
import { MainMenu } from "@excalidraw/excalidraw/index";
|
||||||
import React from "react";
|
import DropdownMenuItemCheckbox from "@excalidraw/excalidraw/components/dropdownMenu/DropdownMenuItemCheckbox";
|
||||||
|
import React, { useState } from "react";
|
||||||
|
|
||||||
import { isDevEnv } from "@excalidraw/common";
|
import { isDevEnv } from "@excalidraw/common";
|
||||||
|
|
||||||
@@ -13,7 +14,29 @@ import type { Theme } from "@excalidraw/element/types";
|
|||||||
import { LanguageList } from "../app-language/LanguageList";
|
import { LanguageList } from "../app-language/LanguageList";
|
||||||
import { isExcalidrawPlusSignedUser } from "../app_constants";
|
import { isExcalidrawPlusSignedUser } from "../app_constants";
|
||||||
|
|
||||||
import { saveDebugState } from "./DebugCanvas";
|
import {
|
||||||
|
isVisualDebuggerEnabled,
|
||||||
|
loadConsoleLoggerState,
|
||||||
|
saveDebugState,
|
||||||
|
setConsoleLoggerEnabled,
|
||||||
|
} from "./DebugCanvas";
|
||||||
|
|
||||||
|
const ConsoleLoggerToggle = () => {
|
||||||
|
const [checked, setChecked] = useState(() => loadConsoleLoggerState());
|
||||||
|
return (
|
||||||
|
<DropdownMenuItemCheckbox
|
||||||
|
checked={checked}
|
||||||
|
onSelect={(event) => {
|
||||||
|
const next = !checked;
|
||||||
|
setChecked(next);
|
||||||
|
setConsoleLoggerEnabled(next);
|
||||||
|
event.preventDefault();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Show console log overlay
|
||||||
|
</DropdownMenuItemCheckbox>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export const AppMainMenu: React.FC<{
|
export const AppMainMenu: React.FC<{
|
||||||
onCollabDialogOpen: () => any;
|
onCollabDialogOpen: () => any;
|
||||||
@@ -77,7 +100,13 @@ export const AppMainMenu: React.FC<{
|
|||||||
</MainMenu.Item>
|
</MainMenu.Item>
|
||||||
)}
|
)}
|
||||||
<MainMenu.Separator />
|
<MainMenu.Separator />
|
||||||
<MainMenu.DefaultItems.Preferences />
|
<MainMenu.DefaultItems.Preferences
|
||||||
|
additionalItems={
|
||||||
|
isDevEnv() && isVisualDebuggerEnabled() ? (
|
||||||
|
<ConsoleLoggerToggle />
|
||||||
|
) : null
|
||||||
|
}
|
||||||
|
/>
|
||||||
<MainMenu.DefaultItems.ToggleTheme
|
<MainMenu.DefaultItems.ToggleTheme
|
||||||
allowSystemTheme
|
allowSystemTheme
|
||||||
theme={props.theme}
|
theme={props.theme}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import {
|
|||||||
} from "@excalidraw/excalidraw/renderer/helpers";
|
} from "@excalidraw/excalidraw/renderer/helpers";
|
||||||
import { type AppState } from "@excalidraw/excalidraw/types";
|
import { type AppState } from "@excalidraw/excalidraw/types";
|
||||||
import { arrayToMap, throttleRAF } from "@excalidraw/common";
|
import { arrayToMap, throttleRAF } from "@excalidraw/common";
|
||||||
import { useCallback } from "react";
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
getGlobalFixedPointForBindableElement,
|
getGlobalFixedPointForBindableElement,
|
||||||
@@ -435,6 +435,34 @@ export const loadSavedDebugState = () => {
|
|||||||
export const isVisualDebuggerEnabled = () =>
|
export const isVisualDebuggerEnabled = () =>
|
||||||
Array.isArray(window.visualDebug?.data);
|
Array.isArray(window.visualDebug?.data);
|
||||||
|
|
||||||
|
export const loadConsoleLoggerState = (): boolean => {
|
||||||
|
try {
|
||||||
|
const raw = localStorage.getItem(STORAGE_KEYS.LOCAL_STORAGE_DEBUG_CONSOLE);
|
||||||
|
if (raw !== null) {
|
||||||
|
return JSON.parse(raw) === true;
|
||||||
|
}
|
||||||
|
} catch {}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const saveConsoleLoggerState = (enabled: boolean) => {
|
||||||
|
try {
|
||||||
|
localStorage.setItem(
|
||||||
|
STORAGE_KEYS.LOCAL_STORAGE_DEBUG_CONSOLE,
|
||||||
|
JSON.stringify(enabled),
|
||||||
|
);
|
||||||
|
} catch {}
|
||||||
|
};
|
||||||
|
|
||||||
|
const CONSOLE_LOGGER_TOGGLE_EVENT = "excalidraw-debug-console-toggle";
|
||||||
|
|
||||||
|
export const setConsoleLoggerEnabled = (enabled: boolean) => {
|
||||||
|
saveConsoleLoggerState(enabled);
|
||||||
|
window.dispatchEvent(
|
||||||
|
new CustomEvent<boolean>(CONSOLE_LOGGER_TOGGLE_EVENT, { detail: enabled }),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export const DebugFooter = ({ onChange }: { onChange: () => void }) => {
|
export const DebugFooter = ({ onChange }: { onChange: () => void }) => {
|
||||||
const moveForward = useCallback(() => {
|
const moveForward = useCallback(() => {
|
||||||
if (
|
if (
|
||||||
@@ -459,6 +487,7 @@ export const DebugFooter = ({ onChange }: { onChange: () => void }) => {
|
|||||||
}, [onChange]);
|
}, [onChange]);
|
||||||
const reset = useCallback(() => {
|
const reset = useCallback(() => {
|
||||||
window.visualDebug!.currentFrame = undefined;
|
window.visualDebug!.currentFrame = undefined;
|
||||||
|
_clearLogsCallback?.();
|
||||||
onChange();
|
onChange();
|
||||||
}, [onChange]);
|
}, [onChange]);
|
||||||
const trashFrames = useCallback(() => {
|
const trashFrames = useCallback(() => {
|
||||||
@@ -466,6 +495,7 @@ export const DebugFooter = ({ onChange }: { onChange: () => void }) => {
|
|||||||
window.visualDebug.currentFrame = undefined;
|
window.visualDebug.currentFrame = undefined;
|
||||||
window.visualDebug.data = [];
|
window.visualDebug.data = [];
|
||||||
}
|
}
|
||||||
|
_clearLogsCallback?.();
|
||||||
onChange();
|
onChange();
|
||||||
}, [onChange]);
|
}, [onChange]);
|
||||||
|
|
||||||
@@ -563,4 +593,181 @@ const DebugCanvas = React.forwardRef<HTMLCanvasElement, DebugCanvasProps>(
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
type LogLevel = "log" | "info" | "warn" | "error";
|
||||||
|
|
||||||
|
interface LogEntry {
|
||||||
|
id: number;
|
||||||
|
level: LogLevel;
|
||||||
|
message: string;
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const LOG_COLORS: Record<LogLevel, string> = {
|
||||||
|
log: "rgba(220,220,220,0.9)",
|
||||||
|
info: "rgba(100,180,255,0.9)",
|
||||||
|
warn: "rgba(255,200,60,0.9)",
|
||||||
|
error: "rgba(255,90,90,0.9)",
|
||||||
|
};
|
||||||
|
|
||||||
|
const MAX_LOGS = 500;
|
||||||
|
let logIdCounter = 0;
|
||||||
|
let _clearLogsCallback: (() => void) | null = null;
|
||||||
|
|
||||||
|
export const ConsoleLogger = () => {
|
||||||
|
const [enabled, setEnabled] = useState(() => loadConsoleLoggerState());
|
||||||
|
const [logs, setLogs] = useState<LogEntry[]>([]);
|
||||||
|
const logsRef = useRef<LogEntry[]>([]);
|
||||||
|
const scrollRef = useRef<HTMLDivElement>(null);
|
||||||
|
const dragState = useRef<{ startY: number; startScrollTop: number } | null>(
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
const isDragging = useRef(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handler = (e: Event) => {
|
||||||
|
setEnabled((e as CustomEvent<boolean>).detail);
|
||||||
|
};
|
||||||
|
window.addEventListener(CONSOLE_LOGGER_TOGGLE_EVENT, handler);
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener(CONSOLE_LOGGER_TOGGLE_EVENT, handler);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
_clearLogsCallback = () => {
|
||||||
|
logsRef.current = [];
|
||||||
|
setLogs([]);
|
||||||
|
};
|
||||||
|
return () => {
|
||||||
|
_clearLogsCallback = null;
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const originals: Record<LogLevel, (...args: unknown[]) => void> = {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
log: console.log.bind(console),
|
||||||
|
info: console.info.bind(console),
|
||||||
|
warn: console.warn.bind(console),
|
||||||
|
error: console.error.bind(console),
|
||||||
|
};
|
||||||
|
|
||||||
|
const patch = (level: LogLevel) => {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console[level] = (...args: unknown[]) => {
|
||||||
|
originals[level](...args);
|
||||||
|
const message = args
|
||||||
|
.map((a) =>
|
||||||
|
typeof a === "object" ? JSON.stringify(a, null, 0) : String(a),
|
||||||
|
)
|
||||||
|
.join(" ");
|
||||||
|
const entry: LogEntry = {
|
||||||
|
id: ++logIdCounter,
|
||||||
|
level,
|
||||||
|
message,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
};
|
||||||
|
logsRef.current = [...logsRef.current, entry].slice(-MAX_LOGS);
|
||||||
|
setLogs([...logsRef.current]);
|
||||||
|
if (!isDragging.current && scrollRef.current) {
|
||||||
|
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
(["log", "info", "warn", "error"] as LogLevel[]).forEach(patch);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
(["log", "info", "warn", "error"] as LogLevel[]).forEach((level) => {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console[level] = originals[level];
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const onPointerDown = useCallback((e: React.PointerEvent<HTMLDivElement>) => {
|
||||||
|
if (!scrollRef.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
dragState.current = {
|
||||||
|
startY: e.clientY,
|
||||||
|
startScrollTop: scrollRef.current.scrollTop,
|
||||||
|
};
|
||||||
|
scrollRef.current.setPointerCapture(e.pointerId);
|
||||||
|
isDragging.current = true;
|
||||||
|
e.preventDefault();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const onPointerMove = useCallback((e: React.PointerEvent<HTMLDivElement>) => {
|
||||||
|
if (!dragState.current || !scrollRef.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const delta = dragState.current.startY - e.clientY;
|
||||||
|
scrollRef.current.scrollTop = dragState.current.startScrollTop + delta;
|
||||||
|
e.preventDefault();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const onPointerUp = useCallback(() => {
|
||||||
|
dragState.current = null;
|
||||||
|
isDragging.current = false;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
if (!enabled || logs.length === 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
ref={scrollRef}
|
||||||
|
onPointerDown={onPointerDown}
|
||||||
|
onPointerMove={onPointerMove}
|
||||||
|
onPointerUp={onPointerUp}
|
||||||
|
onPointerCancel={onPointerUp}
|
||||||
|
style={{
|
||||||
|
position: "fixed",
|
||||||
|
top: 8,
|
||||||
|
right: 8,
|
||||||
|
zIndex: 9999,
|
||||||
|
maxWidth: 420,
|
||||||
|
maxHeight: "60vh",
|
||||||
|
overflowY: "auto",
|
||||||
|
display: "flex",
|
||||||
|
flexDirection: "column",
|
||||||
|
gap: 2,
|
||||||
|
pointerEvents: "all",
|
||||||
|
cursor: "grab",
|
||||||
|
userSelect: "none",
|
||||||
|
scrollbarWidth: "none",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{logs.map((entry) => (
|
||||||
|
<div
|
||||||
|
key={entry.id}
|
||||||
|
style={{
|
||||||
|
background: "rgba(18,18,20,0.55)",
|
||||||
|
backdropFilter: "blur(8px) saturate(1.4)",
|
||||||
|
WebkitBackdropFilter: "blur(8px) saturate(1.4)",
|
||||||
|
borderLeft: `3px solid ${LOG_COLORS[entry.level]}`,
|
||||||
|
borderRadius: 4,
|
||||||
|
padding: "2px 8px",
|
||||||
|
fontFamily: "monospace",
|
||||||
|
fontSize: 11,
|
||||||
|
lineHeight: 1.5,
|
||||||
|
color: LOG_COLORS[entry.level],
|
||||||
|
wordBreak: "break-all",
|
||||||
|
whiteSpace: "pre-wrap",
|
||||||
|
opacity: 0.95,
|
||||||
|
boxShadow: "0 1px 4px rgba(0,0,0,0.35)",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<span style={{ opacity: 0.5, marginRight: 6 }}>
|
||||||
|
{entry.level.toUpperCase()}
|
||||||
|
</span>
|
||||||
|
{entry.message}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export default DebugCanvas;
|
export default DebugCanvas;
|
||||||
|
|||||||
@@ -1943,9 +1943,9 @@ export const calculateFixedPointForElbowArrowBinding = (
|
|||||||
return {
|
return {
|
||||||
fixedPoint: normalizeFixedPoint([
|
fixedPoint: normalizeFixedPoint([
|
||||||
(nonRotatedSnappedGlobalPoint[0] - hoveredElement.x) /
|
(nonRotatedSnappedGlobalPoint[0] - hoveredElement.x) /
|
||||||
Math.max(hoveredElement.width, PRECISION),
|
hoveredElement.width,
|
||||||
(nonRotatedSnappedGlobalPoint[1] - hoveredElement.y) /
|
(nonRotatedSnappedGlobalPoint[1] - hoveredElement.y) /
|
||||||
Math.max(hoveredElement.height, PRECISION),
|
hoveredElement.height,
|
||||||
]),
|
]),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -1976,11 +1976,9 @@ export const calculateFixedPointForNonElbowArrowBinding = (
|
|||||||
|
|
||||||
// Calculate the ratio relative to the element's bounds
|
// Calculate the ratio relative to the element's bounds
|
||||||
const fixedPointX =
|
const fixedPointX =
|
||||||
(nonRotatedPoint[0] - hoveredElement.x) /
|
(nonRotatedPoint[0] - hoveredElement.x) / hoveredElement.width;
|
||||||
Math.max(hoveredElement.width, PRECISION);
|
|
||||||
const fixedPointY =
|
const fixedPointY =
|
||||||
(nonRotatedPoint[1] - hoveredElement.y) /
|
(nonRotatedPoint[1] - hoveredElement.y) / hoveredElement.height;
|
||||||
Math.max(hoveredElement.height, PRECISION);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
fixedPoint: normalizeFixedPoint([fixedPointX, fixedPointY]),
|
fixedPoint: normalizeFixedPoint([fixedPointX, fixedPointY]),
|
||||||
|
|||||||
@@ -2124,8 +2124,8 @@ const normalizeArrowElementUpdate = (
|
|||||||
offsetY < -MAX_POS ||
|
offsetY < -MAX_POS ||
|
||||||
offsetY > MAX_POS ||
|
offsetY > MAX_POS ||
|
||||||
offsetX + points[points.length - 1][0] < -MAX_POS ||
|
offsetX + points[points.length - 1][0] < -MAX_POS ||
|
||||||
offsetX + points[points.length - 1][0] > MAX_POS ||
|
offsetY + points[points.length - 1][0] > MAX_POS ||
|
||||||
offsetY + points[points.length - 1][1] < -MAX_POS ||
|
offsetX + points[points.length - 1][1] < -MAX_POS ||
|
||||||
offsetY + points[points.length - 1][1] > MAX_POS
|
offsetY + points[points.length - 1][1] > MAX_POS
|
||||||
) {
|
) {
|
||||||
console.error(
|
console.error(
|
||||||
|
|||||||
@@ -88,7 +88,6 @@ export * from "./selection";
|
|||||||
export * from "./shape";
|
export * from "./shape";
|
||||||
export * from "./showSelectedShapeActions";
|
export * from "./showSelectedShapeActions";
|
||||||
export * from "./sizeHelpers";
|
export * from "./sizeHelpers";
|
||||||
export * from "./snapping";
|
|
||||||
export * from "./sortElements";
|
export * from "./sortElements";
|
||||||
export * from "./store";
|
export * from "./store";
|
||||||
export * from "./textElement";
|
export * from "./textElement";
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import {
|
|||||||
type LocalPoint,
|
type LocalPoint,
|
||||||
pointDistance,
|
pointDistance,
|
||||||
vectorFromPoint,
|
vectorFromPoint,
|
||||||
line,
|
|
||||||
curveLength,
|
curveLength,
|
||||||
curvePointAtLength,
|
curvePointAtLength,
|
||||||
} from "@excalidraw/math";
|
} from "@excalidraw/math";
|
||||||
@@ -30,9 +29,6 @@ import {
|
|||||||
isPathALoop,
|
isPathALoop,
|
||||||
moveArrowAboveBindable,
|
moveArrowAboveBindable,
|
||||||
projectFixedPointOntoDiagonal,
|
projectFixedPointOntoDiagonal,
|
||||||
snapLinearElementPoint,
|
|
||||||
snapToDiscreteAngle,
|
|
||||||
type SnapLine,
|
|
||||||
type Store,
|
type Store,
|
||||||
} from "@excalidraw/element";
|
} from "@excalidraw/element";
|
||||||
|
|
||||||
@@ -52,7 +48,6 @@ import {
|
|||||||
calculateFixedPointForNonElbowArrowBinding,
|
calculateFixedPointForNonElbowArrowBinding,
|
||||||
getBindingStrategyForDraggingBindingElementEndpoints,
|
getBindingStrategyForDraggingBindingElementEndpoints,
|
||||||
isBindingEnabled,
|
isBindingEnabled,
|
||||||
maxBindingDistance_simple,
|
|
||||||
snapToMid,
|
snapToMid,
|
||||||
updateBoundPoint,
|
updateBoundPoint,
|
||||||
} from "./binding";
|
} from "./binding";
|
||||||
@@ -61,7 +56,6 @@ import {
|
|||||||
getElementPointsCoords,
|
getElementPointsCoords,
|
||||||
getMinMaxXYFromCurvePathOps,
|
getMinMaxXYFromCurvePathOps,
|
||||||
} from "./bounds";
|
} from "./bounds";
|
||||||
import { getHoveredElementForBinding } from "./collision";
|
|
||||||
|
|
||||||
import { headingIsHorizontal, vectorToHeading } from "./heading";
|
import { headingIsHorizontal, vectorToHeading } from "./heading";
|
||||||
import { mutateElement } from "./mutateElement";
|
import { mutateElement } from "./mutateElement";
|
||||||
@@ -300,10 +294,7 @@ export class LinearElementEditor {
|
|||||||
scenePointerX: number,
|
scenePointerX: number,
|
||||||
scenePointerY: number,
|
scenePointerY: number,
|
||||||
linearElementEditor: LinearElementEditor,
|
linearElementEditor: LinearElementEditor,
|
||||||
): Pick<
|
): Pick<AppState, "suggestedBinding" | "selectedLinearElement"> | null {
|
||||||
AppState,
|
|
||||||
"suggestedBinding" | "selectedLinearElement" | "snapLines"
|
|
||||||
> | null {
|
|
||||||
const elementsMap = app.scene.getNonDeletedElementsMap();
|
const elementsMap = app.scene.getNonDeletedElementsMap();
|
||||||
const elements = app.scene.getNonDeletedElements();
|
const elements = app.scene.getNonDeletedElements();
|
||||||
const { elementId } = linearElementEditor;
|
const { elementId } = linearElementEditor;
|
||||||
@@ -320,26 +311,36 @@ export class LinearElementEditor {
|
|||||||
linearElementEditor.customLineAngle ??
|
linearElementEditor.customLineAngle ??
|
||||||
determineCustomLinearAngle(pivotPoint, element.points[idx]);
|
determineCustomLinearAngle(pivotPoint, element.points[idx]);
|
||||||
|
|
||||||
const { point: newDraggingPointPosition, snapLines } =
|
// Determine if point movement should happen and how much
|
||||||
LinearElementEditor._getSnappedPointForLinearElement({
|
let deltaX = 0;
|
||||||
app,
|
let deltaY = 0;
|
||||||
event,
|
if (shouldRotateWithDiscreteAngle(event)) {
|
||||||
elements,
|
const [width, height] = LinearElementEditor._getShiftLockedDelta(
|
||||||
elementsMap,
|
|
||||||
element,
|
element,
|
||||||
pointIndex: idx,
|
elementsMap,
|
||||||
scenePointerX,
|
pivotPoint,
|
||||||
scenePointerY,
|
pointFrom(scenePointerX, scenePointerY),
|
||||||
pointerOffset: linearElementEditor.pointerOffset,
|
event[KEYS.CTRL_OR_CMD] ? null : app.getEffectiveGridSize(),
|
||||||
referencePoint: shouldRotateWithDiscreteAngle(event)
|
|
||||||
? pivotPoint
|
|
||||||
: null,
|
|
||||||
selectedPointsIndices: [idx],
|
|
||||||
customLineAngle,
|
customLineAngle,
|
||||||
});
|
);
|
||||||
|
const target = pointFrom<LocalPoint>(
|
||||||
|
width + pivotPoint[0],
|
||||||
|
height + pivotPoint[1],
|
||||||
|
);
|
||||||
|
|
||||||
const deltaX = newDraggingPointPosition[0] - point[0];
|
deltaX = target[0] - point[0];
|
||||||
const deltaY = newDraggingPointPosition[1] - point[1];
|
deltaY = target[1] - point[1];
|
||||||
|
} else {
|
||||||
|
const newDraggingPointPosition = LinearElementEditor.createPointAt(
|
||||||
|
element,
|
||||||
|
elementsMap,
|
||||||
|
scenePointerX - linearElementEditor.pointerOffset.x,
|
||||||
|
scenePointerY - linearElementEditor.pointerOffset.y,
|
||||||
|
event[KEYS.CTRL_OR_CMD] ? null : app.getEffectiveGridSize(),
|
||||||
|
);
|
||||||
|
deltaX = newDraggingPointPosition[0] - point[0];
|
||||||
|
deltaY = newDraggingPointPosition[1] - point[1];
|
||||||
|
}
|
||||||
|
|
||||||
// Apply the point movement if needed
|
// Apply the point movement if needed
|
||||||
let suggestedBinding: AppState["suggestedBinding"] = null;
|
let suggestedBinding: AppState["suggestedBinding"] = null;
|
||||||
@@ -397,8 +398,6 @@ export class LinearElementEditor {
|
|||||||
// PERF: Avoid state updates if not absolutely necessary
|
// PERF: Avoid state updates if not absolutely necessary
|
||||||
if (
|
if (
|
||||||
app.state.selectedLinearElement?.customLineAngle === customLineAngle &&
|
app.state.selectedLinearElement?.customLineAngle === customLineAngle &&
|
||||||
app.state.snapLines.length === 0 &&
|
|
||||||
snapLines.length === 0 &&
|
|
||||||
linearElementEditor.initialState.altFocusPoint &&
|
linearElementEditor.initialState.altFocusPoint &&
|
||||||
(!suggestedBinding ||
|
(!suggestedBinding ||
|
||||||
isShallowEqual(app.state.suggestedBinding ?? [], suggestedBinding))
|
isShallowEqual(app.state.suggestedBinding ?? [], suggestedBinding))
|
||||||
@@ -437,7 +436,6 @@ export class LinearElementEditor {
|
|||||||
return {
|
return {
|
||||||
selectedLinearElement: newLinearElementEditor,
|
selectedLinearElement: newLinearElementEditor,
|
||||||
suggestedBinding,
|
suggestedBinding,
|
||||||
snapLines,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -447,10 +445,7 @@ export class LinearElementEditor {
|
|||||||
scenePointerX: number,
|
scenePointerX: number,
|
||||||
scenePointerY: number,
|
scenePointerY: number,
|
||||||
linearElementEditor: LinearElementEditor,
|
linearElementEditor: LinearElementEditor,
|
||||||
): Pick<
|
): Pick<AppState, "suggestedBinding" | "selectedLinearElement"> | null {
|
||||||
AppState,
|
|
||||||
"suggestedBinding" | "selectedLinearElement" | "snapLines"
|
|
||||||
> | null {
|
|
||||||
const elementsMap = app.scene.getNonDeletedElementsMap();
|
const elementsMap = app.scene.getNonDeletedElementsMap();
|
||||||
const elements = app.scene.getNonDeletedElements();
|
const elements = app.scene.getNonDeletedElements();
|
||||||
const { elbowed, elementId, initialState } = linearElementEditor;
|
const { elbowed, elementId, initialState } = linearElementEditor;
|
||||||
@@ -498,6 +493,7 @@ export class LinearElementEditor {
|
|||||||
lastClickedPoint = element.points.length - 1;
|
lastClickedPoint = element.points.length - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// point that's being dragged (out of all selected points)
|
||||||
const draggingPoint = element.points[lastClickedPoint];
|
const draggingPoint = element.points[lastClickedPoint];
|
||||||
// The adjacent point to the one dragged point
|
// The adjacent point to the one dragged point
|
||||||
const pivotPoint =
|
const pivotPoint =
|
||||||
@@ -511,27 +507,35 @@ export class LinearElementEditor {
|
|||||||
element.points.length - 1,
|
element.points.length - 1,
|
||||||
);
|
);
|
||||||
|
|
||||||
const { point: newDraggingPointPosition, snapLines } =
|
// Determine if point movement should happen and how much
|
||||||
LinearElementEditor._getSnappedPointForLinearElement({
|
let deltaX = 0;
|
||||||
app,
|
let deltaY = 0;
|
||||||
event,
|
if (shouldRotateWithDiscreteAngle(event) && singlePointDragged) {
|
||||||
elements,
|
const [width, height] = LinearElementEditor._getShiftLockedDelta(
|
||||||
elementsMap,
|
|
||||||
element,
|
element,
|
||||||
pointIndex: lastClickedPoint,
|
elementsMap,
|
||||||
scenePointerX,
|
pivotPoint,
|
||||||
scenePointerY,
|
pointFrom(scenePointerX, scenePointerY),
|
||||||
pointerOffset: linearElementEditor.pointerOffset,
|
event[KEYS.CTRL_OR_CMD] ? null : app.getEffectiveGridSize(),
|
||||||
referencePoint:
|
|
||||||
shouldRotateWithDiscreteAngle(event) && singlePointDragged
|
|
||||||
? pivotPoint
|
|
||||||
: null,
|
|
||||||
selectedPointsIndices,
|
|
||||||
customLineAngle,
|
customLineAngle,
|
||||||
});
|
);
|
||||||
|
const target = pointFrom<LocalPoint>(
|
||||||
const deltaX = newDraggingPointPosition[0] - draggingPoint[0];
|
width + pivotPoint[0],
|
||||||
const deltaY = newDraggingPointPosition[1] - draggingPoint[1];
|
height + pivotPoint[1],
|
||||||
|
);
|
||||||
|
deltaX = target[0] - draggingPoint[0];
|
||||||
|
deltaY = target[1] - draggingPoint[1];
|
||||||
|
} else {
|
||||||
|
const newDraggingPointPosition = LinearElementEditor.createPointAt(
|
||||||
|
element,
|
||||||
|
elementsMap,
|
||||||
|
scenePointerX - linearElementEditor.pointerOffset.x,
|
||||||
|
scenePointerY - linearElementEditor.pointerOffset.y,
|
||||||
|
event[KEYS.CTRL_OR_CMD] ? null : app.getEffectiveGridSize(),
|
||||||
|
);
|
||||||
|
deltaX = newDraggingPointPosition[0] - draggingPoint[0];
|
||||||
|
deltaY = newDraggingPointPosition[1] - draggingPoint[1];
|
||||||
|
}
|
||||||
|
|
||||||
// Apply the point movement if needed
|
// Apply the point movement if needed
|
||||||
let suggestedBinding: AppState["suggestedBinding"] = null;
|
let suggestedBinding: AppState["suggestedBinding"] = null;
|
||||||
@@ -670,7 +674,6 @@ export class LinearElementEditor {
|
|||||||
return {
|
return {
|
||||||
selectedLinearElement: newLinearElementEditor,
|
selectedLinearElement: newLinearElementEditor,
|
||||||
suggestedBinding,
|
suggestedBinding,
|
||||||
snapLines,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1175,10 +1178,7 @@ export class LinearElementEditor {
|
|||||||
scenePointerX: number,
|
scenePointerX: number,
|
||||||
scenePointerY: number,
|
scenePointerY: number,
|
||||||
app: AppClassProperties,
|
app: AppClassProperties,
|
||||||
): {
|
): LinearElementEditor | null {
|
||||||
editingLinearElement: LinearElementEditor;
|
|
||||||
snapLines: readonly SnapLine[];
|
|
||||||
} | null {
|
|
||||||
const appState = app.state;
|
const appState = app.state;
|
||||||
if (!appState.selectedLinearElement?.isEditing) {
|
if (!appState.selectedLinearElement?.isEditing) {
|
||||||
return null;
|
return null;
|
||||||
@@ -1187,10 +1187,7 @@ export class LinearElementEditor {
|
|||||||
const elementsMap = app.scene.getNonDeletedElementsMap();
|
const elementsMap = app.scene.getNonDeletedElementsMap();
|
||||||
const element = LinearElementEditor.getElement(elementId, elementsMap);
|
const element = LinearElementEditor.getElement(elementId, elementsMap);
|
||||||
if (!element) {
|
if (!element) {
|
||||||
return {
|
return appState.selectedLinearElement;
|
||||||
editingLinearElement: appState.selectedLinearElement,
|
|
||||||
snapLines: appState.snapLines,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const { points } = element;
|
const { points } = element;
|
||||||
@@ -1202,37 +1199,36 @@ export class LinearElementEditor {
|
|||||||
}
|
}
|
||||||
return appState.selectedLinearElement?.lastUncommittedPoint
|
return appState.selectedLinearElement?.lastUncommittedPoint
|
||||||
? {
|
? {
|
||||||
editingLinearElement: {
|
...appState.selectedLinearElement,
|
||||||
...appState.selectedLinearElement,
|
lastUncommittedPoint: null,
|
||||||
lastUncommittedPoint: null,
|
|
||||||
},
|
|
||||||
snapLines: [],
|
|
||||||
}
|
}
|
||||||
: {
|
: appState.selectedLinearElement;
|
||||||
editingLinearElement: appState.selectedLinearElement,
|
|
||||||
snapLines: [],
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const anchor = points[points.length - 2];
|
let newPoint: LocalPoint;
|
||||||
const elements = app.scene.getNonDeletedElements();
|
|
||||||
const { point: newPoint, snapLines } =
|
if (shouldRotateWithDiscreteAngle(event) && points.length >= 2) {
|
||||||
LinearElementEditor._getSnappedPointForLinearElement({
|
const anchor = points[points.length - 2];
|
||||||
app,
|
const [width, height] = LinearElementEditor._getShiftLockedDelta(
|
||||||
event,
|
|
||||||
elements,
|
|
||||||
elementsMap,
|
|
||||||
element,
|
element,
|
||||||
pointIndex: points.length - 1,
|
elementsMap,
|
||||||
scenePointerX,
|
anchor,
|
||||||
scenePointerY,
|
pointFrom(scenePointerX, scenePointerY),
|
||||||
pointerOffset: appState.selectedLinearElement.pointerOffset,
|
event[KEYS.CTRL_OR_CMD] ? null : app.getEffectiveGridSize(),
|
||||||
referencePoint:
|
);
|
||||||
shouldRotateWithDiscreteAngle(event) && points.length >= 2
|
|
||||||
? anchor
|
newPoint = pointFrom(width + anchor[0], height + anchor[1]);
|
||||||
: null,
|
} else {
|
||||||
selectedPointsIndices: [points.length - 1],
|
newPoint = LinearElementEditor.createPointAt(
|
||||||
});
|
element,
|
||||||
|
elementsMap,
|
||||||
|
scenePointerX - appState.selectedLinearElement.pointerOffset.x,
|
||||||
|
scenePointerY - appState.selectedLinearElement.pointerOffset.y,
|
||||||
|
event[KEYS.CTRL_OR_CMD] || isElbowArrow(element)
|
||||||
|
? null
|
||||||
|
: app.getEffectiveGridSize(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (lastPoint === lastUncommittedPoint) {
|
if (lastPoint === lastUncommittedPoint) {
|
||||||
LinearElementEditor.movePoints(
|
LinearElementEditor.movePoints(
|
||||||
@@ -1240,7 +1236,7 @@ export class LinearElementEditor {
|
|||||||
app.scene,
|
app.scene,
|
||||||
new Map([
|
new Map([
|
||||||
[
|
[
|
||||||
points.length - 1,
|
element.points.length - 1,
|
||||||
{
|
{
|
||||||
point: newPoint,
|
point: newPoint,
|
||||||
},
|
},
|
||||||
@@ -1250,13 +1246,9 @@ export class LinearElementEditor {
|
|||||||
} else {
|
} else {
|
||||||
LinearElementEditor.addPoints(element, app.scene, [newPoint]);
|
LinearElementEditor.addPoints(element, app.scene, [newPoint]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
editingLinearElement: {
|
...appState.selectedLinearElement,
|
||||||
...appState.selectedLinearElement,
|
lastUncommittedPoint: element.points[element.points.length - 1],
|
||||||
lastUncommittedPoint: element.points[element.points.length - 1],
|
|
||||||
},
|
|
||||||
snapLines,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1282,53 +1274,18 @@ export class LinearElementEditor {
|
|||||||
static getPointsGlobalCoordinates(
|
static getPointsGlobalCoordinates(
|
||||||
element: NonDeleted<ExcalidrawLinearElement>,
|
element: NonDeleted<ExcalidrawLinearElement>,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
options: {
|
|
||||||
dragOffset?: { x: number; y: number };
|
|
||||||
excludePointsIndices?: readonly number[];
|
|
||||||
} = {},
|
|
||||||
): GlobalPoint[] {
|
): GlobalPoint[] {
|
||||||
const { dragOffset, excludePointsIndices } = options;
|
|
||||||
|
|
||||||
if (!element.points || element.points.length === 0) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element, elementsMap);
|
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element, elementsMap);
|
||||||
const cx = (x1 + x2) / 2;
|
const cx = (x1 + x2) / 2;
|
||||||
const cy = (y1 + y2) / 2;
|
const cy = (y1 + y2) / 2;
|
||||||
|
return element.points.map((p) => {
|
||||||
let elementX = element.x;
|
const { x, y } = element;
|
||||||
let elementY = element.y;
|
return pointRotateRads(
|
||||||
|
pointFrom(x + p[0], y + p[1]),
|
||||||
if (dragOffset) {
|
|
||||||
elementX += dragOffset.x;
|
|
||||||
elementY += dragOffset.y;
|
|
||||||
}
|
|
||||||
|
|
||||||
const globalPoints: GlobalPoint[] = [];
|
|
||||||
|
|
||||||
for (let i = 0; i < element.points.length; i++) {
|
|
||||||
// Skip the point being edited if specified
|
|
||||||
if (
|
|
||||||
excludePointsIndices?.length &&
|
|
||||||
excludePointsIndices.find((index) => index === i) !== undefined
|
|
||||||
) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const p = element.points[i];
|
|
||||||
const globalX = elementX + p[0];
|
|
||||||
const globalY = elementY + p[1];
|
|
||||||
|
|
||||||
const rotated = pointRotateRads<GlobalPoint>(
|
|
||||||
pointFrom(globalX, globalY),
|
|
||||||
pointFrom(cx, cy),
|
pointFrom(cx, cy),
|
||||||
element.angle,
|
element.angle,
|
||||||
);
|
);
|
||||||
globalPoints.push(rotated);
|
});
|
||||||
}
|
|
||||||
|
|
||||||
return globalPoints;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static getPointAtIndexGlobalCoordinates(
|
static getPointAtIndexGlobalCoordinates(
|
||||||
@@ -1882,222 +1839,6 @@ export class LinearElementEditor {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static _getPointPlacementGridSize(
|
|
||||||
element: NonDeleted<ExcalidrawLinearElement>,
|
|
||||||
app: AppClassProperties,
|
|
||||||
event: Pick<KeyboardEvent | PointerEvent, typeof KEYS.CTRL_OR_CMD>,
|
|
||||||
): NullableGridSize {
|
|
||||||
return event[KEYS.CTRL_OR_CMD] || isElbowArrow(element)
|
|
||||||
? null
|
|
||||||
: app.getEffectiveGridSize();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static _shouldSkipExternalSnapForBindableTarget({
|
|
||||||
appState,
|
|
||||||
elements,
|
|
||||||
elementsMap,
|
|
||||||
element,
|
|
||||||
pointIndex,
|
|
||||||
scenePoint,
|
|
||||||
selectedPointsIndices,
|
|
||||||
}: {
|
|
||||||
appState: AppState;
|
|
||||||
elements: readonly Ordered<NonDeletedExcalidrawElement>[];
|
|
||||||
elementsMap: NonDeletedSceneElementsMap;
|
|
||||||
element: NonDeleted<ExcalidrawLinearElement>;
|
|
||||||
pointIndex: number;
|
|
||||||
scenePoint: GlobalPoint;
|
|
||||||
selectedPointsIndices?: readonly number[];
|
|
||||||
}) {
|
|
||||||
if (
|
|
||||||
isElbowArrow(element) ||
|
|
||||||
!isBindingElement(element) ||
|
|
||||||
!isBindingEnabled(appState) ||
|
|
||||||
selectedPointsIndices?.length !== 1
|
|
||||||
) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pointIndex !== 0 && pointIndex !== element.points.length - 1) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return !!getHoveredElementForBinding(
|
|
||||||
scenePoint,
|
|
||||||
elements,
|
|
||||||
elementsMap,
|
|
||||||
maxBindingDistance_simple(appState.zoom),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static _getSnappedPointForLinearElement({
|
|
||||||
app,
|
|
||||||
event,
|
|
||||||
elements,
|
|
||||||
elementsMap,
|
|
||||||
element,
|
|
||||||
pointIndex,
|
|
||||||
scenePointerX,
|
|
||||||
scenePointerY,
|
|
||||||
pointerOffset,
|
|
||||||
referencePoint,
|
|
||||||
selectedPointsIndices,
|
|
||||||
customLineAngle,
|
|
||||||
}: {
|
|
||||||
app: AppClassProperties;
|
|
||||||
event: PointerEvent | React.PointerEvent<HTMLCanvasElement>;
|
|
||||||
elements: readonly Ordered<NonDeletedExcalidrawElement>[];
|
|
||||||
elementsMap: NonDeletedSceneElementsMap;
|
|
||||||
element: NonDeleted<ExcalidrawLinearElement>;
|
|
||||||
pointIndex: number;
|
|
||||||
scenePointerX: number;
|
|
||||||
scenePointerY: number;
|
|
||||||
pointerOffset: Readonly<{ x: number; y: number }>;
|
|
||||||
referencePoint?: LocalPoint | null;
|
|
||||||
selectedPointsIndices?: readonly number[];
|
|
||||||
customLineAngle?: number | null;
|
|
||||||
}): {
|
|
||||||
point: LocalPoint;
|
|
||||||
snapLines: SnapLine[];
|
|
||||||
} {
|
|
||||||
const gridSize = LinearElementEditor._getPointPlacementGridSize(
|
|
||||||
element,
|
|
||||||
app,
|
|
||||||
event,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (referencePoint) {
|
|
||||||
const referencePointCoords =
|
|
||||||
LinearElementEditor.getPointGlobalCoordinates(
|
|
||||||
element,
|
|
||||||
referencePoint,
|
|
||||||
elementsMap,
|
|
||||||
);
|
|
||||||
const [gridX, gridY] = getGridPoint(
|
|
||||||
scenePointerX,
|
|
||||||
scenePointerY,
|
|
||||||
gridSize,
|
|
||||||
);
|
|
||||||
|
|
||||||
let { width: dxFromReference, height: dyFromReference } =
|
|
||||||
getLockedLinearCursorAlignSize(
|
|
||||||
referencePointCoords[0],
|
|
||||||
referencePointCoords[1],
|
|
||||||
gridX,
|
|
||||||
gridY,
|
|
||||||
customLineAngle ?? undefined,
|
|
||||||
);
|
|
||||||
|
|
||||||
const effectiveGridX = referencePointCoords[0] + dxFromReference;
|
|
||||||
const effectiveGridY = referencePointCoords[1] + dyFromReference;
|
|
||||||
|
|
||||||
let snapLines: SnapLine[] = [];
|
|
||||||
const shouldSkipExternalSnap =
|
|
||||||
LinearElementEditor._shouldSkipExternalSnapForBindableTarget({
|
|
||||||
appState: app.state,
|
|
||||||
elements,
|
|
||||||
elementsMap,
|
|
||||||
element,
|
|
||||||
pointIndex,
|
|
||||||
scenePoint: pointFrom<GlobalPoint>(effectiveGridX, effectiveGridY),
|
|
||||||
selectedPointsIndices,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!isElbowArrow(element)) {
|
|
||||||
const { snapOffset, snapLines: nextSnapLines } = snapLinearElementPoint(
|
|
||||||
elements,
|
|
||||||
element,
|
|
||||||
pointFrom<GlobalPoint>(effectiveGridX, effectiveGridY),
|
|
||||||
app,
|
|
||||||
event,
|
|
||||||
elementsMap,
|
|
||||||
{
|
|
||||||
includeExternalPoints: !shouldSkipExternalSnap,
|
|
||||||
includeSelfPoints: true,
|
|
||||||
selectedPointsIndices,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
snapLines = nextSnapLines;
|
|
||||||
|
|
||||||
if (nextSnapLines.length > 0) {
|
|
||||||
const result = snapToDiscreteAngle(
|
|
||||||
nextSnapLines,
|
|
||||||
line(
|
|
||||||
pointFrom(effectiveGridX, effectiveGridY),
|
|
||||||
pointFrom(referencePointCoords[0], referencePointCoords[1]),
|
|
||||||
),
|
|
||||||
pointFrom(gridX, gridY),
|
|
||||||
referencePointCoords,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (result.snapLines.length > 0) {
|
|
||||||
dxFromReference = result.dxFromReference;
|
|
||||||
dyFromReference = result.dyFromReference;
|
|
||||||
snapLines = result.snapLines;
|
|
||||||
} else {
|
|
||||||
dxFromReference =
|
|
||||||
effectiveGridX + snapOffset.x - referencePointCoords[0];
|
|
||||||
dyFromReference =
|
|
||||||
effectiveGridY + snapOffset.y - referencePointCoords[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const [rotatedX, rotatedY] = pointRotateRads(
|
|
||||||
pointFrom(dxFromReference, dyFromReference),
|
|
||||||
pointFrom(0, 0),
|
|
||||||
-element.angle as Radians,
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
point: pointFrom(
|
|
||||||
referencePoint[0] + rotatedX,
|
|
||||||
referencePoint[1] + rotatedY,
|
|
||||||
),
|
|
||||||
snapLines,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const originalPointerX = scenePointerX - pointerOffset.x;
|
|
||||||
const originalPointerY = scenePointerY - pointerOffset.y;
|
|
||||||
const shouldSkipExternalSnap =
|
|
||||||
LinearElementEditor._shouldSkipExternalSnapForBindableTarget({
|
|
||||||
appState: app.state,
|
|
||||||
elements,
|
|
||||||
elementsMap,
|
|
||||||
element,
|
|
||||||
pointIndex,
|
|
||||||
scenePoint: pointFrom<GlobalPoint>(originalPointerX, originalPointerY),
|
|
||||||
selectedPointsIndices,
|
|
||||||
});
|
|
||||||
|
|
||||||
const { snapOffset, snapLines } = snapLinearElementPoint(
|
|
||||||
elements,
|
|
||||||
element,
|
|
||||||
pointFrom(originalPointerX, originalPointerY),
|
|
||||||
app,
|
|
||||||
event,
|
|
||||||
elementsMap,
|
|
||||||
{
|
|
||||||
includeExternalPoints: !shouldSkipExternalSnap,
|
|
||||||
includeSelfPoints: true,
|
|
||||||
selectedPointsIndices,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
point: LinearElementEditor.createPointAt(
|
|
||||||
element,
|
|
||||||
elementsMap,
|
|
||||||
originalPointerX + snapOffset.x,
|
|
||||||
originalPointerY + snapOffset.y,
|
|
||||||
gridSize,
|
|
||||||
),
|
|
||||||
snapLines,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static getBoundTextElementPosition = (
|
static getBoundTextElementPosition = (
|
||||||
element: ExcalidrawLinearElement,
|
element: ExcalidrawLinearElement,
|
||||||
boundTextElement: ExcalidrawTextElementWithContainer,
|
boundTextElement: ExcalidrawTextElementWithContainer,
|
||||||
|
|||||||
@@ -155,24 +155,6 @@ describe("Test Linear Elements", () => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const dragMove = (startPoint: GlobalPoint, endPoint: GlobalPoint) => {
|
|
||||||
fireEvent.pointerDown(interactiveCanvas, {
|
|
||||||
clientX: startPoint[0],
|
|
||||||
clientY: startPoint[1],
|
|
||||||
});
|
|
||||||
fireEvent.pointerMove(interactiveCanvas, {
|
|
||||||
clientX: endPoint[0],
|
|
||||||
clientY: endPoint[1],
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const dragEnd = (endPoint: GlobalPoint) => {
|
|
||||||
fireEvent.pointerUp(interactiveCanvas, {
|
|
||||||
clientX: endPoint[0],
|
|
||||||
clientY: endPoint[1],
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const deletePoint = (point: GlobalPoint) => {
|
const deletePoint = (point: GlobalPoint) => {
|
||||||
fireEvent.pointerDown(interactiveCanvas, {
|
fireEvent.pointerDown(interactiveCanvas, {
|
||||||
clientX: point[0],
|
clientX: point[0],
|
||||||
@@ -276,73 +258,6 @@ describe("Test Linear Elements", () => {
|
|||||||
expect(h.state.selectedLinearElement?.elementId).toEqual(h.elements[0].id);
|
expect(h.state.selectedLinearElement?.elementId).toEqual(h.elements[0].id);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("shows snap lines and snaps the endpoint when creating a line", () => {
|
|
||||||
const rect = API.createElement({
|
|
||||||
type: "rectangle",
|
|
||||||
x: 100,
|
|
||||||
y: 100,
|
|
||||||
width: 40,
|
|
||||||
height: 40,
|
|
||||||
});
|
|
||||||
API.setElements([rect]);
|
|
||||||
API.setAppState({ objectsSnapModeEnabled: true });
|
|
||||||
|
|
||||||
UI.clickTool("line");
|
|
||||||
|
|
||||||
const startPoint = pointFrom<GlobalPoint>(20, 20);
|
|
||||||
const pointerNearCorner = pointFrom<GlobalPoint>(95, 95);
|
|
||||||
|
|
||||||
dragMove(startPoint, pointerNearCorner);
|
|
||||||
|
|
||||||
expect(h.state.snapLines.length).toBeGreaterThan(0);
|
|
||||||
|
|
||||||
dragEnd(pointerNearCorner);
|
|
||||||
|
|
||||||
const line = h.elements.find(
|
|
||||||
(element): element is ExcalidrawLinearElement => element.type === "line",
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(line).toBeDefined();
|
|
||||||
|
|
||||||
const endpoint = LinearElementEditor.getPointGlobalCoordinates(
|
|
||||||
line!,
|
|
||||||
line!.points[line!.points.length - 1],
|
|
||||||
h.app.scene.getNonDeletedElementsMap(),
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(endpoint).toEqual(pointFrom<GlobalPoint>(100, 100));
|
|
||||||
});
|
|
||||||
|
|
||||||
it("prefers binding over external snaps when creating an arrow endpoint", () => {
|
|
||||||
const rect = API.createElement({
|
|
||||||
type: "rectangle",
|
|
||||||
x: 100,
|
|
||||||
y: 100,
|
|
||||||
width: 40,
|
|
||||||
height: 40,
|
|
||||||
});
|
|
||||||
API.setElements([rect]);
|
|
||||||
API.setAppState({ objectsSnapModeEnabled: true });
|
|
||||||
|
|
||||||
UI.clickTool("arrow");
|
|
||||||
|
|
||||||
const startPoint = pointFrom<GlobalPoint>(20, 20);
|
|
||||||
const pointerNearBindable = pointFrom<GlobalPoint>(96, 118);
|
|
||||||
|
|
||||||
dragMove(startPoint, pointerNearBindable);
|
|
||||||
|
|
||||||
expect(h.state.suggestedBinding?.element.id).toBe(rect.id);
|
|
||||||
expect(h.state.snapLines).toEqual([]);
|
|
||||||
|
|
||||||
dragEnd(pointerNearBindable);
|
|
||||||
|
|
||||||
const arrow = h.elements.find(
|
|
||||||
(element): element is ExcalidrawLinearElement => element.type === "arrow",
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(arrow?.endBinding?.elementId).toBe(rect.id);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should enter line editor via enter (line)", () => {
|
it("should enter line editor via enter (line)", () => {
|
||||||
createTwoPointerLinearElement("line");
|
createTwoPointerLinearElement("line");
|
||||||
expect(h.state.selectedLinearElement?.isEditing).toBe(false);
|
expect(h.state.selectedLinearElement?.isEditing).toBe(false);
|
||||||
@@ -486,77 +401,6 @@ describe("Test Linear Elements", () => {
|
|||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("shows snap lines when dragging a point to another line point axis", () => {
|
|
||||||
const line = API.createElement({
|
|
||||||
type: "line",
|
|
||||||
x: 20,
|
|
||||||
y: 20,
|
|
||||||
width: 100,
|
|
||||||
height: 50,
|
|
||||||
roughness: 0,
|
|
||||||
points: [
|
|
||||||
pointFrom<LocalPoint>(0, 0),
|
|
||||||
pointFrom<LocalPoint>(50, 50),
|
|
||||||
pointFrom<LocalPoint>(100, 0),
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([line]);
|
|
||||||
API.setAppState({ objectsSnapModeEnabled: true });
|
|
||||||
enterLineEditingMode(line);
|
|
||||||
|
|
||||||
const middlePoint = pointFrom<GlobalPoint>(70, 70);
|
|
||||||
const pointerNearEndPointX = pointFrom<GlobalPoint>(117, 65);
|
|
||||||
|
|
||||||
dragMove(middlePoint, pointerNearEndPointX);
|
|
||||||
|
|
||||||
expect(h.state.snapLines.length).toBeGreaterThan(0);
|
|
||||||
|
|
||||||
dragEnd(pointerNearEndPointX);
|
|
||||||
|
|
||||||
expect(API.getElement(line).points[1]).toEqual(
|
|
||||||
pointFrom<LocalPoint>(100, 45),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("prefers binding over external snaps when dragging an existing arrow endpoint", () => {
|
|
||||||
const rect = API.createElement({
|
|
||||||
type: "rectangle",
|
|
||||||
x: 100,
|
|
||||||
y: 100,
|
|
||||||
width: 40,
|
|
||||||
height: 40,
|
|
||||||
});
|
|
||||||
const arrow = API.createElement({
|
|
||||||
type: "arrow",
|
|
||||||
x: 20,
|
|
||||||
y: 20,
|
|
||||||
width: 40,
|
|
||||||
height: 0,
|
|
||||||
points: [pointFrom<LocalPoint>(0, 0), pointFrom<LocalPoint>(40, 0)],
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([rect, arrow]);
|
|
||||||
API.setAppState({ objectsSnapModeEnabled: true });
|
|
||||||
enterLineEditingMode(arrow);
|
|
||||||
|
|
||||||
const endPoint = LinearElementEditor.getPointGlobalCoordinates(
|
|
||||||
arrow,
|
|
||||||
arrow.points[arrow.points.length - 1],
|
|
||||||
h.app.scene.getNonDeletedElementsMap(),
|
|
||||||
);
|
|
||||||
const pointerNearBindable = pointFrom<GlobalPoint>(96, 118);
|
|
||||||
|
|
||||||
dragMove(endPoint, pointerNearBindable);
|
|
||||||
|
|
||||||
expect(h.state.suggestedBinding?.element.id).toBe(rect.id);
|
|
||||||
expect(h.state.snapLines).toEqual([]);
|
|
||||||
|
|
||||||
dragEnd(pointerNearBindable);
|
|
||||||
|
|
||||||
expect(API.getElement(arrow).endBinding?.elementId).toBe(rect.id);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should update the midpoints when element roundness changed", async () => {
|
it("should update the midpoints when element roundness changed", async () => {
|
||||||
createThreePointerLinearElement("line");
|
createThreePointerLinearElement("line");
|
||||||
|
|
||||||
|
|||||||
@@ -239,16 +239,6 @@ import {
|
|||||||
hitElementBoundingBox,
|
hitElementBoundingBox,
|
||||||
isLineElement,
|
isLineElement,
|
||||||
isSimpleArrow,
|
isSimpleArrow,
|
||||||
isGridModeEnabled,
|
|
||||||
SnapCache,
|
|
||||||
isActiveToolNonLinearSnappable,
|
|
||||||
getSnapLinesAtPointer,
|
|
||||||
isSnappingEnabled,
|
|
||||||
getReferenceSnapPoints,
|
|
||||||
getVisibleGaps,
|
|
||||||
snapDraggedElements,
|
|
||||||
snapNewElement,
|
|
||||||
snapResizingElements,
|
|
||||||
StoreDelta,
|
StoreDelta,
|
||||||
type ApplyToOptions,
|
type ApplyToOptions,
|
||||||
positionElementsOnGrid,
|
positionElementsOnGrid,
|
||||||
@@ -406,6 +396,18 @@ import {
|
|||||||
import { Fonts } from "../fonts";
|
import { Fonts } from "../fonts";
|
||||||
import { editorJotaiStore, type WritableAtom } from "../editor-jotai";
|
import { editorJotaiStore, type WritableAtom } from "../editor-jotai";
|
||||||
import { ImageSceneDataError } from "../errors";
|
import { ImageSceneDataError } from "../errors";
|
||||||
|
import {
|
||||||
|
getSnapLinesAtPointer,
|
||||||
|
snapDraggedElements,
|
||||||
|
isActiveToolNonLinearSnappable,
|
||||||
|
snapNewElement,
|
||||||
|
snapResizingElements,
|
||||||
|
isSnappingEnabled,
|
||||||
|
getVisibleGaps,
|
||||||
|
getReferenceSnapPoints,
|
||||||
|
SnapCache,
|
||||||
|
isGridModeEnabled,
|
||||||
|
} from "../snapping";
|
||||||
import { Renderer } from "../scene/Renderer";
|
import { Renderer } from "../scene/Renderer";
|
||||||
import {
|
import {
|
||||||
setEraserCursor,
|
setEraserCursor,
|
||||||
@@ -783,29 +785,6 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
return api;
|
return api;
|
||||||
}
|
}
|
||||||
|
|
||||||
private withStableSnapLines<T extends { snapLines: AppState["snapLines"] }>(
|
|
||||||
state: T,
|
|
||||||
): T {
|
|
||||||
const snapLines = updateStable(this.state.snapLines, state.snapLines);
|
|
||||||
|
|
||||||
return snapLines === state.snapLines
|
|
||||||
? state
|
|
||||||
: {
|
|
||||||
...state,
|
|
||||||
snapLines,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private shouldUpdateSelectedLinearElementState(
|
|
||||||
selectedLinearElement: AppState["selectedLinearElement"],
|
|
||||||
snapLines: AppState["snapLines"],
|
|
||||||
) {
|
|
||||||
return (
|
|
||||||
selectedLinearElement !== this.state.selectedLinearElement ||
|
|
||||||
snapLines !== this.state.snapLines
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(props: AppProps) {
|
constructor(props: AppProps) {
|
||||||
super(props);
|
super(props);
|
||||||
const defaultAppState = getDefaultAppState();
|
const defaultAppState = getDefaultAppState();
|
||||||
@@ -6807,10 +6786,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
!this.state.newElement &&
|
!this.state.newElement &&
|
||||||
(isActiveToolNonLinearSnappable(this.state.activeTool.type) ||
|
isActiveToolNonLinearSnappable(this.state.activeTool.type)
|
||||||
((this.state.activeTool.type === "line" ||
|
|
||||||
this.state.activeTool.type === "arrow") &&
|
|
||||||
this.state.currentItemArrowType !== ARROW_TYPE.elbow))
|
|
||||||
) {
|
) {
|
||||||
const { originOffset, snapLines } = getSnapLinesAtPointer(
|
const { originOffset, snapLines } = getSnapLinesAtPointer(
|
||||||
this.scene.getNonDeletedElements(),
|
this.scene.getNonDeletedElements(),
|
||||||
@@ -6859,7 +6835,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
this.state.selectedLinearElement?.isEditing &&
|
this.state.selectedLinearElement?.isEditing &&
|
||||||
!this.state.selectedLinearElement.isDragging
|
!this.state.selectedLinearElement.isDragging
|
||||||
) {
|
) {
|
||||||
const result = this.state.newElement
|
const editingLinearElement = this.state.newElement
|
||||||
? null
|
? null
|
||||||
: LinearElementEditor.handlePointerMoveInEditMode(
|
: LinearElementEditor.handlePointerMoveInEditMode(
|
||||||
event,
|
event,
|
||||||
@@ -6868,33 +6844,18 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
this,
|
this,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (result) {
|
if (
|
||||||
const { editingLinearElement, snapLines } = result;
|
editingLinearElement &&
|
||||||
const nextState = this.withStableSnapLines({
|
editingLinearElement !== this.state.selectedLinearElement
|
||||||
selectedLinearElement: editingLinearElement,
|
) {
|
||||||
snapLines,
|
// Since we are reading from previous state which is not possible with
|
||||||
});
|
// automatic batching in React 18 hence using flush sync to synchronously
|
||||||
|
// update the state. Check https://github.com/excalidraw/excalidraw/pull/5508 for more details.
|
||||||
if (
|
flushSync(() => {
|
||||||
editingLinearElement &&
|
this.setState({
|
||||||
this.shouldUpdateSelectedLinearElementState(
|
selectedLinearElement: editingLinearElement,
|
||||||
nextState.selectedLinearElement,
|
|
||||||
nextState.snapLines,
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
// Since we are reading from previous state which is not possible with
|
|
||||||
// automatic batching in React 18 hence using flush sync to synchronously
|
|
||||||
// update the state. Check https://github.com/excalidraw/excalidraw/pull/5508 for more details.
|
|
||||||
flushSync(() => {
|
|
||||||
this.setState(nextState);
|
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
if (
|
|
||||||
editingLinearElement.lastUncommittedPoint == null &&
|
|
||||||
this.state.suggestedBinding
|
|
||||||
) {
|
|
||||||
this.setState({ suggestedBinding: null });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -9752,27 +9713,25 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
pointerDownState.lastCoords.x = pointerCoords.x;
|
pointerDownState.lastCoords.x = pointerCoords.x;
|
||||||
pointerDownState.lastCoords.y = pointerCoords.y;
|
pointerDownState.lastCoords.y = pointerCoords.y;
|
||||||
pointerDownState.drag.hasOccurred = true;
|
pointerDownState.drag.hasOccurred = true;
|
||||||
const nextState = this.withStableSnapLines(newState);
|
|
||||||
|
|
||||||
// NOTE: Optimize setState calls because it
|
// NOTE: Optimize setState calls because it
|
||||||
// affects history and performance
|
// affects history and performance
|
||||||
if (
|
if (
|
||||||
nextState.suggestedBinding !== this.state.suggestedBinding ||
|
newState.suggestedBinding !== this.state.suggestedBinding ||
|
||||||
!isShallowEqual(
|
!isShallowEqual(
|
||||||
nextState.selectedLinearElement?.selectedPointsIndices ?? [],
|
newState.selectedLinearElement?.selectedPointsIndices ?? [],
|
||||||
this.state.selectedLinearElement?.selectedPointsIndices ?? [],
|
this.state.selectedLinearElement?.selectedPointsIndices ?? [],
|
||||||
) ||
|
) ||
|
||||||
nextState.selectedLinearElement?.hoverPointIndex !==
|
newState.selectedLinearElement?.hoverPointIndex !==
|
||||||
this.state.selectedLinearElement?.hoverPointIndex ||
|
this.state.selectedLinearElement?.hoverPointIndex ||
|
||||||
nextState.selectedLinearElement?.customLineAngle !==
|
newState.selectedLinearElement?.customLineAngle !==
|
||||||
this.state.selectedLinearElement?.customLineAngle ||
|
this.state.selectedLinearElement?.customLineAngle ||
|
||||||
this.state.selectedLinearElement.isDragging !==
|
this.state.selectedLinearElement.isDragging !==
|
||||||
nextState.selectedLinearElement?.isDragging ||
|
newState.selectedLinearElement?.isDragging ||
|
||||||
this.state.selectedLinearElement?.initialState?.altFocusPoint !==
|
this.state.selectedLinearElement?.initialState?.altFocusPoint !==
|
||||||
nextState.selectedLinearElement?.initialState?.altFocusPoint ||
|
newState.selectedLinearElement?.initialState?.altFocusPoint
|
||||||
nextState.snapLines !== this.state.snapLines
|
|
||||||
) {
|
) {
|
||||||
this.setState(nextState);
|
this.setState(newState);
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@@ -10461,7 +10420,8 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
this.lassoTrail.endPath();
|
this.lassoTrail.endPath();
|
||||||
this.previousPointerMoveCoords = null;
|
this.previousPointerMoveCoords = null;
|
||||||
|
|
||||||
SnapCache.destroy();
|
SnapCache.setReferenceSnapPoints(null);
|
||||||
|
SnapCache.setVisibleGaps(null);
|
||||||
|
|
||||||
this.savePointer(childEvent.clientX, childEvent.clientY, "up");
|
this.savePointer(childEvent.clientX, childEvent.clientY, "up");
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { CANVAS_SEARCH_TAB, DEFAULT_SIDEBAR } from "@excalidraw/common";
|
|||||||
import {
|
import {
|
||||||
isFlowchartNodeElement,
|
isFlowchartNodeElement,
|
||||||
isImageElement,
|
isImageElement,
|
||||||
isGridModeEnabled,
|
|
||||||
isLinearElement,
|
isLinearElement,
|
||||||
isLineElement,
|
isLineElement,
|
||||||
isTextBindableContainer,
|
isTextBindableContainer,
|
||||||
@@ -17,6 +16,7 @@ import type { EditorInterface } from "@excalidraw/common";
|
|||||||
import { t } from "../i18n";
|
import { t } from "../i18n";
|
||||||
import { getShortcutKey } from "../shortcut";
|
import { getShortcutKey } from "../shortcut";
|
||||||
import { isEraserActive } from "../appState";
|
import { isEraserActive } from "../appState";
|
||||||
|
import { isGridModeEnabled } from "../snapping";
|
||||||
|
|
||||||
import "./HintViewer.scss";
|
import "./HintViewer.scss";
|
||||||
|
|
||||||
|
|||||||
@@ -12,11 +12,10 @@ import { frameAndChildrenSelectedTogether } from "@excalidraw/element";
|
|||||||
|
|
||||||
import { elementsAreInSameGroup } from "@excalidraw/element";
|
import { elementsAreInSameGroup } from "@excalidraw/element";
|
||||||
|
|
||||||
import { isGridModeEnabled } from "@excalidraw/element";
|
|
||||||
|
|
||||||
import type { NonDeletedExcalidrawElement } from "@excalidraw/element/types";
|
import type { NonDeletedExcalidrawElement } from "@excalidraw/element/types";
|
||||||
|
|
||||||
import { t } from "../../i18n";
|
import { t } from "../../i18n";
|
||||||
|
import { isGridModeEnabled } from "../../snapping";
|
||||||
import { useExcalidrawAppState, useExcalidrawSetAppState } from "../App";
|
import { useExcalidrawAppState, useExcalidrawSetAppState } from "../App";
|
||||||
import { Island } from "../Island";
|
import { Island } from "../Island";
|
||||||
import { CloseIcon } from "../icons";
|
import { CloseIcon } from "../icons";
|
||||||
|
|||||||
@@ -96,8 +96,6 @@ type RestoredAppState = Omit<
|
|||||||
"offsetTop" | "offsetLeft" | "width" | "height"
|
"offsetTop" | "offsetLeft" | "width" | "height"
|
||||||
>;
|
>;
|
||||||
|
|
||||||
const MAX_ARROW_PX = 75_000;
|
|
||||||
|
|
||||||
export const AllowedExcalidrawActiveTools: Record<
|
export const AllowedExcalidrawActiveTools: Record<
|
||||||
AppState["activeTool"]["type"],
|
AppState["activeTool"]["type"],
|
||||||
boolean
|
boolean
|
||||||
@@ -469,8 +467,8 @@ export const restoreElement = (
|
|||||||
element.endArrowhead === undefined
|
element.endArrowhead === undefined
|
||||||
? "arrow"
|
? "arrow"
|
||||||
: normalizeArrowhead(element.endArrowhead);
|
: normalizeArrowhead(element.endArrowhead);
|
||||||
const x = element.x as number | undefined;
|
const x: number | undefined = element.x;
|
||||||
const y = element.y as number | undefined;
|
const y: number | undefined = element.y;
|
||||||
const points: readonly LocalPoint[] | undefined = // migrate old arrow model to new one
|
const points: readonly LocalPoint[] | undefined = // migrate old arrow model to new one
|
||||||
!Array.isArray(element.points) || element.points.length < 2
|
!Array.isArray(element.points) || element.points.length < 2
|
||||||
? [pointFrom(0, 0), pointFrom(element.width, element.height)]
|
? [pointFrom(0, 0), pointFrom(element.width, element.height)]
|
||||||
@@ -495,8 +493,8 @@ export const restoreElement = (
|
|||||||
startArrowhead,
|
startArrowhead,
|
||||||
endArrowhead,
|
endArrowhead,
|
||||||
points,
|
points,
|
||||||
x: x ?? 0,
|
x,
|
||||||
y: y ?? 0,
|
y,
|
||||||
elbowed: (element as ExcalidrawArrowElement).elbowed,
|
elbowed: (element as ExcalidrawArrowElement).elbowed,
|
||||||
...getSizeFromPoints(points),
|
...getSizeFromPoints(points),
|
||||||
};
|
};
|
||||||
@@ -515,44 +513,12 @@ export const restoreElement = (
|
|||||||
})
|
})
|
||||||
: restoreElementWithProperties(element as ExcalidrawArrowElement, base);
|
: restoreElementWithProperties(element as ExcalidrawArrowElement, base);
|
||||||
|
|
||||||
const normalizedRestoredElement = {
|
return {
|
||||||
...restoredElement,
|
...restoredElement,
|
||||||
...LinearElementEditor.getNormalizeElementPointsAndCoords(
|
...LinearElementEditor.getNormalizeElementPointsAndCoords(
|
||||||
restoredElement,
|
restoredElement,
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Last resort fix for extremely large arrows
|
|
||||||
if (
|
|
||||||
normalizedRestoredElement.width > MAX_ARROW_PX ||
|
|
||||||
normalizedRestoredElement.height > MAX_ARROW_PX
|
|
||||||
) {
|
|
||||||
console.error(
|
|
||||||
`Removing extremely large arrow ${
|
|
||||||
normalizedRestoredElement.id
|
|
||||||
} (type: ${
|
|
||||||
isElbowArrow(normalizedRestoredElement) ? "elbow" : "simple"
|
|
||||||
}, width: ${normalizedRestoredElement.width}, height: ${
|
|
||||||
normalizedRestoredElement.height
|
|
||||||
}, x: ${normalizedRestoredElement.x}, y: ${
|
|
||||||
normalizedRestoredElement.y
|
|
||||||
})`,
|
|
||||||
);
|
|
||||||
return {
|
|
||||||
...normalizedRestoredElement,
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 100,
|
|
||||||
points: [
|
|
||||||
pointFrom<LocalPoint>(0, 0),
|
|
||||||
pointFrom<LocalPoint>(100, 100),
|
|
||||||
],
|
|
||||||
isDeleted: true,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return normalizedRestoredElement;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// generic elements
|
// generic elements
|
||||||
@@ -700,7 +666,6 @@ export const restoreElements = <T extends ExcalidrawElement>(
|
|||||||
const existingElementsMap = existingElements
|
const existingElementsMap = existingElements
|
||||||
? arrayToMap(existingElements)
|
? arrayToMap(existingElements)
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
const restoredElements = syncInvalidIndices(
|
const restoredElements = syncInvalidIndices(
|
||||||
(targetElements || []).reduce((elements, element) => {
|
(targetElements || []).reduce((elements, element) => {
|
||||||
// filtering out selection, which is legacy, no longer kept in elements,
|
// filtering out selection, which is legacy, no longer kept in elements,
|
||||||
@@ -797,7 +762,7 @@ export const restoreElements = <T extends ExcalidrawElement>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE (mtolmacs): Temporary fix for invalid/self-bound elbow arrows
|
// NOTE (mtolmacs): Temporary fix for extremely large arrows
|
||||||
// Need to iterate again so we have attached text nodes in elementsMap
|
// Need to iterate again so we have attached text nodes in elementsMap
|
||||||
return restoredElements.map((element) => {
|
return restoredElements.map((element) => {
|
||||||
if (
|
if (
|
||||||
|
|||||||
@@ -2,8 +2,7 @@ import { pointFrom, type GlobalPoint, type LocalPoint } from "@excalidraw/math";
|
|||||||
|
|
||||||
import { THEME } from "@excalidraw/common";
|
import { THEME } from "@excalidraw/common";
|
||||||
|
|
||||||
import type { PointSnapLine, PointerSnapLine } from "@excalidraw/element";
|
import type { PointSnapLine, PointerSnapLine } from "../snapping";
|
||||||
|
|
||||||
import type { InteractiveCanvasAppState } from "../types";
|
import type { InteractiveCanvasAppState } from "../types";
|
||||||
|
|
||||||
const SNAP_COLOR_LIGHT = "#ff6b6b";
|
const SNAP_COLOR_LIGHT = "#ff6b6b";
|
||||||
|
|||||||
@@ -1,8 +1,4 @@
|
|||||||
import {
|
import {
|
||||||
isCloseTo,
|
|
||||||
line,
|
|
||||||
linesIntersectAt,
|
|
||||||
pointDistance,
|
|
||||||
pointFrom,
|
pointFrom,
|
||||||
pointRotateRads,
|
pointRotateRads,
|
||||||
rangeInclusive,
|
rangeInclusive,
|
||||||
@@ -17,7 +13,7 @@ import {
|
|||||||
getDraggedElementsBounds,
|
getDraggedElementsBounds,
|
||||||
getElementAbsoluteCoords,
|
getElementAbsoluteCoords,
|
||||||
} from "@excalidraw/element";
|
} from "@excalidraw/element";
|
||||||
import { isBoundToContainer, isElbowArrow } from "@excalidraw/element";
|
import { isBoundToContainer } from "@excalidraw/element";
|
||||||
|
|
||||||
import { getMaximumGroups } from "@excalidraw/element";
|
import { getMaximumGroups } from "@excalidraw/element";
|
||||||
|
|
||||||
@@ -33,18 +29,14 @@ import type { MaybeTransformHandleType } from "@excalidraw/element";
|
|||||||
import type {
|
import type {
|
||||||
ElementsMap,
|
ElementsMap,
|
||||||
ExcalidrawElement,
|
ExcalidrawElement,
|
||||||
ExcalidrawLinearElement,
|
|
||||||
NonDeletedExcalidrawElement,
|
NonDeletedExcalidrawElement,
|
||||||
NonDeleted,
|
|
||||||
} from "@excalidraw/element/types";
|
} from "@excalidraw/element/types";
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
AppClassProperties,
|
AppClassProperties,
|
||||||
AppState,
|
AppState,
|
||||||
KeyboardModifiersObject,
|
KeyboardModifiersObject,
|
||||||
} from "@excalidraw/excalidraw/types";
|
} from "./types";
|
||||||
|
|
||||||
import { LinearElementEditor } from "./linearElementEditor";
|
|
||||||
|
|
||||||
const SNAP_DISTANCE = 8;
|
const SNAP_DISTANCE = 8;
|
||||||
|
|
||||||
@@ -130,11 +122,6 @@ export type SnapLine = PointSnapLine | GapSnapLine | PointerSnapLine;
|
|||||||
export class SnapCache {
|
export class SnapCache {
|
||||||
private static referenceSnapPoints: GlobalPoint[] | null = null;
|
private static referenceSnapPoints: GlobalPoint[] | null = null;
|
||||||
|
|
||||||
private static linearElementAxisSnapTargets: {
|
|
||||||
editingElementId: ExcalidrawElement["id"];
|
|
||||||
snapTargets: GlobalPoint[];
|
|
||||||
} | null = null;
|
|
||||||
|
|
||||||
private static visibleGaps: {
|
private static visibleGaps: {
|
||||||
verticalGaps: Gap[];
|
verticalGaps: Gap[];
|
||||||
horizontalGaps: Gap[];
|
horizontalGaps: Gap[];
|
||||||
@@ -148,27 +135,6 @@ export class SnapCache {
|
|||||||
return SnapCache.referenceSnapPoints;
|
return SnapCache.referenceSnapPoints;
|
||||||
};
|
};
|
||||||
|
|
||||||
public static setLinearElementAxisSnapTargets = (
|
|
||||||
editingElementId: ExcalidrawElement["id"],
|
|
||||||
snapTargets: GlobalPoint[] | null,
|
|
||||||
) => {
|
|
||||||
SnapCache.linearElementAxisSnapTargets = snapTargets
|
|
||||||
? {
|
|
||||||
editingElementId,
|
|
||||||
snapTargets,
|
|
||||||
}
|
|
||||||
: null;
|
|
||||||
};
|
|
||||||
|
|
||||||
public static getLinearElementAxisSnapTargets = (
|
|
||||||
editingElementId: ExcalidrawElement["id"],
|
|
||||||
) => {
|
|
||||||
return SnapCache.linearElementAxisSnapTargets?.editingElementId ===
|
|
||||||
editingElementId
|
|
||||||
? SnapCache.linearElementAxisSnapTargets.snapTargets
|
|
||||||
: null;
|
|
||||||
};
|
|
||||||
|
|
||||||
public static setVisibleGaps = (
|
public static setVisibleGaps = (
|
||||||
gaps: {
|
gaps: {
|
||||||
verticalGaps: Gap[];
|
verticalGaps: Gap[];
|
||||||
@@ -184,7 +150,6 @@ export class SnapCache {
|
|||||||
|
|
||||||
public static destroy = () => {
|
public static destroy = () => {
|
||||||
SnapCache.referenceSnapPoints = null;
|
SnapCache.referenceSnapPoints = null;
|
||||||
SnapCache.linearElementAxisSnapTargets = null;
|
|
||||||
SnapCache.visibleGaps = null;
|
SnapCache.visibleGaps = null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -270,19 +235,6 @@ export const getElementsCorners = (
|
|||||||
const halfHeight = (y2 - y1) / 2;
|
const halfHeight = (y2 - y1) / 2;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
(element.type === "line" || element.type === "arrow") &&
|
|
||||||
!boundingBoxCorners
|
|
||||||
) {
|
|
||||||
// For linear elements, use actual points instead of bounding box
|
|
||||||
const linearPoints = LinearElementEditor.getPointsGlobalCoordinates(
|
|
||||||
element as NonDeleted<ExcalidrawLinearElement>,
|
|
||||||
elementsMap,
|
|
||||||
{
|
|
||||||
dragOffset,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
result = linearPoints;
|
|
||||||
} else if (
|
|
||||||
(element.type === "diamond" || element.type === "ellipse") &&
|
(element.type === "diamond" || element.type === "ellipse") &&
|
||||||
!boundingBoxCorners
|
!boundingBoxCorners
|
||||||
) {
|
) {
|
||||||
@@ -681,227 +633,6 @@ export const getReferenceSnapPoints = (
|
|||||||
.flatMap((elementGroup) => getElementsCorners(elementGroup, elementsMap));
|
.flatMap((elementGroup) => getElementsCorners(elementGroup, elementsMap));
|
||||||
};
|
};
|
||||||
|
|
||||||
const getExternalAxisSnapTargets = (
|
|
||||||
elements: readonly NonDeletedExcalidrawElement[],
|
|
||||||
editingElement: ExcalidrawLinearElement,
|
|
||||||
appState: AppState,
|
|
||||||
elementsMap: ElementsMap,
|
|
||||||
) => {
|
|
||||||
const cachedAxisSnapTargets = SnapCache.getLinearElementAxisSnapTargets(
|
|
||||||
editingElement.id,
|
|
||||||
);
|
|
||||||
|
|
||||||
const externalAxisSnapTargets =
|
|
||||||
cachedAxisSnapTargets ??
|
|
||||||
getReferenceSnapPoints(elements, [editingElement], appState, elementsMap);
|
|
||||||
|
|
||||||
if (!cachedAxisSnapTargets) {
|
|
||||||
SnapCache.setLinearElementAxisSnapTargets(
|
|
||||||
editingElement.id,
|
|
||||||
externalAxisSnapTargets,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return externalAxisSnapTargets;
|
|
||||||
};
|
|
||||||
|
|
||||||
const getOwnAxisSnapTargets = (
|
|
||||||
editingElement: ExcalidrawLinearElement,
|
|
||||||
elementsMap: ElementsMap,
|
|
||||||
selectedPointsIndices?: readonly number[],
|
|
||||||
) => {
|
|
||||||
return LinearElementEditor.getPointsGlobalCoordinates(
|
|
||||||
editingElement as NonDeleted<ExcalidrawLinearElement>,
|
|
||||||
elementsMap,
|
|
||||||
{
|
|
||||||
excludePointsIndices: selectedPointsIndices,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getAxisSnapTargets = (
|
|
||||||
elements: readonly NonDeletedExcalidrawElement[],
|
|
||||||
editingElement: ExcalidrawLinearElement,
|
|
||||||
appState: AppState,
|
|
||||||
elementsMap: ElementsMap,
|
|
||||||
options: {
|
|
||||||
includeSelfPoints?: boolean;
|
|
||||||
selectedPointsIndices?: readonly number[];
|
|
||||||
} = {},
|
|
||||||
) => {
|
|
||||||
const externalAxisSnapTargets = getExternalAxisSnapTargets(
|
|
||||||
elements,
|
|
||||||
editingElement,
|
|
||||||
appState,
|
|
||||||
elementsMap,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!options.includeSelfPoints) {
|
|
||||||
return externalAxisSnapTargets;
|
|
||||||
}
|
|
||||||
|
|
||||||
return externalAxisSnapTargets.concat(
|
|
||||||
getOwnAxisSnapTargets(
|
|
||||||
editingElement,
|
|
||||||
elementsMap,
|
|
||||||
options.selectedPointsIndices,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const collectNearestAxisSnapCandidates = (
|
|
||||||
axisSnapTargets: readonly GlobalPoint[],
|
|
||||||
pointerPosition: GlobalPoint,
|
|
||||||
nearestSnapsX: Snaps,
|
|
||||||
nearestSnapsY: Snaps,
|
|
||||||
minOffset: Vector2D,
|
|
||||||
) => {
|
|
||||||
for (const snapTarget of axisSnapTargets) {
|
|
||||||
const offsetX = snapTarget[0] - pointerPosition[0];
|
|
||||||
const offsetY = snapTarget[1] - pointerPosition[1];
|
|
||||||
const absOffsetX = Math.abs(offsetX);
|
|
||||||
const absOffsetY = Math.abs(offsetY);
|
|
||||||
|
|
||||||
if (absOffsetX > minOffset.x && absOffsetY > minOffset.y) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (absOffsetX <= minOffset.x) {
|
|
||||||
if (absOffsetX < minOffset.x) {
|
|
||||||
nearestSnapsX.length = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
nearestSnapsX.push({
|
|
||||||
type: "point",
|
|
||||||
points: [pointerPosition, snapTarget],
|
|
||||||
offset: offsetX,
|
|
||||||
});
|
|
||||||
|
|
||||||
minOffset.x = absOffsetX;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (absOffsetY <= minOffset.y) {
|
|
||||||
if (absOffsetY < minOffset.y) {
|
|
||||||
nearestSnapsY.length = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
nearestSnapsY.push({
|
|
||||||
type: "point",
|
|
||||||
points: [pointerPosition, snapTarget],
|
|
||||||
offset: offsetY,
|
|
||||||
});
|
|
||||||
|
|
||||||
minOffset.y = absOffsetY;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const snapLinearElementPoint = (
|
|
||||||
elements: readonly NonDeletedExcalidrawElement[],
|
|
||||||
editingElement: ExcalidrawLinearElement,
|
|
||||||
pointerPosition: GlobalPoint,
|
|
||||||
app: AppClassProperties,
|
|
||||||
event: KeyboardModifiersObject,
|
|
||||||
elementsMap: ElementsMap,
|
|
||||||
options: {
|
|
||||||
includeExternalPoints?: boolean;
|
|
||||||
includeSelfPoints?: boolean;
|
|
||||||
selectedPointsIndices?: readonly number[];
|
|
||||||
} = {},
|
|
||||||
) => {
|
|
||||||
if (
|
|
||||||
!isSnappingEnabled({ app, event, selectedElements: [editingElement] }) ||
|
|
||||||
isElbowArrow(editingElement)
|
|
||||||
) {
|
|
||||||
return {
|
|
||||||
snapOffset: { x: 0, y: 0 },
|
|
||||||
snapLines: [],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const snapDistance = getSnapDistance(app.state.zoom.value);
|
|
||||||
const minOffset = {
|
|
||||||
x: snapDistance,
|
|
||||||
y: snapDistance,
|
|
||||||
};
|
|
||||||
|
|
||||||
const nearestSnapsX: Snaps = [];
|
|
||||||
const nearestSnapsY: Snaps = [];
|
|
||||||
|
|
||||||
if (options.includeExternalPoints !== false) {
|
|
||||||
collectNearestAxisSnapCandidates(
|
|
||||||
getExternalAxisSnapTargets(
|
|
||||||
elements,
|
|
||||||
editingElement,
|
|
||||||
app.state,
|
|
||||||
elementsMap,
|
|
||||||
),
|
|
||||||
pointerPosition,
|
|
||||||
nearestSnapsX,
|
|
||||||
nearestSnapsY,
|
|
||||||
minOffset,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.includeSelfPoints) {
|
|
||||||
collectNearestAxisSnapCandidates(
|
|
||||||
getOwnAxisSnapTargets(
|
|
||||||
editingElement,
|
|
||||||
elementsMap,
|
|
||||||
options.selectedPointsIndices,
|
|
||||||
),
|
|
||||||
pointerPosition,
|
|
||||||
nearestSnapsX,
|
|
||||||
nearestSnapsY,
|
|
||||||
minOffset,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const snapOffset = {
|
|
||||||
x: nearestSnapsX[0]?.offset ?? 0,
|
|
||||||
y: nearestSnapsY[0]?.offset ?? 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Create snap lines using the snapped position (fixed position)
|
|
||||||
let pointSnapLines: SnapLine[] = [];
|
|
||||||
|
|
||||||
if (snapOffset.x !== 0 || snapOffset.y !== 0) {
|
|
||||||
const snappedPosition = pointFrom<GlobalPoint>(
|
|
||||||
pointerPosition[0] + snapOffset.x,
|
|
||||||
pointerPosition[1] + snapOffset.y,
|
|
||||||
);
|
|
||||||
|
|
||||||
const snappedSnapsX = nearestSnapsX
|
|
||||||
.filter(
|
|
||||||
(snap): snap is PointSnap =>
|
|
||||||
snap.type === "point" && isCloseTo(snap.offset, snapOffset.x, 0.01),
|
|
||||||
)
|
|
||||||
.map((snap) => ({
|
|
||||||
type: "point" as const,
|
|
||||||
points: [snappedPosition, snap.points[1]] as [GlobalPoint, GlobalPoint],
|
|
||||||
offset: 0,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const snappedSnapsY = nearestSnapsY
|
|
||||||
.filter(
|
|
||||||
(snap): snap is PointSnap =>
|
|
||||||
snap.type === "point" && isCloseTo(snap.offset, snapOffset.y, 0.01),
|
|
||||||
)
|
|
||||||
.map((snap) => ({
|
|
||||||
type: "point" as const,
|
|
||||||
points: [snappedPosition, snap.points[1]] as [GlobalPoint, GlobalPoint],
|
|
||||||
offset: 0,
|
|
||||||
}));
|
|
||||||
|
|
||||||
pointSnapLines = createPointSnapLines(snappedSnapsX, snappedSnapsY);
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
snapOffset,
|
|
||||||
snapLines: pointSnapLines,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
const getPointSnaps = (
|
const getPointSnaps = (
|
||||||
selectedElements: ExcalidrawElement[],
|
selectedElements: ExcalidrawElement[],
|
||||||
selectionSnapPoints: GlobalPoint[],
|
selectionSnapPoints: GlobalPoint[],
|
||||||
@@ -1681,79 +1412,3 @@ export const isActiveToolNonLinearSnappable = (
|
|||||||
activeToolType === TOOL_TYPE.text
|
activeToolType === TOOL_TYPE.text
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Snaps to discrete angle rotation logic.
|
|
||||||
* This function handles the common pattern of finding intersections between
|
|
||||||
* angle lines and snap lines, and updating the snap lines accordingly.
|
|
||||||
*
|
|
||||||
* @param snapLines - The original snap lines from snapping
|
|
||||||
* @param angleLine - The line representing the discrete angle constraint
|
|
||||||
* @param gridPosition - The grid position (original pointer position)
|
|
||||||
* @param referencePosition - The reference position (usually the start point)
|
|
||||||
* @returns Object containing updated snap lines and position deltas
|
|
||||||
*/
|
|
||||||
export const snapToDiscreteAngle = (
|
|
||||||
snapLines: SnapLine[],
|
|
||||||
angleLine: [GlobalPoint, GlobalPoint],
|
|
||||||
gridPosition: GlobalPoint,
|
|
||||||
referencePosition: GlobalPoint,
|
|
||||||
): {
|
|
||||||
snapLines: SnapLine[];
|
|
||||||
dxFromReference: number;
|
|
||||||
dyFromReference: number;
|
|
||||||
} => {
|
|
||||||
if (snapLines.length === 0) {
|
|
||||||
return {
|
|
||||||
snapLines: [],
|
|
||||||
dxFromReference: gridPosition[0] - referencePosition[0],
|
|
||||||
dyFromReference: gridPosition[1] - referencePosition[1],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const firstSnapLine = snapLines[0];
|
|
||||||
if (firstSnapLine.type === "points" && firstSnapLine.points.length > 1) {
|
|
||||||
const snapLine = line(firstSnapLine.points[0], firstSnapLine.points[1]);
|
|
||||||
const intersection = linesIntersectAt<GlobalPoint>(
|
|
||||||
line(angleLine[0], angleLine[1]),
|
|
||||||
snapLine,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (intersection) {
|
|
||||||
const dxFromReference = intersection[0] - referencePosition[0];
|
|
||||||
const dyFromReference = intersection[1] - referencePosition[1];
|
|
||||||
|
|
||||||
const furthestPoint = firstSnapLine.points.reduce(
|
|
||||||
(furthest, point) => {
|
|
||||||
const distance = pointDistance(intersection, point);
|
|
||||||
if (distance > furthest.distance) {
|
|
||||||
return { point, distance };
|
|
||||||
}
|
|
||||||
return furthest;
|
|
||||||
},
|
|
||||||
{
|
|
||||||
point: firstSnapLine.points[0],
|
|
||||||
distance: pointDistance(intersection, firstSnapLine.points[0]),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
const updatedSnapLine: PointSnapLine = {
|
|
||||||
type: "points",
|
|
||||||
points: [furthestPoint.point, intersection],
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
|
||||||
snapLines: [updatedSnapLine],
|
|
||||||
dxFromReference,
|
|
||||||
dyFromReference,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If no intersection found, return original snap lines with grid position
|
|
||||||
return {
|
|
||||||
snapLines,
|
|
||||||
dxFromReference: gridPosition[0] - referencePosition[0],
|
|
||||||
dyFromReference: gridPosition[1] - referencePosition[1],
|
|
||||||
};
|
|
||||||
};
|
|
||||||
@@ -8665,14 +8665,7 @@ exports[`regression tests > key 5 selects arrow tool > [end of test] appState 1`
|
|||||||
"openMenu": null,
|
"openMenu": null,
|
||||||
"openPopup": null,
|
"openPopup": null,
|
||||||
"openSidebar": null,
|
"openSidebar": null,
|
||||||
"originSnapOffset": {
|
"originSnapOffset": null,
|
||||||
"x": 0,
|
|
||||||
"y": 0,
|
|
||||||
},
|
|
||||||
"pasteDialog": {
|
|
||||||
"data": null,
|
|
||||||
"shown": false,
|
|
||||||
},
|
|
||||||
"penDetected": false,
|
"penDetected": false,
|
||||||
"penMode": false,
|
"penMode": false,
|
||||||
"preferredSelectionTool": {
|
"preferredSelectionTool": {
|
||||||
@@ -9329,14 +9322,7 @@ exports[`regression tests > key a selects arrow tool > [end of test] appState 1`
|
|||||||
"openMenu": null,
|
"openMenu": null,
|
||||||
"openPopup": null,
|
"openPopup": null,
|
||||||
"openSidebar": null,
|
"openSidebar": null,
|
||||||
"originSnapOffset": {
|
"originSnapOffset": null,
|
||||||
"x": 0,
|
|
||||||
"y": 0,
|
|
||||||
},
|
|
||||||
"pasteDialog": {
|
|
||||||
"data": null,
|
|
||||||
"shown": false,
|
|
||||||
},
|
|
||||||
"penDetected": false,
|
"penDetected": false,
|
||||||
"penMode": false,
|
"penMode": false,
|
||||||
"preferredSelectionTool": {
|
"preferredSelectionTool": {
|
||||||
|
|||||||
@@ -10,8 +10,6 @@ import type { LinearElementEditor } from "@excalidraw/element";
|
|||||||
|
|
||||||
import type { MaybeTransformHandleType } from "@excalidraw/element";
|
import type { MaybeTransformHandleType } from "@excalidraw/element";
|
||||||
|
|
||||||
import type { SnapLine } from "@excalidraw/element";
|
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
PointerType,
|
PointerType,
|
||||||
ExcalidrawLinearElement,
|
ExcalidrawLinearElement,
|
||||||
@@ -57,6 +55,7 @@ import type { ClipboardData } from "./clipboard";
|
|||||||
import type App from "./components/App";
|
import type App from "./components/App";
|
||||||
import type Library from "./data/library";
|
import type Library from "./data/library";
|
||||||
import type { ContextMenuItems } from "./components/ContextMenu";
|
import type { ContextMenuItems } from "./components/ContextMenu";
|
||||||
|
import type { SnapLine } from "./snapping";
|
||||||
import type { ImportedDataState } from "./data/types";
|
import type { ImportedDataState } from "./data/types";
|
||||||
|
|
||||||
import type { Language } from "./i18n";
|
import type { Language } from "./i18n";
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
export const PRECISION = 10e-5;
|
||||||
|
|
||||||
// Legendre-Gauss abscissae (x values) and weights for n=24
|
// Legendre-Gauss abscissae (x values) and weights for n=24
|
||||||
// Refeerence: https://pomax.github.io/bezierinfo/legendre-gauss.html
|
// Refeerence: https://pomax.github.io/bezierinfo/legendre-gauss.html
|
||||||
export const LegendreGaussN24TValues = [
|
export const LegendreGaussN24TValues = [
|
||||||
|
|||||||
Reference in New Issue
Block a user