Skip to content

Native support for copy/cut/paste functionality #141

@ThomasGreenspan

Description

@ThomasGreenspan

Is your feature request related to a problem? Please describe.
This may just be me not knowing how to implement the feature. I've tried implementing copy/cut/paste feature via custom hotkeys but can't get the handler to fire (details below). The issue is specifically tied to using the typical hotkeys (I tried a few different things including Meta+KeyC). With other hotkeys (for example, just KeyC I can get the handlers to fire)

Describe the solution you'd like
This seems like it might be a pretty common feature that people would want. I would expect:

  1. A copyPasteFeature that can be optionally added to the list of features passed in to useTree.
  2. onCopy, onCut, and onPaste hooks made available in the object passed into useTree (only used when feature is included)
  3. "copyItems", "cutItems", and "pasteItems" as native hotkey actions that are properly linked up to everything else.
  4. [opt]: imperative functions that make the copy/cut/paste calls on the tree (i.e. tree.copySelectedItems(), tree.cutSelectedItems, treePasteItems - naming I'm not set on)

Internally, whatever is needed to properly handle taking control of the standard hotkeys would be implemented and whatever needed would be passed through via tree.getContainerProps().

Describe alternatives you've considered
I tried implementing this myself with custom hotkeys but wasn't able to get the handler to fire. I think the issue is with browsers intercepting the standard cut/copy/paste commands?

Here is an example of what I tried:

const clipboardRef = useRef<{
    items: ItemInstance<TreeNode>[];
    cut: boolean;
  } | null>(null);

const tree = useTree<TreeNode>({
  ...
  hotkeys: {
    customCopyItems: {
        hotkey: "Meta+KeyC",
        preventDefault: true,
        isEnabled: (tree) => tree.getSelectedItems().length > 0,
        handler: (_, tree) => {
          console.log(
            "copy",
            tree.getSelectedItems().map((i) => i.getId())
          );
          clipboardRef.current = { items: tree.getSelectedItems(), cut: false };
        },
      },
  },
})

I had a few attempts at trying to disable the browser copy/paste behavior on a container element but that didn't seem to work. That might still be the answer and maybe I just didn't quite get it right.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions