Skip to content

Commit 95293d3

Browse files
committed
Copy & Paste objects
1 parent 2a95faa commit 95293d3

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

packages/base/src/commands.ts

+68
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,71 @@ export function addCommands(
11811181
await dialog.launch();
11821182
}
11831183
});
1184+
commands.addCommand(CommandIDs.copyObject, {
1185+
label: trans.__('Copy Object'),
1186+
isEnabled: () => {
1187+
const current = tracker.currentWidget;
1188+
return current ? current.context.model.sharedModel.editable : false;
1189+
},
1190+
execute: () => {
1191+
const current = tracker.currentWidget;
1192+
if (!current) {
1193+
return;
1194+
}
1195+
1196+
const objectId = getSelectedObjectId(current);
1197+
if (!objectId) {
1198+
console.warn('No object is selected to copy.');
1199+
return;
1200+
}
1201+
1202+
const sharedModel = current.context.model.sharedModel;
1203+
const objectData = sharedModel.getObjectByName(objectId);
1204+
1205+
if (!objectData) {
1206+
console.warn('Could not retrieve object data.');
1207+
return;
1208+
}
1209+
1210+
sharedModel.awareness.setLocalStateField('clipboard', objectData);
1211+
}
1212+
});
1213+
commands.addCommand(CommandIDs.pasteObject, {
1214+
label: trans.__('Paste Object'),
1215+
isEnabled: () => {
1216+
const current = tracker.currentWidget;
1217+
const clipboard =
1218+
current?.context.model.sharedModel.awareness.getLocalState()?.clipboard;
1219+
return current && clipboard && current.context.model.sharedModel.editable;
1220+
},
1221+
execute: () => {
1222+
const current = tracker.currentWidget;
1223+
if (!current) {
1224+
return;
1225+
}
1226+
1227+
const sharedModel = current.context.model.sharedModel;
1228+
const clipboard = sharedModel.awareness.getLocalState()?.clipboard;
1229+
1230+
if (!clipboard) {
1231+
console.warn('No data in clipboard to paste.');
1232+
return;
1233+
}
1234+
1235+
const originalName = clipboard.name || 'Unnamed Object';
1236+
let newName = originalName;
1237+
1238+
let counter = 1;
1239+
while (sharedModel.objects.some(obj => obj.name === newName)) {
1240+
newName = `${originalName} Copy${counter > 1 ? ` ${counter}` : ''}`;
1241+
counter++;
1242+
}
1243+
1244+
const newObject = { ...clipboard, name: newName };
1245+
sharedModel.addObject(newObject);
1246+
}
1247+
});
1248+
11841249
loadKeybindings(commands, keybindings);
11851250
}
11861251

@@ -1208,6 +1273,9 @@ export namespace CommandIDs {
12081273
export const wireframe = 'jupytercad:wireframe';
12091274
export const transform = 'jupytercad:transform';
12101275

1276+
export const copyObject = 'jupytercad:copyObject';
1277+
export const pasteObject = 'jupytercad:pasteObject';
1278+
12111279
export const chamfer = 'jupytercad:chamfer';
12121280
export const fillet = 'jupytercad:fillet';
12131281

packages/base/src/keybindings.json

+10
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,15 @@
118118
"command": "jupytercad:exportJcad",
119119
"keys": ["Accel Alt X"],
120120
"selector": ".data-jcad-keybinding"
121+
},
122+
{
123+
"command": "jupytercad:copyObject",
124+
"keys": ["Accel C"],
125+
"selector": ".data-jcad-keybinding"
126+
},
127+
{
128+
"command": "jupytercad:pasteObject",
129+
"keys": ["Accel V"],
130+
"selector": ".data-jcad-keybinding"
121131
}
122132
]

0 commit comments

Comments
 (0)