blocksuite icon indicating copy to clipboard operation
blocksuite copied to clipboard

Support multiple duplicate with `cmd/ctrl + D`

Open zanwei opened this issue 2 years ago • 3 comments

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

zanwei avatar Nov 25 '23 14:11 zanwei

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

AliceLanniste avatar Jan 20 '24 03:01 AliceLanniste

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 avatar Jan 26 '24 09:01 doouding

@doouding thanks for your tips.I succeeded based on your suggestion. where should I store the offset? localStoarge? now I define the constant offset

AliceLanniste avatar Jan 29 '24 07:01 AliceLanniste