Class ReadFromClipboardAction

All Implemented Interfaces:
Serializable

public class ReadFromClipboardAction extends PromiseAction<ClipboardPayload>
Reads the user's clipboard via navigator.clipboard.read() when the bound trigger fires and delivers the textual contents to the handler on the UI thread.

The Clipboard API requires the call to happen inside a short-lived user gesture (click, key press, …) AND the user to grant the clipboard-read permission — without both, the browser rejects the read. Bind this action to a Trigger that fires during such a gesture, typically a ClickTrigger.

Outcome handling extends PromiseAction: onPayload receives a ClipboardPayload with the text/plain and text/html representations of the first clipboard item (or null if there were no items at all), and onError receives a PromiseAction.Error record with the browser's error name and message (typically "NotAllowedError" when the user denied permission). Both consumers are required and must be non-null; to opt out of either notification, pass payload -> {} or err -> {} explicitly.


 new ClickTrigger(pasteButton)
         .triggers(new ReadFromClipboardAction(payload -> {
             if (payload == null) {
                 notification.show("Clipboard is empty");
             } else {
                 editor.setValue(payload.html() != null ? payload.html()
                         : payload.text());
             }
         }, err -> notification
                 .show("Clipboard read denied: " + err.message())));
 
For internal use only. May be renamed or removed in a future release.
See Also:
  • Constructor Details

    • ReadFromClipboardAction

      public ReadFromClipboardAction(SerializableConsumer<@Nullable ClipboardPayload> onPayload, SerializableConsumer<PromiseAction.Error> onError)
      Creates an action that reads the user's clipboard and delivers the contents to onPayload, or routes any failure to onError.
      Parameters:
      onPayload - invoked on the UI thread with the clipboard contents, or null if the clipboard was empty; not null
      onError - invoked on the UI thread with the browser's error when the clipboard read failed (permission denied, unsupported, …); not null
  • Method Details

    • appendPromiseExpression

      protected void appendPromiseExpression(com.vaadin.flow.component.trigger.internal.JsBuilder builder, StringBuilder out)
      Description copied from class: PromiseAction
      Subclasses append a JS expression that evaluates to a Promise when the trigger fires. The value the promise resolves with is decoded to T on the server and handed to onSuccess. To deliver a typed value, subclasses can wrap their API call in an IIFE — for example, copying a string and resolving with it:
      
       ((v) => navigator.clipboard.writeText(v).then(() => v))(<textExpr>)
       
      Specified by:
      appendPromiseExpression in class PromiseAction<ClipboardPayload>
      Parameters:
      builder - collects element parameter references, not null
      out - buffer to append into, not null