import React from "react"; import { KEYS } from "../keys"; import { register } from "./register"; import { ExcalidrawElement } from "../element/types"; import { duplicateElement, getNonDeletedElements } from "../element"; import { isSomeElementSelected } from "../scene"; import { ToolButton } from "../components/ToolButton"; import { clone } from "../components/icons"; import { t } from "../i18n"; import { getShortcutKey } from "../utils"; export const actionDuplicateSelection = register({ name: "duplicateSelection", perform: (elements, appState) => { const groupIdMap = new Map(); return { appState, elements: elements.reduce( (acc: Array, element: ExcalidrawElement) => { if (appState.selectedElementIds[element.id]) { const newElement = duplicateElement( appState.editingGroupId, groupIdMap, element, { x: element.x + 10, y: element.y + 10, }, ); appState.selectedElementIds[newElement.id] = true; delete appState.selectedElementIds[element.id]; return acc.concat([element, newElement]); } return acc.concat(element); }, [], ), commitToHistory: true, }; }, contextItemLabel: "labels.duplicateSelection", keyTest: (event) => event[KEYS.CTRL_OR_CMD] && event.key === "d", PanelComponent: ({ elements, appState, updateData }) => ( updateData(null)} visible={isSomeElementSelected(getNonDeletedElements(elements), appState)} /> ), });