Class ReadFromClipboardAction
- All Implemented Interfaces:
Serializable
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.vaadin.flow.component.trigger.internal.PromiseAction
PromiseAction.ErrorNested classes/interfaces inherited from class com.vaadin.flow.component.trigger.internal.Action
Action.Input<T> -
Constructor Summary
ConstructorsConstructorDescriptionReadFromClipboardAction(SerializableConsumer<@Nullable ClipboardPayload> onPayload, SerializableConsumer<PromiseAction.Error> onError) Creates an action that reads the user's clipboard and delivers the contents toonPayload, or routes any failure toonError. -
Method Summary
Modifier and TypeMethodDescriptionprotected JsFunctiontoPromiseJs(Trigger trigger) Subclasses return aJsFunctionthat, when invoked with the trigger's event, evaluates to aPromise.Methods inherited from class com.vaadin.flow.component.trigger.internal.PromiseAction
toJsMethods inherited from class com.vaadin.flow.component.trigger.internal.Action
applyTemporarily, warnIfNotVisible
-
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 toonPayload, or routes any failure toonError.- Parameters:
onPayload- invoked on the UI thread with the clipboard contents, ornullif the clipboard was empty; notnullonError- invoked on the UI thread with the browser's error when the clipboard read failed (permission denied, unsupported, …); notnull
-
-
Method Details
-
toPromiseJs
Description copied from class:PromiseActionSubclasses return aJsFunctionthat, when invoked with the trigger's event, evaluates to aPromise. The value the promise resolves with is decoded toTon the server and handed toonSuccess. 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:
toPromiseJsin classPromiseAction<ClipboardPayload>- Parameters:
trigger- the surrounding trigger this render is for, notnull- Returns:
- the promise-yielding JS function, not
null
-