Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy & Paste objects #624

Merged
merged 6 commits into from
Dec 3, 2024
Merged
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
65 changes: 65 additions & 0 deletions packages/base/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,68 @@ export function addCommands(
await dialog.launch();
}
});
commands.addCommand(CommandIDs.copyObject, {
label: trans.__('Copy Object'),
isEnabled: () => {
const current = tracker.currentWidget;
return current ? current.context.model.sharedModel.editable : false;
},
execute: () => {
const current = tracker.currentWidget;
if (!current) {
return;
}

const objectId = getSelectedObjectId(current);
const sharedModel = current.context.model.sharedModel;
const objectData = sharedModel.getObjectByName(objectId);

if (!objectData) {
console.warn('Could not retrieve object data.');
return;
}

current.context.model.setCopiedObject(objectData);
}
});
commands.addCommand(CommandIDs.pasteObject, {
label: trans.__('Paste Object'),
isEnabled: () => {
const current = tracker.currentWidget;
const clipboard = current?.context.model.getCopiedObject();
const editable = current?.context.model.sharedModel.editable;
return !!(current && clipboard && editable);
},
execute: () => {
const current = tracker.currentWidget;
if (!current) {
return;
}

const sharedModel = current.context.model.sharedModel;
const copiedObject = current.context.model.getCopiedObject();
if (!copiedObject) {
console.error('No object in clipboard to paste.');
return;
}

const clipboard = copiedObject;

const originalName = clipboard.name || 'Unnamed Object';
let newName = originalName;

let counter = 1;
while (sharedModel.objects.some(obj => obj.name === newName)) {
newName = `${originalName} Copy${counter > 1 ? ` ${counter}` : ''}`;
counter++;
}
const jcadModel = current.context.model;
const newObject = { ...clipboard, name: newName };
sharedModel.addObject(newObject);
martinRenou marked this conversation as resolved.
Show resolved Hide resolved
jcadModel.syncSelected({ [newObject.name]: { type: 'shape' } }, uuid());
}
});

loadKeybindings(commands, keybindings);
}

Expand Down Expand Up @@ -1208,6 +1270,9 @@ export namespace CommandIDs {
export const wireframe = 'jupytercad:wireframe';
export const transform = 'jupytercad:transform';

export const copyObject = 'jupytercad:copyObject';
export const pasteObject = 'jupytercad:pasteObject';

export const chamfer = 'jupytercad:chamfer';
export const fillet = 'jupytercad:fillet';

Expand Down
10 changes: 10 additions & 0 deletions packages/base/src/keybindings.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,15 @@
"command": "jupytercad:exportJcad",
"keys": ["Accel Alt X"],
"selector": ".data-jcad-keybinding"
},
{
"command": "jupytercad:copyObject",
"keys": ["Accel C"],
"selector": ".data-jcad-keybinding"
},
{
"command": "jupytercad:pasteObject",
"keys": ["Accel V"],
"selector": ".data-jcad-keybinding"
}
]
3 changes: 3 additions & 0 deletions packages/schema/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ export interface IJupyterCadModel extends DocumentRegistry.IModel {
addMetadata(key: string, value: string): void;
removeMetadata(key: string): void;

getCopiedObject(): IJCadObject | null;
setCopiedObject(objectData: IJCadObject): void;

disposed: ISignal<any, void>;
}

Expand Down
12 changes: 11 additions & 1 deletion packages/schema/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { PartialJSONObject } from '@lumino/coreutils';
import { ISignal, Signal } from '@lumino/signaling';
import Ajv from 'ajv';

import { IJCadContent, IJCadModel } from './_interface/jcad';
import { IJCadContent, IJCadModel, IJCadObject } from './_interface/jcad';
import { JupyterCadDoc } from './doc';
import {
Camera,
Expand All @@ -30,6 +30,7 @@ export class JupyterCadModel implements IJupyterCadModel {
}
this._connectSignal();
this.annotationModel = annotationModel;
this._copiedObject = null;
}

readonly collaborative =
Expand Down Expand Up @@ -238,6 +239,14 @@ export class JupyterCadModel implements IJupyterCadModel {
this.sharedModel.removeMetadata(key);
}

setCopiedObject(object: IJCadObject | null): void {
this._copiedObject = object ? { ...object } : null;
}

getCopiedObject(): IJCadObject | null {
return this._copiedObject ? { ...this._copiedObject } : null;
}

protected createSharedModel(): IJupyterCadDoc {
return JupyterCadDoc.create();
}
Expand Down Expand Up @@ -304,6 +313,7 @@ export class JupyterCadModel implements IJupyterCadModel {
readonly annotationModel?: IAnnotationModel;

private _sharedModel: IJupyterCadDoc;
private _copiedObject: IJCadObject | null;

private _dirty = false;
private _readOnly = false;
Expand Down
Loading