Class WriteToClipboardAction
java.lang.Object
com.vaadin.flow.component.trigger.internal.Action
com.vaadin.flow.component.trigger.internal.PromiseAction<String>
com.vaadin.flow.component.trigger.internal.WriteToClipboardAction
- All Implemented Interfaces:
Serializable
Writes a
ClipboardItem to the user's clipboard via
navigator.clipboard.write. Supports up to three concurrent MIME types
in one item: text/plain, text/html and image/png
(typically produced by an ImageBlobInput). Any combination is
allowed; at least one slot must be set.
The Clipboard API requires the write call to happen inside a
short-lived user gesture (click, key press, ...). Bind this action to a
trigger that fires during such a gesture.
Construction comes in three flavours, each available as fire-and-forget and as a with-outcome variant:
- Text/HTML ? the typical case for copying a string
- Image ? the typical case for copying an image; the write has no string
value, so success is reported via a
SerializableRunnable - Multi-format ? pass any combination of the
text,htmlandimageinputs to combine them in one item
onCopied receives the exact string that was copied ? the
text/plain value if present, otherwise the text/html value,
otherwise null (image-only case) ? useful when the input was a
PropertyInput whose value is only known on the client.
onError receives a PromiseAction.Error record with the
browser's error name and message.
For internal use only. May be renamed or removed in a future release.
- Since:
- 25.2
- 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
ConstructorsConstructorDescriptionWriteToClipboardAction(@Nullable Action.Input<String> textInput, @Nullable Action.Input<String> htmlInput) Creates a fire-and-forget text/HTML clipboard-copy action.WriteToClipboardAction(@Nullable Action.Input<String> textInput, @Nullable Action.Input<String> htmlInput, @Nullable Action.Input<?> imageInput) Creates a fire-and-forget multi-format clipboard-copy action.WriteToClipboardAction(@Nullable Action.Input<String> textInput, @Nullable Action.Input<String> htmlInput, @Nullable Action.Input<?> imageInput, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) Creates a multi-format clipboard-copy action whose outcome is reported back to the server.WriteToClipboardAction(@Nullable Action.Input<String> textInput, @Nullable Action.Input<String> htmlInput, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) Creates a text/HTML clipboard-copy action whose outcome is reported back to the server.WriteToClipboardAction(Action.Input<?> imageInput) Creates a fire-and-forget image clipboard-copy action.WriteToClipboardAction(Action.Input<?> imageInput, SerializableRunnable onCopied, SerializableConsumer<PromiseAction.Error> onError) Creates an image clipboard-copy action whose outcome is reported back to the server. -
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
warnIfNotVisible
-
Constructor Details
-
WriteToClipboardAction
public WriteToClipboardAction(@Nullable Action.Input<String> textInput, @Nullable Action.Input<String> htmlInput) Creates a fire-and-forget text/HTML clipboard-copy action.- Parameters:
textInput- input producing thetext/plainpayload, ornullto omithtmlInput- input producing thetext/htmlpayload, ornullto omit- Throws:
IllegalArgumentException- if both inputs arenull
-
WriteToClipboardAction
public WriteToClipboardAction(@Nullable Action.Input<String> textInput, @Nullable Action.Input<String> htmlInput, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) Creates a text/HTML clipboard-copy action whose outcome is reported back to the server.- Parameters:
textInput- input producing thetext/plainpayload, ornullto omithtmlInput- input producing thetext/htmlpayload, ornullto omitonCopied- invoked on the UI thread with the string that was copied after the client reports the write resolved (text/plainif present, otherwisetext/html), ornullif the JS resolved withundefined; notnullonError- invoked on the UI thread with the browser's error after the client reports the write rejected, notnull- Throws:
IllegalArgumentException- if both inputs arenull
-
WriteToClipboardAction
Creates a fire-and-forget image clipboard-copy action.- Parameters:
imageInput- input producing the source<img>for theimage/pngpayload (typically anImageBlobInput), notnull
-
WriteToClipboardAction
public WriteToClipboardAction(Action.Input<?> imageInput, SerializableRunnable onCopied, SerializableConsumer<PromiseAction.Error> onError) Creates an image clipboard-copy action whose outcome is reported back to the server. An image-only write has no string value, so success is reported via aSerializableRunnablerather than a value callback.- Parameters:
imageInput- input producing the source<img>for theimage/pngpayload (typically anImageBlobInput), notnullonCopied- invoked on the UI thread after the client reports the write resolved, notnullonError- invoked on the UI thread with the browser's error after the client reports the write rejected, notnull
-
WriteToClipboardAction
public WriteToClipboardAction(@Nullable Action.Input<String> textInput, @Nullable Action.Input<String> htmlInput, @Nullable Action.Input<?> imageInput) Creates a fire-and-forget multi-format clipboard-copy action. Pass any combination of inputs; at least one must be non-null.- Parameters:
textInput- input producing thetext/plainpayload, ornullto omithtmlInput- input producing thetext/htmlpayload, ornullto omitimageInput- input producing the source<img>for theimage/pngpayload, ornullto omit- Throws:
IllegalArgumentException- if all inputs arenull
-
WriteToClipboardAction
public WriteToClipboardAction(@Nullable Action.Input<String> textInput, @Nullable Action.Input<String> htmlInput, @Nullable Action.Input<?> imageInput, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) Creates a multi-format clipboard-copy action whose outcome is reported back to the server. Pass any combination of inputs; at least one must be non-null.- Parameters:
textInput- input producing thetext/plainpayload, ornullto omithtmlInput- input producing thetext/htmlpayload, ornullto omitimageInput- input producing the source<img>for theimage/pngpayload, ornullto omitonCopied- invoked on the UI thread with the string that was copied after the client reports the write resolved (text/plainif present, otherwisetext/html, otherwisenullin the image-only case); notnullonError- invoked on the UI thread with the browser's error after the client reports the write rejected, notnull- Throws:
IllegalArgumentException- if all inputs arenull
-
-
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<String>- Parameters:
trigger- the surrounding trigger this render is for, notnull- Returns:
- the promise-yielding JS function, not
null
-