Skip to content

Commit

Permalink
[3740] Add keyboard shortcut for object duplication
Browse files Browse the repository at this point in the history
Bug: #3740
Signed-off-by: Laurent Fasani <[email protected]>
Signed-off-by: Florian ROUËNÉ <[email protected]>
  • Loading branch information
frouene committed Jan 3, 2025
1 parent 3b6b2a9 commit 152fd2a
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ const useDuplicateObjectModalStyles = makeStyles()((theme) => ({

export const DuplicateObjectModal = ({
editingContextId,
item,
objectToDuplicateId,
objectToDuplicateKind,
onObjectDuplicated,
onClose,
}: DuplicateObjectModalProps) => {
Expand All @@ -79,11 +80,11 @@ export const DuplicateObjectModal = ({
variables: {
editingContextId,
containerId: state.containerSelection.entries[0].id,
containedObjectId: item.id,
containedObjectId: objectToDuplicateId,
},
});
}
}, [state.containerSelection, item]);
}, [state.containerSelection, objectToDuplicateId]);

useEffect(() => {
if (containmentFeatureNames) {
Expand All @@ -99,7 +100,7 @@ export const DuplicateObjectModal = ({
const onDuplicate = () => {
duplicateObject(
editingContextId,
item.id,
objectToDuplicateId,
state.containerSelection.entries[0]?.id,
state.containmentFeatureName,
state.duplicateContent,
Expand Down Expand Up @@ -141,8 +142,8 @@ export const DuplicateObjectModal = ({
};

const treeId: string = `modelBrowser://container?ownerKind=${encodeURIComponent(
item.kind
)}&targetType=${encodeURIComponent(item.kind)}&isContainment=true`;
objectToDuplicateKind
)}&targetType=${encodeURIComponent(objectToDuplicateKind)}&isContainment=true`;
const { tree } = useDuplicateDialogTreeSubscription(editingContextId, treeId, state.expanded, state.maxDepth);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import { Selection } from '@eclipse-sirius/sirius-components-core';

export interface DuplicateObjectModalProps {
editingContextId: string;
item: any;
objectToDuplicateId: string;
objectToDuplicateKind: string;
onObjectDuplicated: (object: Selection) => void;
onClose: () => void;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2024 Obeo.
* Copyright (c) 2019, 2025 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -45,6 +45,7 @@ import { NewDocumentModalContribution } from './TreeToolBarContributions/NewDocu
import { UploadDocumentModalContribution } from './TreeToolBarContributions/UploadDocumentModalContribution';
import { UndoRedo } from './UndoRedo';
import { useProjectAndRepresentationMetadata } from './useProjectAndRepresentationMetadata';
import { KeyboardShortcut } from './KeyboardShortcut';

const useEditProjectViewStyles = makeStyles()((_) => ({
editProjectView: {
Expand Down Expand Up @@ -129,15 +130,17 @@ export const EditProjectView = () => {
<SelectionContextProvider initialSelection={initialSelection}>
<OmniboxProvider initialContextEntries={initialContextEntries}>
<UndoRedo>
<EditProjectNavbar readOnly={readOnly} />
<TreeToolBarProvider>
<Workbench
editingContextId={context.project.currentEditingContext.id}
initialRepresentationSelected={context.representation}
onRepresentationSelected={onRepresentationSelected}
readOnly={readOnly}
/>
</TreeToolBarProvider>
<KeyboardShortcut editingContextId={context.project.currentEditingContext.id} readOnly={readOnly}>
<EditProjectNavbar readOnly={readOnly} />
<TreeToolBarProvider>
<Workbench
editingContextId={context.project.currentEditingContext.id}
initialRepresentationSelected={context.representation}
onRepresentationSelected={onRepresentationSelected}
readOnly={readOnly}
/>
</TreeToolBarProvider>
</KeyboardShortcut>
</UndoRedo>
</OmniboxProvider>
</SelectionContextProvider>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*******************************************************************************
* Copyright (c) 2025 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
import { useEffect, useState } from 'react';
import { Selection, useSelection } from '@eclipse-sirius/sirius-components-core';
import { DuplicateObjectModal } from '../../modals/duplicate-object/DuplicateObjectModal';
import { KeyboardShortcutProps } from './KeyboardShortcut.types';

export const KeyboardShortcut = ({ editingContextId, readOnly, children }: KeyboardShortcutProps) => {
const [open, setOpen] = useState<boolean>(false);
const { selection, setSelection } = useSelection();

const duplicateObjectModalOpen = () => {
if (selection && selection.entries.length === 1) {
const selectionEntryItem = selection.entries[0];
if (selectionEntryItem && selectionEntryItem.kind.startsWith('siriusComponents://semantic')) {
setOpen(true);
}
}
};

const duplicateObjectKeyPressHandler = (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === 'd') {
e.preventDefault();
duplicateObjectModalOpen();
}
};

useEffect(() => {
if (!readOnly) {
window.addEventListener('keydown', duplicateObjectKeyPressHandler);
}
return () => window.removeEventListener('keydown', duplicateObjectKeyPressHandler);
}, [selection, readOnly]);

const onObjectCreated = (selection: Selection) => {
setSelection(selection);
setOpen(false);
};

const onClose = () => {
setOpen(false);
};

return (
<>
{children}
{open ? (
<DuplicateObjectModal
editingContextId={editingContextId}
objectToDuplicateId={selection.entries[0].id}
objectToDuplicateKind={selection.entries[0].kind}
onObjectDuplicated={onObjectCreated}
onClose={onClose}
/>
) : null}
</>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*******************************************************************************
* Copyright (c) 2025 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/

export type KeyboardShortcutProps = {
editingContextId: string;
readOnly: boolean;
children: React.ReactNode;
};
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export const ObjectTreeItemContextMenuContribution = forwardRef(
modalElement = (
<DuplicateObjectModal
editingContextId={editingContextId}
item={item}
objectToDuplicateId={item.id}
objectToDuplicateKind={item.kind}
onObjectDuplicated={onObjectCreated}
onClose={onClose}
/>
Expand Down

0 comments on commit 152fd2a

Please sign in to comment.