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 taking onCopied/onError consumers:
- Text/HTML — the typical case for copying a string
- Image — the typical case for copying an image
- Multi-format via
ClipboardContent— combine any of the three slots 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.
- 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(ClipboardContent content) Creates a fire-and-forget multi-format clipboard-copy action from aClipboardContentdescribing the payload.WriteToClipboardAction(ClipboardContent content, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) Creates a multi-format clipboard-copy action from aClipboardContentwhose outcome is reported back to the server.WriteToClipboardAction(@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, 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, SerializableConsumer<@Nullable String> 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
applyTemporarily, 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, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) Creates an image clipboard-copy action whose outcome is reported back to the server.onCopiedreceivesnull— the image-only write has no meaningful string value.- Parameters:
imageInput- input producing the source<img>for theimage/pngpayload (typically anImageBlobInput), notnullonCopied- invoked on the UI thread withnullafter 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
Creates a fire-and-forget multi-format clipboard-copy action from aClipboardContentdescribing the payload. UseClipboard.onClick(...).write(content)as the typical entry point.- Parameters:
content- the clipboard payload, notnull; must have at least one slot set- Throws:
IllegalArgumentException- ifcontenthas no slots set
-
WriteToClipboardAction
public WriteToClipboardAction(ClipboardContent content, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) Creates a multi-format clipboard-copy action from aClipboardContentwhose outcome is reported back to the server.- Parameters:
content- the clipboard payload, notnull; must have at least one slot setonCopied- 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), 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- ifcontenthas no slots set
-
-
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
-