Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 25 additions & 39 deletions package-lock.json

Large diffs are not rendered by default.

86 changes: 84 additions & 2 deletions src/components/context-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@ import { Box, Divider, Portal, useOutsideClick } from '@chakra-ui/react';
import React from 'react';
import { useTranslation } from 'react-i18next';
import useEvent from 'react-use-event-hook';
import { Id } from '../constants/constants';
import { Id, LineId, NodeId } from '../constants/constants';
import { MAX_MASTER_NODE_FREE } from '../constants/master';
import { useRootDispatch, useRootSelector } from '../redux';
import { saveGraph } from '../redux/param/param-slice';
import { clearSelected, refreshEdgesThunk, refreshNodesThunk, setSelected } from '../redux/runtime/runtime-slice';
import { exportSelectedNodesAndEdges, importSelectedNodesAndEdges } from '../util/clipboard';
import {
exportSelectedNodesAndEdges,
importSelectedNodesAndEdges,
exportNodeSpecificAttrs,
exportEdgeSpecificAttrs,
parseClipboardData,
importNodeSpecificAttrs,
importEdgeSpecificAttrs,
getSelectedElementsType,
NodeSpecificAttrsClipboardData,
EdgeSpecificAttrsClipboardData,
} from '../util/clipboard';
import { pointerPosToSVGCoord, roundToMultiple } from '../util/helpers';
import { MAX_PARALLEL_LINES_FREE } from '../util/parallel';
import { flipSelectedNodes, rotateSelectedNodes } from '../util/transform';
Expand Down Expand Up @@ -45,6 +56,12 @@ const ContextMenu: React.FC<ContextMenuProps> = ({ isOpen, position, onClose })
);
const menuRef = React.useRef<HTMLDivElement>(null);

// Check selection type for copy/paste attributes
const selectionInfo = getSelectedElementsType(graph.current, selected);
const canCopyAttrs = selected.size === 1;
const canPasteAttrs =
selectionInfo.allSameType && (selectionInfo.category === 'node' || selectionInfo.category === 'edge');

useOutsideClick({
ref: menuRef,
handler: onClose,
Expand Down Expand Up @@ -156,6 +173,52 @@ const ContextMenu: React.FC<ContextMenuProps> = ({ isOpen, position, onClose })
}
});

const handleCopyAttrs = useEvent(() => {
if (selected.size !== 1) return;
const [id] = selected;

if (graph.current.hasNode(id)) {
const s = exportNodeSpecificAttrs(graph.current, id as NodeId);
navigator.clipboard.writeText(s);
} else if (graph.current.hasEdge(id)) {
const s = exportEdgeSpecificAttrs(graph.current, id as LineId);
navigator.clipboard.writeText(s);
}
});

const handlePasteAttrs = useEvent(async () => {
try {
const s = await navigator.clipboard.readText();
const parsed = parseClipboardData(s);
if (!parsed) return;

if (parsed.type === 'node-attrs' && selectionInfo.category === 'node') {
const nodeIds = new Set<NodeId>();
selected.forEach(id => {
if (graph.current.hasNode(id)) {
nodeIds.add(id as NodeId);
}
});
if (importNodeSpecificAttrs(graph.current, nodeIds, parsed.data as NodeSpecificAttrsClipboardData)) {
refreshAndSave();
}
} else if (parsed.type === 'edge-attrs' && selectionInfo.category === 'edge') {
const edgeIds = new Set<LineId>();
selected.forEach(id => {
if (graph.current.hasEdge(id)) {
edgeIds.add(id as LineId);
}
});
if (importEdgeSpecificAttrs(graph.current, edgeIds, parsed.data as EdgeSpecificAttrsClipboardData)) {
refreshAndSave();
}
}
} catch (error) {
// Handle clipboard read error
console.warn('Failed to read clipboard:', error);
}
});

