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-07-07 13:53:44 +02:00
|
|
|
import { LinearElementEditor } from "../element/linearElementEditor";
|
|
|
|
import { mutateElement } from "../element/mutateElement";
|
2020-09-11 17:06:07 +02:00
|
|
|
import {
|
|
|
|
selectGroupsForSelectedElements,
|
|
|
|
getSelectedGroupForElement,
|
|
|
|
getElementsInGroup,
|
|
|
|
} from "../groups";
|
2020-08-08 21:04:15 -07:00
|
|
|
import { AppState } from "../types";
|
|
|
|
import { fixBindingsAfterDuplication } from "../element/binding";
|
|
|
|
import { ActionResult } from "./types";
|
2020-03-16 22:53:02 +01:00
|
|
|
|
|
|
|
export const actionDuplicateSelection = register({
|
|
|
|
name: "duplicateSelection",
|
|
|
|
perform: (elements, appState) => {
|
2020-07-07 13:53:44 +02:00
|
|
|
// duplicate point if selected while editing multi-point element
|
|
|
|
if (appState.editingLinearElement) {
|
|
|
|
const { activePointIndex, elementId } = appState.editingLinearElement;
|
|
|
|
const element = LinearElementEditor.getElement(elementId);
|
|
|
|
if (!element || activePointIndex === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const { points } = element;
|
|
|
|
const selectedPoint = points[activePointIndex];
|
|
|
|
const nextPoint = points[activePointIndex + 1];
|
|
|
|
mutateElement(element, {
|
|
|
|
points: [
|
|
|
|
...points.slice(0, activePointIndex + 1),
|
|
|
|
nextPoint
|
|
|
|
? [
|
|
|
|
(selectedPoint[0] + nextPoint[0]) / 2,
|
|
|
|
(selectedPoint[1] + nextPoint[1]) / 2,
|
|
|
|
]
|
|
|
|
: [selectedPoint[0] + 30, selectedPoint[1] + 30],
|
|
|
|
...points.slice(activePointIndex + 1),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
appState: {
|
|
|
|
...appState,
|
|
|
|
editingLinearElement: {
|
|
|
|
...appState.editingLinearElement,
|
|
|
|
activePointIndex: activePointIndex + 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
elements,
|
|
|
|
commitToHistory: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-16 22:53:02 +01:00
|
|
|
return {
|
2020-08-08 21:04:15 -07:00
|
|
|
...duplicateElements(elements, appState),
|
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
|
|
|
});
|
2020-08-08 21:04:15 -07:00
|
|
|
|
|
|
|
const duplicateElements = (
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState,
|
|
|
|
): Partial<ActionResult> => {
|
|
|
|
const groupIdMap = new Map();
|
|
|
|
const newElements: ExcalidrawElement[] = [];
|
|
|
|
const oldElements: ExcalidrawElement[] = [];
|
|
|
|
const oldIdToDuplicatedId = new Map();
|
2020-09-11 17:06:07 +02:00
|
|
|
|
|
|
|
const duplicateAndOffsetElement = (element: ExcalidrawElement) => {
|
|
|
|
const newElement = duplicateElement(
|
|
|
|
appState.editingGroupId,
|
|
|
|
groupIdMap,
|
|
|
|
element,
|
|
|
|
{
|
|
|
|
x: element.x + 10,
|
|
|
|
y: element.y + 10,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
oldIdToDuplicatedId.set(element.id, newElement.id);
|
|
|
|
oldElements.push(element);
|
|
|
|
newElements.push(newElement);
|
|
|
|
return newElement;
|
|
|
|
};
|
|
|
|
|
|
|
|
const finalElements: ExcalidrawElement[] = [];
|
|
|
|
|
|
|
|
let i = 0;
|
|
|
|
while (i < elements.length) {
|
|
|
|
const element = elements[i];
|
|
|
|
if (appState.selectedElementIds[element.id]) {
|
|
|
|
if (element.groupIds.length) {
|
|
|
|
const groupId = getSelectedGroupForElement(appState, element);
|
|
|
|
// if group selected, duplicate it atomically
|
|
|
|
if (groupId) {
|
|
|
|
const groupElements = getElementsInGroup(elements, groupId);
|
|
|
|
finalElements.push(
|
|
|
|
...groupElements,
|
|
|
|
...groupElements.map((element) =>
|
|
|
|
duplicateAndOffsetElement(element),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
i = i + groupElements.length;
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-08 21:04:15 -07:00
|
|
|
}
|
2020-09-11 17:06:07 +02:00
|
|
|
finalElements.push(element, duplicateAndOffsetElement(element));
|
|
|
|
} else {
|
|
|
|
finalElements.push(element);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2020-08-08 21:04:15 -07:00
|
|
|
fixBindingsAfterDuplication(finalElements, oldElements, oldIdToDuplicatedId);
|
2020-09-11 17:06:07 +02:00
|
|
|
|
2020-08-08 21:04:15 -07:00
|
|
|
return {
|
|
|
|
elements: finalElements,
|
|
|
|
appState: selectGroupsForSelectedElements(
|
|
|
|
{
|
|
|
|
...appState,
|
|
|
|
selectedGroupIds: {},
|
|
|
|
selectedElementIds: newElements.reduce((acc, element) => {
|
|
|
|
acc[element.id] = true;
|
|
|
|
return acc;
|
|
|
|
}, {} as any),
|
|
|
|
},
|
|
|
|
getNonDeletedElements(finalElements),
|
|
|
|
),
|
|
|
|
};
|
|
|
|
};
|