Support multiple duplicate with `cmd/ctrl + D`
We would like have an improvement is that in most whiteboard software will support using cmd/ctrl + D for continuous copying after using "alt + drag" to copy the first shape
Please refer this demo:
https://github.com/toeverything/blocksuite/assets/40333372/6eef028c-bf42-4f9e-831f-a9039846acd8
hi,I tried to implement the hotkey based on surface.addElement, there's a problem with the offset that is being dragged. I don't know how to get or compute the offset between the origin and the copy.
edgeless-keyboard.ts:
'Control-d': ctx => {
if (IS_WINDOWS) {
// console.log('windows');
ctx.get('defaultState').event.preventDefault();
}
this._duplicate();
return true;
},
private _duplicate() {
const edgeless = this.pageElement;
if (edgeless.selectionManager.editing) {
return;
}
const selectedElement = edgeless.selectionManager.firstElement;
// console.log('duplicate', selectedElement instanceof ShapeElement);
if (selectedElement instanceof ShapeElement) {
duplicateElements(edgeless.surface, selectedElement);
}
}
export function duplicateElements(
surface: SurfaceBlockComponent,
element: ShapeElement
) {
surface.addElement(element.type, { shapeType: element.shapeType });
}
I implemented the duplicate function in the above code, but the lack of offset caused it to always be the same location on the first copy shape element
Hi @AliceLanniste, thank you for your interest. Firstly, the addElement method has been moved from the 'surface' to the 'edgeless service', and it's now accessible via edgeless.service.addElement. Secondly, regarding the 'alt + drag' functionality for copying elements, the editor currently doesn't store the original element's information. To enable the cmd/ctrl + D for duplication, you'll need to modify default-tool.ts to preserve this information during the copying. This will then be used to calculate the offset when cmd + D is pressed.
@doouding thanks for your tips.I succeeded based on your suggestion. where should I store the offset? localStoarge? now I define the constant offset