Async call in pasteFromClipboard in LocalStorageCommandHandler

I need to call an async call in pasteFromClipboard LocalStorageCommandHandler. How can I put await in LocalStorageCommandHandler.js?

The simple answer is that you can’t, because CommandHandler.pasteFromClipboard has to return a collection of newly pasted Parts.

But you could implement your own async function that is just like CommandHandler.pasteSelection. Here is its definition:

  public pasteSelection(pos?: Point): void {
    if (pos === undefined) pos = null;
    const diagram = this.diagram;
    try {
      diagram.currentCursor = 'wait';
      diagram.raiseDiagramEvent('ChangingSelection', diagram.selection);
      diagram.startTransaction('Paste');
      const copies = this.pasteFromClipboard();
      // now update the Diagram.selection
      if (copies.count > 0) diagram.clearSelection(true);  // don't raise ChangingSelection/Finished
      const it = copies.iterator;
      while (it.next()) {
        const part = it.value;
        part.isSelected = true;
      }
      if (pos !== null) {
        const b = diagram.computePartsBounds(diagram.selection);
        if (b.isReal()) {
          const tool = diagram.toolManager.draggingTool;
          const coll = this.computeEffectiveCollection(diagram.selection);
          tool.moveParts(coll, new Point(pos.x - b.centerX, pos.y - b.centerY), tool.dragOptions, false);
        }
      }
      diagram.raiseDiagramEvent('ClipboardPasted', copies);
    } finally {
      diagram.commitTransaction('Paste');
      diagram.raiseDiagramEvent('ChangedSelection', diagram.selection);
      diagram.currentCursor = '';
    }
  }

I think you can copy this code, make it an async function, and implement your own pasteFromClipboard functionality.

@walter This works! Thanks! Do you mind also give the definition for copySelection.?

Yes, but it won’t be directly useful to you:

  public copySelection(): void {
    const diagram = this.diagram;
    try {
      // collect all of the parts to copy
      const allobjs = new go.Set<Part>();
      const it = diagram.selection.iterator;
      while (it.next()) {
        const part = it.value;
        go.Part.gatherCollection(allobjs, part, true,
            this.copiesTree ? Infinity : 0,
            this.copiesConnectedLinks,
            function (p) { return p.canCopy(); });
      }
      // copy to clipboard
      this.copyToClipboard(allobjs);
    } finally {
      // empty
    }
  }

That’s because it calls an internal static function.

I suggest that you override CommandHandler.copyToClipboard just so that you can get your hands on the correct collection of Parts to be copied, in addition to calling the super method. Then in your async copySelection function you can synchronously call CommandHandler.copySelection so that you get the collection returned by CommandHandler.copyToClipboard, which you can pass on to your async copyToClipboard function.

Thank you. I do override both copyToClipboard and pasteFromClipboard and both have some aysnc call in them. Thank you!