Skip to content

Commit 9a8df10

Browse files
committed
support rotated elements
1 parent 7be30bf commit 9a8df10

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

packages/tdev/excalidoc/ImageMarkupEditor/helpers/createExcalidrawMarkup.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,18 @@ const handleStandaloneDrawing = (excalidrawState: ExcalidrawInitialDataState): E
6161
const standaloneDrawingIdx = excalidrawState.elements.findIndex(
6262
(e) => e.id === EXCALIDRAW_STANDALONE_DRAWING_ID
6363
);
64-
if (standaloneDrawingIdx < 0) {
65-
return excalidrawState;
64+
const standaloneElement =
65+
standaloneDrawingIdx < 0
66+
? EXCALIDRAW_STANDALONE_DRAWING_RECTANGLE
67+
: excalidrawState.elements[standaloneDrawingIdx];
68+
if (standaloneDrawingIdx >= 0) {
69+
(excalidrawState.elements as ExcalidrawElement[]).splice(standaloneDrawingIdx, 1);
6670
}
6771
const elements = withoutMetaElements(excalidrawState.elements);
6872
const { x, y, width, height } = getBoundingRect(elements);
6973

70-
(excalidrawState.elements as ExcalidrawElement[]).splice(standaloneDrawingIdx, 1, {
71-
...excalidrawState.elements[standaloneDrawingIdx],
74+
(excalidrawState.elements as ExcalidrawElement[]).splice(0, 0, {
75+
...standaloneElement,
7276
strokeWidth: EXCALIDRAW_STANDALONE_DRAWING_RECTANGLE.strokeWidth,
7377
strokeColor: EXCALIDRAW_STANDALONE_DRAWING_RECTANGLE.strokeColor,
7478
width,
@@ -87,15 +91,6 @@ export const updateRectangleDimensions = (
8791
}
8892
const backgroundImage = excalidrawState.elements.find((e) => e.id === EXCALIDRAW_BACKGROUND_IMAGE_ID);
8993
if (!backgroundImage) {
90-
const isStandaloneDrawing = excalidrawState.elements.some(
91-
(e) => e.id === EXCALIDRAW_STANDALONE_DRAWING_ID
92-
);
93-
if (!isStandaloneDrawing) {
94-
// add standalone markup drawing rectangle
95-
(excalidrawState.elements as ExcalidrawElement[]).splice(0, 0, {
96-
...EXCALIDRAW_STANDALONE_DRAWING_RECTANGLE
97-
});
98-
}
9994
return handleStandaloneDrawing(excalidrawState);
10095
}
10196
if (!backgroundImage || backgroundImage.type !== 'image') {

packages/tdev/excalidoc/ImageMarkupEditor/helpers/getBoundingRect.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,35 @@ interface Rect {
77
height: number;
88
}
99

10+
const normalizeRotation = (element: ExcalidrawElement) => {
11+
if (!element.angle || element.angle === 0) {
12+
return {
13+
x: element.x,
14+
y: element.y,
15+
width: element.width,
16+
height: element.height,
17+
strokeWidth: element.strokeWidth
18+
};
19+
}
20+
const sin = Math.sin(element.angle);
21+
const cos = Math.cos(element.angle);
22+
const width = Math.abs(element.width * cos) + Math.abs(element.height * sin);
23+
const height = Math.abs(element.width * sin) + Math.abs(element.height * cos);
24+
const x = element.x + (element.width - width) / 2;
25+
const y = element.y + (element.height - height) / 2;
26+
return { x, y, width, height, strokeWidth: element.strokeWidth };
27+
};
28+
1029
export const getBoundingRect = (elements: readonly ExcalidrawElement[]): Rect => {
1130
if (elements.length === 0) {
1231
return { x: 0, y: 0, width: 400, height: 300 };
1332
}
14-
const minX = Math.min(...elements.map((e) => e.x - e.strokeWidth / 2));
15-
const minY = Math.min(...elements.map((e) => e.y - e.strokeWidth / 2));
16-
const maxX = Math.max(...elements.map((e) => e.x + e.width + e.strokeWidth / 2));
17-
const maxY = Math.max(...elements.map((e) => e.y + e.height + e.strokeWidth / 2));
33+
const normalized = elements.map((e) => normalizeRotation(e));
34+
35+
const minX = Math.min(...normalized.map((e) => e.x - e.strokeWidth / 2));
36+
const minY = Math.min(...normalized.map((e) => e.y - e.strokeWidth / 2));
37+
const maxX = Math.max(...normalized.map((e) => e.x + e.width + e.strokeWidth / 2));
38+
const maxY = Math.max(...normalized.map((e) => e.y + e.height + e.strokeWidth / 2));
1839
return {
1940
x: minX,
2041
y: minY,

0 commit comments

Comments
 (0)