const handleRotate = useEvent((angle: number) => {
if (rotateSelectedNodes(graph.current, selected, angle)) {
refreshAndSave();
Expand Down Expand Up @@ -235,6 +298,25 @@ const ContextMenu: React.FC<ContextMenuProps> = ({ isOpen, position, onClose })
{t('contextMenu.delete')}
</MenuItem>
<Divider />
<MenuItem
onClick={() => {
handleCopyAttrs();
onClose();
}}
isDisabled={!canCopyAttrs}
>
{t('contextMenu.copyAttrs')}
</MenuItem>
<MenuItem
onClick={() => {
handlePasteAttrs();
onClose();
}}
isDisabled={!canPasteAttrs}
>
{t('contextMenu.pasteAttrs')}
</MenuItem>
<Divider />
<MenuItem
onClick={() => {
handleZIndex(10);
Expand Down
36 changes: 36 additions & 0 deletions src/components/page-header/settings-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,42 @@ const SettingsModal = (props: { isOpen: boolean; onClose: () => void }) => {
</Td>
<Td>{t('header.settings.shortcuts.paste')}</Td>
</Tr>
<Tr>
<Td>
{isMacClient ? (
<>
<Kbd sx={macKeyStyle}>&#8679;</Kbd>
{' + '}
<Kbd sx={macKeyStyle}>&#8984;</Kbd>
{' + '}
<Kbd>c</Kbd>
</>
) : (
<>
<Kbd>ctrl</Kbd> + <Kbd>shift</Kbd> + <Kbd>c</Kbd>
</>
)}
</Td>
<Td>{t('header.settings.shortcuts.copyAttrs')}</Td>
</Tr>
<Tr>
<Td>
{isMacClient ? (
<>
<Kbd sx={macKeyStyle}>&#8679;</Kbd>
{' + '}
<Kbd sx={macKeyStyle}>&#8984;</Kbd>
{' + '}
<Kbd>v</Kbd>
</>
) : (
<>
<Kbd>ctrl</Kbd> + <Kbd>shift</Kbd> + <Kbd>v</Kbd>
</>
)}
</Td>
<Td>{t('header.settings.shortcuts.pasteAttrs')}</Td>
</Tr>
<Tr>
<Td>
{isMacClient ? <Kbd sx={macKeyStyle}>&#8984;</Kbd> : <Kbd>ctrl</Kbd>}
Expand Down
118 changes: 95 additions & 23 deletions src/components/panels/details/details.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Box, Button, Heading, HStack } from '@chakra-ui/react';
import { Box, Button, Heading, HStack, VStack } from '@chakra-ui/react';
import { RmgSidePanel, RmgSidePanelBody, RmgSidePanelFooter, RmgSidePanelHeader } from '@railmapgen/rmg-components';
import { nanoid } from 'nanoid';
import React from 'react';
import { useTranslation } from 'react-i18next';
import { Id, StnId } from '../../../constants/constants';
import { Id, LineId, NodeId, StnId } from '../../../constants/constants';
import { MAX_MASTER_NODE_FREE } from '../../../constants/master';
import { MiscNodeType } from '../../../constants/nodes';
import { useRootDispatch, useRootSelector } from '../../../redux';
Expand All @@ -14,7 +14,17 @@ import {
refreshEdgesThunk,
refreshNodesThunk,
} from '../../../redux/runtime/runtime-slice';
import { exportSelectedNodesAndEdges } from '../../../util/clipboard';
import {
exportSelectedNodesAndEdges,
exportNodeSpecificAttrs,
exportEdgeSpecificAttrs,
parseClipboardData,
importNodeSpecificAttrs,
importEdgeSpecificAttrs,
getSelectedElementsType,
NodeSpecificAttrsClipboardData,
EdgeSpecificAttrsClipboardData,
} from '../../../util/clipboard';
import { isPortraitClient } from '../../../util/helpers';
import { checkAndChangeStationIntType } from '../../../util/change-types';
import InfoSection from './info-section';
Expand Down Expand Up @@ -44,6 +54,12 @@ const DetailsPanel = () => {

const isMasterDisabled = !activeSubscriptions.RMP_CLOUD && masterNodesCount + 1 > MAX_MASTER_NODE_FREE;

// Check if we can paste specific attributes
const selectionInfo = getSelectedElementsType(graph.current, selected);
const canCopyAttrs = selected.size === 1;
const canPasteAttrs =
selectionInfo.allSameType && (selectionInfo.category === 'node' || selectionInfo.category === 'edge');

const handleClose = () => {
if (!isPortraitClient()) {
dispatch(clearSelected());
Expand Down Expand Up @@ -82,6 +98,52 @@ const DetailsPanel = () => {
hardRefresh();
};

const handleCopyAttrs = () => {
if (selected.size !== 1) return;
const id = selectedFirst;

if (graph.current.hasNode(id)) {
const s = exportNodeSpecificAttrs(graph.current, id as NodeId);
navigator.clipboard.writeText(s);
} else if (graph.current.hasEdge(id)) {
const s = exportEdgeSpecificAttrs(graph.current, id as LineId);
navigator.clipboard.writeText(s);
}
};

const handlePasteAttrs = async () => {
try {
const s = await navigator.clipboard.readText();
const parsed = parseClipboardData(s);
if (!parsed) return;

if (parsed.type === 'node-attrs' && selectionInfo.category === 'node') {
const nodeIds = new Set<NodeId>();
selected.forEach(id => {
if (graph.current.hasNode(id)) {
nodeIds.add(id as NodeId);
}
});
if (importNodeSpecificAttrs(graph.current, nodeIds, parsed.data as NodeSpecificAttrsClipboardData)) {
hardRefresh();
}
} else if (parsed.type === 'edge-attrs' && selectionInfo.category === 'edge') {
const edgeIds = new Set<LineId>();
selected.forEach(id => {
if (graph.current.hasEdge(id)) {
edgeIds.add(id as LineId);
}
});
if (importEdgeSpecificAttrs(graph.current, edgeIds, parsed.data as EdgeSpecificAttrsClipboardData)) {
hardRefresh();
}
}
} catch (error) {
// Handle clipboard read error
console.warn('Failed to read clipboard:', error);
}
};

return (
<RmgSidePanel isOpen={isDetailsOpen === 'show'} width={300} header="Dummy header" alwaysOverlay>
<RmgSidePanelHeader onClose={handleClose}>{t('panel.details.header')}</RmgSidePanelHeader>
Expand All @@ -103,27 +165,37 @@ const DetailsPanel = () => {
)}
</RmgSidePanelBody>
<RmgSidePanelFooter>
<HStack>
{selected.size === 1 && graph.current.hasNode(selectedFirst) && (
<Button
size="sm"
variant="outline"
onClick={() => handleDuplicate(selectedFirst)}
isDisabled={
graph.current.getNodeAttributes(selectedFirst).type === MiscNodeType.Master &&
isMasterDisabled
}
>
{t('panel.details.footer.duplicate')}
<VStack spacing={2} align="stretch">
<HStack>
{selected.size === 1 && graph.current.hasNode(selectedFirst) && (
<Button
size="sm"
variant="outline"
onClick={() => handleDuplicate(selectedFirst)}
isDisabled={
graph.current.getNodeAttributes(selectedFirst).type === MiscNodeType.Master &&
isMasterDisabled
}
>
{t('panel.details.footer.duplicate')}
</Button>
)}
<Button size="sm" variant="outline" onClick={() => handleCopy(selected)}>
{t('panel.details.footer.copy')}
</Button>
<Button size="sm" variant="outline" onClick={() => handleRemove(selected)}>
{t('panel.details.footer.remove')}
</Button>
</HStack>
<HStack>
<Button size="sm" variant="outline" onClick={handleCopyAttrs} isDisabled={!canCopyAttrs}>
{t('panel.details.footer.copyAttrs')}
</Button>
<Button size="sm" variant="outline" onClick={handlePasteAttrs} isDisabled={!canPasteAttrs}>
{t('panel.details.footer.pasteAttrs')}
</Button>
)}
<Button size="sm" variant="outline" onClick={() => handleCopy(selected)}>
{t('panel.details.footer.copy')}
</Button>
<Button size="sm" variant="outline" onClick={() => handleRemove(selected)}>
{t('panel.details.footer.remove')}
</Button>
</HStack>
</HStack>
</VStack>
</RmgSidePanelFooter>
</RmgSidePanel>
);
Expand Down
63 changes: 61 additions & 2 deletions src/components/svg-wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React from 'react';
import { MdDoubleArrow } from 'react-icons/md';
import useEvent from 'react-use-event-hook';
import { NODES_MOVE_DISTANCE } from '../constants/canvas';
import { Events, Id, NodeId, RuntimeMode, StnId } from '../constants/constants';
import { Events, Id, NodeId, RuntimeMode, StnId, LineId } from '../constants/constants';
import { LinePathType } from '../constants/lines';
import { MAX_MASTER_NODE_FREE } from '../constants/master';
import { MiscNodeType } from '../constants/nodes';
Expand All @@ -25,7 +25,18 @@ import {
showDetailsPanel,
} from '../redux/runtime/runtime-slice';
import { checkAndChangeStationIntType } from '../util/change-types';
import { exportSelectedNodesAndEdges, importSelectedNodesAndEdges } from '../util/clipboard';
import {
exportSelectedNodesAndEdges,
importSelectedNodesAndEdges,
exportNodeSpecificAttrs,
exportEdgeSpecificAttrs,
parseClipboardData,
importNodeSpecificAttrs,
importEdgeSpecificAttrs,
getSelectedElementsType,
NodeSpecificAttrsClipboardData,
EdgeSpecificAttrsClipboardData,
} from '../util/clipboard';
import { findEdgesConnectedByNodes, findNodesInRectangle } from '../util/graph';
import {
getCanvasSize,
Expand Down Expand Up @@ -323,6 +334,54 @@ const SvgWrapper = () => {
const allElements = structuredClone(nodes) as Set<Id>;
edges.forEach(s => allElements.add(s));
dispatch(setSelected(allElements));
} else if (e.key === 'C' && (isMacClient ? e.metaKey && e.shiftKey : e.ctrlKey && e.shiftKey)) {
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The keyboard shortcut implementation is inconsistent with the existing pattern in this file. Line 386 uses lowercase 'z' with an explicit shiftKey check for the Cmd+Shift+Z shortcut. For consistency with the codebase conventions, consider using lowercase 'c' here: e.key === 'c' && (isMacClient ? e.metaKey && e.shiftKey : e.ctrlKey && e.shiftKey). Note that using uppercase 'C' is technically more correct according to the KeyboardEvent spec, but lowercase with explicit shiftKey check matches the existing pattern in this file.

Copilot uses AI. Check for mistakes.
// Copy specific attributes (Ctrl+Shift+C or Cmd+Shift+C)
if (selected.size === 1) {
const [id] = selected;
if (graph.current.hasNode(id)) {
const s = exportNodeSpecificAttrs(graph.current, id as NodeId);
navigator.clipboard.writeText(s);
} else if (graph.current.hasEdge(id)) {
const s = exportEdgeSpecificAttrs(graph.current, id as LineId);
navigator.clipboard.writeText(s);
}
}
} else if (e.key === 'V' && (isMacClient ? e.metaKey && e.shiftKey : e.ctrlKey && e.shiftKey)) {
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The keyboard shortcut implementation is inconsistent with the existing pattern in this file. Line 386 uses lowercase 'z' with an explicit shiftKey check for the Cmd+Shift+Z shortcut. For consistency with the codebase conventions, consider using lowercase 'v' here: e.key === 'v' && (isMacClient ? e.metaKey && e.shiftKey : e.ctrlKey && e.shiftKey). Note that using uppercase 'V' is technically more correct according to the KeyboardEvent spec, but lowercase with explicit shiftKey check matches the existing pattern in this file.

Copilot uses AI. Check for mistakes.
// Paste specific attributes (Ctrl+Shift+V or Cmd+Shift+V)
try {
const s = await navigator.clipboard.readText();
const parsed = parseClipboardData(s);
if (!parsed) return;

const selectionInfo = getSelectedElementsType(graph.current, selected);
if (parsed.type === 'node-attrs' && selectionInfo.category === 'node') {
const nodeIds = new Set<NodeId>();
selected.forEach(id => {
if (graph.current.hasNode(id)) {
nodeIds.add(id as NodeId);
}
});
if (
importNodeSpecificAttrs(graph.current, nodeIds, parsed.data as NodeSpecificAttrsClipboardData)
) {
refreshAndSave();
}
} else if (parsed.type === 'edge-attrs' && selectionInfo.category === 'edge') {
const edgeIds = new Set<LineId>();
selected.forEach(id => {
if (graph.current.hasEdge(id)) {
edgeIds.add(id as LineId);
}
});
if (
importEdgeSpecificAttrs(graph.current, edgeIds, parsed.data as EdgeSpecificAttrsClipboardData)
) {
refreshAndSave();
}
}
} catch (error) {
console.warn('Failed to read clipboard:', error);
}
} else if (
(isMacClient && e.key === 'z' && e.metaKey && e.shiftKey) ||
(!isMacClient && e.key === 'y' && e.ctrlKey)
Expand Down
Loading
Loading