2020-04-02 00:13:53 +09:00
|
|
|
import React from "react";
|
2020-03-16 22:53:02 +01:00
|
|
|
import { KEYS } from "../keys";
|
|
|
|
import { register } from "./register";
|
|
|
|
import { ExcalidrawElement } from "../element/types";
|
2020-04-08 09:49:52 -07:00
|
|
|
import { duplicateElement, getNonDeletedElements } from "../element";
|
2020-04-02 00:13:53 +09:00
|
|
|
import { isSomeElementSelected } from "../scene";
|
|
|
|
import { ToolButton } from "../components/ToolButton";
|
|
|
|
import { clone } from "../components/icons";
|
|
|
|
import { t } from "../i18n";
|
|
|
|
import { getShortcutKey } from "../utils";
|
2020-03-16 22:53:02 +01:00
|
|
|
|
|
|
|
export const actionDuplicateSelection = register({
|
|
|
|
name: "duplicateSelection",
|
|
|
|
perform: (elements, appState) => {
|
2020-05-26 13:07:46 -07:00
|
|
|
const groupIdMap = new Map();
|
2020-03-16 22:53:02 +01:00
|
|
|
return {
|
|
|
|
appState,
|
|
|
|
elements: elements.reduce(
|
|
|
|
(acc: Array<ExcalidrawElement>, element: ExcalidrawElement) => {
|
|
|
|
if (appState.selectedElementIds[element.id]) {
|
2020-05-26 13:07:46 -07:00
|
|
|
const newElement = duplicateElement(
|
|
|
|
appState.editingGroupId,
|
|
|
|
groupIdMap,
|
|
|
|
element,
|
|
|
|
{
|
|
|
|
x: element.x + 10,
|
|
|
|
y: element.y + 10,
|
|
|
|
},
|
|
|
|
);
|
2020-03-16 22:53:02 +01:00
|
|
|
appState.selectedElementIds[newElement.id] = true;
|
|
|
|
delete appState.selectedElementIds[element.id];
|
|
|
|
return acc.concat([element, newElement]);
|
|
|
|
}
|
|
|
|
return acc.concat(element);
|
|
|
|
},
|
|
|
|
[],
|
|
|
|
),
|
2020-03-19 14:51:05 +01:00
|
|
|
commitToHistory: true,
|
2020-03-16 22:53:02 +01:00
|
|
|
};
|
|
|
|
},
|
|
|
|
contextItemLabel: "labels.duplicateSelection",
|
2020-03-23 13:05:07 +02:00
|
|
|
keyTest: (event) => event[KEYS.CTRL_OR_CMD] && event.key === "d",
|
2020-04-02 00:13:53 +09:00
|
|
|
PanelComponent: ({ elements, appState, updateData }) => (
|
|
|
|
<ToolButton
|
|
|
|
type="button"
|
|
|
|
icon={clone}
|
2020-04-07 14:39:06 +03:00
|
|
|
title={`${t("labels.duplicateSelection")} — ${getShortcutKey(
|
2020-04-02 00:13:53 +09:00
|
|
|
"CtrlOrCmd+D",
|
|
|
|
)}`}
|
|
|
|
aria-label={t("labels.duplicateSelection")}
|
|
|
|
onClick={() => updateData(null)}
|
2020-04-08 09:49:52 -07:00
|
|
|
visible={isSomeElementSelected(getNonDeletedElements(elements), appState)}
|
2020-04-02 00:13:53 +09:00
|
|
|
/>
|
|
|
|
),
|
2020-03-16 22:53:02 +01:00
|
|
|
});
|