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

    • toPromiseJs

      protected JsFunction toPromiseJs(Trigger trigger)
      Description copied from class: PromiseAction
      Subclasses return a JsFunction that, when invoked with the trigger's event, evaluates to a Promise. 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 inside the function body — for example, copying a string and resolving with it:
      
       return JsFunction.of(
               "return ((v) => navigator.clipboard.writeText(v).then(() => v))($0(event))",
               textInput.toJs(trigger)).withArguments("event");
       
      Specified by:
      toPromiseJs in class PromiseAction<ClipboardPayload>
      Parameters:
      trigger - the surrounding trigger this render is for, not null
      Returns:
      the promise-yielding JS function, not null