select single element on cmd-click (#2087)

This commit is contained in:
David Luzar 2020-08-27 20:59:46 +02:00 committed by GitHub
parent b8f8bc2e32
commit 4c2d34ffd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 840 additions and 17 deletions

View file

@ -50,6 +50,20 @@ const group = (elements: ExcalidrawElement[]) => {
});
};
const assertSelectedElements = (...elements: ExcalidrawElement[]) => {
expect(
getSelectedElements().map((element) => {
return element.id;
}),
).toEqual(expect.arrayContaining(elements.map((element) => element.id)));
};
const clearSelection = () => {
// @ts-ignore
h.app.clearSelection(null);
expect(getSelectedElements().length).toBe(0);
};
let altKey = false;
let shiftKey = false;
let ctrlKey = false;
@ -191,8 +205,7 @@ class Pointer {
/** if multiple elements supplied, they're shift-selected */
elements: ExcalidrawElement | ExcalidrawElement[],
) {
// @ts-ignore
h.app.clearSelection(null);
clearSelection();
withModifierKeys({ shift: true }, () => {
elements = Array.isArray(elements) ? elements : [elements];
elements.forEach((element) => {
@ -1649,6 +1662,40 @@ describe("regression tests", () => {
expect(h.state.selectedGroupIds).toEqual(selectedGroupIds_prev);
expect(getSelectedElements()).toEqual(selectedElements_prev);
});
it("Cmd/Ctrl-click exclusively select element under pointer", () => {
const rect1 = createElement("rectangle", { x: 0 });
const rect2 = createElement("rectangle", { x: 30 });
group([rect1, rect2]);
assertSelectedElements(rect1, rect2);
withModifierKeys({ ctrl: true }, () => {
mouse.clickOn(rect1);
});
assertSelectedElements(rect1);
clearSelection();
withModifierKeys({ ctrl: true }, () => {
mouse.clickOn(rect1);
});
assertSelectedElements(rect1);
const rect3 = createElement("rectangle", { x: 60 });
group([rect1, rect3]);
assertSelectedElements(rect1, rect2, rect3);
withModifierKeys({ ctrl: true }, () => {
mouse.clickOn(rect1);
});
assertSelectedElements(rect1);
clearSelection();
withModifierKeys({ ctrl: true }, () => {
mouse.clickOn(rect3);
});
assertSelectedElements(rect3);
});
});
it(