Skip to content

Commit 58de594

Browse files
committed
simplify the code, add meta to ctrl for macos
1 parent a9bc503 commit 58de594

File tree

2 files changed

+41
-65
lines changed

2 files changed

+41
-65
lines changed

src/components/EditorCanvas/Canvas.jsx

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,9 @@ import {
2727
} from "../../hooks";
2828
import { useTranslation } from "react-i18next";
2929
import { useEventListener } from "usehooks-ts";
30-
import { areFieldsCompatible } from "../../utils/utils";
31-
import {
32-
getRectFromEndpoints,
33-
isInsideRect,
34-
areaRect,
35-
noteRect,
36-
tableRect,
37-
pointIsInsideRect,
38-
} from "../../utils/rect";
39-
import { State } from "../../data/constants";
30+
import { areFieldsCompatible, getTableHeight } from "../../utils/utils";
31+
import { getRectFromEndpoints, isInsideRect } from "../../utils/rect";
32+
import { State, noteWidth } from "../../data/constants";
4033

4134
export default function Canvas() {
4235
const { t } = useTranslation();
@@ -96,13 +89,14 @@ export default function Canvas() {
9689
width: 0,
9790
height: 0,
9891
});
99-
const [bulkSelectRectPts, setBulkSelectRectPts] = useState({
92+
const [bulkSelectRect, setBulkSelectRect] = useState({
10093
x1: 0,
10194
y1: 0,
10295
x2: 0,
10396
y2: 0,
10497
show: false,
10598
ctrlKey: false,
99+
metaKey: false,
106100
});
107101
// this is used to store the element that is clicked on
108102
// at the moment, and shouldn't be a part of the state
@@ -113,15 +107,15 @@ export default function Canvas() {
113107
};
114108

115109
const collectSelectedElements = () => {
116-
const rect = getRectFromEndpoints(bulkSelectRectPts);
110+
const rect = getRectFromEndpoints(bulkSelectRect);
117111
const elements = [];
118112
const shouldAddElement = (elementRect, element) => {
119113
// if ctrl key is pressed, only add the elements that are not already selected
120114
// can theoretically be optimized later if the selected elements is
121115
// a map from id to element (after the ids are made unique)
122116
return (
123117
isInsideRect(elementRect, rect) &&
124-
(!bulkSelectRectPts.ctrlKey ||
118+
((!bulkSelectRect.ctrlKey && !bulkSelectRect.metaKey) ||
125119
!bulkSelectedElements.some((el) => isSameElement(el, element)))
126120
);
127121
};
@@ -135,7 +129,13 @@ export default function Canvas() {
135129
currentCoords: { x: table.x, y: table.y },
136130
initialCoords: { x: table.x, y: table.y },
137131
};
138-
if (shouldAddElement(tableRect(table, settings), element)) {
132+
const tableRect = {
133+
x: table.x,
134+
y: table.y,
135+
width: settings.tableWidth,
136+
height: getTableHeight(table),
137+
};
138+
if (shouldAddElement(tableRect, element)) {
139139
elements.push(element);
140140
}
141141
});
@@ -149,7 +149,13 @@ export default function Canvas() {
149149
currentCoords: { x: area.x, y: area.y },
150150
initialCoords: { x: area.x, y: area.y },
151151
};
152-
if (shouldAddElement(areaRect(area), element)) {
152+
const areaRect = {
153+
x: area.x,
154+
y: area.y,
155+
width: area.width,
156+
height: area.height,
157+
};
158+
if (shouldAddElement(areaRect, element)) {
153159
elements.push(element);
154160
}
155161
});
@@ -163,12 +169,18 @@ export default function Canvas() {
163169
currentCoords: { x: note.x, y: note.y },
164170
initialCoords: { x: note.x, y: note.y },
165171
};
166-
if (shouldAddElement(noteRect(note), element)) {
172+
const noteRect = {
173+
x: note.x,
174+
y: note.y,
175+
width: noteWidth,
176+
height: note.height,
177+
};
178+
if (shouldAddElement(noteRect, element)) {
167179
elements.push(element);
168180
}
169181
});
170182

171-
if (bulkSelectRectPts.ctrlKey) {
183+
if (bulkSelectRect.ctrlKey || bulkSelectRect.metaKey) {
172184
setBulkSelectedElements([...bulkSelectedElements, ...elements]);
173185
} else {
174186
setBulkSelectedElements(elements);
@@ -180,7 +192,7 @@ export default function Canvas() {
180192

181193
if (!e.isPrimary) return;
182194

183-
if (!element.locked || !e.ctrlKey) {
195+
if (!element.locked || !(e.ctrlKey || e.metaKey)) {
184196
setSelectedElement((prev) => ({
185197
...prev,
186198
element: type,
@@ -190,13 +202,13 @@ export default function Canvas() {
190202
}
191203

192204
if (element.locked) {
193-
if (!e.ctrlKey) {
205+
if (!(e.ctrlKey || e.metaKey)) {
194206
setBulkSelectedElements([]);
195207
}
196208
return;
197209
}
198210

199-
setBulkSelectRectPts((prev) => ({
211+
setBulkSelectRect((prev) => ({
200212
...prev,
201213
show: false,
202214
}));
@@ -214,7 +226,7 @@ export default function Canvas() {
214226
isSameElement(el, elementInBulk),
215227
);
216228

217-
if (e.ctrlKey) {
229+
if (e.ctrlKey || e.metaKey) {
218230
if (isSelected) {
219231
if (bulkSelectedElements.length > 1) {
220232
setBulkSelectedElements(
@@ -365,8 +377,8 @@ export default function Canvas() {
365377
return;
366378
}
367379

368-
if (bulkSelectRectPts.show) {
369-
setBulkSelectRectPts((prev) => ({
380+
if (bulkSelectRect.show) {
381+
setBulkSelectRect((prev) => ({
370382
...prev,
371383
x2: pointer.spaces.diagram.x,
372384
y2: pointer.spaces.diagram.y,
@@ -392,13 +404,14 @@ export default function Canvas() {
392404
const isMouseMiddleButton = e.button === 1;
393405

394406
if (isMouseLeftButton) {
395-
setBulkSelectRectPts({
407+
setBulkSelectRect({
396408
x1: pointer.spaces.diagram.x,
397409
y1: pointer.spaces.diagram.y,
398410
x2: pointer.spaces.diagram.x,
399411
y2: pointer.spaces.diagram.y,
400412
show: elementPointerDown === null || !elementPointerDown.element.locked,
401413
ctrlKey: e.ctrlKey,
414+
metaKey: e.metaKey,
402415
});
403416
if (elementPointerDown !== null) {
404417
handlePointerDownOnElement(e, elementPointerDown);
@@ -476,8 +489,8 @@ export default function Canvas() {
476489
);
477490
}
478491

479-
if (bulkSelectRectPts.show) {
480-
setBulkSelectRectPts((prev) => ({
492+
if (bulkSelectRect.show) {
493+
setBulkSelectRect((prev) => ({
481494
...prev,
482495
x2: pointer.spaces.diagram.x,
483496
y2: pointer.spaces.diagram.y,
@@ -726,9 +739,9 @@ export default function Canvas() {
726739
}}
727740
/>
728741
))}
729-
{bulkSelectRectPts.show && (
742+
{bulkSelectRect.show && (
730743
<rect
731-
{...getRectFromEndpoints(bulkSelectRectPts)}
744+
{...getRectFromEndpoints(bulkSelectRect)}
732745
stroke="grey"
733746
fill="grey"
734747
fillOpacity={0.15}

src/utils/rect.js

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { getTableHeight } from "./utils";
2-
import { noteWidth } from "../data/constants";
3-
41
export function getRectFromEndpoints({ x1, x2, y1, y2 }) {
52
const width = Math.abs(x1 - x2);
63
const height = Math.abs(y1 - y2);
@@ -11,13 +8,6 @@ export function getRectFromEndpoints({ x1, x2, y1, y2 }) {
118
return { x, y, width, height };
129
}
1310

14-
export function pointIsInsideRect(point, rect) {
15-
return (
16-
point.x > rect.x && point.x < rect.x + rect.width &&
17-
point.y > rect.y && point.y < rect.y + rect.height
18-
);
19-
}
20-
2111
export function isInsideRect(rect1, rect2) {
2212
return (
2313
rect1.x > rect2.x &&
@@ -26,30 +16,3 @@ export function isInsideRect(rect1, rect2) {
2616
rect1.y + rect1.height < rect2.y + rect2.height
2717
);
2818
}
29-
30-
export function areaRect(area) {
31-
return {
32-
x: area.x,
33-
y: area.y,
34-
width: area.width,
35-
height: area.height,
36-
};
37-
}
38-
39-
export function noteRect(note) {
40-
return {
41-
x: note.x,
42-
y: note.y,
43-
width: noteWidth,
44-
height: note.height,
45-
};
46-
}
47-
48-
export function tableRect(table, settings) {
49-
return {
50-
x: table.x,
51-
y: table.y,
52-
width: settings.tableWidth,
53-
height: getTableHeight(table),
54-
};
55-
}

0 commit comments

Comments
 (0)