Shift drag to add to selection (#350)

This commit is contained in:
Timur Khazamov 2020-01-13 00:56:10 +05:00 committed by Christopher Chedeau
parent 58ad6d741d
commit ce467f7b65
3 changed files with 23 additions and 12 deletions

View File

@ -15,7 +15,7 @@ import {
import { import {
clearSelection, clearSelection,
deleteSelectedElements, deleteSelectedElements,
setSelection, getElementsWithinSelection,
isOverScrollBars, isOverScrollBars,
restoreFromLocalStorage, restoreFromLocalStorage,
saveToLocalStorage, saveToLocalStorage,
@ -746,13 +746,24 @@ export class App extends React.Component<{}, AppState> {
this.state.scrollY; this.state.scrollY;
draggingElement.width = width; draggingElement.width = width;
// Make a perfect square or circle when shift is enabled // Make a perfect square or circle when shift is enabled
draggingElement.height = e.shiftKey draggingElement.height =
// Shift key on selection must add items to selection
e.shiftKey && this.state.elementType !== "selection"
? Math.abs(width) * Math.sign(height) ? Math.abs(width) * Math.sign(height)
: height; : height;
draggingElement.shape = null; draggingElement.shape = null;
if (this.state.elementType === "selection") { if (this.state.elementType === "selection") {
elements = setSelection(elements, draggingElement); const elementsWithinSelection = getElementsWithinSelection(
elements,
draggingElement
);
if (!e.shiftKey) {
elements = clearSelection(elements);
}
elementsWithinSelection.forEach(
element => (element.isSelected = true)
);
} }
// We don't want to save history when moving an element // We don't want to save history when moving an element
history.skipRecording(); history.skipRecording();

View File

@ -4,7 +4,7 @@ export {
getSelectedIndices, getSelectedIndices,
deleteSelectedElements, deleteSelectedElements,
someElementIsSelected, someElementIsSelected,
setSelection, getElementsWithinSelection,
getSelectedAttribute getSelectedAttribute
} from "./selection"; } from "./selection";
export { export {

View File

@ -1,7 +1,7 @@
import { ExcalidrawElement } from "../element/types"; import { ExcalidrawElement } from "../element/types";
import { getElementAbsoluteCoords } from "../element"; import { getElementAbsoluteCoords } from "../element";
export function setSelection( export function getElementsWithinSelection(
elements: readonly ExcalidrawElement[], elements: readonly ExcalidrawElement[],
selection: ExcalidrawElement selection: ExcalidrawElement
) { ) {
@ -11,22 +11,22 @@ export function setSelection(
selectionX2, selectionX2,
selectionY2 selectionY2
] = getElementAbsoluteCoords(selection); ] = getElementAbsoluteCoords(selection);
elements.forEach(element => {
return elements.filter(element => {
const [ const [
elementX1, elementX1,
elementY1, elementY1,
elementX2, elementX2,
elementY2 elementY2
] = getElementAbsoluteCoords(element); ] = getElementAbsoluteCoords(element);
element.isSelected = return (
element.type !== "selection" && element.type !== "selection" &&
selectionX1 <= elementX1 && selectionX1 <= elementX1 &&
selectionY1 <= elementY1 && selectionY1 <= elementY1 &&
selectionX2 >= elementX2 && selectionX2 >= elementX2 &&
selectionY2 >= elementY2; selectionY2 >= elementY2
);
}); });
return elements;
} }
export function clearSelection(elements: readonly ExcalidrawElement[]) { export function clearSelection(elements: readonly ExcalidrawElement[]) {